Experiment the Pair contract
Once you are inside the soroban-preview-9 container (by bash quickstart.sh and bash run.sh) you can start experimenting with the SoroswapPair contract.
1. Open and read the contract:
Open in your favourite IDE or text editor, and check the pair folder:
In the pair/src folder you will find:
The
lib.rsfile with theSoroswapPairfunctionsThe
tokenfolder, which is a fork of thetokencontract fromsoroban-examples, with two added functions: Theinternal_burnand theinternal_mintfunction. As theSoroswapPaircontract is a token itself, if it's not making a cross-contract call to iself in order to mint or burn tokens, it will have problems withrequire_auth. Read more about this in the following issue: #16The
test.rs.The
events.rsto emmit eventsThe
factory.rsused as a Interface of some of the factory functionsThe
createfolder, that contains thecreate mod, used in testings in order to create the pair contract.
Also, you will see that in the root folder, there is a ./soroban_token_contract.wasm file. This was taken by compiling the []soroban-examples/token contract v0.8.4](https://github.com/stellar/soroban-examples/releases/tag/v0.8.4)
2. Compile the contract
We can compile both contracts by just calling make build from the root directory, however, we can go contract by contract:
cd pair
make build3. Read and run the tests:
4.- Experiment with the SoroswapFactory contract using the soroban CLI:
You can see the followng step-by-step. However, if you decide you can read and just run the initialize_factory.sh contract:
1.- First, set your enviromental variables
Here, as we are using the soroban-network docker network, containers can call each other just using their name. In the case of the stellar quickstart container, it's name is stellar:
Choose your network. In this example we will use standalone, but you can also use futurenet:
Then:
2.- Let's create two dummy tokens:
We need to create 2 tokens in order to interact with the Pair contract
Because the Pair token always uses token_a and token_b so token_a<token_b, this is something we need to check before initializing the pair contract with our two tokens. Later, this is something that will be done automatically by the Factory contract:
3.- Build, deploy and initialize the Pair contract
4.- Creare a USER account and mint tokens to that account.
5.- Deposit tokens in the PAIR contract
Now is the fun part! Interacting with our pair contract!!!
After this, you should receive a sucess message... But how do we know that we actually deposited 100 units of each token? The user shoud have new pair tokens, and it should have 100 token_a less and 100 token_b less!
6.- Swap tokens.
Once the SoroswapPair contract (which is a liquidity pool) has been initialized with some units of token_a and token_b, any user can perform a trade executing the swap function.
Here we will call the fn swap(e: Env, to: Address, buy_a: bool, out: i128, in_max: i128) function. If "buy_a" is true, the swap will buy token_a and sell token_b. This is flipped if "buy_a" is false "out" is the amount being bought, with in_max being a safety to make sure you receive at least that amount. The swap function will transfer the selling token "to" to this contract, and then the contract will transfer the buying token to "to".
7.- The final part: Withdraw
The final test is when the user want's to exit the liquidity pool and withdraw its tokens by giving back the pair_tokens that it has:
The function here will be
If you want to continue experimenting directly with the CLI, here aresome env variable setting that might be useful <3
You can go through all this steps just by doing:
In the next, we will experiment the creation of Pair contracts through the SoroswapFactory contract!
Last updated