Skip to content
Home ยป Answers ยป How To Receive and Send USDT in Solidity?

How To Receive and Send USDT in Solidity?

How To Receive and Send USDT in Solidity

To receive and send USDT (Tether) in a smart contract written in Solidity, we need to integrate the ERC-20 standard functionalities into our contract. USDT is an ERC-20 token, meaning it follows a set of rules and functions that allow it to be compatible with any other smart contracts or wallets that support the ERC-20 standard.

Steps to receive and send USDT in Solidity

Here’s the step-by-step guide to receive and send UDST in Solidity.

1. Import the ERC-20 interface

First, you need to import the ERC-20 interface into your Solidity contract. This interface defines the standard functions that an ERC-20 token contract must implement.

pragma solidity ^0.8.0;

interface ERC20 {
    function totalSupply() external view returns (uint256);
    function balanceOf(address account) external view returns (uint256);
    function transfer(address recipient, uint256 amount) external returns (bool);
    function allowance(address owner, address spender) external view returns (uint256);
    function approve(address spender, uint256 amount) external returns (bool);
    function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
    event Transfer(address indexed from, address indexed to, uint256 value);
    event Approval(address indexed owner, address indexed spender, uint256 value);
}

2. Implement receive function (optional)

If you want your contract to be able to receive USDT directly, you can implement the receive function. This function is called whenever the contract receives ether or tokens.

receive() external payable {
    // Handle incoming Ether or tokens
}

3. Implement send function

To send USDT from your contract to another address, you’ll need to call the transfer function of the USDT contract. Here’s how you can do it:

function sendUSDT(address _to, uint256 _amount) external {
    ERC20 usdt = ERC20(0xdAC17F958D2ee523a2206206994597C13D831ec7); // USDT contract address

    require(usdt.balanceOf(address(this)) >= _amount, "Insufficient balance in contract");
    require(usdt.transfer(_to, _amount), "Failed to send USDT");
}

In this function:

  • _to is the address you want to send USDT to.
  • _amount is the amount of USDT you want to send.

๐Ÿ”ฅ Check this course out:ย Create Your Own Ethereum Token in Just 30 Mins

4. Handling approval and allowance

Before your contract can send USDT on behalf of an external address, that address must approve your contract to spend USDT on its behalf. This involves calling the approve function of the USDT contract.

function approveContract(address _spender, uint256 _amount) external {
    ERC20 usdt = ERC20(0xdAC17F958D2ee523a2206206994597C13D831ec7); // USDT contract address

    require(usdt.approve(_spender, _amount), "Approval failed");
}

In this function:

  • _spender is your contract’s address.
  • _amount is the amount of USDT the external address approves for your contract to spend.

5. Check Balance

It’s always a good practice to include a function to check the USDT balance of your contract.

function getUSDTBalance() external view returns (uint256) {
    ERC20 usdt = ERC20(0xdAC17F958D2ee523a2206206994597C13D831ec7); // USDT contract address

    return usdt.balanceOf(address(this));
}

This function simply returns the USDT balance of your contract.

Example Usage

Now, let’s see how you can use these functions:

contract USDTContract {
    function sendUSDT(address _to, uint256 _amount) external {
        // Implementation
    }

    function approveContract(address _spender, uint256 _amount) external {
        // Implementation
    }

    function getUSDTBalance() external view returns (uint256) {
        // Implementation
    }
}

๐Ÿ”ฅ Check this course out:ย Build a One Piece Personality dApp With Solidity

Conclusion

By implementing the ERC-20 interface and integrating the necessary functions, you can send and receive USDT within your Solidity smart contract. Make sure to handle error cases properly and consider security implications when dealing with external tokens.

Try it out, ask us questions, and let us know how it went by tagging Metaschool on Social Media.

Follow us on โ€“

๐Ÿ”ฎTwitter โ€“ย https://twitter.com/0xmetaschool

๐Ÿ”—LinkedIn โ€“ย https://www.linkedin.com/company/0xmetaschool/