token module contains types for interacting with token contracts, including the Stellar Asset Contract. It provides a standardized interface defined by SEP-41.
Overview
The token interface provides a standard way to interact with fungible tokens on Soroban, including transfers, allowances, balances, and metadata.Interfaces
TokenInterface
Standard interface for token contracts defined by SEP-41.TokenClient
Client for calling token contract functions.Balance Functions
balance
Returns the balance of an address.id- The address to query. Returns 0 if the address has no balance.
Transfer Functions
transfer
Transfers tokens from one address to another.from- The address holding the tokens (requires authorization)to- The recipient address (can be muxed)amount- The amount of tokens to transfer
transfer event with topics [from, to] and data {to_muxed_id, amount}.
Example:
transfer_from
Transfers tokens using an allowance.spender- The address authorizing the transfer (requires authorization)from- The address holding the tokensto- The recipient addressamount- The amount to transfer
spender has sufficient allowance from from.
Events: Emits a transfer event.
Example:
Allowance Functions
allowance
Returns the allowance for a spender.from- The address holding the tokensspender- The address authorized to spend
from’s balance.
approve
Sets an allowance for a spender.from- The address holding the tokens (requires authorization)spender- The address being authorizedamount- The amount to approveexpiration_ledger- Ledger number when the allowance expires
approve event with topics [from, spender] and data [amount, expiration_ledger].
Example:
Burn Functions
burn
Burns (destroys) tokens from an address.from- The address holding the tokens (requires authorization)amount- The amount to burn
burn event with topics [from] and data amount.
burn_from
Burns tokens using an allowance.spender- The address authorizing the burn (requires authorization)from- The address holding the tokensamount- The amount to burn
burn event.
Metadata Functions
decimals
Returns the number of decimals used to represent token amounts.name
Returns the name of the token.symbol
Returns the symbol of the token.StellarAssetInterface
Extended interface for Stellar Asset Contracts with admin capabilities.StellarAssetClient
Client for calling Stellar Asset Contract functions.Admin Functions (Stellar Asset Contract)
admin
Returns the admin address of the asset contract.set_admin
Sets a new administrator for the asset contract.new_admin- The new administrator address
set_admin event.
authorized
Checks if an address is authorized to use the token.set_authorized
Sets whether an address is authorized.id- The address to authorize/deauthorizeauthorize-trueto authorize,falseto deauthorize
set_authorized event.
mint
Mints new tokens to an address.to- The recipient addressamount- The amount to mint
mint event with topics [to] and data amount.
clawback
Claws back (burns) tokens from an address.from- The address to claw back fromamount- The amount to claw back
clawback event.
Examples
Simple Token Transfer
Token Swap with Allowances
Checking Token Balance
Best Practices
- Always require authorization: Call
require_auth()before token operations - Check balances: Verify sufficient balance before transfers
- Handle allowances carefully: Set appropriate expiration times for allowances
- Use events: Monitor token events for tracking transfers and approvals
- Test with Stellar Assets: Test your contract with both custom tokens and Stellar Asset Contracts
See Also
- Address - For authorization and addresses
- MuxedAddress - For multiplexed addresses
- SEP-41 - Token interface specification