๐ŸŽด
Transfer Tokens
Transfer ERC20 tokens on any blockchain - ETH (Ethereum), BNB (Binance Smart Chain), MATIC (Polygon)

Transfer ERC20 Tokens

To transfer ERC20 tokens, follow the steps:
  1. 1.
    Construct anoptionsobject and set
    1. 1.
      type:"erc20"
    2. 2.
      amount: Moralis.Units.Token("0.5", "18") //Converts toweiโ€‹
    3. 3.
      receiver: "0x000..." //wallet address
    4. 4.
      contractAddress: "0x..." //contract of the ERC20 token
  2. 2.
    Call the Moralis transfer function as shown below
JS
React
1
// sending 0.5 tokens with 18 decimals
2
const options = {
3
type: "erc20",
4
amount: Moralis.Units.Token("0.5", "18"),
5
receiver: "0x..",
6
contractAddress: "0x..",
7
};
8
let result = await Moralis.transfer(options);
Copied!
1
import React from "react";
2
import { useWeb3Transfer } from "react-moralis";
3
โ€‹
4
const TransferWeth = () => {
5
const { fetch, error, isFetching } = useWeb3Transfer({
6
amount: Moralis.Units.Token(20, 18),
7
receiver: "0x0000000000000000000000000000000000000000",
8
type: "erc20",
9
contractAddress: "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
10
});
11
โ€‹
12
return (
13
// Use your custom error component to show errors
14
<div>
15
{error && <ErrorMessage error={error} />}
16
<button onClick={() => fetch()} disabled={isFetching}>
17
Transfer
18
</button>
19
</div>
20
);
21
};
Copied!
By using Moralis.Units.Tokenhelper function you can multiply the amount you want to send by the number of decimals. This is required to construct an ERC20 transaction.
The contract address and the number of decimals for a token can normally be found on Etherscan or on the website of the project.
Get the address and decimals programmatically of a token by using Moralis SDK Moralis.Web3API.token.getTokenMetadataBySymbol().

Resolving the results

Moralis.transfer() returns a transaction response after it is executed. The below page shows how to consume the data returned.