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: Use latest toolkit function for useSendTransaction hook #31

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

lukema95
Copy link

@lukema95 lukema95 commented Dec 24, 2024

issue: #30

Summary by CodeRabbit

  • Dependency Update

    • Updated @zetachain/toolkit to version 13.0.0-rc10
  • Transaction Handling Improvements

    • Enhanced methods for Bitcoin and ZRC20 transaction processing
    • Streamlined cross-chain swap and deposit mechanisms
    • Introduced more robust error handling and transaction configuration options

Copy link
Contributor

coderabbitai bot commented Dec 24, 2024

📝 Walkthrough

Walkthrough

The pull request introduces a focused update to the project's dependency management and transaction handling mechanisms. The changes center around updating the @zetachain/toolkit dependency to a specific pre-release version and refactoring the useSendTransaction hook to utilize new transaction methods for improved cross-chain transaction processing.

Changes

File Change Summary
package.json Updated @zetachain/toolkit dependency from ^12.0.0 to 13.0.0-rc10
src/components/Swap/hooks/useSendTransaction.ts Refactored transaction methods to use new zetachainWithdraw, evmDeposit, and evmDepositAndCall methods

Sequence Diagram

sequenceDiagram
    participant Client
    participant Transaction Hook
    participant Blockchain

    Client->>Transaction Hook: Initiate Transaction
    Transaction Hook->>Blockchain: zetachainWithdraw (BTC)
    Transaction Hook->>Blockchain: zetachainWithdraw (ZRC20)
    Transaction Hook->>Blockchain: evmDeposit (Native)
    Transaction Hook->>Blockchain: evmDeposit (ERC20)
    Transaction Hook->>Blockchain: evmDepositAndCall
Loading

The sequence diagram illustrates the updated transaction flow, showcasing the new method calls for different transaction types across various blockchain interactions.

Finishing Touches

  • 📝 Generate Docstrings

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.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

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
Copy link
Author

lukema95 commented Jan 8, 2025

@fadeev Could you upload the new package for this pr? I need it to test.

@fadeev
Copy link
Member

fadeev commented Jan 8, 2025

https://github.com/zeta-chain/toolkit/releases/tag/v13.0.0-rc10

@lukema95
Copy link
Author

lukema95 commented Jan 9, 2025

Have tested:

DepositAndSwap

  1. tBNB(bsc_testnet) -> Matic.Amoy(zeta_testnet)

  2. tBNB(bsc_testnet) -> Matic.Amoy(amoy_testnet)

Deposit

Deposit tBNB(bsc_testnet)

@lukema95 lukema95 marked this pull request as ready for review January 9, 2025 06:36
@lukema95
Copy link
Author

lukema95 commented Jan 9, 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: 4

🔭 Outside diff range comments (1)
src/components/Swap/hooks/useSendTransaction.ts (1)

Line range hint 185-483: Standardize gas price values across transactions

There is inconsistency in the gasPrice values specified in different functions (10000000000 and 50000000000). To ensure predictable transaction costs and avoid potential overpayment, consider standardizing the gasPrice or making it configurable based on network conditions.

Example adjustment:

 const TX_OPTIONS_STANDARD = {
   gasLimit: 7_000_000,
-  gasPrice: ethers.BigNumber.from("10_000_000_000"),
+  gasPrice: ethers.BigNumber.from("50_000_000_000"), // Adjusted for consistency
 };
🧹 Nitpick comments (2)
src/components/Swap/hooks/useSendTransaction.ts (2)

299-312: Utilize constants for transaction options in depositNative

The depositNative function redefines revertOptions and txOptions. Consistently applying the extracted constants will reduce redundancy.

Update the function accordingly:

 const tx = await client.evmDeposit({
   amount: sourceAmount,
   erc20: "",
   receiver: addressSelected,
-  revertOptions: {
-    callOnRevert: false,
-    onRevertGasLimit: 7000000,
-    revertAddress: "0x0000000000000000000000000000000000000000",
-    revertMessage: "0x",
-  },
+  revertOptions: REVERT_OPTIONS,
-  txOptions: {
-    gasLimit: 7000000,
-    gasPrice: ethers.BigNumber.from("50000000000"),
-  },
+  txOptions: {
+    ...TX_OPTIONS_STANDARD,
+    gasPrice: ethers.BigNumber.from("50_000_000_000"),
+  },
 });

465-483: Consolidate transaction options in crossChainSwapHandle

The crossChainSwapHandle function defines txOptions and revertOptions inline. For consistency and maintainability, use the predefined constants.

Adjust the function to incorporate the constants:

 let tx;
 tx = await client.evmDepositAndCall({
   amount: sourceAmount,
   erc20: sourceTokenSelected.contract,
   receiver: omnichainSwapContractAddress,
-  revertOptions: {
-    callOnRevert: false,
-    onRevertGasLimit: 7000000,
-    revertAddress: "0x0000000000000000000000000000000000000000",
-    revertMessage: "0x",
-  },
+  revertOptions: REVERT_OPTIONS,
-  txOptions: {
-    gasLimit: 7000000,
-    gasPrice: ethers.BigNumber.from("10000000000"),
-  },
+  txOptions: TX_OPTIONS_STANDARD,
   types: ["address", "bytes", "bool"],
   values: [zrc20, recipient, withdraw.toString()],
 });
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 4f1a3c2 and 08373d2.

⛔ Files ignored due to path filters (1)
  • yarn.lock is excluded by !**/yarn.lock, !**/*.lock
📒 Files selected for processing (2)
  • package.json (1 hunks)
  • src/components/Swap/hooks/useSendTransaction.ts (5 hunks)
🔇 Additional comments (1)
package.json (1)

37-37: Verify compatibility of the updated @zetachain/toolkit dependency

The @zetachain/toolkit version has been updated to "13.0.0-rc10", a pre-release candidate. Ensure that this version is stable for production use and compatible with the existing codebase. Pre-release versions may contain breaking changes or unresolved issues.

Consider running comprehensive tests to confirm that the updated toolkit functions as expected with your application.

Comment on lines +185 to +198
const tx = await client.zetachainWithdraw({
amount: sourceAmount,
recipient: addressSelected,
zrc20: sourceTokenSelected.contract,
receiver: bitcoinAddress,
revertOptions: {
callOnRevert: false,
onRevertGasLimit: 7000000,
revertAddress: "0x0000000000000000000000000000000000000000",
revertMessage: "0x",
},
txOptions: {
gasLimit: 7000000,
gasPrice: ethers.BigNumber.from("10000000000"),
},
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Ensure bitcoinAddress is validated before usage

In the withdrawBTC function, bitcoinAddress is used as the receiver parameter without prior validation. If bitcoinAddress is undefined, it could lead to runtime errors or unintended behavior during transaction execution.

Apply this diff to add a validation check for bitcoinAddress:

 m.withdrawBTC = async () => {
   if (!sourceTokenSelected || !destinationTokenSelected) {
     return;
   }
+  if (!bitcoinAddress) {
+    throw new Error("Bitcoin address is undefined.");
+  }
   const from = sourceTokenSelected.chain_name;
   const to = destinationTokenSelected.chain_name;
   const token = sourceTokenSelected.symbol;

Committable suggestion skipped: line range outside the PR's diff.

Comment on lines +265 to +278
const tx = await client.zetachainWithdraw({
amount: sourceAmount,
zrc20,
recipient: addressSelected,
receiver: addressSelected,
revertOptions: {
callOnRevert: false,
onRevertGasLimit: 7000000,
revertAddress: "0x0000000000000000000000000000000000000000",
revertMessage: "0x",
},
txOptions: {
gasLimit: 7000000,
gasPrice: ethers.BigNumber.from("10000000000"),
},
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Apply standardized constants for revertOptions and txOptions

In the withdrawZRC20 function, the same revertOptions and txOptions are redefined. Utilize the previously extracted constants to enhance consistency and maintainability.

Modify the function to use the constants:

 const tx = await client.zetachainWithdraw({
   amount: sourceAmount,
   zrc20,
   receiver: addressSelected,
-  revertOptions: {
-    callOnRevert: false,
-    onRevertGasLimit: 7000000,
-    revertAddress: "0x0000000000000000000000000000000000000000",
-    revertMessage: "0x",
-  },
+  revertOptions: REVERT_OPTIONS,
-  txOptions: {
-    gasLimit: 7000000,
-    gasPrice: ethers.BigNumber.from("10000000000"),
-  },
+  txOptions: TX_OPTIONS_STANDARD,
 });

Committable suggestion skipped: line range outside the PR's diff.

Comment on lines +404 to +428
const from = sourceTokenSelected.chain_name;
const to = destinationTokenSelected.chain_name;
const token = sourceTokenSelected.symbol;
const tx = await client.evmDeposit({
amount: sourceAmount,
erc20: sourceTokenSelected.contract,
receiver: addressSelected,
revertOptions: {
callOnRevert: false,
onRevertGasLimit: 7000000,
revertAddress: "0x0000000000000000000000000000000000000000",
revertMessage: "0x",
},
txOptions: {
gasLimit: 7000000,
gasPrice: ethers.BigNumber.from("50000000000"),
},
});
if (track) {
track({
hash: tx.hash,
desc: `Sent ${sourceAmount} ${token} from ${from} to ${to}`,
});
}
console.log(tx.hash);
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Refactor depositERC20 to use standardized options

In the depositERC20 function, revertOptions and txOptions are again duplicated. Apply the constants to maintain uniformity across transaction functions.

Revise the function as follows:

 const tx = await client.evmDeposit({
   amount: sourceAmount,
   erc20: sourceTokenSelected.contract,
   receiver: addressSelected,
-  revertOptions: {
-    callOnRevert: false,
-    onRevertGasLimit: 7000000,
-    revertAddress: "0x0000000000000000000000000000000000000000",
-    revertMessage: "0x",
-  },
+  revertOptions: REVERT_OPTIONS,
-  txOptions: {
-    gasLimit: 7000000,
-    gasPrice: ethers.BigNumber.from("50000000000"),
-  },
+  txOptions: {
+    ...TX_OPTIONS_STANDARD,
+    gasPrice: ethers.BigNumber.from("50_000_000_000"),
+  },
 });

Committable suggestion skipped: line range outside the PR's diff.

Comment on lines +189 to +198
revertOptions: {
callOnRevert: false,
onRevertGasLimit: 7000000,
revertAddress: "0x0000000000000000000000000000000000000000",
revertMessage: "0x",
},
txOptions: {
gasLimit: 7000000,
gasPrice: ethers.BigNumber.from("10000000000"),
},
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Refactor revertOptions and txOptions into reusable constants

The revertOptions and txOptions objects are duplicated across multiple functions, which hampers maintainability and increases the risk of inconsistencies. Extracting these into constants will promote code reuse and simplify future updates.

Define the constants at the appropriate scope:

+const REVERT_OPTIONS = {
+  callOnRevert: false,
+  onRevertGasLimit: 7_000_000,
+  revertAddress: "0x0000000000000000000000000000000000000000",
+  revertMessage: "0x",
+};

+const TX_OPTIONS_STANDARD = {
+  gasLimit: 7_000_000,
+  gasPrice: ethers.BigNumber.from("10_000_000_000"),
+};

 m.withdrawBTC = async () => {
   // Existing code...
   const tx = await client.zetachainWithdraw({
     amount: sourceAmount,
     zrc20: sourceTokenSelected.contract,
     receiver: bitcoinAddress,
-    revertOptions: {
-      callOnRevert: false,
-      onRevertGasLimit: 7000000,
-      revertAddress: "0x0000000000000000000000000000000000000000",
-      revertMessage: "0x",
-    },
+    revertOptions: REVERT_OPTIONS,
-    txOptions: {
-      gasLimit: 7000000,
-      gasPrice: ethers.BigNumber.from("10000000000"),
-    },
+    txOptions: TX_OPTIONS_STANDARD,
   });

Committable suggestion skipped: line range outside the PR's diff.

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