Skip to content

Commit

Permalink
feat: (WIP) Configurable chain in quickstart
Browse files Browse the repository at this point in the history
Note: We need to change all ETH_RPC_URL to just RPC_URL. I couldn't change it here since it's still ETH_RPC_URL in Tycho execution.
  • Loading branch information
TAMARA LIPOWSKI committed Feb 27, 2025
1 parent 45f154d commit 043c6de
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 22 deletions.
4 changes: 2 additions & 2 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
Expand Up @@ -40,7 +40,7 @@ lazy_static = "1.4.0"
# Tycho dependencies
tycho-core = { git = "https://github.com/propeller-heads/tycho-indexer.git", package = "tycho-core", tag = "0.56.5" }
tycho-client = { git = "https://github.com/propeller-heads/tycho-indexer.git", package = "tycho-client", tag = "0.56.5" }
tycho-execution = { git = "https://github.com/propeller-heads/tycho-execution.git", package = "tycho-execution", features = ["evm"], rev = "5aec1c08117c249ebe68421367ad6a68636fca9b" }
tycho-execution = { git = "https://github.com/propeller-heads/tycho-execution.git", package = "tycho-execution", features = ["evm"], rev = "6b57e81afcbee45017249a8806a31fcca9ee2bfa" }

# EVM dependencies
foundry-config = { git = "https://github.com/foundry-rs/foundry", rev = "57bb12e", optional = true }
Expand Down
9 changes: 6 additions & 3 deletions examples/quickstart/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@ This quickstart guide enables you to:
## How to run

```bash
export ETH_RPC_URL=<your-eth-rpc-url>
export RPC_URL=<your-rpc-url>
cargo run --release --example quickstart
```

By default, the example will trade 1 WETH -> USDC. If you want a different trade you can do:
By default, the example will trade 1 WETH -> USDC on Ethereum Mainnet. If you want a different trade or chain,
you can do:

```bash
cargo run --release --example quickstart -- --sell-token "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48" --buy-token "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2" --sell-amount 10
export TYCHO_URL=<tycho-api-url-for-chain>
export TYCHO_API_KEY=<tycho-api-key-for-chain>
cargo run --release --example quickstart -- --sell-token "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913" --buy-token "0x4200000000000000000000000000000000000006" --sell-amount 10 --chain "base"
```

for 10000 USDC -> WBTC.
Expand Down
46 changes: 30 additions & 16 deletions examples/quickstart/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ struct Cli {
/// The tvl threshold to filter the graph by
#[arg(short, long, default_value_t = 100.0)]
tvl_threshold: f64,
#[arg(short, long, default_value = "ethereum")]
chain: String,
}

#[tokio::main]
Expand All @@ -82,11 +84,13 @@ async fn main() {
let cli = Cli::parse();
let tvl_filter = ComponentFilter::with_tvl_range(cli.tvl_threshold, cli.tvl_threshold);

let chain = Chain::from_str(&cli.chain).expect("Invalid chain");
println!("Loading tokens from Tycho... {}", tycho_url.as_str());
let all_tokens = load_all_tokens(
tycho_url.as_str(),
false,
Some(tycho_api_key.as_str()),
Chain::Ethereum,
chain,
None,
None,
)
Expand All @@ -113,19 +117,19 @@ async fn main() {
let mut pairs: HashMap<String, ProtocolComponent> = HashMap::new();
let mut amounts_out: HashMap<String, BigUint> = HashMap::new();

let mut protocol_stream = ProtocolStreamBuilder::new(&tycho_url, Chain::Ethereum)
let mut protocol_stream = ProtocolStreamBuilder::new(&tycho_url, chain)
.exchange::<UniswapV2State>("uniswap_v2", tvl_filter.clone(), None)
.exchange::<UniswapV3State>("uniswap_v3", tvl_filter.clone(), None)
.exchange::<EVMPoolState<PreCachedDB>>(
"vm:balancer_v2",
tvl_filter.clone(),
Some(balancer_pool_filter),
)
.exchange::<UniswapV4State>(
"uniswap_v4",
tvl_filter.clone(),
Some(uniswap_v4_pool_with_hook_filter),
)
// .exchange::<UniswapV3State>("uniswap_v3", tvl_filter.clone(), None)
// .exchange::<EVMPoolState<PreCachedDB>>(
// "vm:balancer_v2",
// tvl_filter.clone(),
// Some(balancer_pool_filter),
// )
// .exchange::<UniswapV4State>(
// "uniswap_v4",
// tvl_filter.clone(),
// Some(uniswap_v4_pool_with_hook_filter),
// )
.auth_key(Some(tycho_api_key.clone()))
.skip_state_decode_failures(true)
.set_tokens(all_tokens.clone())
Expand All @@ -142,7 +146,7 @@ async fn main() {

// Initialize the encoder
let encoder = EVMEncoderBuilder::new()
.chain(Chain::Ethereum)
.chain(chain)
.tycho_router_with_permit2(None, swapper_pk.clone())
.expect("Failed to create encoder builder")
.build()
Expand Down Expand Up @@ -188,6 +192,7 @@ async fn main() {
buy_token.clone(),
amount_in.clone(),
user_address.clone(),
chain,
);

println!("Do you want to simulate, execute or skip this swap?");
Expand Down Expand Up @@ -361,6 +366,7 @@ fn encode(
buy_token: Token,
sell_amount: BigUint,
user_address: Bytes,
chain: Chain,
) -> Transaction {
// Prepare data to encode. First we need to create a swap object
let simple_swap = Swap::new(
Expand All @@ -372,6 +378,15 @@ fn encode(
0f64,
);

let router_addresses: HashMap<Chain, Bytes> = HashMap::from([
(Chain::Ethereum, Bytes::from_str("0xFfA5ec2e444e4285108e4a17b82dA495c178427B").expect("Failed to create router address")),
(Chain::Base, Bytes::from_str("0x94ebf984511b06bab48545495b754760bfaa566e").expect("Failed to create router address")),
]);
let router_address = router_addresses
.get(&chain)
.expect("Router address not found")
.clone();

// Then we create a solution object with the previous swap
let solution = Solution {
sender: user_address.clone(),
Expand All @@ -382,8 +397,7 @@ fn encode(
exact_out: false, // it's an exact in solution
checked_amount: None, // the amount out will not be checked in execution
swaps: vec![simple_swap],
router_address: Bytes::from_str("0xFfA5ec2e444e4285108e4a17b82dA495c178427B")
.expect("Failed to create router address"),
router_address: router_address,
..Default::default()
};

Expand Down

0 comments on commit 043c6de

Please sign in to comment.