Quickstart
The quickstart program is an example usage of the SDK.
This program opens a Tycho stream, listen to it, build or update orderbooks each time states has changed, and prints out the formatted orderbooks.
If you provide a private key in the the environment variable, it will build the approve and swap transaction to build swap transaction and calldata.
By default, the script attempts to build the approval and swap transactions for 0.01 ETH to USDC on the Tycho router contract on mainnet.
For example, the .env.quickstart.ex
file may contain:
TYCHO_API_KEY="sampletoken"
REAL_EXEC=false
SENDER="0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045"
NETWORK="ethereum"
PV_KEY="0x123"
Terminal
You'll find the quickstart code in the examples
folder.
You must have Rust installed.
To run the program:
sh examples/quickstart.sh ethereum
Or directly with Cargo:
export RUST_LOG="off,tycho_orderbook=trace,quickstart=trace"
cargo run --bin quickstart
If activated, the logs show what's going on under the hood. Here's an extract.
quickstart: --- --- --- Launching Quickstart Tycho Orderbook --- --- ---
2025-04-14T15:11:30.168122Z WARN quickstart: ๐ Private key not found in environment variables. Continuing without executing any transaction.
quickstart: Tycho API Key: sampletoken
quickstart: Network: ethereum
quickstart: Sender: 0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045
quickstart: Real Execution: false
quickstart: Tycho Stream for 'ethereum' network
tycho_orderbook::provider: ๐ธ OBP: TychoStream: b#22268040 with 370 states, pairs: +370 -0
tycho_orderbook::provider: First stream (initialised was false). Writing the entire streamed data into the shared struct.
quickstart: Event: Initialised: : โ
Initialised at block 22268040
tycho_orderbook::provider: ๐ธ OBP: TychoStream: b#22268041 with 9 states, pairs: +0 -0
quickstart: Event: NewHeader: #22268041 with 9 components updated
quickstart: ๐งฑ OBP Event: Orderbook 0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2-0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48 isn't build yet, building it ...
tycho_orderbook::provider: Building orderbook for pair WETH-USDC | Single point: false
[...]
- #4 | In: 0.7852034 WETH, Out: 1300.6974984 USDC at avg price 1656.5103595 (vs spot_price 1661.7318269) | Price impact %: 0.3100 | Gas cost 0.56759$ | Distribution: [100.0] on 100.000 | Distributed: [100.0] on 100.000 | Took: 0 ms
- #12 | In: 16.7348724 WETH, Out: 27641.4170184 USDC at avg price 1651.7255886 (vs spot_price 1661.7318269) | Price impact %: 0.6000 | Gas cost 0.56759$ | Distribution: [100.0] on 100.000 | Distributed: [100.0] on 100.000 | Took: 0 ms
- #9 | In: 5.3135199 WETH, Out: 8794.6675714 USDC at avg price 1655.1490863 (vs spot_price 1661.7318269) | Price impact %: 0.4000 | Gas cost 0.56759$ | Distribution: [100.0] on 100.000 | Distributed: [100.0] on 100.000 | Took: 0 ms
- #2 | In: 0.3654451 WETH, Out: 605.4096664 USDC at avg price 1656.6366555 (vs spot_price 1661.7318269) | Price impact %: 0.3100 | Gas cost 0.56759$ | Distribution: [100.0] on 100.000 | Distributed: [100.0] on 100.000 | Took: 0 ms
- #3 | In: 0.5356759 WETH, Out: 887.3929114 USDC at avg price 1656.5854349 (vs spot_price 1661.7318269) | Price impact %: 0.3100 | Gas cost 0.56759$ | Distribution: [100.0] on 100.000 | Distributed: [100.0] on 100.000 | Took: 0 ms
2025-04-14T15:12:04.463214Z TRACE tycho_orderbook::core::book: ๐ Bids done, now switching to asks
- #17 | In: 188184.3507052 USDC, Out: 110.6304009 WETH at avg price 0.0005879 (vs spot_price 0.0006018) | Price impact %: 2.3100 | Gas cost 0.56759$ | Distribution: [100.0] on 100.000 | Distributed: [100.0] on 100.000 | Took: 0 ms
- #23 | In: 1866653.4527898 USDC, Out: 930.1509486 WETH at avg price 0.0004983 (vs spot_price 0.0006018) | Price impact %: 17.2000 | Gas cost 0.56759$ | Distribution: [100.0] on 100.000 | Distributed: [100.0] on 100.000 | Took: 0 ms
- #2 | In: 607.2716716 USDC, Out: 0.3643245 WETH at avg price 0.0005999 (vs spot_price 0.0006018) | Price impact %: 0.3100 | Gas cost 0.56759$ | Distribution: [100.0] on 100.000 | Distributed: [100.0] on 100.000 | Took: 0 ms
- #1 | In: 414.2886069 USDC, Out: 0.2485522 WETH at avg price 0.0005999 (vs spot_price 0.0006018) | Price impact %: 0.3000 | Gas cost 0.56759$ | Distribution: [100.0] on 100.000 | Distributed: [100.0] on 100.000 | Took: 0 ms
- #16 | In: 128381.8036254 USDC, Out: 75.9600514 WETH at avg price 0.0005917 (vs spot_price 0.0006018) | Price impact %: 1.6800 | Gas cost 0.56759$ | Distribution: [100.0] on 100.000 | Distributed: [100.0] on 100.000 | Took: 0 ms
[...]
tycho_orderbook::core::book: Done. Returning simulated orderbook for pair (base-quote) => 'WETH-USDC'
quickstart: OBP Event: Orderbook received
OBP Event: Orderbook WETH-USDC has been updated
Bids (25)
- 0.51530 WETH at a price of 1656.40850 USDC per WETH
- 1.10718 WETH at a price of 1656.39881 USDC per WETH
- 1.62292 WETH at a price of 1656.39036 USDC per WETH
- 4988.59959 WETH at a price of 1613.94163 USDC per WETH
- 7312.37877 WETH at a price of 1593.60013 USDC per WETH
Asks (25)
- 0.51522 WETH at a price of 1656.44691 USDC per WETH
- 1.10694 WETH at a price of 1656.55099 USDC per WETH
- 1.62248 WETH at a price of 1656.64168 USDC per WETH
- 3347.98094 WETH at a price of 1683.54981 USDC per WETH
- 4875.13554 WETH at a price of 1694.73690 USDC per WETH
- 7085.53125 WETH at a price of 1709.21460 USDC per WETH
OBP Event: Orderbook WETH-USDC is the one we want to execute a trade on.
Creating the transactions to execute ...
Building transactions for request. Private key provided: false
Balances of sender 0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045: Input: 16395109787715287566 | Output: 123223706565
Building swap calldata and transactions ...
Preparing swap. Sender: 0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045 | Orderbook: "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2-0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48"
Single swap: true | single_swap_index = 1
Initial distribution sum: 100 (should be close to 100). Adjusted distribution = [0.0, 0.0, 0.0, 0.0, 0.0] (full 0 if single swap)
Req.Amount: 0.001 (pow = 1000000000000000) of WETH
Expected: 1656416.616353867 of USDC | Checked: 1652275.5748129825
Nonce: 1548
Last updated
Was this helpful?