1
votes

as you know, ERC-20 network has many tokens like USDT, SHIB, LINK, . . .

I want to create a website , when user need to buy USDT token I should send the USDT token in his wallet or use need to send USDT to another wallet on the blockchain and I want do all of these things into the blockchain, and the user could see the detail of his USDT transaction :

USDT Transactions

now I have a big question about transfer tokens in ERC-20 network.

i write this codes in remix and solidity :

// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.7.0 <0.9.0;

import 'https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/IERC20.sol';

contract DEX {

    struct Token {
        string token;
        address contractAddress;
    }

    mapping(string => Token) public tokens;
    string[] public tokenLis;

    address public admin;

    constructor() {
        admin = msg.sender;
    }

    function addToken(string memory token, address contractAddress) external {
        tokens[token] = Token(ticker,contractAddress);
        tokenLis.push(ticker);
    }

    function getTokenAddress(string memory token) external view returns(address moemory) {
       return tokens[token].contractAddress;
    }

    function sendToken(string memory token, address from , uint256 amount)
     external
      {

        IERC20(tokens[token].contractAddress).transferFrom(
            from,
            address(this),
            amount
        );


    }

}

I want to add dynamically tokens to my website and smart contract, for this write this codes :

  struct Token {
    string token;
    address contractAddress;
}

mapping(string => Token) public tokens;


function addToken(string memory token, address contractAddress) external {
    tokens[token] = Token(ticker,contractAddress);
    tokenLis.push(ticker);
}

I called addToken with this info :

Token : USDT

contractAddress : 0xdac17f958d2ee523a2206206994597c13d831ec7 ( USDT mainnet contract address )

it's work and add success USDT in tokens .

now I want to send some USDT to the user with this function ( imported the openzepelin IERC20) :

function sendToken(string memory ticker , address from , uint256 amount)
 external
  {

    IERC20(tokens[ticker].contractAddress).transferFrom(
        from,
        address(this),
        amount
    );
}

now when I want to transfer amount from remix address one to remixaddress to into the USDT contract address it show me this error :

enter image description here enter image description here

enter image description here

Question B: what is the problem? how can I solve this error?

Question A: what is the best way for my scenario? use the smart contract or user web3.js?

1

1 Answers

1
votes
  1. Your contract is on a different network (Remix VM emulator) than the USDT contract (Ethereum mainnet), so they cannot interact. You can fork the mainnet and connect to the forked network from your Remix IDE, see this answer for more details.

  2. Then you're probably going to run into another issue within the sendToken() function (since you don't cover it in your question, I'm assuming you haven't performed this step). In order to successfully invoke transferFrom(), the function caller (in this case your contract address) needs to be approved by the token owner (in this case the from variable) to spend their tokens. You can find more info in the ERC-20 specification to the approve() function.

    Summary: The from address needs to invoke the approve() method on the token contract directly (not through your or any other contract), passing it your contract address as the first param, so that your contract is able to pull their tokens.