diff --git a/contracts/BytesHelperLib.sol b/contracts/BytesHelperLib.sol index ca6ec9f..8266d29 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 e70724c..4a9afae 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 b6c9fa1..6bc9fce 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")