A standard interface for fungible tokens on Chert Coin blockchain, similar to ERC-20 on Ethereum.
- ✅ Transfer - Send tokens between accounts
- ✅ Approve/TransferFrom - Delegated transfers via allowances
- ✅ Balance Queries - Check account balances
- ✅ Total Supply - Query total token supply
- ✅ Mint - Create new tokens (owner only)
- ✅ Metadata - Token name, symbol, and decimals
- ✅ Events - Transfer and Approval events for indexing
fn initialize()Initializes the token contract with metadata and mints initial supply to deployer.
Events:
Transfer { from: "0x0", to: deployer, amount: initial_supply }
fn transfer(to: String, amount: u64)Transfers tokens from sender to recipient.
Requirements:
- Sender must have sufficient balance
- Amount must be > 0
Events:
Transfer { from: sender, to: recipient, amount }
fn approve(spender: String, amount: u64)Approves a spender to transfer tokens on behalf of the sender.
Events:
Approval { owner: sender, spender, amount }
fn transfer_from(from: String, to: String, amount: u64)Transfers tokens from one account to another using an allowance.
Requirements:
- Caller must have sufficient allowance
- From account must have sufficient balance
Events:
Transfer { from, to, amount }
fn balance_of(account: String) -> u64Returns the token balance of an account.
fn total_supply() -> u64Returns the total token supply.
fn decimals() -> u8Returns the number of decimals (e.g., 18).
fn mint(to: String, amount: u64)Mints new tokens to an address. Only callable by contract owner.
Requirements:
- Caller must be contract owner
- Must not cause overflow
Events:
Transfer { from: "0x0", to, amount }
# Build optimized WASM
cargo build --target wasm32-unknown-unknown --release
# The output will be in:
# target/wasm32-unknown-unknown/release/crc20_token.wasm// Deploy the contract
let wasm_code = include_bytes!("crc20_token.wasm");
let contract_address = client.deploy_contract(wasm_code).await?;
// Initialize with metadata
client.call_contract(
contract_address,
"initialize",
&InitializeArgs {
name: "My Token",
symbol: "MTK",
decimals: 18,
initial_supply: 1_000_000_000_000,
}
).await?;// Transfer tokens
client.call_contract(
token_address,
"transfer",
&TransferArgs {
to: "recipient_address",
amount: 100_000_000_000,
}
).await?;
// Approve spending
client.call_contract(
token_address,
"approve",
&ApproveArgs {
spender: "spender_address",
amount: 50_000_000_000,
}
).await?;
// Query balance
let balance: u64 = client.query_contract(
token_address,
"balance_of",
&BalanceOfArgs {
account: "account_address",
}
).await?;Transfer {
from: String,
to: String,
amount: u64,
}Emitted when tokens are transferred.
Approval {
owner: String,
spender: String,
amount: u64,
}Emitted when an allowance is set.
- ✅ Overflow protection on all arithmetic operations
- ✅ Balance checks before transfers
- ✅ Allowance checks before delegated transfers
- ✅ Owner-only mint function
- ✅ Input validation
MIT License