Sub topic 10
Sending tokens
This chapter covers how a token transfer differs from a native coin transfer, what happens inside the token contract, and the errors you are most likely to meet. It assumes you have read the earlier chapters on accounts, native coins versus tokens, gas, nonces and the transaction lifecycle. It also assumes you can read basic JavaScript.
What a token transfer really is
A native coin, such as ETH on Ethereum, is recorded in the protocol itself. Sending it changes two account balances directly. A token is different. It is a number stored inside a smart contract, which is a program living at an address on the chain. Your "balance" of a token is an entry in a table inside that contract, keyed by your address.
So you never send a token from your wallet to someone's wallet in the literal sense. You send a transaction to the token contract, asking it to run its transfer function. The contract subtracts the amount from your entry and adds it to the recipient's entry. Nothing moves between addresses on the chain level. Only the contract's records change.
Three consequences follow:
- The transaction's
tofield is the token contract address, not the recipient. - The recipient and the amount are encoded in the transaction's
datafield. - You pay gas in the network's native coin, even if you hold plenty of the token.
The ERC-20 interface
Most fungible tokens on Ethereum-compatible networks follow the ERC-20 standard. It defines a small set of functions that every wallet and application can rely on.
| Function | Purpose |
|---|---|
| `balanceOf(owner)` | Returns the token balance of an address, in the smallest unit |
| `decimals()` | Returns how many decimal places the token uses |
| `transfer(to, amount)` | Moves tokens from the caller to `to` |
| `approve(spender, amount)` | Lets another address spend up to `amount` of your tokens |
| `transferFrom(from, to, amount)` | Moves tokens on behalf of `from`, within an approved allowance |
| `allowance(owner, spender)` | Returns how much `spender` may still spend |
For a plain person-to-person payment you only need transfer. The approve and transferFrom pair is used when a contract, such as an exchange or a payment processor, pulls tokens from you. Section "Allowances" below covers why that matters for safety.
Other networks use different standards for similar ideas. Solana uses the SPL token program, and Bitcoin-based systems have their own token schemes. The concepts here carry over, but the function names and account structures differ.
Decimals and smallest units
Contracts store integers only. A token with 6 decimals, such as many dollar-pegged tokens, represents 5.0 tokens as the integer 5000000. A token with 18 decimals represents 5.0 tokens as 5000000000000000000.
Different tokens use different decimals, so you must read decimals() from the contract and never assume 18. Sending with the wrong scale is a real error. If you assume 18 decimals for a 6-decimal token, you ask to send a trillion times more than you intended, and the transaction will probably fail for insufficient balance. If you assume 6 for an 18-decimal token, you send a trillionth of what you meant, and it will succeed. That transfer cannot be corrected by the contract.
Sending a token in a wallet
In a wallet application, the steps are:
- Confirm the network at the top of the wallet matches the network where the recipient expects the token.
- Select the token, not the native coin, from your asset list. If it is missing, add it by its contract address.
- Paste the recipient address and check the first and last characters against the source.
- Enter the amount.
- Review the fee. The fee is shown in the native coin.
- Confirm and wait for the transaction to be included.
If the wallet says the fee cannot be paid, you have tokens but no native coin on that network. You must first receive a small amount of the native coin, then retry.
Adding a token by contract address needs care. Anyone can deploy a contract and give it the same name and symbol as a well-known token. Copy the contract address from the token issuer's official site or from a block explorer entry you trust, not from a message or an ad.
Sending a token in code
The following example uses ethers v6. It reads the token's own decimals, converts a human amount, and calls transfer. Use a testnet RPC URL and a throwaway key while learning, and replace the placeholder recipient.
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)",
];
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 decimals = await token.decimals();
const amount = parseUnits("5.0", decimals);
const tx = await token.transfer("0x0000000000000000000000000000000000000001", amount);
await tx.wait();
console.log("New sender balance:", await token.balanceOf(wallet.address));Points worth noting in this code:
- The
Contractis created withwalletas its runner, so calls that change state are signed and sent by that wallet. Calls markedvieware free reads. parseUnits("5.0", decimals)returns abigint. Token amounts exceed the safe range of ordinary JavaScript numbers, so never useNumberfor them.tx.wait()resolves when the transaction is included in a block. It does not tell you the transfer succeeded unless the call reverts, in which case ethers v6 throws an error for a failed receipt. Wrap it intryandcatchin real code.- The sender still needs native coin for gas. The call will fail at fee estimation if the balance is zero.
Before sending, a careful program also checks that balanceOf(wallet.address) is at least amount. A failed transaction still costs gas.
Why a transfer can fail
Because the token logic is a program, it can reject a transfer. Common reasons:
- Insufficient token balance. The contract reverts. The error message depends on the contract, for example "transfer amount exceeds balance".
- Insufficient native coin for gas. The transaction is never accepted by the node, or it fails during estimation.
- Paused or restricted tokens. Some tokens let the issuer pause transfers or block specific addresses. Regulated stablecoins often include a blocklist.
- Transfer fees or tax logic. Some tokens deduct a percentage on every transfer. The recipient then gets less than you sent. The contract call still returns success.
- Non-standard return values. A few older tokens do not return a boolean from
transfer. Libraries that expect a return value can misread these. Tools like OpenZeppelin'sSafeERC20exist for this reason in contract code. - Wrong contract. If the address you call is not a token contract,
decimals()ortransferwill fail or return nothing meaningful.
When a transaction fails after inclusion, the sender still pays the gas used up to the point of failure. Simulating the call first, for example with ethers token.transfer.staticCall(to, amount), reveals most reverts without spending gas.
Verifying a token transfer
A successful transaction receipt is not the final proof. Check the result in two ways.
First, read the recipient's token balance with balanceOf before and after. Fee-on-transfer tokens show a smaller increase than the amount sent.
Second, look at the Transfer event in the receipt logs. ERC-20 contracts emit a Transfer(from, to, value) event on every movement. Block explorers show these under a "Token transfers" tab. This is how you confirm which contract moved which amount.
A token transfer does not appear in the recipient's native coin history as a value transfer. The transaction's value field is zero, because the value moved inside the contract. Many people think a payment is missing because they look at the wrong tab.
Allowances
The approve function gives a spender permission to pull tokens up to a set amount. Applications request it before swaps, deposits and similar actions. It is a separate transaction from the action itself.
The risk is that approvals often last until you revoke them, and many applications ask for an unlimited amount. If the approved contract is later compromised, or if you approved a malicious one, it can drain that token from your account without any new signature from you. Good habits:
- Approve only the amount you need where the app allows editing it.
- Review active approvals from time to time in a block explorer's approval checker or a revocation tool.
- Revoke approvals you no longer use. Revoking is an on-chain transaction and costs gas.
Plain transfer needs no approval at all.
Checklist before sending any token
| Check | Why |
|---|---|
| Network matches the recipient's expected network | The same address can exist on many chains, and the token there is a different asset |
| Token contract address is the official one | Lookalike tokens share names and symbols |
| Recipient address is correct | Transfers cannot be reversed |
| Native coin balance covers gas | Without it the transfer cannot be sent |
| Amount uses the token's `decimals` | Wrong scale changes the amount by powers of ten |
| Small test transfer first for large sums | A cheap way to confirm the whole path works |
Exercise
You hold 100 USDC on Ethereum and 0 ETH. You try to send 10 USDC to a friend and the wallet refuses. Explain why, and name the amount of a 6-decimal token that the contract stores for 10.0 tokens.
Show answer
Token transfers are transactions that call the token contract, and gas for them is paid in the network's native coin, ETH. With 0 ETH you cannot pay the fee, so the wallet cannot send. You must receive some ETH first. A 6-decimal token stores 10.0 as the integer 10000000 (10 times 10^6).
Exercise
A transfer of a token to a recipient shows "success" in the receipt, yet the recipient's balance rose by only 95 when you sent 100. Give one likely explanation and one way to confirm it.
Show answer
The token probably charges a fee or tax on transfers (fee-on-transfer), so the contract credits less than the amount requested while still reporting success. Confirm by reading the Transfer events in the receipt or on a block explorer, and by comparing the recipient's balanceOf before and after.