Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✍️Improved docs of soroswap with typescript & cross contract calls #29

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions 03-technical-reference/04-SoroswapRouter.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,78 @@ fn router_get_amounts_in(e: Env, amount_out: i128, path: Vec<Address>) -> Vec<i1
Performs chained `getAmountIn` calculations on any number of pairs. Given an output amount, it calculates the required input amounts through the specified token path.

These library functions are used in the router to facilitate various operations, such as estimating amounts, performing swaps, and managing liquidity. They play a crucial role in ensuring accurate and efficient token exchange within the Soroswap ecosystem.

## Cross contract calls

### Prerequisites:
Ensure you have ``soroban-sdk`` version ``20.5.0`` added to your Cargo.toml file.

``` toml
[dependencies]
soroban-sdk = { version = "20.5.0" }
```

### Steps:

1. Import the Contract as .wasm
2. Make Contract Calls:
Inside the function of your contract where you want to interact with router contracts, use the following functions to make calls:


>[!Note]
>You can see all avaliable methods in the [Soroswap/core repo](https://github.com/soroswap/core/blob/main/contracts/router/src/lib.rs).


``` rust
use soroban_sdk::token::Client as TokenClient;

// Import the router contract as .wasm
soroban_sdk::contractimport!(
file = "./soroswap_router.optimized.wasm"
);

pub type SoroswapRouterClient<'a> = Client<'a>;

fn my_function(
e: Env,
//{...} any other args required for your function
) -> Result<(i128, i128, i128), ContractError> {
/*
Define the call parameters here...
*/

//Create an instance of the router client
let soroswap_router_client = SoroswapRouterClient::new(&e, &soroswap_router_address);
//Calls the swap function
let swap = soroswap_router_client.swap_exact_tokens_for_tokens(
&amount_in,
&amount_out_min,
&path,
&to,
&deadline,
)

//Calls the add_liquidity function
let add_liquidty = soroswap_router_client.add_liquidity(
&token_a,
&token_b,
&amount_a,
&amount_b,
&0,
&0,
&current_contract,
&deadline,
);

//Calls the remove_liquidity function
let remove_liquidty = soroswap_router_client.remove_liquidity(
&token_a,
&token_b,
&liquidity,
&amount_a_min,
&amount_b_min,
&to,
&deadline,
);
}
```
6 changes: 3 additions & 3 deletions 03-technical-reference/09-using-soroswap-with-TypeScript.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ The Soroswap protocol allows you to interact with Stellar's smart contract platf

### 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](https://stellar.github.io/js-stellar-sdk/#usage) since we will use its [TransactionBuilder](https://stellar.github.io/js-stellar-sdk/TransactionBuilder.html) class to create operations, simulate, sign, and send transactions. Additionally, some types and functions for transforming values.
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/stellar-sdk](https://stellar.github.io/js-stellar-sdk/#usage) since we will use its [TransactionBuilder](https://stellar.github.io/js-stellar-sdk/TransactionBuilder.html) 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](https://github.com/soroswap/core/tree/main/scripts) and [paltalabs/mercury-client.](https://github.com/paltalabs/mercury-client)
Expand All @@ -20,13 +20,13 @@ In this guide, we will be using the ` ^11.2.2 ` version of Stellar SDK, availab
To do this, we will install it as follows:

```bash
npm i soroswap-router-sdk@11.2.2
npm i @stellar/stellar-sdk@11.2.2
```

or

```bash
yarn add soroswap-router-sdk@11.2.2
yarn add @stellar/stellar-sdk@11.2.2
```

### Building the transaction:
Expand Down