Rise In Logo



Build on Stellar

What is the Token Interface?

The Token Interface on Soroban sets a common design for creating token systems. It includes key features like managing permissions, moving tokens, and checking token amounts. This design helps different parts of the Soroban network work well together, making it easier for various programs and services to connect and use tokens.

A Tip: If you don’t know about interfaces, you can think like this. An interface is like a set of rules or a blueprint that different systems follow to communicate or work together.

Below you can see the Token Interface. When you implement this interface to create your custom token you must define all of these functions: “allowance, approve, balance”.

pub trait TokenInterface {
  fn allowance(env: Env, from: Address, spender: Address) -> i128;
  fn approve(env: Env, from: Address, spender: Address, amount: i128, live_until_ledger: u32);
  fn balance(env: Env, id: Address) -> i128;
  fn transfer(env: Env, from: Address, to: Address, amount: i128);
  fn transfer_from(env: Env, spender: Address, from: Address, to: Address, amount: i128);
  fn burn(env: Env, from: Address, amount: i128);
  fn burn_from(env: Env, spender: Address, from: Address, amount: i128);
  fn decimals(env: Env) -> u32;
  fn name(env: Env) -> String;
  fn symbol(env: Env) -> String;
}

Let’s take a look at these functions:

allowance:

fn allowance(env: Env, from: Address, spender: Address) -> i128;

The allowance function is used to check how many tokens a given spender is allowed to transfer on behalf of the token owner (from address). It takes three parameters: the environment (env), the owner's address (from), and the spender's address (spender). The function returns the amount of tokens (as i128) that the spender is authorized to use.

approve:

fn approve(env: Env, from: Address, spender: Address, amount: i128, live_until_ledger: u32);

The approve function allows a token holder to authorize a specified spender address to spend a certain amount of tokens from their account, up until a specified “live_until_ledger” ledger number. This function is crucial for enabling delegated token spending, ensuring that the spender's allowance is explicitly approved by the token holder for a limited duration or until manually revoked.

balance:

fn balance(env: Env, id: Address) -> i128;

The balance function returns the contract's current balance.

transfer: 

fn transfer(env: Env, from: Address, to: Address, amount: i128);

This function transfers tokens from one account to another. The amount and recipient of the transferred tokens are specified. The sender and recipient balances are also updated accordingly. Permission check is done for the transfer operation. 

transfer_from:

fn transfer_from(env: Env, spender: Address, from: Address, to: Address, amount: i128);

This function allows an account to transfer tokens on behalf of another account. The amount, sender, and recipient of the transferred tokens are specified. The sender and recipient balances are also updated accordingly. Permission check is done for the transfer operation.

burn:

fn burn(env: Env, from: Address, amount: i128);

This function allows the owner of the contract to destroy existing tokens. The amount and owner of the destroyed tokens are specified. The total supply is also reduced accordingly.

burn_from:

fn burn_from(env: Env, spender: Address, from: Address, amount: i128);

This function is used to burn a certain amount of tokens from a specific account. Burning tokens means reducing the supply of tokens. To call this function, the authorization of the token holder or an approved spender is required. In addition, the token holder must have sufficient funds in their account. This function issues an event when a token burn occurs.

symbol:

fn symbol(env: Env) -> String;

This function determines the shorthand symbol of the token. For example, "XLM".

name:

fn name(env: Env) -> String;

This function determines the full name of the token. For example, "Stellar Lumen".

decimals:

fn decimals(env: Env) -> u32;

This function determines how many decimal places the token has. For example, a token with 7 decimal places can be divided into 0.0000001 token units.

Comments

Anonymous

0/500

You need to enroll in the course to be able to comment!

Stay in the know

Never miss updates on new programs and opportunities.

Rise In Logo

Rise together in web3!