Sub topic 10
Sending Tokens
Prerequisites and working model
This chapter assumes you understand wallet addresses, blockchain networks, native coins, and transaction fees. You should also know basic JavaScript if you want to run the code example.
A token is an asset recorded by a smart contract. On Ethereum-compatible networks, many tokens follow the ERC-20 standard. The token contract stores balances and provides functions such as balanceOf and transfer.
Sending an ERC-20 token does not move the token directly from one wallet application to another. Your wallet submits a transaction that calls the token contract. The contract checks the sender's token balance, subtracts the amount, and adds it to the recipient's balance.
The amount and the token's decimals
Token balances are stored as integers in the smallest unit. A token with 6 decimals represents 1.5 tokens as 1,500,000 base units. A token with 18 decimals represents it as 1,500,000,000,000,000,000 base units.
Always read the token contract's decimals value before converting a human amount. Do not assume every token uses 18 decimals.
| Human amount | Decimals | Contract amount |
|---|---|---|
| 5.0 | 6 | 5,000,000 |
The sender also needs enough of the network's native coin to pay gas. Holding 5 tokens does not pay the transaction fee unless the network specifically supports that arrangement.
Send tokens from code
The following Node.js example uses ethers v6. Set RPC_URL, PRIVATE_KEY, and TOKEN_ADDRESS for the same network. Use a testnet and a throwaway key while learning. Replace the recipient address with an address you control.
import { JsonRpcProvider, Wallet, Contract, parseUnits } from "ethers";
const abi = [
"function decimals() view returns (uint8)",
"function balanceOf(address owner) view returns (uint256)",
"function transfer(address to, uint256 amount) returns (bool)",
];
async function main() {
const provider = new JsonRpcProvider(process.env.RPC_URL);
const wallet = new Wallet(process.env.PRIVATE_KEY, provider);
const token = new Contract(process.env.TOKEN_ADDRESS, abi, wallet);
const recipient = "0x0000000000000000000000000000000000000001";
const decimals = await token.decimals();
const amount = parseUnits("5.0", decimals);
const tx = await token.transfer(recipient, amount);
console.log("Submitted:", tx.hash);
const receipt = await tx.wait();
if (receipt.status !== 1) {
throw new Error("Token transfer failed");
}
console.log("Sender balance:", await token.balanceOf(wallet.address));
}
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});transfer returns a transaction response, not an immediate confirmation. tx.wait() waits until the transaction is included in a block. A successful transaction means the contract call completed. You should still verify the recipient's balance.
Common errors
- Wrong network: The same address can exist on several networks, but balances are separate.
- Wrong token contract: A familiar symbol does not identify a unique token. Verify the contract address from a trusted source.
- Incorrect decimals: Sending a value in the wrong unit can send too much or too little.
- Insufficient native coin: The wallet may have tokens but lack gas.
- Unsupported token behavior: Some tokens charge fees, pause transfers, or impose restrictions. Read the project's documentation and test with a small amount.
- Contract approval confusion:
approveis usually needed when another contract spends your tokens. A direct transfer from your wallet normally usestransfer.
Exercise
A token uses 6 decimals. What contract amount represents 2.75 tokens, and which asset pays the gas fee?
Show answer
The contract amount is 2,750,000 base units. The network's native coin pays the gas fee.