Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to receive and send USDT in a smart contract?

Is there any guide or code that can serve as an example to implement the functionality where a smart contract receives and sends USDT to other addresses.

I appreciate your help

like image 407
jeissoni22 Avatar asked May 12 '21 22:05

jeissoni22


People also ask

Is USDT a smart contract?

USDT is the USD-pegged stablecoin issued by Tether on the TRON network. The token will be able to complete issuance, holding and transfer via smart contract on TRON, with a completely free and transparent process, zero transfer fee, and instant delivery; it will also be able to write programs that are highly expansible ...

Can smart contracts send money?

This contract now meets the Owner model where it checks if address calling the function is the owner before allowing it to proceed further. So that's it for this simple smart contract that allows money to be sent to it, and only allows the owner to withdraw all the money from the smart contract.

What is USDT smart contract address?

What's the smart contract address for Tether USD (BEP-20)? Tether USD (BEP-20)'s smart contract address is 0x55d398326f99059ff775485246999027b3197955.


Video Answer


1 Answers

The token balance is stored in the token contract (in this case USDT), not in yours. So sending tokens is a straightforward process - you just execute the correct function on the token contract. Mind that your contract needs to hold at least the amount it's about to send, otherwise the transaction would revert.

pragma solidity ^0.8;

interface IERC20 {
    function transfer(address _to, uint256 _value) external returns (bool);
    
    // don't need to define other functions, only using `transfer()` in this case
}

contract MyContract {
    // Do not use in production
    // This function can be executed by anyone
    function sendUSDT(address _to, uint256 _amount) external {
         // This is the mainnet USDT contract address
         // Using on other networks (rinkeby, local, ...) would fail
         //  - there's no contract on this address on other networks
        IERC20 usdt = IERC20(address(0xdAC17F958D2ee523a2206206994597C13D831ec7));
        
        // transfers USDT that belong to your contract to the specified address
        usdt.transfer(_to, _amount);
    }
}

But because the balance is stored in the external contract, you can't just make a user send you tokens by executing a function in your contract. See my other answer that shows an example how this could be misused if it were possible (just replace approve to transfer, but the logic is the same).

Some token standards (such as ERC-1155 or ERC-721) allow sending a hook to your contract when your contract receives tokens. The hook function names and required parameters are in the linked documents. But whether the token contract sends you a hook, that depends on

  • implementation of the token contract (which specifically USDT doesn't implement)
  • your contract (you would have to implement the hook function for all token standards that you want to receive - or at least a generic fallback() which would work in some cases)
  • and sometimes on the sender as well.

You can ask your users to approve some USDT to be spent by your address, and then your contract can execute the transferFrom() function of the USDT contract (where the "from" is the user who approved you to spend their tokens). However, the approval needs to be done outside of your contract, as the linked other answer suggests.

You can also have an off-chain app that listens to event logs emitted by the token contract (in your case Transfer() event on the USDT contract). The event logs contain the transfer information including the receiver and amount. So your (off-chain) app can filter only events where your address is the receiver, and perform some action when it handles the event log.

like image 64
Petr Hejda Avatar answered Oct 19 '22 20:10

Petr Hejda