-
Notifications
You must be signed in to change notification settings - Fork 24
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
fix get gateway address #211
Conversation
📝 WalkthroughWalkthroughThe pull request introduces a series of modifications across multiple files in the ZetaChain client library. The primary change involves updating the Changes
Sequence DiagramsequenceDiagram
participant Client as ZetaChainClient
participant Wallet as Wallet/Signer
participant Gateway as Gateway Address Resolver
Client->>Wallet: getChainId()
Wallet-->>Client: Return Chain ID
Client->>Gateway: getGatewayAddress()
Gateway-->>Client: Return Gateway Address
Client->>Client: Use Gateway Address
The sequence diagram illustrates the new asynchronous flow for retrieving the gateway address, showing how the client now awaits the chain ID and gateway address resolution before proceeding with further operations. Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
Documentation and Community
|
@fadeev pls review |
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.
Actionable comments posted: 0
🔭 Outside diff range comments (1)
packages/client/src/client.ts (1)
Line range hint
179-217
: Refactor to reduce code duplication and improve error handlingThe current implementation has duplicate logic for wallet and signer paths. Consider refactoring to improve maintainability and reduce complexity.
Here's a suggested implementation:
public async getGatewayAddress(): Promise<string> { if (this.network === "localnet" || this.network === "localhost") { const gateway = (this.contracts as LocalnetAddress[]).find( (item) => item.type === "gatewayZEVM" ); if (!gateway) { throw new Error("Gateway address not found in localnet configuration"); } return gateway.address; } - let gateway; - if (this.wallet) { - try { - const chainId = await this.wallet!.getChainId(); - gateway = (this.contracts as MainnetTestnetAddress[]).find( - (item) => chainId === item.chain_id && item.type === "gateway" - ); - } catch (error) { - throw new Error("Failed to get gateway address: " + error); - } - } else { - try { - const chainId = await this.signer!.getChainId(); - gateway = (this.contracts as MainnetTestnetAddress[]).find( - (item) => chainId === item.chain_id && item.type === "gateway" - ); - } catch (error) { - throw new Error("Failed to get gateway address: " + error); - } - } + try { + const provider = this.wallet || this.signer; + if (!provider) { + throw new Error("No wallet or signer available"); + } + const chainId = await provider.getChainId(); + const gateway = (this.contracts as MainnetTestnetAddress[]).find( + (item) => chainId === item.chain_id && item.type === "gateway" + ); + if (!gateway) { + throw new Error(`Gateway address not found for chain ID ${chainId}`); + } + return gateway.address; + } catch (error) { + throw new Error(`Failed to get gateway address: ${error}`); + } - if (!gateway) { - throw new Error(`Gateway address not found in signer or wallet`); - } - return gateway.address; }
🧹 Nitpick comments (6)
packages/client/src/evmCall.ts (1)
37-37
: Add error handling for gateway address retrievalWhile the async conversion is correct, consider adding error handling for the gateway address retrieval to prevent unhandled promise rejections and provide better error messages.
- const gatewayEvmAddress = args.gatewayEvm || (await this.getGatewayAddress()); + let gatewayEvmAddress; + try { + gatewayEvmAddress = args.gatewayEvm || (await this.getGatewayAddress()); + } catch (error) { + throw new Error(`Failed to retrieve gateway address: ${error.message}`); + }packages/client/src/evmDeposit.ts (1)
38-38
: Add error handling for gateway address retrievalSimilar to evmCall.ts, add error handling for the gateway address retrieval to ensure robust error reporting.
- const gatewayEvmAddress = args.gatewayEvm || (await this.getGatewayAddress()); + let gatewayEvmAddress; + try { + gatewayEvmAddress = args.gatewayEvm || (await this.getGatewayAddress()); + } catch (error) { + throw new Error(`Failed to retrieve gateway address: ${error.message}`); + }packages/client/src/zetachainWithdraw.ts (1)
42-43
: Add error handling and improve readability
- Add error handling for gateway address retrieval
- Consider keeping the assignment on a single line for better readability
- const gatewayZetaChainAddress = - args.gatewayZetaChain || (await this.getGatewayAddress()); + let gatewayZetaChainAddress; + try { + gatewayZetaChainAddress = args.gatewayZetaChain || (await this.getGatewayAddress()); + } catch (error) { + throw new Error(`Failed to retrieve gateway address: ${error.message}`); + }packages/client/src/zetachainCall.ts (2)
47-48
: Add error handling and improve readability
- Add error handling for gateway address retrieval
- Consider keeping the assignment on a single line for better readability
- const gatewayZetaChainAddress = - args.gatewayZetaChain || (await this.getGatewayAddress()); + let gatewayZetaChainAddress; + try { + gatewayZetaChainAddress = args.gatewayZetaChain || (await this.getGatewayAddress()); + } catch (error) { + throw new Error(`Failed to retrieve gateway address: ${error.message}`); + }
Line range hint
37-37
: Overall implementation looks good with room for improvementThe async conversion of
getGatewayAddress()
has been implemented consistently across all files, which aligns well with the PR objectives. The changes maintain a uniform pattern, making the code predictable and maintainable.Consider implementing the suggested error handling improvements across all files to make the codebase more robust.
Also applies to: 38-38, 42-43, 47-48
packages/client/src/zetachainWithdrawAndCall.ts (1)
50-51
: LGTM! Correctly handles async gateway address retrievalThe change properly awaits the asynchronous
getGatewayAddress
call.Consider improving readability by avoiding line breaks in the assignment:
- const gatewayZetaChainAddress = - args.gatewayZetaChain || (await this.getGatewayAddress()); + const gatewayZetaChainAddress = args.gatewayZetaChain || (await this.getGatewayAddress());
📜 Review details
Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (1)
yarn.lock
is excluded by!**/yarn.lock
,!**/*.lock
📒 Files selected for processing (8)
package.json
(1 hunks)packages/client/src/client.ts
(2 hunks)packages/client/src/evmCall.ts
(1 hunks)packages/client/src/evmDeposit.ts
(1 hunks)packages/client/src/evmDepositAndCall.ts
(1 hunks)packages/client/src/zetachainCall.ts
(1 hunks)packages/client/src/zetachainWithdraw.ts
(1 hunks)packages/client/src/zetachainWithdrawAndCall.ts
(1 hunks)
✅ Files skipped from review due to trivial changes (1)
- package.json
🔇 Additional comments (2)
packages/client/src/evmDepositAndCall.ts (1)
42-42
: LGTM! Correctly handles async gateway address retrievalThe change properly awaits the asynchronous
getGatewayAddress
call.packages/client/src/zetachainWithdrawAndCall.ts (1)
Line range hint
179-217
: Verify all usages of getGatewayAddress are updatedLet's ensure all calls to
getGatewayAddress
have been updated to handle the async nature.✅ Verification successful
All getGatewayAddress usages properly handle async behavior ✅
All calls to
getGatewayAddress()
are properly awaited and used in async context across the codebase.🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Search for any remaining synchronous usage of getGatewayAddress rg "getGatewayAddress\(\)" -A 2 -B 2 # Search for potential missing await keywords rg "this\.getGatewayAddress\(\)" -A 2 -B 2Length of output: 5038
issue: zeta-chain/protocol-contracts#440
Update:
@zetachain/protocol-contracts
to11.0.0-rc4
getGatewayAddress()
Summary by CodeRabbit
Release Notes
Dependencies
@zetachain/protocol-contracts
from version11.0.0-rc3
to11.0.0-rc4
Bug Fixes
The release focuses on enhancing the reliability of gateway address retrieval in the ZetaChain client library.