Skip to content

Commit

Permalink
Swap: swap from EVM task
Browse files Browse the repository at this point in the history
  • Loading branch information
fadeev committed Oct 8, 2024
1 parent 2f1b130 commit 3f3e5ad
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 12 deletions.
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
4 changes: 2 additions & 2 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.0.4",
"@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"
}
}
77 changes: 77 additions & 0 deletions examples/swap/tasks/swapFromEVM.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
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")
.addFlag("withdraw", "Withdraw to destination or keep token on ZetaChain");
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const main = async (args: any, hre: HardhatRuntimeEnvironment) => {
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")
Expand Down
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.0.4":
version "3.0.4"
resolved "https://registry.yarnpkg.com/@zetachain/localnet/-/localnet-3.0.4.tgz#ff457f732f9ea52f491c16dc4cc5ac77d9561a9b"
integrity sha512-biZhhonyUrtXSZCPzunT6x6pKK3GANtFtwkXHoEnDfQsjICRgfBMMrC76IsfM8XgEdRF15dp845QeDHMa19GkA==
"@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/[email protected]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/[email protected]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

0 comments on commit 3f3e5ad

Please sign in to comment.