{"id":7470,"date":"2024-03-25T07:49:37","date_gmt":"2024-03-25T07:49:37","guid":{"rendered":"https:\/\/metaschool.so\/articles\/?p=7470"},"modified":"2024-03-25T07:49:40","modified_gmt":"2024-03-25T07:49:40","slug":"how-to-receive-and-send-usdt-in-solidity","status":"publish","type":"post","link":"https:\/\/metaschool.so\/articles\/how-to-receive-and-send-usdt-in-solidity\/","title":{"rendered":"How To Receive and Send USDT in Solidity?"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_56_1 ez-toc-wrap-left counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title \" >Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/metaschool.so\/articles\/how-to-receive-and-send-usdt-in-solidity\/#Steps_to_receive_and_send_USDT_in_Solidity\" title=\"Steps to receive and send USDT in Solidity\">Steps to receive and send USDT in Solidity<\/a><ul class='ez-toc-list-level-3'><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/metaschool.so\/articles\/how-to-receive-and-send-usdt-in-solidity\/#1_Import_the_ERC-20_interface\" title=\"1. Import the ERC-20 interface\">1. Import the ERC-20 interface<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/metaschool.so\/articles\/how-to-receive-and-send-usdt-in-solidity\/#2_Implement_receive_function_optional\" title=\"2. Implement receive function (optional)\">2. Implement receive function (optional)<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/metaschool.so\/articles\/how-to-receive-and-send-usdt-in-solidity\/#3_Implement_send_function\" title=\"3. Implement send function\">3. Implement send function<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/metaschool.so\/articles\/how-to-receive-and-send-usdt-in-solidity\/#4_Handling_approval_and_allowance\" title=\"4. Handling approval and allowance\">4. Handling approval and allowance<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/metaschool.so\/articles\/how-to-receive-and-send-usdt-in-solidity\/#5_Check_Balance\" title=\"5. Check Balance\">5. Check Balance<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/metaschool.so\/articles\/how-to-receive-and-send-usdt-in-solidity\/#Example_Usage\" title=\"Example Usage\">Example Usage<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/metaschool.so\/articles\/how-to-receive-and-send-usdt-in-solidity\/#Conclusion\" title=\"Conclusion\">Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n\n<p>To receive and send USDT (Tether) in a smart contract written in Solidity, we need to integrate the ERC-20 standard functionalities into our <a href=\"https:\/\/metaschool.so\/articles\/guide-move-smart-contract-programming-language\/\">contract<\/a>. 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 <a href=\"https:\/\/metaschool.so\/articles\/how-to-create-erc-20-token\/\">ERC-20<\/a> standard.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"steps-to-receive-and-send-usdt-in-solidity\"><span class=\"ez-toc-section\" id=\"Steps_to_receive_and_send_USDT_in_Solidity\"><\/span>Steps to receive and send USDT in Solidity<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Here&#8217;s the step-by-step guide to receive and send UDST in Solidity.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1-import-the-erc-20-interface\"><span class=\"ez-toc-section\" id=\"1_Import_the_ERC-20_interface\"><\/span>1. Import the ERC-20 interface<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>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.<\/p>\n\n\n\n<pre class=\"wp-block-code has-nv-site-bg-background-color has-background\" style=\"font-size:18px\"><code>pragma solidity ^0.8.0;\n\ninterface ERC20 {\n    function totalSupply() external view returns (uint256);\n    function balanceOf(address account) external view returns (uint256);\n    function transfer(address recipient, uint256 amount) external returns (bool);\n    function allowance(address owner, address spender) external view returns (uint256);\n    function approve(address spender, uint256 amount) external returns (bool);\n    function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);\n    event Transfer(address indexed from, address indexed to, uint256 value);\n    event Approval(address indexed owner, address indexed spender, uint256 value);\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-implement-receive-function-optional\"><span class=\"ez-toc-section\" id=\"2_Implement_receive_function_optional\"><\/span>2. Implement receive function (optional)<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>If you want your contract to be able to receive USDT directly, you can implement the <code>receive<\/code> function. This function is called whenever the contract receives ether or tokens.<\/p>\n\n\n\n<pre class=\"wp-block-code has-nv-site-bg-background-color has-background\" style=\"font-size:18px\"><code>receive() external payable {\n    \/\/ Handle incoming Ether or tokens\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3-implement-send-function\"><span class=\"ez-toc-section\" id=\"3_Implement_send_function\"><\/span>3. Implement send function<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>To send USDT from your contract to another address, you&#8217;ll need to call the <code>transfer<\/code> function of the USDT contract. Here&#8217;s how you can do it:<\/p>\n\n\n\n<pre class=\"wp-block-code has-nv-site-bg-background-color has-background\" style=\"font-size:18px\"><code>function sendUSDT(address _to, uint256 _amount) external {\n    ERC20 usdt = ERC20(0xdAC17F958D2ee523a2206206994597C13D831ec7); \/\/ USDT contract address\n\n    require(usdt.balanceOf(address(this)) >= _amount, \"Insufficient balance in contract\");\n    require(usdt.transfer(_to, _amount), \"Failed to send USDT\");\n}<\/code><\/pre>\n\n\n\n<p>In this function:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>_to<\/code> is the address you want to send USDT to.<\/li><li><code>_amount<\/code> is the amount of USDT you want to send.<\/li><\/ul>\n\n\n\n<p><strong>\ud83d\udd25 Check this course out:<\/strong>\u00a0<a href=\"https:\/\/metaschool.so\/courses\/create-your-own-ethereum-token-in-just-30-mins\" target=\"_blank\" rel=\"noreferrer noopener\">Create Your Own Ethereum Token in Just 30 Mins<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"4-handling-approval-and-allowance\"><span class=\"ez-toc-section\" id=\"4_Handling_approval_and_allowance\"><\/span>4. Handling approval and allowance<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>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 <code>approve<\/code> function of the USDT contract.<\/p>\n\n\n\n<pre class=\"wp-block-code has-nv-site-bg-background-color has-background\" style=\"font-size:18px\"><code>function approveContract(address _spender, uint256 _amount) external {\n    ERC20 usdt = ERC20(0xdAC17F958D2ee523a2206206994597C13D831ec7); \/\/ USDT contract address\n\n    require(usdt.approve(_spender, _amount), \"Approval failed\");\n}<\/code><\/pre>\n\n\n\n<p>In this function:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>_spender<\/code> is your contract&#8217;s address.<\/li><li><code>_amount<\/code> is the amount of USDT the external address approves for your contract to spend.<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"5-check-balance\"><span class=\"ez-toc-section\" id=\"5_Check_Balance\"><\/span>5. Check Balance<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>It&#8217;s always a good practice to include a function to check the USDT balance of your contract.<\/p>\n\n\n\n<pre class=\"wp-block-code has-nv-site-bg-background-color has-background\" style=\"font-size:18px\"><code>function getUSDTBalance() external view returns (uint256) {\n    ERC20 usdt = ERC20(0xdAC17F958D2ee523a2206206994597C13D831ec7); \/\/ USDT contract address\n\n    return usdt.balanceOf(address(this));\n}<\/code><\/pre>\n\n\n\n<p>This function simply returns the USDT balance of your contract.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example-usage\"><span class=\"ez-toc-section\" id=\"Example_Usage\"><\/span>Example Usage<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Now, let&#8217;s see how you can use these functions:<\/p>\n\n\n\n<pre class=\"wp-block-code has-nv-site-bg-background-color has-background\" style=\"font-size:18px\"><code>contract USDTContract {\n    function sendUSDT(address _to, uint256 _amount) external {\n        \/\/ Implementation\n    }\n\n    function approveContract(address _spender, uint256 _amount) external {\n        \/\/ Implementation\n    }\n\n    function getUSDTBalance() external view returns (uint256) {\n        \/\/ Implementation\n    }\n}<\/code><\/pre>\n\n\n\n<p><strong>\ud83d\udd25 Check this course out:<\/strong>\u00a0<a href=\"https:\/\/metaschool.so\/courses\/one-piece-personality-dapp-solidity\" target=\"_blank\" rel=\"noreferrer noopener\">Build a One Piece Personality dApp With Solidity<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"conclusion\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>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.<\/p>\n\n\n\n<p><strong>Try it out, ask us questions, and let us know how it went by tagging Metaschool on Social Media.<\/strong><\/p>\n\n\n\n<p><strong>Follow us on<\/strong>&nbsp;\u2013<\/p>\n\n\n\n<p>\ud83d\udd2eTwitter \u2013\u00a0<a href=\"https:\/\/twitter.com\/0xmetaschool\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/twitter.com\/0xmetaschool<\/a><\/p>\n\n\n\n<p>\ud83d\udd17LinkedIn \u2013\u00a0<a href=\"https:\/\/www.linkedin.com\/company\/0xmetaschool\/\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/www.linkedin.com\/company\/0xmetaschool\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":15,"featured_media":7471,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"neve_meta_sidebar":"","neve_meta_container":"","neve_meta_enable_content_width":"","neve_meta_content_width":0,"neve_meta_title_alignment":"","neve_meta_author_avatar":"","neve_post_elements_order":"","neve_meta_disable_header":"","neve_meta_disable_footer":"","neve_meta_disable_title":"","footnotes":""},"categories":[292],"tags":[51,46,29],"class_list":["post-7470","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-answers","tag-blockchain","tag-solidity","tag-web3"],"_links":{"self":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/7470","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/users\/15"}],"replies":[{"embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/comments?post=7470"}],"version-history":[{"count":1,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/7470\/revisions"}],"predecessor-version":[{"id":7472,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/7470\/revisions\/7472"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/media\/7471"}],"wp:attachment":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/media?parent=7470"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/categories?post=7470"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/tags?post=7470"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}