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

feat: Provide ProtocolStreamBuilder to simplify decoding #53

Merged
merged 18 commits into from
Dec 13, 2024

Conversation

kayibal
Copy link
Collaborator

@kayibal kayibal commented Nov 10, 2024

No description provided.

@kayibal kayibal force-pushed the ah/simplify-decoding branch 4 times, most recently from 537f337 to 97edb21 Compare November 29, 2024 08:43
@kayibal kayibal changed the title WIP: simplify decoding feat: Provide ProtocolStreamBuilder to simplify decoding Nov 29, 2024
Cargo.toml Outdated
Comment on lines 41 to 43
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" }
Copy link
Collaborator Author

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

Copy link
Contributor

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?

Copy link
Collaborator Author

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

Comment on lines 52 to 68
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");
Copy link
Collaborator Author

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.

sync_get_code(connection_string.as_str(), addr)
}

fn sync_get_code(connection_string: &str, addr: H160) -> Result<Bytecode, SimulationError> {
Copy link
Collaborator Author

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...

Copy link
Contributor

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..

Copy link
Collaborator

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

Copy link
Collaborator Author

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.

Copy link
Collaborator Author

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.

Copy link
Contributor

@louise-poole louise-poole left a 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 👍

@@ -55,6 +51,8 @@ use crate::{
models::GetAmountOutResult,
},
};
use ethers::types::U256;
use tycho_core::dto::ProtocolStateDelta;
Copy link
Contributor

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.

Copy link
Collaborator Author

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?

Comment on lines 43 to 45
pub new_pairs: HashMap<String, ProtocolComponent>,
/// The pairs that were removed in this block
pub removed_pairs: HashMap<String, ProtocolComponent>,
Copy link
Contributor

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?

sync_get_code(connection_string.as_str(), addr)
}

fn sync_get_code(connection_string: &str, addr: H160) -> Result<Bytecode, SimulationError> {
Copy link
Contributor

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..

Comment on lines 278 to 340
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::*;
Copy link
Contributor

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
Comment on lines 41 to 43
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" }
Copy link
Contributor

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?

Copy link
Contributor

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?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for this.

Copy link
Collaborator

@dianacarvalho1 dianacarvalho1 left a 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?

Comment on lines 11 to 17
use ethers::types::U256;
use std::{any::Any, collections::HashMap};
use tycho_core::{dto::ProtocolStateDelta, Bytes};
Copy link
Collaborator

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

Copy link
Collaborator

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 😂 )

Copy link
Collaborator Author

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.

sync_get_code(connection_string.as_str(), addr)
}

fn sync_get_code(connection_string: &str, addr: H160) -> Result<Bytecode, SimulationError> {
Copy link
Collaborator

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);
Copy link
Collaborator

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

Copy link
Contributor

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

Copy link
Collaborator

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?

Copy link
Collaborator Author

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...

Comment on lines 73 to 85
pub fn set_tokens(self, tokens: HashMap<Bytes, ERC20Token>) -> Self {
self.decoder.set_tokens(tokens);
self
}
Copy link
Contributor

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?

Copy link
Collaborator Author

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.

@kayibal kayibal force-pushed the ah/simplify-decoding branch 2 times, most recently from eb7d340 to 4f89479 Compare December 12, 2024 11:33
Copy link
Collaborator

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 😕

@kayibal kayibal force-pushed the ah/simplify-decoding branch 3 times, most recently from 049d505 to 36db1dd Compare December 12, 2024 20:39
@kayibal kayibal force-pushed the ah/simplify-decoding branch from c80fa1a to d3bc09d Compare December 13, 2024 12:19
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.
@kayibal kayibal force-pushed the ah/simplify-decoding branch 3 times, most recently from e0e5416 to 87895bc Compare December 13, 2024 14:48
@kayibal kayibal force-pushed the ah/simplify-decoding branch from 87895bc to b832495 Compare December 13, 2024 15:04
@kayibal kayibal enabled auto-merge (squash) December 13, 2024 15:20
Copy link
Collaborator

@dianacarvalho1 dianacarvalho1 left a 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!!

@kayibal kayibal merged commit bc82193 into main Dec 13, 2024
3 checks passed
@kayibal kayibal deleted the ah/simplify-decoding branch December 13, 2024 15:31
Copy link
Contributor

@tamaralipows tamaralipows left a 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty useless comment

Suggested change
// Method to register a decoder

Copy link
Collaborator Author

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

propellerci bot pushed a commit that referenced this pull request Dec 13, 2024
## [0.60.0](0.59.3...0.60.0) (2024-12-13)

### Features

* Provide ProtocolStreamBuilder to simplify decoding ([#53](#53)) ([bc82193](bc82193))
@propellerci
Copy link

propellerci bot commented Dec 13, 2024

This PR is included in version 0.60.0 🎉

@propellerci propellerci bot added the true label Dec 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants