Using Soroswap with TypeScript
The Soroswap protocol allows you to interact with Stellar's smart contract platform: Soroban. In this section, we will explore how to write TypeScript scripts to use the contracts in our own automations or applications:
Prerequisites:
Before starting, it is necessary to clarify that to understand what we are doing here, you need to have a good understanding of TypeScript, smart contracts, and how a blockchain works. In addition, you need to know how to use stellar-sdk since we will use its TransactionBuilder class to create operations, simulate, sign, and send transactions. Additionally, some types and functions for transforming values.
[!Tip] If you need practical examples of how to create a transaction builder or how to use the SDK in general, you can guide yourself from our projects soroswap/core and paltalabs/mercury-client.
Build, sign & send:
[!Warning] For educational purposes, we will use an adaptation of the TransactionBuilder used in the soroswap/core repository. The code will need adjustments depending on your project and work methodology, so we recommend always working hand-in-hand with the official Stellar SDK Documentation to be able to build one tailored to your needs.
Installing Stellar SDK
In this guide, we will be using the ^11.2.2
version of Stellar SDK, available through npm or yarn as"@stellar/stellar-sdk". To do this, we will install it as follows:
or
Building the transaction:
In order to execute our operations on the blockchain, we will first need to create a transaction to send (Forward in this guide you will find the available methods and their predefined parameters):
First, we must create an instance of the router contract using the Contract class, giving as an argument the Address of the contract and using its call method, we create the operation delivering as arguments the method of the operation (for example: "swap_exact_assets_for_assets") and the parameters defined to then create the transaction with our TransactionBuilder:
[!Tip] If you need to thoroughly review all the methods available in the router contract or simply want to know how the contract works, you can review it directly here in the official repository of Soroswap.
[!Tip] To obtain the routerAddress respective to the network on which you want to operate, you can make a direct call to the Soroswap api in the following way:
Simulate, sign & send the transaction:
Once you have created the transaction, we must deliver it as an argument to our function to invoke transactions together with the keypair of the account with which we are going to operate. This function will be responsible for simulating the transaction (to verify the validity of this same one) and if everything is correct, we will proceed to assemble, sign and send the transaction:
After calling this function, we can inspect the response
object to verify that everything went as expected.
Methods:
[!Note] The operations available in the router contract that we will review in this documentation are:
Add_liquidity: "add_liquidity"
Remove_liquidity: "remove_liquidity"
Swap: "swap_exact_assets_for_assets"
Add liquidity to a pool:
[!Note]
method: "add_liquidity"
Reference
To add liquidity to a Soroswap pool (or deposit funds), we will need to define the following parameters:
[asset_a, asset_b]: These are the respective addresses of the asset pair to which we want to add liquidity.
[amount_a_desired, amount_b_desired]: These are the liquidity amounts you want to add to the respective assets.
[amount_a_min, amount_b_min]: These are the minimum amounts required to add to each asset, respectively.
account: This is the address where the tokens will be sent.
getCurrentTimePlusOneHour: This is the maximum date by which the transaction can be executed.
[!Note] All of these values must be converted to ScVal as shown below.
Remove liquidity from a pool:
[!Note]
method: "remove_liquidity"
Reference
To remove liquidity from a Soroswap pool (or withdraw funds), we will need to define the following parameters:
[asset_a, asset_b]: These are the respective addresses of the asset pair from which we want to remove liquidity.
Liquidity: This represents the desired amount of assets to remove from the liquidity pool.
[amount_a_min, amount_b_min]: These are the minimum amounts required to receive from each asset, respectively.
account: This is the address where the tokens will be sent.
getCurrentTimePlusOneHour: This is the maximum date by which the transaction can be executed.
[!Note] All of these values must be converted to ScVal as shown below.
Swap:
[!Note]
method: "swap_exact_assets_for_assets"
Reference
To create a Swap operation on Soroswap, we will need to define the following parameters:
amount_in: Represents the desired amount to be exchanged.
amount_out_min: Represents the minimum acceptable amount to receive for this operation.
path: Represents the exchange path to follow to obtain the requested asset.
account: Represents the account where the transaction will be executed.
getCurrentTimePlusOneHour: Represents the maximum date by which this transaction can be executed.
[!Note] All of these values must be converted to ScVal as shown below.
Finding the Most Optimal Path:
It is important to note that: the swap methods in the router will iterate through the path array step by step, performing the indicated exchanges between assets (0 <-> 1, 1 <-> 2, ... n <-> n+1) until the entire route is completed. This is why it is crucial to find the most optimal route to avoid wasting resources on unnecessary transactions.
This is why we at Soroswap have developed soroswap-router-sdk, a tool that helps you find the most efficient route for exchanging assets, taking into account the available reserves in Soroswap's liquidity pools.
To utilize this tool, we'll install the 1.2.4
version of Soroswap Router SDK into our project. It's available through npm or yarn as "soroswap-router-sdk".
or
Then, we import it into our project and use it to calculate the optimal path.
This will give us the route
object, which contains an ordered array of addresses representing the most optimal route for the exchange within the trade.path
property. If you need more information on how to use the Router-sdk or how it works, you can do it directly in the repository of soroswap/soroswap-router-sdk
Putting it All Together:
Once we have created our methods for interacting with the blockchain and defined the type of operation to be performed along with its parameters, we only need to call the functions to execute our transaction:
for this example we will perform a swap operation on testnet with a random account:
Last updated