Skip to content

Commit

Permalink
refactor: update swap to use templating (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
fadeev authored Jul 26, 2023
1 parent cddf158 commit 218f674
Show file tree
Hide file tree
Showing 11 changed files with 1,549 additions and 793 deletions.
69 changes: 61 additions & 8 deletions omnichain/swap/README.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
# Template for a ZetaChain Hardhat Project

This is a simple Hardhat template that provides a starting point for developing smart contract applications on the ZetaChain blockchain.
This is a simple Hardhat template that provides a starting point for developing
smart contract applications on the ZetaChain blockchain.

## Prerequisites

Before getting started, ensure that you have [Node.js](https://nodejs.org/en/download) and [Yarn](https://yarnpkg.com/) installed on your system.
Before getting started, ensure that you have
[Node.js](https://nodejs.org/en/download) and [Yarn](https://yarnpkg.com/)
installed on your system.

## Getting Started

To get started, install the necessary dependencies by running the following command in your terminal:
To get started, install the necessary dependencies by running the following
command in your terminal:

```
yarn
```

## Hardhat Tasks

This template includes two Hardhat tasks that can be used to generate a random wallet and request tokens from ZetaChain's faucet.
This template includes Hardhat tasks that can be used make it easier to build
with ZetaChain.

### Generating a Random Wallet

Expand All @@ -26,16 +31,64 @@ To generate a random wallet, run the following command in your terminal:
npx hardhat account --save
```

This will generate a random wallet, print information about the wallet to the terminal, and save the private key to a `.env` file to make it accessible to Hardhat. If you don't want to save the wallet (for example, if you just need an address to send tokens to), you can run the command without the `--save` flag.
This will generate a random wallet, print information about the wallet to the
terminal, and save the private key to a `.env` file to make it accessible to
Hardhat. If you don't want to save the wallet (for example, if you just need an
address to send tokens to), you can run the command without the `--save` flag.

### Querying for Token Balances

To query for token balances, run the following command in your terminal:

```
npx hardhat balances
```

This command will query token balances for the account address derived from the
private key specified in the `.env`.

If you want to query for token balances for a different account, you can use the
`--address` flag:

```
npx hardhat balances --address <address>
```

### Requesting Tokens from the Faucet

To request tokens from ZetaChain's faucet using the account from the `.env` file, run the following command in your terminal:
To install a faucet, run the following command in your terminal:

```
yarn global add @zetachain/faucet-cli@athens3
```

You can then use it with the following command:

```
zetafaucet -h
```

### Verifying a Contract

You can verify a deployed contract with the following command:

```
npx hardhat verify:zeta --contract <address>
```

Select the contract to verify:

```
npx hardhat faucet
? Select a contract to verify: (Use arrow keys)
@zetachain/zevm-protocol-contracts/contracts/interfaces/IZRC20.sol:IZRC20
@zetachain/zevm-protocol-contracts/contracts/interfaces/zContract.sol:zContract
❯ contracts/Withdraw.sol:Withdraw
```

After the confirmation the contract will be verified.

## Next Steps

To learn more about building decentralized apps on ZetaChain, follow the tutorials available in on [the documentation](https://www.zetachain.com/docs/developers/overview/).
To learn more about building decentralized apps on ZetaChain, follow the
tutorials available in
[the documentation](https://www.zetachain.com/docs/developers/overview/).
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import "@zetachain/protocol-contracts/contracts/zevm/interfaces/zContract.sol";

import "@zetachain/toolkit/contracts/SwapHelperLib.sol";

contract ZetaSwapV2 is zContract {
contract Swap is zContract {
SystemContract public immutable systemContract;

constructor(address systemContractAddress) {
Expand All @@ -18,7 +18,7 @@ contract ZetaSwapV2 is zContract {
uint256 amount,
bytes calldata message
) external virtual override {
(address targetZRC20, bytes32 receipient, uint256 minAmountOut) = abi
(address targetZRC20, bytes32 recipient, uint256 minAmountOut) = abi
.decode(message, (address, bytes32, uint256));
uint256 outputAmount = SwapHelperLib._doSwap(
systemContract.wZetaContractAddress(),
Expand All @@ -29,6 +29,6 @@ contract ZetaSwapV2 is zContract {
targetZRC20,
minAmountOut
);
SwapHelperLib._doWithdrawal(targetZRC20, outputAmount, receipient);
SwapHelperLib._doWithdrawal(targetZRC20, outputAmount, recipient);
}
}
6 changes: 0 additions & 6 deletions omnichain/swap/contracts/TestContracts.sol

This file was deleted.

6 changes: 0 additions & 6 deletions omnichain/swap/contracts/TestUniswap.sol

This file was deleted.

14 changes: 4 additions & 10 deletions omnichain/swap/hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
import { HardhatUserConfig } from "hardhat/config";
import "./tasks/interact";
import "./tasks/deploy";
import "@nomicfoundation/hardhat-toolbox";
import { getHardhatConfigNetworks } from "@zetachain/networks";
import "@zetachain/toolkit/tasks";

import "./tasks/deploy";
import "./tasks/swap";
import { HardhatUserConfig } from "hardhat/config";

const config: HardhatUserConfig = {
solidity: {
compilers: [
{ version: "0.6.6" /** For uniswap v2 */ },
{ version: "0.8.7" },
],
},
solidity: "0.8.7",
networks: {
...getHardhatConfigNetworks(),
},
Expand Down
28 changes: 18 additions & 10 deletions omnichain/swap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "echo \"Error: no test specified\" && exit 1",
"lint:fix": "npx eslint . --ext .js,.ts --fix",
"lint": "npx eslint . --ext .js,.ts"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@ethersproject/abi": "^5.4.7",
"@ethersproject/providers": "^5.4.7",
"@inquirer/select": "^1.1.7",
"@inquirer/select": "^1.1.3",
"@nomicfoundation/hardhat-chai-matchers": "^1.0.0",
"@nomicfoundation/hardhat-network-helpers": "^1.0.0",
"@nomicfoundation/hardhat-toolbox": "^2.0.0",
Expand All @@ -23,23 +25,29 @@
"@types/chai": "^4.2.0",
"@types/mocha": ">=9.1.0",
"@types/node": ">=12.0.0",
"@uniswap/v2-core": "^1.0.1",
"@uniswap/v2-periphery": "^1.1.0-beta.0",
"@zetachain/addresses": "^0.0.10",
"@zetachain/addresses-tools": "^0.0.8",
"@zetachain/faucet-cli": "^2.0.1",
"@typescript-eslint/eslint-plugin": "^5.59.9",
"@typescript-eslint/parser": "^5.59.9",
"@zetachain/faucet-cli": "^2.0.1-beta.2",
"@zetachain/interfaces": "^0.0.1",
"@zetachain/networks": "^2.3.0-athens3",
"@zetachain/protocol-contracts": "^1.0.0-athens3",
"@zetachain/toolkit": "^1.0.2-athens3",
"@zetachain/zevm-example-contracts": "^0.0.1",
"@zetachain/protocol-contracts": "^1.0.1-athens3",
"@zetachain/toolkit": "^1.0.8-athens3",
"axios": "^1.3.6",
"chai": "^4.2.0",
"dotenv": "^16.0.3",
"envfile": "^6.18.0",
"eslint": "^8.42.0",
"eslint-config-prettier": "^8.8.0",
"eslint-import-resolver-typescript": "^3.5.5",
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-simple-import-sort": "^10.0.0",
"eslint-plugin-sort-keys-fix": "^1.1.2",
"eslint-plugin-typescript-sort-keys": "^2.3.0",
"ethers": "^5.4.7",
"hardhat": "^2.14.0",
"hardhat-gas-reporter": "^1.0.8",
"prettier": "^2.8.8",
"solidity-coverage": "^0.8.0",
"ts-node": ">=8.0.0",
"typechain": "^8.1.0",
Expand Down
10 changes: 4 additions & 6 deletions omnichain/swap/tasks/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { getAddress } from "@zetachain/protocol-contracts";
import { task } from "hardhat/config";
import { HardhatRuntimeEnvironment } from "hardhat/types";
import { getAddress } from "@zetachain/protocol-contracts";

const contractName = "ZetaSwapV2";

const main = async (args: any, hre: HardhatRuntimeEnvironment) => {
if (hre.network.name !== "zeta_testnet") {
Expand All @@ -14,10 +12,10 @@ const main = async (args: any, hre: HardhatRuntimeEnvironment) => {
const [signer] = await hre.ethers.getSigners();
console.log(`🔑 Using account: ${signer.address}\n`);

const SYSTEM_CONTRACT = getAddress("systemContract", hre.network.name);
const systemContract = getAddress("systemContract", "zeta_testnet");

const factory = await hre.ethers.getContractFactory(contractName);
const contract = await factory.deploy(SYSTEM_CONTRACT);
const factory = await hre.ethers.getContractFactory("Swap");
const contract = await factory.deploy(systemContract);
await contract.deployed();

console.log(`🚀 Successfully deployed contract on ZetaChain.
Expand Down
36 changes: 36 additions & 0 deletions omnichain/swap/tasks/interact.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { task } from "hardhat/config";
import { HardhatRuntimeEnvironment } from "hardhat/types";
import { parseEther } from "@ethersproject/units";
import { getAddress } from "@zetachain/protocol-contracts";
import { prepareData, trackCCTX } from "@zetachain/toolkit/helpers";
import { BigNumber } from "@ethersproject/bignumber";

const main = async (args: any, hre: HardhatRuntimeEnvironment) => {
const [signer] = await hre.ethers.getSigners();
console.log(`🔑 Using account: ${signer.address}\n`);

const targetZRC20 = getAddress("zrc20", args.destination);
const minAmountOut = BigNumber.from("0");

const data = prepareData(
args.contract,
["address", "bytes32", "uint256"],
[targetZRC20, args.recipient, minAmountOut]
);
const to = getAddress("tss", hre.network.name as any);
const value = parseEther(args.amount);

const tx = await signer.sendTransaction({ data, to, value });

console.log(`
🚀 Successfully broadcasted a token transfer transaction on ${hre.network.name} network.
📝 Transaction hash: ${tx.hash}
`);
await trackCCTX(tx.hash);
};

task("interact", "Interact with the contract", main)
.addParam("contract", "The address of the withdraw contract on ZetaChain")
.addParam("amount", "Amount of tokens to send")
.addParam("recipient")
.addParam("destination");
39 changes: 0 additions & 39 deletions omnichain/swap/tasks/swap.ts

This file was deleted.

Loading

0 comments on commit 218f674

Please sign in to comment.