From 26f85e55fe3bd4302309ee21e51de78121083b66 Mon Sep 17 00:00:00 2001 From: Denis Fadeev Date: Fri, 5 Jul 2024 16:58:53 +0300 Subject: [PATCH 1/4] fix: deposit ERC-20 --- packages/client/src/deposit.ts | 31 ++++++++++++++----------------- packages/tasks/src/deposit.ts | 4 ++-- 2 files changed, 16 insertions(+), 19 deletions(-) diff --git a/packages/client/src/deposit.ts b/packages/client/src/deposit.ts index e70724cf..6080abfb 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". @@ -35,11 +35,11 @@ export const deposit = async function ( erc20, message, }: { - amount: string; chain: string; + amount: string; + recipient: string; erc20?: string; message?: [string[], string[]]; - recipient?: string; } ) { let signer; @@ -56,11 +56,14 @@ 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 + ? abiCoder.encode(message[0], message[1]).slice(2) + : ""; + const data = recipientHex + encodedMessage; + if (erc20) { const custody = getAddress( "erc20Custody", @@ -83,28 +86,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 = { to: tss, value: ethers.utils.parseUnits(amount, 18), + data, }; - 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") From 323ebc8b773125e953388c505f337a93ac5a5ee5 Mon Sep 17 00:00:00 2001 From: Denis Fadeev Date: Fri, 5 Jul 2024 17:00:14 +0300 Subject: [PATCH 2/4] lint --- contracts/BytesHelperLib.sol | 11 +++++------ packages/client/src/deposit.ts | 6 +++--- 2 files changed, 8 insertions(+), 9 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 6080abfb..80ce3782 100644 --- a/packages/client/src/deposit.ts +++ b/packages/client/src/deposit.ts @@ -35,11 +35,11 @@ export const deposit = async function ( erc20, message, }: { - chain: string; amount: string; - recipient: string; + chain: string; erc20?: string; message?: [string[], string[]]; + recipient: string; } ) { let signer; @@ -98,9 +98,9 @@ export const deposit = async function ( throw new Error(`No TSS contract found for ${chain} chain.`); } const tx = { + data, to: tss, value: ethers.utils.parseUnits(amount, 18), - data, }; return await signer.sendTransaction(tx); } From e14700f54d21b6d25643d43ad7b84759888df4af Mon Sep 17 00:00:00 2001 From: Denis Fadeev Date: Fri, 5 Jul 2024 17:24:20 +0300 Subject: [PATCH 3/4] Update packages/client/src/deposit.ts Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- packages/client/src/deposit.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/client/src/deposit.ts b/packages/client/src/deposit.ts index 80ce3782..df9a746b 100644 --- a/packages/client/src/deposit.ts +++ b/packages/client/src/deposit.ts @@ -56,7 +56,6 @@ export const deposit = async function ( if (message && !recipient) { throw new Error("Please, provide a valid contract address as recipient."); } - const abiCoder = ethers.utils.defaultAbiCoder; const recipientHex = ethers.utils.hexZeroPad(recipient, 20); const encodedMessage = message From fafaa0f1a282a3cf057cbe1f42b38e1a71dbcb73 Mon Sep 17 00:00:00 2001 From: Denis Fadeev Date: Fri, 5 Jul 2024 17:46:36 +0300 Subject: [PATCH 4/4] refactor --- packages/client/src/deposit.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/client/src/deposit.ts b/packages/client/src/deposit.ts index df9a746b..4a9afae8 100644 --- a/packages/client/src/deposit.ts +++ b/packages/client/src/deposit.ts @@ -56,10 +56,10 @@ export const deposit = async function ( if (message && !recipient) { throw new Error("Please, provide a valid contract address as recipient."); } - const abiCoder = ethers.utils.defaultAbiCoder; + const recipientHex = ethers.utils.hexZeroPad(recipient, 20); const encodedMessage = message - ? abiCoder.encode(message[0], message[1]).slice(2) + ? ethers.utils.defaultAbiCoder.encode(message[0], message[1]).slice(2) : ""; const data = recipientHex + encodedMessage;