11 : Use the native token (XLM) inside a smart contract.
Check this guide in https://token-playground.gitbook.io/
Edit this guide in it's repo: https://github.com/esteblock/token-playground/
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 write a smart contract that will interact with our XML balance!
2. A donations contract
In order to interact with our XML balance inside a Soroban smart contract, let's write a "donations" contract where any donor can send XML to the contract, and a "recipient" can then withdraw all the funds:
Check the code: All the code used in this chapter is available in https://github.com/esteblock/donations-dapp-soroban
Our contract will have this functions:
// Contract Trait
pub trait DonationsTrait {
// Sets the recepient address and the token that will be accepted as donation
fn initialize(e: Env, recipient: Address, token: Address);
// Donates amount units of the accepted token
fn donate(e: Env, donor: Address, amount: i128);
// Transfer all the accumulated donations to the recipient. Can be called by anyone
fn withdraw(e: Env);
// Get the token address that is accepted as donations
fn token(e:Env) -> Address;
// Get the donations recipient address
fn recipient(e:Env) -> Address;
}The full code will be:
2. Testing the contract with rs-soroban-sdk
The first thing we allways do when we write an smart contract is to test it inside a test.rs file and we test it with make test. This will test the contract in a Soroban environment provided by rs-soroban-sdk. (the rust soroban-sdk)
How can we tell the contract that we want to use the native XML? Well.... I an not pretty sure... and this is why I am opening this discussion on Discord (here)[https://discord.com/channels/897514728459468821/1145462925109231726/1145462925109231726]
In the test.rs file you'll find 2 tests. One is for any type of tokens, and works perfect. The second test is ment to be only for the native XML token....
When you create the XML native token inside test.rs you get:
Until there everything is OK, but if you'll later want to check any user's balance.... how van we do it inside test.rs? I get these errors:
3. Testing the contract inside a Quickstart Standalone blockchain and soroban-cli
Because tests in soroban-sdk did not work very well, we'll then deploy the contract in a real quickstart blockchain and we'll tell the contract that we'll use the nattive token address used in the previous chapers.
Check the code in: https://github.com/esteblock/donations-dapp-soroban/blob/main/test_soroban_cli.sh
Specifically we'll set:
and then:
Also, when checking for accounts balances, in order to be sure, we'll check the balance both using the native token contract address and the classic way with node and the js-soroban-sdk (the javascript soroban-sdk)
If you wanna do the whole tests, just follow the instructions in the repo! You should get:
The result should be:
Why is the first donation failing? I don't know yet. Opened Discussion in Discord (here)[https://discord.com/channels/897514728459468821/1145688416432963705/1145688416432963705]
This Playground chapter has been written by @esteblock
Last updated