Contribute to this guide in the ./docs folder of the repo
1. Introduction
What about when we want to use XLM inside a Soroban smart contract? How do we trigger those transactions? Can we trigger transactions on behalf the user using the require_auth method?
In this chapter we will use our XLM inside Soroban using soroban-cli!
2. The native Stellar Lumens (XLM) contract address
Soroban is great! And in order to use the native XLM tokens, we just need to treat it as another asset that behaves as an Stellar Asset Contract (SAC). It indeed has it's own contract address.
We will call this the native token address
This contract address is unique per network (standalone, futurenet... as well as it will later be with testnet and mainnet), so be sure to call it correctly.
Set up your environment: Basic setup
NETWORK="standalone"SOROBAN_RPC_HOST="http://stellar:8000"SOROBAN_RPC_URL="$SOROBAN_RPC_HOST/soroban/rpc"FRIENDBOT_URL="$SOROBAN_RPC_HOST/friendbot"SOROBAN_NETWORK_PASSPHRASE="Standalone Network ; February 2017"echoAddingnetworksorobanconfignetworkadd"$NETWORK" \--rpc-url"$SOROBAN_RPC_URL" \--network-passphrase"$SOROBAN_NETWORK_PASSPHRASE"
In order to wrap any token you'll need to spend some XLM. So you need to call this function from a funded account:
Wrap the native token: In order to get the XLM "contract address" you first need to "wrap" the native asset it into a token inside Soroban. This can be done only once, but you'll be needing to do it each time you open a new Standalone instance.
However,
If you use only Futurenet, you'll probably never need to do this:
This command will return the address, so no need for the next step. If this commands fails, this means that the token has already been wrapped before :)
Get the native token's contracts address: Once the native token has been wrapped, you can also it's address like this:
So, can we use this address as if it was any other token that complies with the token interface?
Yes! So let's do it.
Implementing the token intrface means that the contract will have the following functions: (please take a look at the link above)
pubtraitContract {// --------------------------------------------------------------------------------// Admin interface – privileged functions.// --------------------------------------------------------------------------------//// All the admin functions have to be authorized by the admin with all input// arguments, i.e. they have to call `admin.require_auth()`./// Clawback "amount" from "from" account. "amount" is burned./// Emit event with topics = ["clawback", admin: Address, to: Address], data = [amount: i128]fnclawback( env: soroban_sdk::Env, from:Address, amount:i128, );/// Mints "amount" to "to"./// Emit event with topics = ["mint", admin: Address, to: Address], data = [amount: i128]fnmint( env: soroban_sdk::Env, to:Address, amount:i128, );/// Sets the administrator to the specified address "new_admin"./// Emit event with topics = ["set_admin", admin: Address], data = [new_admin: Address]fnset_admin( env: soroban_sdk::Env, new_admin:Address, );/// Sets whether the account is authorized to use its balance./// If "authorized" is true, "id" should be able to use its balance./// Emit event with topics = ["set_authorized", id: Address], data = [authorize: bool]fnset_authorized( env: soroban_sdk::Env, id:Address, authorized:bool, );// --------------------------------------------------------------------------------// Token interface// --------------------------------------------------------------------------------//// All the functions here have to be authorized by the token spender// (usually named `from` here) using all the input arguments, i.e. they have// to call `from.require_auth()`./// Set the allowance by "amount" for "spender" to transfer/burn from "from"./// "expiration_ledger" is the ledger number where this allowance expires. It cannot/// be less than the current ledger number unless the amount is being set to 0./// An expired entry (where "expiration_ledger" < the current ledger number)/// should be treated as a 0 amount allowance./// Emit event with topics = ["approve", from: Address, spender: Address], data = [amount: i128, expiration_ledger: u32]fnapprove( env: soroban_sdk::Env, from:Address, spender:Address, amount:i128, expiration_ledger:u32, );/// Transfer "amount" from "from" to "to"./// Emit event with topics = ["transfer", from: Address, to: Address], data = [amount: i128]fntransfer( env: soroban_sdk::Env, from:Address, to:Address, amount:i128, );/// Transfer "amount" from "from" to "to", consuming the allowance of "spender"./// Authorized by spender (`spender.require_auth()`)./// Emit event with topics = ["transfer", from: Address, to: Address], data = [amount: i128]fntransfer_from( env: soroban_sdk::Env, spender:Address, from:Address, to:Address, amount:i128, );/// Burn "amount" from "from"./// Emit event with topics = ["burn", from: Address], data = [amount: i128]fnburn( env: soroban_sdk::Env, from:Address, amount:i128, );/// Burn "amount" from "from", consuming the allowance of "spender"./// Emit event with topics = ["burn", from: Address], data = [amount: i128]fnburn_from( env: soroban_sdk::Env, spender:Address, from:Address, amount:i128, );// --------------------------------------------------------------------------------// Read-only Token interface// --------------------------------------------------------------------------------//// The functions here don't need any authorization and don't emit any// events./// Get the balance of "id".fnbalance(env: soroban_sdk::Env, id:Address) ->i128;/// Get the spendable balance of "id". This will return the same value as balance()/// unless this is called on the Stellar Asset Contract, in which case this can/// be less due to reserves/liabilities.fnspendable_balance(env: soroban_sdk::Env id:Address) ->i128;// Returns true if "id" is authorized to use its balance.fnauthorized(env: soroban_sdk::Env, id:Address) ->bool;/// Get the allowance for "spender" to transfer from "from".fnallowance( env: soroban_sdk::Env, from:Address, spender:Address, ) ->i128;// --------------------------------------------------------------------------------// Descriptive Interface// --------------------------------------------------------------------------------// Get the number of decimals used to represent amounts of this token.fndecimals(env: soroban_sdk::Env) ->u32;// Get the name for this token.fnname(env: soroban_sdk::Env) -> soroban_sdk::Bytes;// Get the symbol for this token.fnsymbol(env: soroban_sdk::Env) -> soroban_sdk::Bytes;}
4. Check your balance using soroban-cli and the native token's contract address.
We have the native token's address and the functions names we can call. We just need to call it!
0.- Set your environment. Here we suppose that you are using the soroban-preview:10 image as it was explained in previous chapters:
TOKEN_ADDRESS="$(sorobanlabtokenid--assetnative--networkstandalone)"NETWORK="standalone"SOROBAN_RPC_HOST="http://stellar:8000"SOROBAN_RPC_URL="$SOROBAN_RPC_HOST/soroban/rpc"FRIENDBOT_URL="$SOROBAN_RPC_HOST/friendbot"SOROBAN_NETWORK_PASSPHRASE="Standalone Network ; February 2017"sorobanconfignetworkadd"$NETWORK" \--rpc-url"$SOROBAN_RPC_URL" \--network-passphrase"$SOROBAN_NETWORK_PASSPHRASE"ARGS="--network standalone --source-account my-account"
If you want to use our code in the Token Playground's Repo, you can just call our script with the soroban-preview-10 docker containter
You can run it by:
bashsrc/chapter9/native_XLM_soroban_read.sh
Check all the code in the repo!
What is next?
In this chapter we wrapped the native token, read it's address, generated a token WASM and interacted with these using soroban-cli in order to read some information about the native asset (balance, name)!
In the next chapter we'll use this contract address in order to send some XLM!
Are you ready?
This Playground chapter has been written by @esteblock