Transfer Tokens
Send native currency (ETH/SOL) or tokens (ERC-20/SPL) to another address.
Installs viem, @solana/web3.js, @solana/spl-token
"use client";
import { useAccount, useSendTransaction, useWriteContract, useWaitForTransactionReceipt } from "wagmi";
import { parseEther, parseUnits } from "viem";
import { useState } from "react";
// ★ ERC-20 transfer ABI
const erc20TransferAbi = [
{
name: "transfer",
type: "function",
stateMutability: "nonpayable",
inputs: [
{ name: "to", type: "address" },
{ name: "amount", type: "uint256" },
],
outputs: [{ name: "", type: "bool" }],
},
] as const;
export function TransferNative() {
const [to, setTo] = useState("");
const [amount, setAmount] = useState("");
const { sendTransaction, data: hash, isPending } = useSendTransaction();
const { isLoading: isConfirming, isSuccess } = useWaitForTransactionReceipt({ hash });
const handleTransfer = () => {
sendTransaction({
to: to as `0x${string}`,
value: parseEther(amount),
});
};
return (
<div>
<h2>Transfer ETH</h2>
<input value={to} onChange={(e) => setTo(e.target.value)} placeholder="Recipient address (0x...)" />
<input value={amount} onChange={(e) => setAmount(e.target.value)} placeholder="Amount (e.g., 0.01)" />
<button onClick={handleTransfer} disabled={isPending}>
{isPending ? "Confirming..." : "Send"}
</button>
{isConfirming && <p>Waiting for confirmation...</p>}
{isSuccess && <p>Transfer confirmed! Tx: {hash}</p>}
</div>
);
}
export function TransferToken() {
const [tokenAddress, setTokenAddress] = useState("");
const [to, setTo] = useState("");
const [amount, setAmount] = useState("");
const [decimals, setDecimals] = useState("6");
const { writeContract, data: hash, isPending } = useWriteContract();
const { isLoading: isConfirming, isSuccess } = useWaitForTransactionReceipt({ hash });
const handleTransfer = () => {
writeContract({
address: tokenAddress as `0x${string}`,
abi: erc20TransferAbi,
functionName: "transfer",
args: [to as `0x${string}`, parseUnits(amount, Number(decimals))],
});
};
return (
<div>
<h2>Transfer ERC-20 Token</h2>
<input value={tokenAddress} onChange={(e) => setTokenAddress(e.target.value)} placeholder="Token contract address" />
<input value={to} onChange={(e) => setTo(e.target.value)} placeholder="Recipient address (0x...)" />
<input value={amount} onChange={(e) => setAmount(e.target.value)} placeholder="Amount (e.g., 10.5)" />
<input value={decimals} onChange={(e) => setDecimals(e.target.value)} placeholder="Decimals (e.g., 6)" />
<button onClick={handleTransfer} disabled={isPending}>
{isPending ? "Confirming..." : "Send"}
</button>
{isConfirming && <p>Waiting for confirmation...</p>}
{isSuccess && <p>Transfer confirmed! Tx: {hash}</p>}
</div>
);
}Transfer Tokens — Learn
How token transfers work on-chain
EVM: Updating a mapping
An ERC-20 transfer is a function call on the token's smart contract:
function transfer(address to, uint256 amount) public returns (bool) {
_balances[msg.sender] -= amount;
_balances[to] += amount;
emit Transfer(msg.sender, to, amount);
return true;
}
That's it. The contract decrements the sender's balance and increments the recipient's. The Transfer event is emitted for indexers and UIs to track.
Native ETH transfers are simpler — the chain itself moves the balance, no contract call needed.
Solana: Instruction-based transfers
Solana transfers work differently. Instead of calling a function on a contract, you create an "instruction" that the SPL Token program executes:
Instruction:
Program: SPL Token
Action: Transfer
From: sender's token account
To: recipient's token account
Authority: sender's wallet (must sign)
Amount: 1000000
The key difference: you're not calling a function on the token — you're telling the Token program to move tokens between accounts.
The Associated Token Account (ATA) pattern
On EVM, any address can hold any ERC-20 token — the balance is just a number in a mapping inside the token contract.
On Solana, each wallet needs a dedicated "token account" for each token type. The standard is the Associated Token Account (ATA):
ATA address = derivedFrom(wallet + mint + TOKEN_PROGRAM)
If the recipient has never held this token before, their ATA doesn't exist. The sender must create it (costs ~0.002 SOL in "rent") before transferring.
This is why Solana token transfers are slightly more complex — you may need to create an account before you can send tokens.
Transaction lifecycle
EVM
- Build the transaction (to, value, data)
- Wallet signs it
- Transaction broadcast to the network
- Miners/validators include it in a block
- Wait for the transaction receipt (confirmation)
Solana
- Build the transaction (instructions, recent blockhash)
- Wallet signs it
- Transaction sent to a validator
- Validator includes it in a slot
- Confirm with desired commitment level (processed → confirmed → finalized)
Gas and fees
EVM: Gas = computational cost. Each operation has a gas cost, and you pay gasUsed * gasPrice in the chain's native token. A simple ETH transfer costs ~21,000 gas. An ERC-20 transfer costs ~65,000 gas.
Solana: Fixed base fee of 5,000 lamports (~$0.0005) per signature, plus optional priority fees for faster inclusion. Much cheaper and more predictable than EVM gas.