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

fix get gateway address #211

Merged
merged 1 commit into from
Jan 7, 2025
Merged

fix get gateway address #211

merged 1 commit into from
Jan 7, 2025

Conversation

lukema95
Copy link
Collaborator

@lukema95 lukema95 commented Jan 7, 2025

issue: zeta-chain/protocol-contracts#440
Update:

  • Update @zetachain/protocol-contracts to 11.0.0-rc4
  • Fix getGatewayAddress()

Summary by CodeRabbit

Release Notes

  • Dependencies

    • Updated @zetachain/protocol-contracts from version 11.0.0-rc3 to 11.0.0-rc4
  • Bug Fixes

    • Improved asynchronous handling of gateway address retrieval across multiple client methods
    • Ensured consistent and correct resolution of gateway addresses in various function calls

The release focuses on enhancing the reliability of gateway address retrieval in the ZetaChain client library.

Copy link
Contributor

coderabbitai bot commented Jan 7, 2025

📝 Walkthrough

Walkthrough

The pull request introduces a series of modifications across multiple files in the ZetaChain client library. The primary change involves updating the getGatewayAddress() method to be asynchronous, which necessitates corresponding changes in various functions that call this method. The dependency version for @zetachain/protocol-contracts was also updated from 11.0.0-rc3 to 11.0.0-rc4. These changes ensure proper handling of asynchronous address retrieval across the client's core functionality.

Changes

File Change Summary
package.json Updated @zetachain/protocol-contracts dependency version from 11.0.0-rc3 to 11.0.0-rc4
packages/client/src/client.ts Modified getGatewayAddress() method to be asynchronous, returning Promise<string>
packages/client/src/evmCall.ts Updated gateway address retrieval to use await this.getGatewayAddress()
packages/client/src/evmDeposit.ts Modified gateway address assignment to use await this.getGatewayAddress()
packages/client/src/evmDepositAndCall.ts Updated gateway address retrieval to use await this.getGatewayAddress()
packages/client/src/zetachainCall.ts Modified gateway address retrieval to use await this.getGatewayAddress()
packages/client/src/zetachainWithdraw.ts Updated gateway address retrieval to use await this.getGatewayAddress()
packages/client/src/zetachainWithdrawAndCall.ts Modified gateway address retrieval to use await this.getGatewayAddress()

Sequence Diagram

sequenceDiagram
    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
Loading

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?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@lukema95 lukema95 marked this pull request as ready for review January 7, 2025 14:26
@lukema95 lukema95 requested review from andresaiello, fadeev and a team as code owners January 7, 2025 14:26
@lukema95
Copy link
Collaborator Author

lukema95 commented Jan 7, 2025

@fadeev pls review

Copy link
Contributor

@coderabbitai coderabbitai bot left a 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 handling

The 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 retrieval

While 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 retrieval

Similar 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

  1. Add error handling for gateway address retrieval
  2. 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

  1. Add error handling for gateway address retrieval
  2. 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 improvement

The 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 retrieval

The 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

📥 Commits

Reviewing files that changed from the base of the PR and between b4c9254 and 51c699e.

⛔ 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 retrieval

The change properly awaits the asynchronous getGatewayAddress call.

packages/client/src/zetachainWithdrawAndCall.ts (1)

Line range hint 179-217: Verify all usages of getGatewayAddress are updated

Let'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 2

Length of output: 5038

@fadeev fadeev merged commit 70298d0 into zeta-chain:main Jan 7, 2025
5 checks passed
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.

2 participants