Skip to content

Commit

Permalink
fix: deposit ERC-20
Browse files Browse the repository at this point in the history
  • Loading branch information
fadeev committed Jul 5, 2024
1 parent 3ada76c commit 26f85e5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 19 deletions.
31 changes: 14 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 @@ -35,11 +35,11 @@ export const deposit = async function (
erc20,
message,
}: {
amount: string;
chain: string;
amount: string;

Check failure on line 39 in packages/client/src/deposit.ts

View workflow job for this annotation

GitHub Actions / build

Expected interface keys to be in ascending order. 'amount' should be before 'chain'
recipient: string;
erc20?: string;

Check failure on line 41 in packages/client/src/deposit.ts

View workflow job for this annotation

GitHub Actions / build

Expected interface keys to be in ascending order. 'erc20' should be before 'recipient'
message?: [string[], string[]];
recipient?: string;
}
) {
let signer;
Expand All @@ -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",
Expand All @@ -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,

Check failure on line 103 in packages/client/src/deposit.ts

View workflow job for this annotation

GitHub Actions / build

Expected object keys to be in ascending order. 'data' should be before 'value'
};
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 26f85e5

Please sign in to comment.