Sub topic 5
Native coins and tokens
This chapter assumes you know what a private key and an address are, that a network (chain) is a separate ledger with its own rules, and that you have read the earlier chapters on accounts and networks. The code examples need basic JavaScript and the ethers v6 library.
Two kinds of value on one network
Most account-based networks hold two different kinds of value. A native coin is the asset built into the protocol itself. On Ethereum it is ether (ETH). On Polygon PoS it is POL. On BNB Smart Chain it is BNB. A token is an asset created by a smart contract that runs on the network. Examples are USDC, DAI and LINK.
The difference decides how a transfer works, what you pay, and how a mistake shows up.
| Property | Native coin | Token |
|---|---|---|
| Where the balance lives | In the account's own state, stored by the protocol | In the token contract's storage, as a table of address to number |
| How you send it | A transaction with a `value` field | A transaction that calls the token contract's `transfer` function |
| Who pays the fee (gas) | You, in the native coin | You, in the native coin, even when you send a token |
| Who defines the rules | The protocol | The contract's code and its owner |
| Can it be frozen or paused | Not by a contract | Sometimes, if the contract allows it |
The last row matters in practice. A token is only as trustworthy as its contract code.
Where the balance actually lives
Your wallet shows "5 USDC" next to "0.2 ETH" as if both sit in your account. They do not.
- Your 0.2 ETH is a field in your account's state on the network.
- Your 5 USDC is an entry in the USDC contract's own table. The entry says "this address owns this amount." Your account holds no tokens.
When you send a token, you do not move anything out of your account. You ask the contract to change two entries in its table: subtract from yours, add to the recipient's. The contract's transfer function does this, and it emits a Transfer event so that wallets and explorers can see it.
Two consequences follow:
- Gas is always paid in the native coin. An address with 100 USDC and 0 ETH cannot send that USDC on Ethereum. It has nothing to pay the fee with.
- The same address can be valid for many tokens. Because tokens are just contract tables, any address can receive any token without any setup. The wallet only displays tokens it has been told to look for.
Token standards
A token standard is an agreed interface, so wallets and apps can work with any token without custom code. The most common one on Ethereum-compatible networks is ERC-20, for fungible tokens, where every unit is interchangeable. Its main functions are:
balanceOf(owner)returns an address's balance.transfer(to, amount)moves tokens from the caller.approve(spender, amount)lets another address or contract spend up to a limit on your behalf.transferFrom(from, to, amount)is how an approved spender moves your tokens.decimals()says how many decimal places the amount uses.
Other standards cover other needs. ERC-721 is for unique items (NFTs), and ERC-1155 is for mixed collections. Other networks have their own: BNB Smart Chain uses the same ERC-20 interface (often labeled BEP-20), and Solana uses the SPL Token program, where balances live in separate token accounts. This chapter uses ERC-20 for its examples.
Decimals: the amount is an integer
Contracts do not store fractions. They store whole numbers in the token's smallest unit, and decimals tells you where to put the decimal point.
- ETH has 18 decimals. 1 ETH is
1000000000000000000wei. - USDC on Ethereum has 6 decimals. 1 USDC is
1000000units. - Other tokens vary. Some use 8 or 0.
If you assume 18 decimals for a 6-decimal token, you will send an amount a million million times too large or too small. Always read decimals() from the token contract, as the example below does with parseUnits.
Sending a native coin in code
This example signs and sends 0.01 of the native coin, waits for inclusion, and reads the receipt status. Use a testnet RPC URL and a throwaway key while learning, and replace the placeholder recipient.
import { JsonRpcProvider, Wallet, parseEther } from "ethers";
const provider = new JsonRpcProvider(process.env.RPC_URL);
const wallet = new Wallet(process.env.PRIVATE_KEY, provider);
const tx = await wallet.sendTransaction({
to: "0x0000000000000000000000000000000000000001",
value: parseEther("0.01"),
});
console.log("Submitted:", tx.hash);
const receipt = await tx.wait();
console.log("Status:", receipt.status === 1 ? "success" : "failed");The value field carries the coin. There is no contract call, and the recipient is a normal address.
Sending a token in code
Sending a token uses a contract call. The transaction goes to the token contract, not to the recipient. The recipient appears only inside the call data.
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));The abi array lists only the functions the script uses. The sender pays gas in the native coin, so the wallet needs some of it as well as the tokens.
A token transfer usually costs more gas than a native transfer. A plain native transfer uses 21,000 gas. An ERC-20 transfer typically uses about 45,000 to 65,000, because the contract reads and writes storage.
What to check before sending a token
Many losses happen because a user picks the wrong asset or the wrong contract. Check these:
- The network. The same token name can exist on several networks. USDC on Ethereum and USDC on Polygon are different contracts with different balances.
- The contract address. Anyone can deploy a token called "USDC." Only one contract is the real one. Copy the address from the issuer's official documentation, then compare it with what your wallet shows.
- The decimals. Let the wallet or library read them. Do not type raw units by hand.
- Gas balance. Confirm you hold enough of the native coin for the fee.
- The recipient's support. The receiving service must accept that token on that network.
Wrapped and bridged assets
Some tokens represent another asset. Wrapped ETH (WETH) is an ERC-20 token backed one-to-one by ETH held in a contract. It exists because ETH itself does not follow the ERC-20 interface, and many applications require that interface. You can swap between ETH and WETH by depositing into or withdrawing from the WETH contract.
A bridged token is a representation of an asset that lives on another network. A bridge locks or burns the original on one chain and issues a copy on the other. The copy carries the risk of the bridge as well as the risk of the asset. If the bridge fails, the copy may lose value even if the original does not. Some networks also hold two versions of the same asset, for example a native USDC and a bridged USDC, and exchanges may accept only one.
Fake and unwanted tokens
Because anyone can send any token to any address, your wallet can show tokens you never asked for. These are often scams. Typical patterns include:
- A token with a name that imitates a real one, sent in bulk to many addresses.
- A token whose name includes a link or a "claim your reward" message.
- A token that lets you buy but blocks you from selling.
Do not click links in token names, and do not approve spending for tokens you do not recognize. Receiving an unknown token costs you nothing and does no harm by itself. The harm comes when you interact with it. Most wallets let you hide such tokens.
Allowances: a token-only risk
Native coins have no equivalent of approve. With tokens, an app often asks you to approve it to spend up to some amount, and then moves your tokens with transferFrom. An approval for an unlimited amount stays active until you revoke it. If that app's contract is compromised, an attacker can use the allowance to drain that token from your account. Approve only the amount you need, and review old approvals from time to time with a tool such as your wallet's approval manager or a block explorer's token-approval checker.
Exercise
An address holds 250 USDC and 0 ETH on Ethereum. The owner tries to send 50 USDC to a friend and the wallet refuses. Explain why. Then say what the owner must do, and which value the script should read from the token contract to turn "50" into the correct integer amount.
Show answer
Sending a token is a contract call, and every transaction on Ethereum pays gas in the native coin, ETH. With 0 ETH the owner cannot pay the fee, so the transaction cannot be submitted. The owner must first receive a small amount of ETH on the same network, enough to cover the gas of a token transfer (roughly 45,000 to 65,000 gas at the current gas price). The script should read decimals() from the USDC contract (6 for USDC on Ethereum) and compute parseUnits("50", decimals), which gives 50000000.
Exercise
You see "USDC" in your wallet that you did not buy, and its name includes a website to "claim a bonus." What should you do, and why is the token not stealing from you just by being there?
Show answer
Do not visit the site or approve anything. Hide the token in the wallet. The token appears only because someone updated the token contract's table to list your address, which requires nothing from you and gives the sender no access to your funds. Risk begins when you sign an approval or a transaction that the scam site asks for.