-
Notifications
You must be signed in to change notification settings - Fork 11
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
feat: Provide ProtocolStreamBuilder to simplify decoding #53
Conversation
537f337
to
97edb21
Compare
Cargo.toml
Outdated
tycho-core = { git = "https://github.com/propeller-heads/tycho-indexer.git", package = "tycho-core", rev = "f8d765554194ddd222e4c6f07811e8c99700615a" } | ||
tycho-client = { git = "https://github.com/propeller-heads/tycho-indexer.git", package = "tycho-client", rev = "f8d765554194ddd222e4c6f07811e8c99700615a" } | ||
tycho-ethereum = { git = "https://github.com/propeller-heads/tycho-indexer.git", package = "tycho-ethereum", rev = "f8d765554194ddd222e4c6f07811e8c99700615a" } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have to changes these
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a PR we need to review on tycho-indexer that correlates to this PR then?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No it's already merged
examples/explorer/main.rs
Outdated
let mut protocol_stream = ProtocolStreamBuilder::new(&tycho_url, Chain::Ethereum) | ||
.exchange::<UniswapV2State>( | ||
"uniswap_v2", | ||
ComponentFilter::with_tvl_range(cli.tvl_threshold, cli.tvl_threshold), | ||
) | ||
.exchange::<UniswapV3State>( | ||
"uniswap_v3", | ||
ComponentFilter::with_tvl_range(cli.tvl_threshold, cli.tvl_threshold), | ||
) | ||
.auth_key(Some(tycho_api_key.clone())) | ||
.set_tokens(all_tokens) | ||
.build() | ||
.await | ||
.expect("Failed building protocol stream"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is how you subscribe with tycho-simulation
to protocol state updates now.
src/evm/protocol/vm/utils.rs
Outdated
sync_get_code(connection_string.as_str(), addr) | ||
} | ||
|
||
fn sync_get_code(connection_string: &str, addr: H160) -> Result<Bytecode, SimulationError> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I need to add a comment here:
The Provider futures are not Sync, so this would break the try_from_with_block
trait requirements. I fixed this by moving the not sync part to a separate thread. It's not great ideally we should upgrade to alloy and hope their futures are Sync...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to confirm: this is only used if tycho does not provide the code right? Which ideally shouldn't happen..
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you add this in a TODO in the code please? I feel that here it will get lost easily
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No I think this is mainly used to get stateless contracts.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dianacarvalho1 will fix this before merging.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A couple questions/suggestions... but for the most part it looks great 👍
src/protocol/state.rs
Outdated
@@ -55,6 +51,8 @@ use crate::{ | |||
models::GetAmountOutResult, | |||
}, | |||
}; | |||
use ethers::types::U256; | |||
use tycho_core::dto::ProtocolStateDelta; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FMI: why are you moving all the imports around like this? We've been trying to keep them divided with:
- external imports
- other tycho repos imports
- internal imports
There has been a lot less duplicate import lines since we've done this... by duplicate imports I mean avoiding situations where you import different things from the same file in multiple lines:
use crate::protocol::errors::SimulationError;
other imports...
use crate::protocol::errors::TransitionError;
This is also solved by us removing all new lines between the imports I guess. But that means we have giant import blocks for some files which isn't too nice. I think we need to choose a convention and stick to it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just cargo +nightly format
I had a lot of terrible merge errors because of this... How do you do it?
src/protocol/stream_decoder.rs
Outdated
pub new_pairs: HashMap<String, ProtocolComponent>, | ||
/// The pairs that were removed in this block | ||
pub removed_pairs: HashMap<String, ProtocolComponent>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Question: why do we need to track new/removed pools when we have a Hashmap of all the current pools anyway?
src/evm/protocol/vm/utils.rs
Outdated
sync_get_code(connection_string.as_str(), addr) | ||
} | ||
|
||
fn sync_get_code(connection_string: &str, addr: H160) -> Result<Bytecode, SimulationError> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to confirm: this is only used if tycho does not provide the code right? Which ideally shouldn't happen..
src/protocol/decoder.rs
Outdated
use crate::{ | ||
models::ERC20Token, | ||
evm::protocol::uniswap_v2::state::UniswapV2State, | ||
protocol::{ | ||
decoder::{StreamDecodeError, TychoStreamDecoder}, | ||
}, | ||
models::ERC20Token, | ||
protocol::decoder::{StreamDecodeError, TychoStreamDecoder}, | ||
}; | ||
use ethers::types::U256; | ||
use rstest::*; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not use use super::*;
here and skip re-importing many of these imports already used in this file?
Cargo.toml
Outdated
tycho-core = { git = "https://github.com/propeller-heads/tycho-indexer.git", package = "tycho-core", rev = "f8d765554194ddd222e4c6f07811e8c99700615a" } | ||
tycho-client = { git = "https://github.com/propeller-heads/tycho-indexer.git", package = "tycho-client", rev = "f8d765554194ddd222e4c6f07811e8c99700615a" } | ||
tycho-ethereum = { git = "https://github.com/propeller-heads/tycho-indexer.git", package = "tycho-ethereum", rev = "f8d765554194ddd222e4c6f07811e8c99700615a" } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a PR we need to review on tycho-indexer that correlates to this PR then?
src/protocol/decoder.rs
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure this should belong in the protocol directory per se... it feels more higher level than the protocol specifics housed here. Maybe we can have a stream
directory under src
?
src/protocol/stream.rs
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same for this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you @kayibal 🙏🏼 Only have small questions/requests.
However.. you removed the vm support entirely? 😢 can you bring it back please?
src/evm/protocol/uniswap_v2/state.rs
Outdated
use ethers::types::U256; | ||
use std::{any::Any, collections::HashMap}; | ||
use tycho_core::{dto::ProtocolStateDelta, Bytes}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@louise-poole is going to be mad about this import order 👀
I thought we were doing:
- std imports
- external imports
- tycho
- local project
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(I didn't see her review before posting mine 😂 )
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
VM is supported I just haven't added it to the example. Any protocol state that implement the TryFromWithBlock
trait is automatically supported no need to change anything on the code.
src/evm/protocol/vm/utils.rs
Outdated
sync_get_code(connection_string.as_str(), addr) | ||
} | ||
|
||
fn sync_get_code(connection_string: &str, addr: H160) -> Result<Bytecode, SimulationError> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you add this in a TODO in the code please? I feel that here it will get lost easily
let entry = self | ||
.items | ||
.iter() | ||
.find_position(|e| e.component.address == *address); | ||
.find_position(|e| e.component.address == eth_address); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would rename this to ethereum_address
. eth_address
seems it's the ETH address
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think ethereum_address
and eth_address
have the same meaning in my head. For ETH address I would call it the native_address
since that's what it's been throughout our defibot code - so in my opinion this is fine
src/protocol/decoder.rs
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we rename this file to tycho_stream_decoder?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not limited to Tycho I hope...
src/protocol/stream.rs
Outdated
pub fn set_tokens(self, tokens: HashMap<Bytes, ERC20Token>) -> Self { | ||
self.decoder.set_tokens(tokens); | ||
self | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we document this. When do we need tokens set? Is it only when we stream vm protocols or is it needed for native too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's always needed to correctly create ProtocolComponents
structs.
eb7d340
to
4f89479
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
huumm this was already done in master 😕
049d505
to
36db1dd
Compare
Adjust the decoder for the changed interfaces. Also adjust the interfaces to accept a reference to a hashmap instead of passing hashmaps or vectors by value.
c80fa1a
to
d3bc09d
Compare
The decoder has currently no trait and is evm specific. The stream module is pretty general but depends atm on the decoder since there is no trait for it.
e0e5416
to
87895bc
Compare
87895bc
to
b832495
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you so much @kayibal 🙏🏼 this was a hard baby!!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's great - a lot cleaner. Thank you 🙏
guard.tokens = tokens; | ||
} | ||
|
||
// Method to register a decoder |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pretty useless comment
// Method to register a decoder |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I need to still do the docs for this one
## [0.60.0](0.59.3...0.60.0) (2024-12-13) ### Features * Provide ProtocolStreamBuilder to simplify decoding ([#53](#53)) ([bc82193](bc82193))
This PR is included in version 0.60.0 🎉 |
No description provided.