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(subscriptions): sub_blocks, watch_contract_event #5

Merged
merged 14 commits into from
Mar 25, 2024

Conversation

yash-atreya
Copy link
Member

  • subscribe_blocks and watch_blocks using ws.
  • watch_contract_event. Substitute for subscribe_events_by_type and subscribe_log in ethers as currently there are only two types of subscribe_* methods which are _blocks and _pending_transactions.

@zerosnacks
Copy link
Member

Made some minor changes, largely cosmetic.

Comment on lines +15 to +16
let sub = provider.subscribe_blocks().await?;
let mut stream = sub.into_stream().take(2);
Copy link
Member

Choose a reason for hiding this comment

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

Do we always call into_stream()? Should we return that in the subscribe_blocks return type instead @DaniPopes by impl Stream on the type?

Comment on lines +14 to +15
#[sol(rpc, bytecode = "0x60806040526000805534801561001457600080fd5b50610260806100246000396000f3fe608060405234801561001057600080fd5b50600436106100415760003560e01c80632baeceb71461004657806361bc221a14610050578063d09de08a1461006e575b600080fd5b61004e610078565b005b6100586100d9565b6040516100659190610159565b60405180910390f35b6100766100df565b005b600160008082825461008a91906101a3565b925050819055506000543373ffffffffffffffffffffffffffffffffffffffff167fdc69c403b972fc566a14058b3b18e1513da476de6ac475716e489fae0cbe4a2660405160405180910390a3565b60005481565b60016000808282546100f191906101e6565b925050819055506000543373ffffffffffffffffffffffffffffffffffffffff167ff6d1d8d205b41f9fb9549900a8dba5d669d68117a3a2b88c1ebc61163e8117ba60405160405180910390a3565b6000819050919050565b61015381610140565b82525050565b600060208201905061016e600083018461014a565b92915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b60006101ae82610140565b91506101b983610140565b92508282039050818112600084121682821360008512151617156101e0576101df610174565b5b92915050565b60006101f182610140565b91506101fc83610140565b92508282019050828112156000831216838212600084121516171561022457610223610174565b5b9291505056fea26469706673582212208d0d34c26bfd2938ff07dd54c3fcc2bc4509e4ae654edff58101e5e7ab8cf18164736f6c63430008180033")]
contract EventExample {
Copy link
Member

Choose a reason for hiding this comment

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

when we ship compiler it'll be interesting to see if we can bundle it with the sol macro

Ok(())
}

async fn init() -> (RootProvider<Ethereum, PubSubFrontend>, AnvilInstance) {
Copy link
Member

Choose a reason for hiding this comment

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

@DaniPopes what's a good way to type erase here? impl Provider? wonder if we can make functions that return providers easy to use basically so that they don't need to manually write the network and the transport type

Comment on lines 56 to 58
let mut increment_stream = increment_poller.into_stream().flat_map(stream::iter).take(2);

let mut decrement_stream = decrement_poller.into_stream().flat_map(stream::iter).take(2);
Copy link
Member

Choose a reason for hiding this comment

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

still don't love the flat_map(stream::iter) syntax, but i understand it's because we also return the stream data by block, so i wonder if we should just have .flatten() as a helper method that just does flat_map(stream::iter) @DaniPopes ?

examples/subscriptions/examples/watch_contract_event.rs Outdated Show resolved Hide resolved
examples/subscriptions/examples/watch_contract_event.rs Outdated Show resolved Hide resolved
Copy link
Member

@mattsse mattsse left a comment

Choose a reason for hiding this comment

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

lgtm


#[tokio::main]
async fn main() -> Result<()> {
let (provider, _anvil) = init().await;
Copy link
Member

Choose a reason for hiding this comment

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

also flatten, no function, not useful for examples

Comment on lines 13 to 22
[dev-dependencies]
alloy-contract.workspace = true
alloy-network.workspace = true
alloy-node-bindings.workspace = true
alloy-provider = { workspace = true, features = ["pubsub", "ws"] }
alloy-pubsub.workspace = true
alloy-primitives.workspace = true
alloy-rpc-client.workspace = true
alloy-rpc-types.workspace = true
alloy-sol-types = { workspace = true }
Copy link
Member

Choose a reason for hiding this comment

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

Please use the new Alloy namespace (see: #11)

Cargo.toml Outdated
@@ -11,6 +11,7 @@ license = "MIT OR Apache-2.0"
homepage = "https://github.com/alloy-rs/examples"
repository = "https://github.com/alloy-rs/examples"
publish = false
exclude = ["examples/"]
Copy link
Member

Choose a reason for hiding this comment

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

can be removed as publishing is disabled

@yash-atreya yash-atreya merged commit 77e7746 into main Mar 25, 2024
2 checks passed
@zerosnacks zerosnacks deleted the sub_examples branch March 27, 2024 09:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants