-
Notifications
You must be signed in to change notification settings - Fork 38
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(providers): add connect_builtin example #34
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f3caa8e
feat(providers): add connect_builtin transport example
yash-atreya 1ab217c
apply temp fix for #26
yash-atreya 61252e0
ignore connect_builtin in ci
yash-atreya 25ec522
Merge branch 'main' into connect_builtin
yash-atreya e78a249
bump deps and nits
yash-atreya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,6 +51,7 @@ jobs: | |
-e 'ipc' \ | ||
-e 'ws' \ | ||
-e 'ws_auth' \ | ||
-e 'connect_builtin' \ | ||
| xargs -n1 echo | ||
)" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
//! Example of using the `RootProvider<N, T: BoxTransport>::connect_builtin` to create a provider | ||
//! from a connection string. The connection string can be a HTTP, WS or IPC endpoint. | ||
|
||
use alloy::node_bindings::Anvil; | ||
use alloy_network::Ethereum; | ||
use alloy_provider::{Provider, RootProvider}; | ||
use alloy_transport::BoxTransport; | ||
use eyre::Result; | ||
use futures_util::StreamExt; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<()> { | ||
let anvil = Anvil::new().block_time(1).try_spawn()?; | ||
let http = anvil.endpoint(); | ||
let ws = anvil.ws_endpoint(); | ||
|
||
// Instantiate a HTTP transport provider by passing the http endpoint url | ||
let http_provider = | ||
RootProvider::<Ethereum, BoxTransport>::connect_builtin(http.as_str()).await?; | ||
|
||
// Get latest block number | ||
let block_number = http_provider.get_block_number().await?; | ||
|
||
println!("Latest block number: {block_number:?}"); | ||
|
||
// This requires the `pubsub` and `ws` features to be enabled on alloy-provider | ||
let ws_provider = RootProvider::<Ethereum, BoxTransport>::connect_builtin(ws.as_str()).await?; | ||
|
||
let sub = ws_provider.subscribe_blocks().await?; | ||
|
||
let mut stream = sub.into_stream().take(2); | ||
|
||
println!("Awaiting blocks..."); | ||
|
||
let handle = tokio::spawn(async move { | ||
while let Some(block) = stream.next().await { | ||
println!("{:?}", block.header.number); | ||
} | ||
}); | ||
|
||
handle.await?; | ||
|
||
let ipc_path = "/tmp/reth.ipc"; | ||
|
||
// This requires the `pubsub` and `ipc` features to be enabled on alloy-provider | ||
// This would throw a runtime error if the ipc does not exist | ||
let ipc_provider = RootProvider::<Ethereum, BoxTransport>::connect_builtin(ipc_path).await?; | ||
|
||
let _block_number = ipc_provider.get_block_number().await?; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
More of a general comment and a personal opinion but I'm not a fan of the
connect_builtin
name. I don't think it is particularly clear to users and sounds like an internal method rather than the equivalent of adefault
.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.
Possibly beyond the scope of the intended PR but I would also prefer to see the parameter being strongly typed (
Url
-like) instead of a&str