Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: deposit ERC-20 #160

Merged
merged 4 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
29 changes: 13 additions & 16 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,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 = {
data,
to: tss,
value: ethers.utils.parseUnits(amount, 18),
};
tx.data = recipient
? `${recipient}${data.slice(2) ?? ""}`
: ethers.utils.hexlify([]);
return await signer.sendTransaction(tx);
}
fadeev marked this conversation as resolved.
Show resolved Hide resolved
};
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
Loading