Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion examples/swap/hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import "./tasks/deploy";
import "./tasks/swap";
import "./tasks/swapFromZetaChain";
import "./tasks/swapFromEVM";
import "@zetachain/localnet/tasks";
import "@nomicfoundation/hardhat-toolbox";
import "@zetachain/toolkit/tasks";
Expand Down
6 changes: 3 additions & 3 deletions examples/swap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"@types/node": ">=12.0.0",
"@typescript-eslint/eslint-plugin": "^5.59.9",
"@typescript-eslint/parser": "^5.59.9",
"@zetachain/localnet": "^3.2.0",
"@zetachain/localnet": "^3.3.0",
"axios": "^1.3.6",
"chai": "^4.2.0",
"dotenv": "^16.0.3",
Expand Down Expand Up @@ -57,6 +57,6 @@
"@solana/spl-memo": "^0.2.5",
"@solana/web3.js": "^1.95.2",
"@zetachain/protocol-contracts": "10.0.0-rc10",
"@zetachain/toolkit": "13.0.0-rc4"
"@zetachain/toolkit": "13.0.0-rc5"
}
}
}
82 changes: 82 additions & 0 deletions examples/swap/tasks/swapFromEVM.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { task, types } from "hardhat/config";
import type { HardhatRuntimeEnvironment } from "hardhat/types";

import { ZetaChainClient } from "@zetachain/toolkit/client";

export const evmDepositAndCall = async (
args: any,
hre: HardhatRuntimeEnvironment
) => {
try {
const [signer] = await hre.ethers.getSigners();
const client = new ZetaChainClient({ network: "testnet", signer });
const tx = await client.evmDepositAndCall({
amount: args.amount,
erc20: args.erc20,
gatewayEvm: args.gatewayEvm,
receiver: args.receiver,
revertOptions: {
callOnRevert: args.callOnRevert,
onRevertGasLimit: args.onRevertGasLimit,
revertAddress: args.revertAddress,
revertMessage: args.revertMessage,
},
txOptions: {
gasLimit: args.gasLimit,
gasPrice: args.gasPrice,
},
types: ["address", "bytes", "bool"],
values: [args.target, args.recipient, JSON.stringify(args.withdraw)],
});
if (tx) {
const receipt = await tx.wait();
console.log("Transaction hash:", receipt.transactionHash);
}
} catch (e) {
console.error("Transaction error:", e);
}
};

task("swap-from-evm", "Swap tokens from EVM", evmDepositAndCall)
.addParam("receiver", "Receiver address on ZetaChain")
.addOptionalParam(
"gatewayEvm",
"contract address of gateway on EVM",
"0x9fE46736679d2D9a65F0992F2272dE9f3c7fa6e0"
)
.addFlag("callOnRevert", "Whether to call on revert")
.addOptionalParam(
"revertAddress",
"Revert address",
"0x0000000000000000000000000000000000000000",
types.string
)
.addOptionalParam(
"gasPrice",
"The gas price for the transaction",
50000000000,
types.int
)
.addOptionalParam(
"gasLimit",
"The gas limit for the transaction",
7000000,
types.int
)
.addOptionalParam(
"onRevertGasLimit",
"The gas limit for the revert transaction",
7000000,
types.int
)
.addOptionalParam("revertMessage", "Revert message", "0x")
.addParam("amount", "amount of ETH to send with the transaction")
.addOptionalParam("erc20", "ERC-20 token address")
.addParam("target", "ZRC-20 address of the token to swap for")
.addParam("recipient", "Recipient address")
.addOptionalParam(
"withdraw",
"Withdraw to destination or keep token on ZetaChain",
true,
types.boolean
);
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { task } from "hardhat/config";
import { task, types } from "hardhat/config";
import { HardhatRuntimeEnvironment } from "hardhat/types";
import { parseEther } from "@ethersproject/units";
import { ethers } from "ethers";
Expand All @@ -10,35 +10,35 @@ const main = async (args: any, hre: HardhatRuntimeEnvironment) => {
const factory = await hre.ethers.getContractFactory("SwapToAnyToken");
const contract = factory.attach(args.contract);

const amount = parseEther(args.amount);
const inputToken = args.inputToken;
const targetToken = args.targetToken;
const recipient = ethers.utils.arrayify(args.recipient);
const withdraw = JSON.parse(args.withdraw);
const zrc20 = new ethers.Contract(args.zrc20, ZRC20.abi, signer);

const zrc20 = new ethers.Contract(args.inputToken, ZRC20.abi, signer);
// const inputTokenContract = zrc20.attach(args.inputToken);
const amount = parseEther(args.amount);

const approval = await zrc20.approve(args.contract, amount);
await approval.wait();

const tx = await contract.swap(
inputToken,
args.zrc20,
amount,
targetToken,
recipient,
withdraw
args.target,
ethers.utils.arrayify(args.recipient),
JSON.parse(args.withdraw)
);

await tx.wait();
console.log(`Transaction hash: ${tx.hash}`);
};

task("swap", "Interact with the Swap contract from ZetaChain", main)
task("swap-from-zetachain", "Swap tokens from ZetaChain", main)
.addFlag("json", "Output JSON")
.addParam("contract", "Contract address")
.addParam("amount", "Token amount to send")
.addParam("inputToken", "Input token address")
.addParam("targetToken", "Target token address")
.addParam("zrc20", "Input token address")
.addParam("target", "Target token address")
.addParam("recipient", "Recipient address")
.addParam("withdraw", "Withdraw flag (true/false)");
.addOptionalParam(
"withdraw",
"Withdraw tokens to destination chain",
true,
types.boolean
);
16 changes: 8 additions & 8 deletions examples/swap/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2140,10 +2140,10 @@
typescript "5.5.4"
zod "3.22.4"

"@zetachain/localnet@^3.2.0":
version "3.2.0"
resolved "https://registry.yarnpkg.com/@zetachain/localnet/-/localnet-3.2.0.tgz#66c09120397dbaf4f0c461ba6671f249c3379f6b"
integrity sha512-oqyxrxuSraj1A6/CAkeEBa9VfIvp+QxBIivTu/L9jK1zUa5MQw+rLqTL/rD1LR/k9om6x/zJHzhzZKU9Vpk3Bw==
"@zetachain/localnet@^3.3.0":
version "3.3.0"
resolved "https://registry.yarnpkg.com/@zetachain/localnet/-/localnet-3.3.0.tgz#eb026e1e56ef4ea78fd5efb15df2f931daeba884"
integrity sha512-8PbS6GQrROYicyEHk3QGhspwKnm3Nn8tmgISbtVw2ca4I+9lIAnuo4WstBPXhV3/kR9zPxgxErWnBWNSE78BuA==
dependencies:
"@inquirer/prompts" "^5.5.0"
"@uniswap/v2-core" "^1.0.1"
Expand Down Expand Up @@ -2176,10 +2176,10 @@
resolved "https://registry.yarnpkg.com/@zetachain/protocol-contracts/-/protocol-contracts-9.0.0.tgz#c20ad5da43f6f3676f31556b303d1cb4ea17357e"
integrity sha512-L4A8bddlyhjaBAsIv/x1Bvxc38RJz8U8rbbBtxK5oVyOAd5Zz04ZiT3HqzO4FuKq6RGGM1uiA8jvUfmRkKchXw==

"@zetachain/toolkit@13.0.0-rc4":
version "13.0.0-rc4"
resolved "https://registry.yarnpkg.com/@zetachain/toolkit/-/toolkit-13.0.0-rc4.tgz#e137fc16043f1416469f709c48808cfa301d9299"
integrity sha512-4z4MKbQKjRIeNruUyDBjZDRO5oTLa1w/7wVn+PfMsXn++mFFGkiawkLcitnkH9dadBJiERzmo89MRJxsLQ3U6w==
"@zetachain/toolkit@13.0.0-rc5":
version "13.0.0-rc5"
resolved "https://registry.yarnpkg.com/@zetachain/toolkit/-/toolkit-13.0.0-rc5.tgz#56b2603c8367819a5265fcc7354e93ffdfbc3ea3"
integrity sha512-/97eSf3ALdrLHc9vyaO/xcjuAI9wa4JUNkB8NtnNT6dMeSBPI2ME9lmG+Qk2YDoZkrYlzmEzeru+zcZI9v7cog==
dependencies:
"@coral-xyz/anchor" "^0.30.1"
"@inquirer/prompts" "^2.1.1"
Expand Down
Loading