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 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"@web3-name-sdk/core": "^0.2.0",
"@zetachain/protocol-contracts": "^9.0.0",
"@zetachain/networks": "^10.0.0",
"@zetachain/toolkit": "^12.0.0",
"@zetachain/toolkit": "13.0.0-rc10",
"autoprefixer": "^10.4.19",
"bech32": "^2.0.0",
"class-variance-authority": "^0.7.0",
Expand Down
139 changes: 79 additions & 60 deletions src/components/Swap/hooks/useSendTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,21 @@ const useSendTransaction = (
}
const from = sourceTokenSelected.chain_name;
const to = destinationTokenSelected.chain_name;
const btc = bitcoinAddress;
const token = sourceTokenSelected.symbol;
const tx = await client.deposit({
chain: from,
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"),
},
Comment on lines +185 to +198
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 +189 to +198
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.

});
if (track) {
track({
Expand Down Expand Up @@ -253,10 +262,20 @@ const useSendTransaction = (
console.error("ZRC-20 address not found");
return;
}
const tx = await client.withdraw({
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"),
},
Comment on lines +265 to +278
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.

});
const token = sourceTokenSelected.symbol;
const from = sourceTokenSelected.chain_name;
Expand All @@ -277,10 +296,20 @@ const useSendTransaction = (
const from = sourceTokenSelected.chain_name;
const to = destinationTokenSelected.chain_name;
const token = sourceTokenSelected.symbol;
const tx = await client.deposit({
chain: from,
const tx = await client.evmDeposit({
amount: sourceAmount,
recipient: addressSelected,
erc20: "",
receiver: addressSelected,
revertOptions: {
callOnRevert: false,
onRevertGasLimit: 7000000,
revertAddress: "0x0000000000000000000000000000000000000000",
revertMessage: "0x",
},
txOptions: {
gasLimit: 7000000,
gasPrice: ethers.BigNumber.from("50000000000"),
},
});
if (track) {
track({
Expand Down Expand Up @@ -372,47 +401,31 @@ const useSendTransaction = (
if (!sourceTokenSelected || !destinationTokenSelected) {
return;
}
const custodyAddress = getAddress(
"erc20Custody",
sourceTokenSelected.chain_name as ParamChainName
);
const custodyContract = new ethers.Contract(
custodyAddress as string,
ERC20Custody.abi,
client.signer
);
const assetAddress = sourceTokenSelected.contract;
const amount = ethers.utils.parseUnits(
sourceAmount,
sourceTokenSelected.decimals
);
try {
const contract = new ethers.Contract(
assetAddress as string,
ERC20_ABI.abi,
client.signer
);
await (await contract.approve(custodyAddress, amount)).wait();
const tx = await custodyContract.deposit(
addressSelected,
assetAddress,
amount,
"0x"
);
await tx.wait();
const token = sourceTokenSelected.symbol;
const from = sourceTokenSelected.chain_name;
const dest = destinationTokenSelected.chain_name;
if (track) {
track({
hash: tx.hash,
desc: `Sent ${sourceAmount} ${token} from ${from} to ${dest}`,
});
}
console.log(tx.hash);
} catch (error) {
console.error("Error during deposit: ", error);
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);
Comment on lines +404 to +428
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.

};

m.transferBTC = async () => {
Expand Down Expand Up @@ -449,19 +462,25 @@ const useSendTransaction = (
}
const d = destinationTokenSelected;
const zrc20 = d.coin_type === "ZRC20" ? d.contract : d.zrc20;
const params = {
chain: from,
let tx;
tx = await client.evmDepositAndCall({
amount: sourceAmount,
recipient: omnichainSwapContractAddress,
message: [
["address", "bytes", "bool"],
[zrc20, recipient, withdraw],
],
erc20: sourceTokenSelected.contract,
};
console.log("swap", params);
const tx = await client.deposit(params);

receiver: omnichainSwapContractAddress,
revertOptions: {
callOnRevert: false,
onRevertGasLimit: 7000000,
revertAddress: "0x0000000000000000000000000000000000000000",
revertMessage: "0x",
},
txOptions: {
gasLimit: 7000000,
gasPrice: ethers.BigNumber.from("10000000000"),
},
types: ["address", "bytes", "bool"],
values: [zrc20, recipient, withdraw.toString()],
});
console.log(tx);
if (tx && track) {
track({
hash: tx.hash,
Expand Down
Loading