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

Add UniswapV3 liquidity to driver #388

Merged
merged 17 commits into from
Aug 5, 2022
Merged
Show file tree
Hide file tree
Changes from 10 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
7 changes: 5 additions & 2 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/contracts/artifacts/UniswapV3SwapRouter.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"abi":[{"inputs":[{"internalType":"address","name":"_factory","type":"address"},{"internalType":"address","name":"_WETH9","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[],"name":"WETH9","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"components":[{"internalType":"bytes","name":"path","type":"bytes"},{"internalType":"address","name":"recipient","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"},{"internalType":"uint256","name":"amountIn","type":"uint256"},{"internalType":"uint256","name":"amountOutMinimum","type":"uint256"}],"internalType":"struct ISwapRouter.ExactInputParams","name":"params","type":"tuple"}],"name":"exactInput","outputs":[{"internalType":"uint256","name":"amountOut","type":"uint256"}],"stateMutability":"payable","type":"function"},{"inputs":[{"components":[{"internalType":"address","name":"tokenIn","type":"address"},{"internalType":"address","name":"tokenOut","type":"address"},{"internalType":"uint24","name":"fee","type":"uint24"},{"internalType":"address","name":"recipient","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"},{"internalType":"uint256","name":"amountIn","type":"uint256"},{"internalType":"uint256","name":"amountOutMinimum","type":"uint256"},{"internalType":"uint160","name":"sqrtPriceLimitX96","type":"uint160"}],"internalType":"struct ISwapRouter.ExactInputSingleParams","name":"params","type":"tuple"}],"name":"exactInputSingle","outputs":[{"internalType":"uint256","name":"amountOut","type":"uint256"}],"stateMutability":"payable","type":"function"},{"inputs":[{"components":[{"internalType":"bytes","name":"path","type":"bytes"},{"internalType":"address","name":"recipient","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"},{"internalType":"uint256","name":"amountOut","type":"uint256"},{"internalType":"uint256","name":"amountInMaximum","type":"uint256"}],"internalType":"struct ISwapRouter.ExactOutputParams","name":"params","type":"tuple"}],"name":"exactOutput","outputs":[{"internalType":"uint256","name":"amountIn","type":"uint256"}],"stateMutability":"payable","type":"function"},{"inputs":[{"components":[{"internalType":"address","name":"tokenIn","type":"address"},{"internalType":"address","name":"tokenOut","type":"address"},{"internalType":"uint24","name":"fee","type":"uint24"},{"internalType":"address","name":"recipient","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"},{"internalType":"uint256","name":"amountOut","type":"uint256"},{"internalType":"uint256","name":"amountInMaximum","type":"uint256"},{"internalType":"uint160","name":"sqrtPriceLimitX96","type":"uint160"}],"internalType":"struct ISwapRouter.ExactOutputSingleParams","name":"params","type":"tuple"}],"name":"exactOutputSingle","outputs":[{"internalType":"uint256","name":"amountIn","type":"uint256"}],"stateMutability":"payable","type":"function"},{"inputs":[],"name":"factory","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes[]","name":"data","type":"bytes[]"}],"name":"multicall","outputs":[{"internalType":"bytes[]","name":"results","type":"bytes[]"}],"stateMutability":"payable","type":"function"},{"inputs":[],"name":"refundETH","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"value","type":"uint256"},{"internalType":"uint256","name":"deadline","type":"uint256"},{"internalType":"uint8","name":"v","type":"uint8"},{"internalType":"bytes32","name":"r","type":"bytes32"},{"internalType":"bytes32","name":"s","type":"bytes32"}],"name":"selfPermit","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"nonce","type":"uint256"},{"internalType":"uint256","name":"expiry","type":"uint256"},{"internalType":"uint8","name":"v","type":"uint8"},{"internalType":"bytes32","name":"r","type":"bytes32"},{"internalType":"bytes32","name":"s","type":"bytes32"}],"name":"selfPermitAllowed","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"nonce","type":"uint256"},{"internalType":"uint256","name":"expiry","type":"uint256"},{"internalType":"uint8","name":"v","type":"uint8"},{"internalType":"bytes32","name":"r","type":"bytes32"},{"internalType":"bytes32","name":"s","type":"bytes32"}],"name":"selfPermitAllowedIfNecessary","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"value","type":"uint256"},{"internalType":"uint256","name":"deadline","type":"uint256"},{"internalType":"uint8","name":"v","type":"uint8"},{"internalType":"bytes32","name":"r","type":"bytes32"},{"internalType":"bytes32","name":"s","type":"bytes32"}],"name":"selfPermitIfNecessary","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"amountMinimum","type":"uint256"},{"internalType":"address","name":"recipient","type":"address"}],"name":"sweepToken","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"amountMinimum","type":"uint256"},{"internalType":"address","name":"recipient","type":"address"},{"internalType":"uint256","name":"feeBips","type":"uint256"},{"internalType":"address","name":"feeRecipient","type":"address"}],"name":"sweepTokenWithFee","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"int256","name":"amount0Delta","type":"int256"},{"internalType":"int256","name":"amount1Delta","type":"int256"},{"internalType":"bytes","name":"_data","type":"bytes"}],"name":"uniswapV3SwapCallback","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountMinimum","type":"uint256"},{"internalType":"address","name":"recipient","type":"address"}],"name":"unwrapWETH9","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountMinimum","type":"uint256"},{"internalType":"address","name":"recipient","type":"address"},{"internalType":"uint256","name":"feeBips","type":"uint256"},{"internalType":"address","name":"feeRecipient","type":"address"}],"name":"unwrapWETH9WithFee","outputs":[],"stateMutability":"payable","type":"function"},{"stateMutability":"payable","type":"receive"}]}
nlordell marked this conversation as resolved.
Show resolved Hide resolved
5 changes: 5 additions & 0 deletions crates/contracts/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,11 @@ fn main() {
.add_network_str("4", "0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D")
.add_network_str("5", "0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D")
});
generate_contract_with_config("UniswapV3SwapRouter", |builder| {
builder
.add_network_str("1", "0xE592427A0AEce92De3Edee1F18E0157C05861564")
.add_network_str("5", "0xE592427A0AEce92De3Edee1F18E0157C05861564")
});
generate_contract_with_config("WETH9", |builder| {
builder
.add_network_str("1", "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2")
Expand Down
2 changes: 2 additions & 0 deletions crates/contracts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ include!(concat!(env!("OUT_DIR"), "/SwaprRouter.rs"));
include!(concat!(env!("OUT_DIR"), "/ISwaprPair.rs"));
include!(concat!(env!("OUT_DIR"), "/UniswapV2Factory.rs"));
include!(concat!(env!("OUT_DIR"), "/UniswapV2Router02.rs"));
include!(concat!(env!("OUT_DIR"), "/UniswapV3SwapRouter.rs"));
include!(concat!(env!("OUT_DIR"), "/WETH9.rs"));
include!(concat!(env!("OUT_DIR"), "/IUniswapV3Factory.rs"));
include!(concat!(env!("OUT_DIR"), "/IZeroEx.rs"));
Expand Down Expand Up @@ -147,6 +148,7 @@ mod tests {
assert_has_deployment_address!(HoneyswapRouter for *network);
}
assert_has_deployment_address!(BalancerV2StablePoolFactoryV2 for 1);
assert_has_deployment_address!(UniswapV3SwapRouter for 1);
}

#[test]
Expand Down
1 change: 1 addition & 0 deletions crates/e2e/tests/e2e/eth_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ async fn eth_integration(web3: Web3) {
uniswap_like_liquidity: vec![uniswap_liquidity],
balancer_v2_liquidity: None,
zeroex_liquidity: None,
uniswap_v3_liquidity: None,
};
let network_id = web3.net().version().await.unwrap();
let submitted_transactions = GlobalTxPool::default();
Expand Down
1 change: 1 addition & 0 deletions crates/e2e/tests/e2e/onchain_settlement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ async fn onchain_settlement(web3: Web3) {
uniswap_like_liquidity: vec![uniswap_liquidity],
balancer_v2_liquidity: None,
zeroex_liquidity: None,
uniswap_v3_liquidity: None,
};
let network_id = web3.net().version().await.unwrap();
let submitted_transactions = GlobalTxPool::default();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ async fn onchain_settlement_without_liquidity(web3: Web3) {
uniswap_like_liquidity: vec![uniswap_liquidity],
balancer_v2_liquidity: None,
zeroex_liquidity: None,
uniswap_v3_liquidity: None,
};
let network_id = web3.net().version().await.unwrap();
let market_makable_token_list = TokenList::new(maplit::hashmap! {
Expand Down
1 change: 1 addition & 0 deletions crates/e2e/tests/e2e/smart_contract_orders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ async fn smart_contract_orders(web3: Web3) {
uniswap_like_liquidity: vec![uniswap_liquidity],
balancer_v2_liquidity: None,
zeroex_liquidity: None,
uniswap_v3_liquidity: None,
};
let network_id = web3.net().version().await.unwrap();
let submitted_transactions = GlobalTxPool::default();
Expand Down
1 change: 1 addition & 0 deletions crates/e2e/tests/e2e/vault_balances.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ async fn vault_balances(web3: Web3) {
uniswap_like_liquidity: vec![uniswap_liquidity],
balancer_v2_liquidity: None,
zeroex_liquidity: None,
uniswap_v3_liquidity: None,
};
let network_id = web3.net().version().await.unwrap();
let submitted_transactions = GlobalTxPool::default();
Expand Down
2 changes: 1 addition & 1 deletion crates/shared/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ lru = "0.7"
maplit = "1.0"
mockall = "0.11"
model = { path = "../model" }
num = "0.4"
num = { version = "0.4", features = ["serde"] }
number_conversions = { path = "../number_conversions" }
primitive-types = "0.10"
prometheus = "0.13"
Expand Down
72 changes: 72 additions & 0 deletions crates/shared/src/http_solver/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use serde::{Deserialize, Serialize};
use serde_with::serde_as;
use std::collections::{BTreeMap, HashMap};

use crate::sources::uniswap_v3::pool_fetching::PoolInfo;

#[derive(Clone, Debug, Default, Serialize)]
pub struct BatchAuctionModel {
pub tokens: BTreeMap<H160, TokenInfoModel>,
Expand Down Expand Up @@ -57,6 +59,7 @@ pub enum AmmParameters {
ConstantProduct(ConstantProductPoolParameters),
WeightedProduct(WeightedProductPoolParameters),
Stable(StablePoolParameters),
Concentrated(ConcentratedPoolParameters),
}

#[serde_as]
Expand Down Expand Up @@ -90,6 +93,12 @@ pub struct StablePoolParameters {
pub amplification_parameter: BigRational,
}

#[serde_as]
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct ConcentratedPoolParameters {
pub pool: PoolInfo,
}

#[serde_as]
#[derive(Clone, Debug, Default, Serialize)]
pub struct TokenInfoModel {
Expand Down Expand Up @@ -314,6 +323,8 @@ pub struct ExecutionPlanCoordinatesModel {

#[cfg(test)]
mod tests {
use crate::sources::uniswap_v3::graph_api::Token;

use super::*;
use maplit::btreemap;
use serde_json::json;
Expand Down Expand Up @@ -455,6 +466,32 @@ mod tests {
},
mandatory: true,
};
let concentrated_pool_model = AmmModel {
parameters: AmmParameters::Concentrated(ConcentratedPoolParameters {
pool: PoolInfo {
address: H160::from_low_u64_be(1),
tokens: vec![
Token {
id: buy_token,
symbol: "CAT".to_string(),
decimals: 6,
},
Token {
id: sell_token,
symbol: "DOG".to_string(),
decimals: 18,
},
],
..Default::default()
},
}),
fee: BigRational::new(3.into(), 1000.into()),
cost: TokenAmount {
amount: U256::from(3),
token: native_token,
},
mandatory: false,
};
let model = BatchAuctionModel {
tokens: btreemap! {
buy_token => TokenInfoModel {
Expand All @@ -477,6 +514,7 @@ mod tests {
0 => constant_product_pool_model,
1 => weighted_product_pool_model,
2 => stable_pool_model,
3 => concentrated_pool_model,
},
metadata: Some(MetadataModel {
environment: Some(String::from("Such Meta")),
Expand Down Expand Up @@ -575,6 +613,40 @@ mod tests {
},
"mandatory": true,
},
"3": {
"kind": "Concentrated",
"pool": {
"address": "0x0000000000000000000000000000000000000001",
"tokens": [
{
"id": "0x0000000000000000000000000000000000000539",
"symbol": "CAT",
"decimals": "6",
},
{
"id": "0x000000000000000000000000000000000000a866",
"symbol": "DOG",
"decimals": "18",
}
],
"state": {
"sqrt_price": "0",
"liquidity": "0",
"tick": "0",
"liquidity_net": {},
"fee": "0",
},
"gas_stats": {
"mean": "0",
}
},
"fee": "0.003",
"cost": {
"amount": "3",
"token": "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee",
},
"mandatory": false,
},
},
"metadata": {
"environment": "Such Meta",
Expand Down
1 change: 1 addition & 0 deletions crates/shared/src/price_estimation/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ impl HttpPriceEstimator {
AmmParameters::ConstantProduct(params) => tokens.extend(params.reserves.keys()),
AmmParameters::WeightedProduct(params) => tokens.extend(params.reserves.keys()),
AmmParameters::Stable(params) => tokens.extend(params.reserves.keys()),
AmmParameters::Concentrated(_) => (), // not used since no output amounts are calculated in driver
sunce86 marked this conversation as resolved.
Show resolved Hide resolved
}
}
let tokens: Vec<_> = tokens.drain().collect();
Expand Down
4 changes: 4 additions & 0 deletions crates/shared/src/sources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub mod honeyswap;
pub mod sushiswap;
pub mod swapr;
pub mod uniswap_v2;
pub mod uniswap_v3;
pub mod uniswap_v3_pair_provider;

use self::uniswap_v2::{
Expand All @@ -30,6 +31,7 @@ pub enum BaselineSource {
Baoswap,
Swapr,
ZeroEx,
UniswapV3,
}

pub fn defaults_for_chain(chain_id: u64) -> Result<Vec<BaselineSource>> {
Expand All @@ -39,6 +41,7 @@ pub fn defaults_for_chain(chain_id: u64) -> Result<Vec<BaselineSource>> {
BaselineSource::SushiSwap,
BaselineSource::BalancerV2,
BaselineSource::ZeroEx,
BaselineSource::UniswapV3,
sunce86 marked this conversation as resolved.
Show resolved Hide resolved
],
4 => vec![
BaselineSource::UniswapV2,
Expand Down Expand Up @@ -72,6 +75,7 @@ pub async fn uniswap_like_liquidity_sources(
BaselineSource::Swapr => swapr::get_liquidity_source(web3).await?,
BaselineSource::BalancerV2 => continue,
BaselineSource::ZeroEx => continue,
BaselineSource::UniswapV3 => continue,
};

liquidity_sources.insert(*source, liquidity_source);
Expand Down
3 changes: 3 additions & 0 deletions crates/shared/src/sources/uniswap_v3.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//! Uniswap V3 baseline liquidity source implementation.
pub mod graph_api;
pub mod pool_fetching;
Loading