solidity ERC20 transferFrom gives no error but does not execute

mortain

I have a Solidity code that aims to Exchange BUSD with myToken (both ERC20) which calls a BUSD.transferFrom() and a myToken.transfer(), but despite the core executes with no errors only myToken is transferred. The account is approved and has enough balance.

Can you please point the error?

bytes4 private constant SELECTOR_TRANSFER = bytes4(keccak256(bytes('transfer(address,uint256)')));
bytes4 private constant SELECTOR_TRANSFERFROM = bytes4(keccak256(bytes('transferFrom(address,address,uint256)')));


  function _myTokenTransfer(uint256 amount) private {
      _safeTransfer(myToken_addr, _msgSender(), amount);
  }
  
  function _busdTransfer(uint256 amount) private {
      _safeTransferFrom(busd_addr, _msgSender(), address(this), amount);
  }

  function _safeTransferFrom(address token, address from, address to, uint value) private {
      (bool success, bytes memory data) = token.call(abi.encodeWithSelector(SELECTOR_TRANSFERFROM, from , to, value));
      require(success && (data.length == 0 || abi.decode(data, (bool))), 'myTokenPrivateSale: TRANSFERFROM_FAILED');
  }
  

  function _safeTransfer(address token, address to, uint value) private {
      (bool success, bytes memory data) = token.call(abi.encodeWithSelector(SELECTOR_TRANSFER, to, value));
      require(success && (data.length == 0 || abi.decode(data, (bool))), 'myTokenPrivateSale: TRANSFER_FAILED');
  }
  
}

Petr Hejda

Your busd_addr is unset (i.e. 0x0).

When the _safeTransferFrom() function gets executed, it sends an internal transaction to the 0x0 address. The transaction doesn't revert (there's no contract that would throw an exception on the 0x0 address) and doesn't return any data (again, no contract that would return any data).

(bool success, bytes memory data)
// `success` is true because it didn't revert
// `data` is 0x00, the default value

The validation then passes as if it were sucessful.

require(success && (data.length == 0 || ...));
// `success` is true
// `data.length` is 0, so the rest of the OR condition is ignored

I see that you executed the setBUSD() function. But this function doesn't set the busd_addr value, that is used in the _safeTransferFrom() function. (It only sets the busd and busd_set.)

There's currently no way to set the busd_addr value in your code. So you'll need to make the proper changes in your code - set busd_addr value in the setBUSD() function, and then redeploy your contract.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Erc20 token transferFrom

erc20 solidity initial supply

TransferFrom() function for ETH(as native token not ERC20)?

Solidity BEP20 : transferFrom not (or not yet) declared or visible at this point

Identifier not found in new Solidity ERC20 contract

How to use ERC20 token to transfer eth in solidity?

Render JavaScript number as solidity ERC20 decimals

ParserError: Expected identifier but got 'public' - Solidity ERC20

ERC20 transferFrom() private key with web3js and metamask

How to appoint the address in the approval process for future transferFrom in ERC20 smart contract

Why does ERC20 balanceof() use constant?

Error when transfering ERC20 from account to contract

Error when trying to deposit ERC20 token into smart contract

Uniswap V2 ERC20 token solidity code: Are token names hard coded?

What is the correct way to transfer ERC20 tokens using delegateCall in Solidity?

Execute raw transaction for ERC20 token 'transfer function' for smart contracts deployed on RinkeBy Testnet

Solidity crowdsale function fails on my web page but is successful when sent directly but doesn't send ERC20 on successful transaction

When I approve a smart contract lets say unlimited ERC20 tokens does that mean it can send any and all ERC20 tokens? ETH, USDT, BNB all at one go?

How to diagnose an ERC20 error "Error: execution reverted" on a local testnet?

ERC20 token contract: does approve function need to check caller balance?

I'm trying to code a malicious ERC20 smartcontract approve function (for study purposes) but this does not work

how does Ethereum accounts works with regards to balance of Ethereum and ERC20 tokens

I'm getting compiler debug error while deploying my erc20 token on etherium testnet

Error when calling an erc20 approve function using ethers.js

"revert ERC20: transfer amount exceeds allowance" error when transferring from a smart contract

show dbs gives "Not Authorized to execute command" error

Cannot execute the else statement it gives out an error

Error when I call transfer function in ERC20 contract from other contract, But, can use mint/burn

JMeter does not load and gives error

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    pump.io port in URL

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  14. 14

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  15. 15

    How to use merge windows unallocated space into Ubuntu using GParted?

  16. 16

    flutter: dropdown item programmatically unselect problem

  17. 17

    Pandas - check if dataframe has negative value in any column

  18. 18

    Nuget add packages gives access denied errors

  19. 19

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  20. 20

    Generate random UUIDv4 with Elm

  21. 21

    Client secret not provided in request error with Keycloak

HotTag

Archive