diff --git a/examples/swap/hardhat.config.ts b/examples/swap/hardhat.config.ts index 55078de3..6acf02c2 100644 --- a/examples/swap/hardhat.config.ts +++ b/examples/swap/hardhat.config.ts @@ -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"; diff --git a/examples/swap/package.json b/examples/swap/package.json index 2d5c7eae..da960e00 100644 --- a/examples/swap/package.json +++ b/examples/swap/package.json @@ -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", @@ -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" } } diff --git a/examples/swap/tasks/swapFromEVM.ts b/examples/swap/tasks/swapFromEVM.ts new file mode 100644 index 00000000..e32836c8 --- /dev/null +++ b/examples/swap/tasks/swapFromEVM.ts @@ -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"); diff --git a/examples/swap/tasks/swap.ts b/examples/swap/tasks/swapFromZetaChain.ts similarity index 95% rename from examples/swap/tasks/swap.ts rename to examples/swap/tasks/swapFromZetaChain.ts index 0e719334..d21f3906 100644 --- a/examples/swap/tasks/swap.ts +++ b/examples/swap/tasks/swapFromZetaChain.ts @@ -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") diff --git a/examples/swap/yarn.lock b/examples/swap/yarn.lock index a991d4d3..124bcb73 100644 --- a/examples/swap/yarn.lock +++ b/examples/swap/yarn.lock @@ -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" @@ -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"