8 : Use the native Stellar Lumens (XLM) the classic way.

Token Playground Chapter 8 : Use the native Stellar Lumens (XLM) the classic way.

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 start using XLM, asking for our balance the classic way (without Soroban). In the next chapter we will use Soroban!

2. Check your XLM's own balance using the classic way

Let's do our first experiment with the native XLM contract. We will create a new account, fund it with friendbot and check our balance in the classic way.

0.- Run the quickstart.sh script and enter into the soroban-preview container. Read chapter 1 and 2.

bash quickstart.sh
bash run.sh

1.- Create a new account:

soroban config identity generate my-account
MY_ACCOUNT_ADDRESS="$(soroban config identity address my-account)"

2.- Fund it with friendbot. Here I'll assume you are inside the soroban-preview containter

SOROBAN_RPC_HOST="http://stellar:8000"
FRIENDBOT_URL="$SOROBAN_RPC_HOST/friendbot"
curl  -X POST "$FRIENDBOT_URL?addr=$MY_ACCOUNT_ADDRESS"

3.- Check your balance with the classic way Using javascript and node, and assuming that MY_ACCOUNT_ADDRESS="GC6IYOXRAGMVSUDCAHBGBP2ZCAMZVNIF7CZCBY6545EQMIDNQQZCOY5G"

var user_address="GC6IYOXRAGMVSUDCAHBGBP2ZCAMZVNIF7CZCBY6545EQMIDNQQZCOY5G"
var server = new StellarSdk.Server(settings.horizonUrl, {allowHttp: true});

server.loadAccount(user_address)
  .then(account => {
    // Find the XLM balance
    const xlmBalance = account.balances.find(balance => balance.asset_type === 'native');

    console.log(`Your XLM balance: ${xlmBalance.balance}`);
  })
  .catch(error => {
    console.error('Error loading account:', error);
  });

You'll get a result like this:

Your XLM balance: 10000.0000000

3. 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:

bash src/chapter8/use_XLM_native.sh 

Check all the code in the repo!

What is next?

In this chapter we created a new Stellar account and we fund it with 10000 XLM and we managed to check it's balance in the classic way. In the next chapter we will check its balance using smart contracts in Soroban!

Are you ready?


This Playground chapter has been written by @esteblock

Last updated