Ask or search…
K
Links

Approve the transfer of a fungible token like USDT

In the context of ERC20 tokens, "approve" refers to a function in the ERC20 standard that allows a token holder to grant permission to another address (called the "spender") to transfer a specified amount of tokens on their behalf. This mechanism is useful for enabling smart contracts or other addresses to perform token transactions on behalf of the token owner without the need to transfer the tokens directly.
MetaMask is designed as a browser extension to provide a user-friendly interface and secure key management for interacting with dApps and web services. Connecting from Node.js is not supported because MetaMask focuses on end-user interactions within web browsers, while Node.js is a server-side JavaScript runtime typically used for backend development.
In summary, approving the transfer of an ERC20 token is a way to delegate the ability to spend tokens to another address while maintaining control over the tokens and their allowance.

How to approve a USDT transaction for someone else with MetaMask from your browser-based application

Use the TatumSDK (@tatumio/tatum) to prepare, sign and broadcast the transaction using MetaMask.
You will leverage the WalletProvider submodule, which includes multiple browser-based wallet extensions. MetaMask is just one of them.
TypeScript
JavaScript
1
// yarn add @tatumio/tatum
2
import {TatumSDK, Network, Ethereum, MetaMask} from '@tatumio/tatum'
3
4
const tatum = await TatumSDK.init<Ethereum>({network: Network.ETHEREUM})
5
6
//This is the USDT token address
7
const USDT = '0xdAC17F958D2ee523a2206206994597C13D831ec7'
8
9
const txId: string = await tatum.walletProvider.use(MetaMask).approveErc20('0x4675C7e5BaAFBFFbca748158bEcBA61ef3b0a263', '1.5', USDT)
10
11
console.log(txId)
12
13
// We have prepared an approval operation of 1.5 USDT from your default connected MetaMask account to the spender - 0x4675C7e5BaAFBFFbca748158bEcBA61ef3b0a263
1
// Install with: npm install @tatumio/tatum
2
const { TatumSDK, Network, MetaMask } = require("@tatumio/tatum");
3
4
(async () => {
5
try {
6
const tatum = await TatumSDK.init({ network: Network.ETHEREUM });
7
//This is the USDT token address
8
const USDT = '0xdAC17F958D2ee523a2206206994597C13D831ec7'
9
10
const txId = await tatum.walletProvider.use(MetaMask).approveErc20('0x4675C7e5BaAFBFFbca748158bEcBA61ef3b0a263', '1.5', USDT);
11
console.log(txId);
12
} catch (error) {
13
console.error("Error signing a transaction using MetaMask:", error);
14
}
15
})();
16
17
// We have prepared an approval operation of 1.5 USDT from your default connected MetaMask account to the spender - 0x4675C7e5BaAFBFFbca748158bEcBA61ef3b0a263

Request parameters

  • spender - string, address, which will be able to transfer the tokens on behalf of the owner
    • Example: "0x4675C7e5BaAFBFFbca748158bEcBA61ef3b0a263"
  • amount - string, amount to be approved, always in a token currency like USDT
    • Example: "1.5"
  • tokenAddress - string, token address of a token you want to approve
    • Example: "0xdAC17F958D2ee523a2206206994597C13D831ec7"

Response

  • txId - string, transaction hash of signed and broadcasted transaction
    • Example: "0xdb1e03f4cea29265f031bfc0514b07c15a5fc5e5cc2fd47f7d9a54c74f5c5637"
© Tatum Technology, LLC