Skip to content

Commit

Permalink
Add swap router (#29)
Browse files Browse the repository at this point in the history
* Add swap router and modify trade minimum amount calculation

This commit introduces the swap router module that provides the functionality to generate call parameters for a trade. It also modifies the `minimum_amount_out` method in `trade.rs` by adding an optional `amount_out` argument. This allows us to either use a given amount or calculate the output amount based on the current trade. The changes to the function is also reflected in all relevant test cases. The package version is updated to 0.23.0.

* Refactor test cases to use a common test utility module

Refactored the test cases across various modules to use a newly introduced common test utility module. This change reduces code duplication and simplifies the tests. The common test utility module centralizes the definition of frequently used entities such as tokens and pools.
  • Loading branch information
shuhuiluo authored Feb 15, 2024
1 parent 9d1bdbb commit 6a664cc
Show file tree
Hide file tree
Showing 15 changed files with 466 additions and 329 deletions.
12 changes: 6 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "uniswap-v3-sdk"
version = "0.22.0"
version = "0.23.0"
edition = "2021"
authors = ["Shuhui Luo <twitter.com/aureliano_law>"]
description = "Uniswap V3 SDK for Rust"
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
A Rust SDK for building applications on top of Uniswap V3. Migration from the
TypeScript [Uniswap/v3-sdk](https://github.com/Uniswap/v3-sdk).

WIP.
It is feature-complete with unit tests matching the TypeScript SDK. But error handling is not as graceful as one may
expect. The error handling is still a work in progress.

## Features

Expand All @@ -23,7 +24,7 @@ WIP.
Add the following to your `Cargo.toml` file:

```toml
uniswap-v3-sdk = { version = "0.20.0", features = ["extensions"] }
uniswap-v3-sdk = { version = "0.23.0", features = ["extensions"] }
```

### Usage
Expand Down
48 changes: 48 additions & 0 deletions src/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,4 +228,52 @@ sol! {
uint256 gasEstimate
);
}

interface ISwapRouter {
struct ExactInputSingleParams {
address tokenIn;
address tokenOut;
uint24 fee;
address recipient;
uint256 deadline;
uint256 amountIn;
uint256 amountOutMinimum;
uint160 sqrtPriceLimitX96;
}

function exactInputSingle(ExactInputSingleParams calldata params) external payable returns (uint256 amountOut);

struct ExactInputParams {
bytes path;
address recipient;
uint256 deadline;
uint256 amountIn;
uint256 amountOutMinimum;
}

function exactInput(ExactInputParams calldata params) external payable returns (uint256 amountOut);

struct ExactOutputSingleParams {
address tokenIn;
address tokenOut;
uint24 fee;
address recipient;
uint256 deadline;
uint256 amountOut;
uint256 amountInMaximum;
uint160 sqrtPriceLimitX96;
}

function exactOutputSingle(ExactOutputSingleParams calldata params) external payable returns (uint256 amountIn);

struct ExactOutputParams {
bytes path;
address recipient;
uint256 deadline;
uint256 amountOut;
uint256 amountInMaximum;
}

function exactOutput(ExactOutputParams calldata params) external payable returns (uint256 amountIn);
}
}
21 changes: 1 addition & 20 deletions src/entities/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,29 +450,10 @@ impl<P> PartialEq for Pool<P> {
#[cfg(test)]
mod tests {
use super::*;
use uniswap_sdk_core::token;
use crate::tests::*;

const ONE_ETHER: U256 = U256::from_limbs([10u64.pow(18), 0, 0, 0]);

static USDC: Lazy<Token> = Lazy::new(|| {
token!(
1,
"0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
6,
"USDC",
"USD Coin"
)
});
static DAI: Lazy<Token> = Lazy::new(|| {
token!(
1,
"0x6B175474E89094C44Da98b954EedeAC495271d0F",
18,
"DAI",
"DAI Stablecoin"
)
});

mod constructor {
use super::*;

Expand Down
20 changes: 1 addition & 19 deletions src/entities/position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,28 +451,10 @@ impl<P> PartialEq for Position<P> {
#[cfg(test)]
mod tests {
use super::*;
use crate::tests::*;
use alloy_primitives::uint;
use once_cell::sync::Lazy;
use uniswap_sdk_core::token;

static USDC: Lazy<Token> = Lazy::new(|| {
token!(
1,
"0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
6,
"USDC",
"USD Coin"
)
});
static DAI: Lazy<Token> = Lazy::new(|| {
token!(
1,
"0x6B175474E89094C44Da98b954EedeAC495271d0F",
18,
"DAI",
"DAI Stablecoin"
)
});
static POOL_SQRT_RATIO_START: Lazy<U256> =
Lazy::new(|| encode_sqrt_ratio_x96(BigInt::from(10).pow(8), BigInt::from(10).pow(20)));
static POOL_TICK_CURRENT: Lazy<i32> =
Expand Down
43 changes: 1 addition & 42 deletions src/entities/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,53 +105,12 @@ impl<TInput: CurrencyTrait, TOutput: CurrencyTrait, P> Route<TInput, TOutput, P>
#[cfg(test)]
mod tests {
use super::*;
use crate::prelude::*;
use crate::{prelude::*, tests::*};
use once_cell::sync::Lazy;
use uniswap_sdk_core::token;

static ETHER: Lazy<Ether> = Lazy::new(|| Ether::on_chain(1));
static TOKEN0: Lazy<Token> =
Lazy::new(|| token!(1, "0x0000000000000000000000000000000000000001", 18, "t0"));
static TOKEN1: Lazy<Token> =
Lazy::new(|| token!(1, "0x0000000000000000000000000000000000000002", 18, "t1"));
static TOKEN2: Lazy<Token> =
Lazy::new(|| token!(1, "0x0000000000000000000000000000000000000003", 18, "t2"));
static WETH: Lazy<Token> = Lazy::new(|| WETH9::new().get(1).unwrap().clone());

mod path {
use super::*;

static POOL_0_1: Lazy<Pool<NoTickDataProvider>> = Lazy::new(|| {
Pool::new(
TOKEN0.clone(),
TOKEN1.clone(),
FeeAmount::MEDIUM,
encode_sqrt_ratio_x96(1, 1),
0,
)
.unwrap()
});
static POOL_0_WETH: Lazy<Pool<NoTickDataProvider>> = Lazy::new(|| {
Pool::new(
TOKEN0.clone(),
WETH.clone(),
FeeAmount::MEDIUM,
encode_sqrt_ratio_x96(1, 1),
0,
)
.unwrap()
});
static POOL_1_WETH: Lazy<Pool<NoTickDataProvider>> = Lazy::new(|| {
Pool::new(
TOKEN1.clone(),
WETH.clone(),
FeeAmount::MEDIUM,
encode_sqrt_ratio_x96(1, 1),
0,
)
.unwrap()
});

#[test]
fn constructs_a_path_from_the_tokens() {
let route = Route::new(vec![POOL_0_1.clone()], TOKEN0.clone(), TOKEN1.clone());
Expand Down
Loading

0 comments on commit 6a664cc

Please sign in to comment.