Skip to content

Commit

Permalink
fix: deposit ERC-20 (#160)
Browse files Browse the repository at this point in the history
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
  • Loading branch information
fadeev and coderabbitai[bot] authored Jul 10, 2024
1 parent 89565d3 commit 1cfaebb
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 25 deletions.
11 changes: 5 additions & 6 deletions contracts/BytesHelperLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pragma solidity =0.8.7;

library BytesHelperLib {
error OffsetOutOfBounds();

function bytesToAddress(
bytes calldata data,
uint256 offset
Expand Down Expand Up @@ -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();
}
Expand Down
30 changes: 13 additions & 17 deletions packages/client/src/deposit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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".
Expand All @@ -39,7 +39,7 @@ export const deposit = async function (
chain: string;
erc20?: string;
message?: [string[], string[]];
recipient?: string;
recipient: string;
}
) {
let signer;
Expand All @@ -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",
Expand All @@ -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);
}
};
4 changes: 2 additions & 2 deletions packages/tasks/src/deposit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
}
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 1cfaebb

Please sign in to comment.