From 84d162d418f383bb9dee56ba281f24f686bff19d Mon Sep 17 00:00:00 2001 From: Diana Carvalho Date: Fri, 31 Jan 2025 18:42:23 +0000 Subject: [PATCH] feat: Add simple quickstart example --- don't change below this line --- ENG-4087 Took 14 seconds --- examples/quickstart/Readme.md | 12 ++++++ examples/quickstart/main.rs | 73 +++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 examples/quickstart/Readme.md create mode 100644 examples/quickstart/main.rs diff --git a/examples/quickstart/Readme.md b/examples/quickstart/Readme.md new file mode 100644 index 0000000..0978ffd --- /dev/null +++ b/examples/quickstart/Readme.md @@ -0,0 +1,12 @@ +# QuickStart + +This quickstart guide enables you to: + +1. Create a Solution object +2. Encode the solution to interact with the Ethereum blockchain + +## How to run + +```bash +cargo run --release --example quickstart +``` \ No newline at end of file diff --git a/examples/quickstart/main.rs b/examples/quickstart/main.rs new file mode 100644 index 0000000..c68be70 --- /dev/null +++ b/examples/quickstart/main.rs @@ -0,0 +1,73 @@ +use std::str::FromStr; + +use num_bigint::BigUint; +use tycho_core::{dto::ProtocolComponent, models::Chain, Bytes}; +use tycho_execution::encoding::{ + evm::{ + strategy_encoder::strategy_selector::EVMStrategySelector, tycho_encoder::EVMTychoEncoder, + }, + models::{Solution, Swap}, + tycho_encoder::TychoEncoder, +}; + +fn main() { + // Setup variables + let router_address = "0x1234567890abcdef1234567890abcdef12345678".to_string(); + let signer_pk = + Some("0x123456789abcdef123456789abcdef123456789abcdef123456789abcdef1234".to_string()); + let user_address = Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2") + .expect("Failed to create user address"); + + // Initialize the encoder + let encoder = + EVMTychoEncoder::new(EVMStrategySelector, router_address, signer_pk, Chain::Ethereum) + .expect("Failed to create encoder"); + + // Prepare data to encode. We will encode a simple swap from WETH to DAI + // First we need to create a swap object + let weth = Bytes::from_str("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2") + .expect("Failed to create WETH address"); + let dai = Bytes::from_str("0x6b175474e89094c44da98b954eedeac495271d0f") + .expect("Failed to create DAI address"); + + let swap = Swap { + // The protocol component data comes from tycho-indexer + component: ProtocolComponent { + id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(), + protocol_system: "uniswap_v2".to_string(), + ..Default::default() + }, + token_in: weth.clone(), + token_out: dai.clone(), + // Split represents the percentage of the amount to be swapped. If it's 0 it represents 100% + // or what is left of the amount in. + split: 0f64, + }; + + // Then we create a solution object with the previous swap + let solution = Solution { + sender: user_address.clone(), + receiver: user_address, + given_token: weth, + given_amount: BigUint::from_str("1_000000000000000000").expect("Failed to create amount"), + checked_token: dai, + exact_out: false, // it's an exact in solution + check_amount: None, // the amount out will not be checked in execution + swaps: vec![swap], + ..Default::default() + }; + + // Encode the solution + let transactions = encoder + .encode_router_calldata(vec![solution]) + .expect("Failed to encode router calldata"); + let tx = transactions[0].clone(); + + println!( + "The encoded transaction should be sent to address {:?} with the value of {:?} and the \ + following encoded data: {:?}", + tx.to, + tx.value, + hex::encode(tx.data) + ); +}