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

add frontier/ethereum example #1557

Merged
merged 4 commits into from Apr 26, 2024
Merged

add frontier/ethereum example #1557

merged 4 commits into from Apr 26, 2024

Conversation

niklasad1
Copy link
Member

@niklasad1 niklasad1 commented Apr 25, 2024

The ethereum config doesn't work with the default PolkadotConfig which this shows how to do.

Tested with a frontier node

The ethereum config doesn't work with the default PolkadotConfig which
this shows how to do.

Tested with a frontier node
Copy link
Collaborator

@jsdw jsdw left a comment

Choose a reason for hiding this comment

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

Awesome!

I could have sworn that we had a very similar example somewhere, but I couldn't find it offhand so this looks good to me!

@niklasad1 niklasad1 changed the title add ethereum example add frontier/ethereum example Apr 25, 2024
@niklasad1
Copy link
Member Author

@CodiumAI-Agent /improve

@CodiumAI-Agent
Copy link

PR Code Suggestions

CategorySuggestions                                                                                                                                                       
Enhancement
Make the WebSocket URL configurable to enhance flexibility.

Replace the hardcoded WebSocket URL with a configurable option, allowing flexibility in
specifying different node endpoints.

subxt/examples/tx_basic_frontier.rs [38]

-let api = OnlineClient::<EthRuntimeConfig>::from_insecure_url("ws://127.0.0.1:9944").await?;
+let node_url = std::env::var("NODE_URL").unwrap_or_else(|_| "ws://127.0.0.1:9944".to_string());
+let api = OnlineClient::<EthRuntimeConfig>::from_insecure_url(&node_url).await?;
 
Replace println! with log::info! for more structured logging.

Use a logging framework instead of println! for better control over logging levels and
outputs.

subxt/examples/tx_basic_frontier.rs [60]

-println!("Balance transfer success: {event:?}");
+log::info!("Balance transfer success: {event:?}");
 
Add retry logic to network requests to improve reliability.

Consider adding retry logic or a fallback mechanism for network requests to handle
transient errors gracefully.

subxt/examples/tx_basic_frontier.rs [51-54]

-let events = api
-    .tx()
-    .sign_and_submit_then_watch_default(&balance_transfer_tx, &alith)
-    .await?;
+let events = retry::retry_async(retry::delay::Exponential::from_millis(10), || async {
+    api.tx()
+        .sign_and_submit_then_watch_default(&balance_transfer_tx, &alith)
+        .await
+}).await?;
 
Possible issue
Add error handling for hex encoding to prevent potential runtime panics.

Handle potential errors from the hex::encode function calls to prevent runtime panics in
case of encoding failures.

subxt/examples/tx_basic_frontier.rs [44-45]

-println!("baltathar pub:  {}", hex::encode(baltathar.public_key().0));
-println!("baltathar addr: {}", hex::encode(dest));
+println!("baltathar pub:  {}", hex::encode(baltathar.public_key().0).unwrap_or_else(|e| e.to_string()));
+println!("baltathar addr: {}", hex::encode(dest).unwrap_or_else(|e| e.to_string()));
 
Best practice
Improve error handling for environment variable retrieval to enhance robustness.

Replace the unwrap calls with proper error handling to avoid panics when environment
variables are not set or have invalid values.

subxt/examples/tx_basic_frontier.rs [38]

-let node_url = std::env::var("NODE_URL").unwrap_or_else(|_| "ws://127.0.0.1:9944".to_string());
+let node_url = std::env::var("NODE_URL").unwrap_or_else(|e| {
+    eprintln!("Error reading NODE_URL: {e}");
+    "ws://127.0.0.1:9944".to_string()
+});
 

✨ Improve tool usage guide:

Overview:
The improve tool scans the PR code changes, and automatically generates suggestions for improving the PR code. The tool can be triggered automatically every time a new PR is opened, or can be invoked manually by commenting on a PR.

  • When commenting, to edit configurations related to the improve tool (pr_code_suggestions section), use the following template:
/improve --pr_code_suggestions.some_config1=... --pr_code_suggestions.some_config2=...
[pr_code_suggestions]
some_config1=...
some_config2=...

See the improve usage page for a comprehensive guide on using this tool.

@niklasad1
Copy link
Member Author

@CodiumAI-Agent /describe

@niklasad1 niklasad1 merged commit 25193d7 into master Apr 26, 2024
13 checks passed
@niklasad1 niklasad1 deleted the na-add-eth-signer-example branch April 26, 2024 14:18
@jsdw jsdw mentioned this pull request May 16, 2024
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.

None yet

3 participants