From 748c2548d670882aa375ebec6f68569c0b5a2702 Mon Sep 17 00:00:00 2001 From: Denis Fadeev Date: Wed, 10 Jul 2024 06:51:30 +0300 Subject: [PATCH 1/3] fix: getQuote should work with ZETA (#156) --- packages/client/src/getQuote.ts | 44 +++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/packages/client/src/getQuote.ts b/packages/client/src/getQuote.ts index b9494024..8047a427 100644 --- a/packages/client/src/getQuote.ts +++ b/packages/client/src/getQuote.ts @@ -144,37 +144,43 @@ export const getWithdrawFeeInInputToken = async function ( export const getQuote = async function ( this: ZetaChainClient, inputAmount: string, - inputZRC20: string, - outputZRC20: string + inputToken: string, + outputToken: string ) { const rpc = this.getEndpoint("evm", "zeta_testnet"); const provider = new ethers.providers.JsonRpcProvider(rpc); const zetaToken = getZetaToken(); - const inputContract = new ethers.Contract(inputZRC20, ZRC20.abi, provider); - const outputContract = new ethers.Contract(outputZRC20, ZRC20.abi, provider); + const inputContract = new ethers.Contract(inputToken, ZRC20.abi, provider); + const outputContract = new ethers.Contract(outputToken, ZRC20.abi, provider); const inputDecimals = await inputContract.decimals(); const amountIn = parseUnits(inputAmount, inputDecimals).toString(); const outputDecimals = await outputContract.decimals(); - const outputAmountInZETA = await getAmounts( - "out", - provider, - amountIn, - inputZRC20, - zetaToken - ); + let out; - const outputAmount = await getAmounts( - "out", - provider, - outputAmountInZETA[1], - zetaToken, - outputZRC20 - ); + if (inputToken === zetaToken || outputToken === zetaToken) { + out = await getAmounts("out", provider, amountIn, inputToken, outputToken); + } else { + const outInZETA = await getAmounts( + "out", + provider, + amountIn, + inputToken, + zetaToken + ); + + out = await getAmounts( + "out", + provider, + outInZETA[1], + zetaToken, + outputToken + ); + } - return { amount: outputAmount[1], decimals: outputDecimals }; + return { amount: out[1], decimals: outputDecimals }; }; /** From 89565d314b086248bd2502b5d825a452e2d3ec59 Mon Sep 17 00:00:00 2001 From: Denis Fadeev Date: Wed, 10 Jul 2024 06:51:46 +0300 Subject: [PATCH 2/3] chore: update protocol contracts to v8 (#159) --- package.json | 2 +- packages/client/src/getFees.ts | 9 +++++++-- yarn.lock | 8 ++++---- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index 267f8d23..f90675f7 100644 --- a/package.json +++ b/package.json @@ -94,7 +94,7 @@ "@uniswap/v2-periphery": "^1.1.0-beta.0", "@zetachain/faucet-cli": "^4.0.1", "@zetachain/networks": "^8.0.0", - "@zetachain/protocol-contracts": "7.0.0", + "@zetachain/protocol-contracts": "8.0.0-rc1", "axios": "^1.4.0", "bech32": "^2.0.0", "bip39": "^3.1.0", diff --git a/packages/client/src/getFees.ts b/packages/client/src/getFees.ts index 5eee4571..54a5d59b 100644 --- a/packages/client/src/getFees.ts +++ b/packages/client/src/getFees.ts @@ -12,7 +12,12 @@ const fetchZEVMFees = async function ( ) { const provider = new ethers.providers.StaticJsonRpcProvider(rpcUrl); const contract = new ethers.Contract(zrc20.address, ZRC20.abi, provider); - const [, withdrawGasFee] = await contract.withdrawGasFee(); + let withdrawGasFee; + try { + [, withdrawGasFee] = await contract.withdrawGasFee(); + } catch (e) { + return; + } const gasFee = ethers.BigNumber.from(withdrawGasFee); const protocolFee = ethers.BigNumber.from(await contract.PROTOCOL_FLAT_FEE()); const gasToken = foreignCoins.find((c: any) => { @@ -90,7 +95,7 @@ export const getFees = async function (this: ZetaChainClient, gas: Number) { try { const rpcUrl = this.getEndpoint("evm", `zeta_${this.network}`); const fee = await fetchZEVMFees(zrc20, rpcUrl, foreignCoins); - fees.omnichain.push(fee); + if (fee) fees.omnichain.push(fee); } catch (err) { console.log(err); } diff --git a/yarn.lock b/yarn.lock index 019b10f7..c724f99a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1767,10 +1767,10 @@ dependencies: dotenv "^16.1.4" -"@zetachain/protocol-contracts@7.0.0": - version "7.0.0" - resolved "https://registry.yarnpkg.com/@zetachain/protocol-contracts/-/protocol-contracts-7.0.0.tgz#20eb6c62d805d7470408ccdff0e3614684bca174" - integrity sha512-8JTNFZxVZYmDtAXJIEr+tkakuML12X42Fya4bJ1NkfWiVMkcSej92BSTl/35qYtHdjY7vXy9uMrfXEqfw5rsPw== +"@zetachain/protocol-contracts@8.0.0-rc1": + version "8.0.0-rc1" + resolved "https://registry.yarnpkg.com/@zetachain/protocol-contracts/-/protocol-contracts-8.0.0-rc1.tgz#34df589c1b56df77f2695b37bd8a4b79df1787b1" + integrity sha512-VpgbQtVUxy4XYFb0P9UN9nQZkhQ+fr3Bg7A5SqZJCH3fi8QELSn6qaVSwATn+mXdnSMD2uXWSnyy/ANXFqedmw== abbrev@1: version "1.1.1" From 1cfaebb397837429b3b0afa74009c7eab271cbb5 Mon Sep 17 00:00:00 2001 From: Denis Fadeev Date: Wed, 10 Jul 2024 06:51:53 +0300 Subject: [PATCH 3/3] fix: deposit ERC-20 (#160) Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- contracts/BytesHelperLib.sol | 11 +++++------ packages/client/src/deposit.ts | 30 +++++++++++++----------------- packages/tasks/src/deposit.ts | 4 ++-- 3 files changed, 20 insertions(+), 25 deletions(-) diff --git a/contracts/BytesHelperLib.sol b/contracts/BytesHelperLib.sol index ca6ec9fc..8266d291 100644 --- a/contracts/BytesHelperLib.sol +++ b/contracts/BytesHelperLib.sol @@ -3,7 +3,7 @@ pragma solidity =0.8.7; library BytesHelperLib { error OffsetOutOfBounds(); - + function bytesToAddress( bytes calldata data, uint256 offset @@ -51,11 +51,10 @@ library BytesHelperLib { return bech32Bytes; } - function bytesToBool(bytes calldata data, uint256 offset) - internal - pure - returns (bool) - { + function bytesToBool( + bytes calldata data, + uint256 offset + ) internal pure returns (bool) { if (offset >= data.length) { revert OffsetOutOfBounds(); } diff --git a/packages/client/src/deposit.ts b/packages/client/src/deposit.ts index e70724cf..4a9afae8 100644 --- a/packages/client/src/deposit.ts +++ b/packages/client/src/deposit.ts @@ -12,7 +12,7 @@ import { prepareParams } from "./prepareData"; * * @param this - ZetaChainClient instance. * @param options - Deposit options. - * @param options.sourceChain - Label of the connected chain from which the deposit is + * @param options.chain - Label of the connected chain from which the deposit is * made. * @param options.amount - Amount to be deposited in human readable form. For * example, 1.5 ETH is "1.5". @@ -39,7 +39,7 @@ export const deposit = async function ( chain: string; erc20?: string; message?: [string[], string[]]; - recipient?: string; + recipient: string; } ) { let signer; @@ -56,11 +56,13 @@ export const deposit = async function ( if (message && !recipient) { throw new Error("Please, provide a valid contract address as recipient."); } - const to = recipient || this.signer.address; - const abiCoder = ethers.utils.defaultAbiCoder; - const data = message - ? abiCoder.encode(message[0], message[1]) - : ethers.utils.hexlify([]); + + const recipientHex = ethers.utils.hexZeroPad(recipient, 20); + const encodedMessage = message + ? ethers.utils.defaultAbiCoder.encode(message[0], message[1]).slice(2) + : ""; + const data = recipientHex + encodedMessage; + if (erc20) { const custody = getAddress( "erc20Custody", @@ -83,28 +85,22 @@ export const deposit = async function ( throw new Error("Amount cannot be parsed."); } const balance = await contract.balanceOf(signer.address); - if (balance.lt(amount)) { + if (balance.lt(value)) { throw new Error("Insufficient token balance."); } const approveTx = await contract.approve(custody, value); await approveTx.wait(); - return await custodyContract.deposit(to, erc20, value, data); + return await custodyContract.deposit(recipient, erc20, value, data); } else { const tss = getAddress("tss", chain as ParamChainName); if (!tss) { throw new Error(`No TSS contract found for ${chain} chain.`); } - const tx: { - data?: string; - to: string; - value: ethers.BigNumberish; - } = { + const tx = { + data, to: tss, value: ethers.utils.parseUnits(amount, 18), }; - tx.data = recipient - ? `${recipient}${data.slice(2) ?? ""}` - : ethers.utils.hexlify([]); return await signer.sendTransaction(tx); } }; diff --git a/packages/tasks/src/deposit.ts b/packages/tasks/src/deposit.ts index b6c9fa14..6bc9fce7 100644 --- a/packages/tasks/src/deposit.ts +++ b/packages/tasks/src/deposit.ts @@ -115,7 +115,7 @@ const main = async (args: any, hre: HardhatRuntimeEnvironment) => { Networks: ${chain} → zeta_testnet Amount sent: ${amount} ${symbol || ""} Sender: ${signer.address} -Recipient: ${args.recipient || signer.address}`); +Recipient: ${args.recipient}`); if (message) { console.log(`Message: ${args.message}`); } @@ -148,7 +148,7 @@ export const depositTask = task( main ) .addParam("amount", "Amount tokens to send") - .addOptionalParam("recipient", "Recipient address") + .addParam("recipient", "Recipient address") .addOptionalParam("erc20", "ERC-20 token address") .addOptionalParam("message", `Message, like '[["string"], ["hello"]]'`) .addFlag("json", "Output in JSON")