Skip to content

Commit

Permalink
call: deposit/depositAndCall ERC-20s
Browse files Browse the repository at this point in the history
  • Loading branch information
fadeev committed Nov 12, 2024
1 parent c5e2348 commit 5e05fb0
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 14 deletions.
23 changes: 23 additions & 0 deletions examples/call/contracts/Connected.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ contract Connected {
gateway.deposit{value: msg.value}(receiver, revertOptions);
}

function deposit(
address receiver,
uint256 amount,
address asset,
RevertOptions memory revertOptions
) external {
IERC20(asset).transferFrom(msg.sender, address(this), amount);
IERC20(asset).approve(address(gateway), amount);
gateway.deposit(receiver, amount, asset, revertOptions);
}

Check failure

Code scanning / Slither

Unchecked transfer High

Check warning

Code scanning / Slither

Unused return Medium


function call(
address receiver,
bytes calldata message,
Expand All @@ -34,6 +45,18 @@ contract Connected {
gateway.call(receiver, message, revertOptions);
}

function depositAndCall(
address receiver,
uint256 amount,
address asset,
bytes calldata message,
RevertOptions memory revertOptions
) external {
IERC20(asset).transferFrom(msg.sender, address(this), amount);
IERC20(asset).approve(address(gateway), amount);
gateway.depositAndCall(receiver, amount, asset, message, revertOptions);
}

Check failure

Code scanning / Slither

Unchecked transfer High

Check warning

Code scanning / Slither

Unused return Medium


function depositAndCall(
address receiver,
bytes calldata message,
Expand Down
20 changes: 20 additions & 0 deletions examples/call/scripts/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ echo -e "\n🚀 Compiling contracts..."
npx hardhat compile --force --quiet

ZRC20_ETHEREUM=$(jq -r '.addresses[] | select(.type=="ZRC-20 ETH on 5") | .address' localnet.json)
ERC20_ETHEREUM=$(jq -r '.addresses[] | select(.type=="ERC-20 USDC" and .chain=="ethereum") | .address' localnet.json)
ZRC20_BNB=$(jq -r '.addresses[] | select(.type=="ZRC-20 BNB on 97") | .address' localnet.json)
GATEWAY_ETHEREUM=$(jq -r '.addresses[] | select(.type=="gatewayEVM" and .chain=="ethereum") | .address' localnet.json)
GATEWAY_ZETACHAIN=$(jq -r '.addresses[] | select(.type=="gatewayZEVM" and .chain=="zetachain") | .address' localnet.json)
Expand All @@ -27,6 +28,15 @@ npx hardhat connected-deposit \

npx hardhat localnet-check

npx hardhat connected-deposit \
--contract "$CONTRACT_ETHEREUM" \
--receiver "$CONTRACT_ZETACHAIN" \
--network localhost \
--erc20 "$ERC20_ETHEREUM" \
--amount 1

npx hardhat localnet-check

npx hardhat connected-call \
--contract "$CONTRACT_ETHEREUM" \
--receiver "$CONTRACT_ZETACHAIN" \
Expand All @@ -44,6 +54,16 @@ npx hardhat connected-deposit-and-call \

npx hardhat localnet-check

npx hardhat connected-deposit-and-call \
--contract "$CONTRACT_ETHEREUM" \
--receiver "$CONTRACT_ZETACHAIN" \
--network localhost \
--amount 1 \
--erc20 "$ERC20_ETHEREUM" \
--types '["string"]' alice

npx hardhat localnet-check

npx hardhat universal-withdraw \
--contract "$CONTRACT_ZETACHAIN" \
--receiver "$CONTRACT_ETHEREUM" \
Expand Down
38 changes: 32 additions & 6 deletions examples/call/tasks/connectedDeposit.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import ERC20_ABI from "@openzeppelin/contracts/build/contracts/ERC20.json";
import { task, types } from "hardhat/config";
import type { HardhatRuntimeEnvironment } from "hardhat/types";

Expand All @@ -23,12 +24,36 @@ const main = async (args: any, hre: HardhatRuntimeEnvironment) => {
const factory = (await hre.ethers.getContractFactory("Connected")) as any;
const contract = factory.attach(args.contract).connect(signer);

const value = hre.ethers.utils.parseEther(args.amount);

const tx = await contract.deposit(args.receiver, revertOptions, {
value,
...txOptions,
});
let tx;
if (args.erc20) {
const erc20Contract = new ethers.Contract(
args.erc20,
ERC20_ABI.abi,
signer
);
const decimals = await erc20Contract.decimals();
const value = hre.ethers.utils.parseUnits(args.amount, decimals);
const approveTx = await erc20Contract
.connect(signer)
.approve(args.contract, value);
await approveTx.wait();
const method =
"deposit(address,uint256,address,(address,bool,address,bytes,uint256))";
tx = await contract[method](
args.receiver,
value,
args.erc20,
revertOptions,
txOptions
);
} else {
const value = hre.ethers.utils.parseEther(args.amount);
const method = "deposit(address,(address,bool,address,bytes,uint256))";
tx = await contract[method](args.receiver, revertOptions, {
...txOptions,
value,
});
}

await tx.wait();
console.log(`Transaction hash: ${tx.hash}`);
Expand Down Expand Up @@ -65,4 +90,5 @@ task("connected-deposit", "Deposit tokens to ZetaChain", main)
7000000,
types.int
)
.addOptionalParam("erc20", "The address of the ERC20 token to deposit")
.addParam("amount", "The amount of tokens to deposit");
47 changes: 39 additions & 8 deletions examples/call/tasks/connectedDepositAndCall.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import ERC20_ABI from "@openzeppelin/contracts/build/contracts/ERC20.json";
import { task, types } from "hardhat/config";
import type { HardhatRuntimeEnvironment } from "hardhat/types";

Expand Down Expand Up @@ -51,14 +52,43 @@ const main = async (args: any, hre: HardhatRuntimeEnvironment) => {
const factory = (await hre.ethers.getContractFactory(args.name)) as any;
const contract = factory.attach(args.contract).connect(signer);

const value = hre.ethers.utils.parseEther(args.amount);

const tx = await contract.depositAndCall(
args.receiver,
encodedParameters,
revertOptions,
{ value, ...txOptions }
);
let tx;
if (args.erc20) {
const erc20Contract = new ethers.Contract(
args.erc20,
ERC20_ABI.abi,
signer
);
const decimals = await erc20Contract.decimals();
const value = hre.ethers.utils.parseUnits(args.amount, decimals);
const approveTx = await erc20Contract
.connect(signer)
.approve(args.contract, value);
await approveTx.wait();
const method =
"depositAndCall(address,uint256,address,bytes,(address,bool,address,bytes,uint256))";
tx = await contract[method](
args.receiver,
value,
args.erc20,
encodedParameters,
revertOptions,
txOptions
);
} else {
const value = hre.ethers.utils.parseEther(args.amount);
const method =
"depositAndCall(address,bytes,(address,bool,address,bytes,uint256))";
tx = await contract[method](
args.receiver,
encodedParameters,
revertOptions,
{
...txOptions,
value,
}
);
}

await tx.wait();
console.log(`Transaction hash: ${tx.hash}`);
Expand Down Expand Up @@ -101,5 +131,6 @@ task(
)
.addParam("amount", "The amount of tokens to deposit")
.addParam("name", "The name of the contract", "Connected")
.addOptionalParam("erc20", "The address of the ERC20 token to deposit")
.addParam("types", `The types of the parameters (example: '["string"]')`)
.addVariadicPositionalParam("values", "The values of the parameters");

0 comments on commit 5e05fb0

Please sign in to comment.