10 : Write the native token (XLM) using soroban-cli.

Token Playground Chapter 10 : Write the native token (XLM) contract using soroban-cli

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 see how to interact and write (call functions that change the state of the Blockchain) the native token (XLM) smart contract inside Soroban using soroban-cli.

Specifically we will call the transfer, approve and transfer_from functions:

2. Setting up our environment

Configure soroban-cli, wrap the native token (see chapter 8) and get its address:

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"

echo Adding network
soroban config network add "$NETWORK" \
  --rpc-url "$SOROBAN_RPC_URL" \
  --network-passphrase "$SOROBAN_NETWORK_PASSPHRASE"
echo ---

soroban config identity generate my-account
MY_ACCOUNT_ADDRESS="$(soroban config identity address my-account)"
curl  --silent -X POST "$FRIENDBOT_URL?addr=$MY_ACCOUNT_ADDRESS" > null
ARGS="--network standalone --source-account my-account"

echo Wrapping token
TOKEN_ADDRESS=$(soroban lab token wrap $ARGS --asset native)
echo Wrapped with address result: $TOKEN_ADDRESS
echo ---

echo Wrapping might fail if it was done before, so we are also getting the address:
TOKEN_ADDRESS="$(soroban lab token id --asset native --network standalone)"
echo Native token address: $TOKEN_ADDRESS
echo ---

3. Setting up your identities

In this chapter we will use a my-account, spender and recipient accounts. The my-account account will be the main account that will holds XLM and send them to the recipient. Also, the my-account will allow the spender to send XLM on his behalf:

4. Transfer XLM

  1. Check the initial balance of the recipient

The result should be:

  1. Call the transfer function using my-account as source-account

  1. Check the new balance of the recipient

The result should be:

5. Using transfer_from

The result should be:

6. Use our code

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:

Check all the code in the repo!

What is next?

In this chapter we used the native token contract address in order to send some XLM using the transfer, aprove and transfer_from functions.

In the next chapter we'll use the native token inside our own written smart contract!

Are you ready?


This Playground chapter has been written by @esteblock

Last updated