Skip to content

Commit

Permalink
ci: integration test (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
fadeev authored Oct 3, 2023
1 parent 3b21660 commit cd1637d
Show file tree
Hide file tree
Showing 10 changed files with 187 additions and 36 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/integration.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Integration Tests

on:
schedule:
- cron: "0 0 * * *" # Runs every day at midnight
workflow_dispatch:

jobs:
integration:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Install jq
run: |
sudo apt-get update
sudo apt-get install -y jq
- name: Install dependencies
run: |
yarn
yarn link
- name: Build
run: yarn build

- name: Integration
run: ./scripts/integration.sh
env:
PRIVATE_KEY: ${{ secrets.PRIVATE_KEY }}
1 change: 1 addition & 0 deletions contracts/SwapHelperLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pragma solidity =0.8.7;
import "@uniswap/v2-periphery/contracts/interfaces/IUniswapV2Router02.sol";
import "@uniswap/v2-periphery/contracts/interfaces/IUniswapV2Router01.sol";
import "@zetachain/protocol-contracts/contracts/zevm/interfaces/IZRC20.sol";
import "@zetachain/protocol-contracts/contracts/zevm/SystemContract.sol";

library SwapHelperLib {
uint16 internal constant MAX_DEADLINE = 200;
Expand Down
42 changes: 21 additions & 21 deletions helpers/tx.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { getEndpoints } from "@zetachain/networks";
import { getHardhatConfigNetworks } from "@zetachain/networks";
import axios from "axios";
import clc from "cli-color";
import { HardhatRuntimeEnvironment } from "hardhat/types";
import { ethers } from "ethers";
import Spinnies from "spinnies";
import WebSocket from "ws";

const getEndpoint = (key: any): string => {
const endpoint = getEndpoints(key, "zeta_testnet")[0]?.url;
Expand Down Expand Up @@ -52,30 +52,22 @@ const fetchCCTXByInbound = async (
} catch (error) {}
};

const SUBSCRIBE_MESSAGE = {
id: 1,
jsonrpc: "2.0",
method: "subscribe",
params: ["tm.event='NewBlock'"],
};

const fetchCCTXData = async (
cctxHash: string,
spinnies: any,
API: string,
cctxList: any,
pendingNonces: any,
json: Boolean,
hre: HardhatRuntimeEnvironment
json: Boolean
) => {
const { networks } = hre.config;
const networks = getHardhatConfigNetworks();
const cctx = await getCCTX(cctxHash, API);
const receiver_chainId = cctx?.outbound_tx_params[0]?.receiver_chainId;
const outbound_tx_hash = cctx?.outbound_tx_params[0]?.outbound_tx_hash;
let confirmed_on_destination = false;
if (outbound_tx_hash) {
const rpc = findByChainId(networks, parseInt(receiver_chainId))?.url;
const provider = new hre.ethers.providers.JsonRpcProvider(rpc);
const provider = new ethers.providers.JsonRpcProvider(rpc);
const confirmed = await provider.getTransaction(outbound_tx_hash);
confirmed_on_destination = confirmed !== null;
}
Expand Down Expand Up @@ -156,8 +148,7 @@ const fetchTSS = async (API: string) => {

export const trackCCTX = async (
hash: string,
hre: HardhatRuntimeEnvironment,
json: Boolean
json: Boolean = false
): Promise<void> => {
const spinnies = new Spinnies();

Expand Down Expand Up @@ -205,8 +196,7 @@ export const trackCCTX = async (
API,
cctxList,
pendingNonces,
json,
hre
json
);
} catch (error) {}
}
Expand All @@ -221,10 +211,20 @@ export const trackCCTX = async (
.filter((s) => !["OutboundMined", "Aborted", "Reverted"].includes(s))
.length === 0
) {
if (json) console.log(JSON.stringify(cctxList, null, 2));
clearInterval(loopInterval); // Clear the interval
resolve();
const allOutboundMined = Object.keys(cctxList)
.map((c: any) => {
const last = cctxList[c][cctxList[c].length - 1];
return last?.status;
})
.every((s) => s === "OutboundMined");

if (!allOutboundMined) {
reject("CCTX aborted or reverted");
} else {
if (json) console.log(JSON.stringify(cctxList, null, 2));
resolve();
}
}
}, 3000); // Execute every 3 seconds
}, 3000);
});
};
90 changes: 90 additions & 0 deletions scripts/integration.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#!/bin/bash

set -e

git clone https://github.com/zeta-chain/template

cd template

yarn

yarn link @zetachain/toolkit

npx hardhat balances

npx hardhat fees

npx hardhat account --save

if [[ ! -f .env ]]; then
echo ".env file was not created."
exit 1
fi

if grep -E "^PRIVATE_KEY=[a-fA-F0-9]{64}$" .env; then
echo "The .env file contains a valid PRIVATE_KEY entry without the 0x prefix."
rm .env
else
echo "The .env file doesn't contain a valid PRIVATE_KEY entry or it contains the 0x prefix."
exit 1
fi

echo "TESTING OMNICHAIN CONTRACT"

git reset --hard HEAD
npx hardhat omnichain Swap targetZRC20:address recipient minAmountOut:uint256
npx hardhat compile --force --no-typechain

OMNICHAIN_CONTRACT=$(npx hardhat deploy --network zeta_testnet --json | jq -r '.address')

echo "Deployed omnichain contract address: $OMNICHAIN_CONTRACT"

echo "TESTING TRANSACTION THAT SHOULD SUCCEED"

OMNICHAIN_TX_SHOULD_SUCCEED=$(npx hardhat interact --contract $OMNICHAIN_CONTRACT --network goerli_testnet --amount 0.000000000000000001 --target-z-r-c20 $OMNICHAIN_CONTRACT --recipient $OMNICHAIN_CONTRACT --min-amount-out 0 --json | jq -r '.hash')

echo "TX hash: $OMNICHAIN_TX_SHOULD_SUCCEED"

OMNICHAIN_CCTX_SHOULD_SUCCEED=$(npx hardhat cctx $OMNICHAIN_TX_SHOULD_SUCCEED --json)

echo "CCTX: $OMNICHAIN_CCTX_SHOULD_SUCCEED"

echo "TESTING TRANSACTION THAT SHOULD FAIL"

OMNICHAIN_TX_SHOULD_FAIL=$(npx hardhat interact --contract 0x0000000000000000000000000000000000000000 --network goerli_testnet --amount 0.000000000000000001 --target-z-r-c20 $OMNICHAIN_CONTRACT --recipient $OMNICHAIN_CONTRACT --min-amount-out 0 --json | jq -r '.hash')

echo "TX hash: $OMNICHAIN_TX_SHOULD_FAIL"

npx hardhat cctx $OMNICHAIN_TX_SHOULD_FAIL --json || {
exit_status=$?
if [[ $exit_status -eq 0 ]]; then
echo "The command was expected to fail but it succeeded."
exit 1
fi
}

echo "TESTING CROSS-CHAIN MESSAGING"

git reset --hard HEAD
npx hardhat messaging CrossChainMessage message:string --fees zeta
npx hardhat compile --force --no-typechain

CCM_CONTRACT=$(npx hardhat deploy --networks goerli_testnet,mumbai_testnet --json | jq -r '.goerli_testnet')

echo "Deployed CCM contract address: $CCM_CONTRACT"

CCM_FEE=$(npx hardhat fees --json | jq -r ".feesCCM.mumbai_testnet.totalFee")

echo "CCM fee: $CCM_FEE"

CCM_TX_OUT=$(npx hardhat interact --network goerli_testnet --contract $CCM_CONTRACT --message "Hello World" --destination mumbai_testnet --amount $CCM_FEE --json)

echo "CCM TX out: $CCM_TX_OUT"

CCM_TX=$(echo $CCM_TX_OUT | jq -r '.hash')

echo "CCM TX hash: $CCM_TX"

CCM_CCTX=$(npx hardhat cctx $CCM_TX --json)

echo "CCM CCTX: $CCM_CCTX"
3 changes: 2 additions & 1 deletion tasks/balances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ const main = async (args: any, hre: HardhatRuntimeEnvironment) => {
btc_address = bitcoinAddress(pk);
} else {
spinner.stop();
return console.error(walletError + balancesError);
console.error(walletError + balancesError);
return process.exit(1);
}
const balancePromises = Object.keys(config.networks).map((networkName) => {
const { url } = config.networks[networkName] as any;
Expand Down
4 changes: 2 additions & 2 deletions tasks/cctx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import { trackCCTX } from "../helpers";
declare const hre: any;

const main = async (args: any, hre: HardhatRuntimeEnvironment) => {
await trackCCTX(args.tx, hre, args.json);
await trackCCTX(args.tx);
};

export const cctxTask = task(
"cctx",
"Track cross-chain transaction status",
main
)
.addPositionalParam("tx", "TX hash of an inbound transaction or a CCTX")
.addPositionalParam("tx", "Hash of an inbound or a cross-chain transaction")
.addFlag("json", "Output as JSON");
24 changes: 19 additions & 5 deletions templates/messaging/tasks/interact.ts.hbs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { task } from "hardhat/config";
import { HardhatRuntimeEnvironment } from "hardhat/types";
import { parseEther } from "@ethersproject/units";

const contractName = "{{contractName}}";
{{#unless arguments.feesNative}}
import { getAddress } from "@zetachain/protocol-contracts";
{{/unless}}

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

const factory = await hre.ethers.getContractFactory(contractName);
const factory = await hre.ethers.getContractFactory("{{contractName}}");
const contract = factory.attach(args.contract);

const destination = hre.config.networks[args.destination]?.chainId;
Expand All @@ -21,17 +21,31 @@ const main = async (args: any, hre: HardhatRuntimeEnvironment) => {

const value = parseEther(args.amount);

{{#unless arguments.feesNative}}
const zetaTokenAddress = getAddress("zetaToken", hre.network.name as any);
const zetaFactory = await hre.ethers.getContractFactory("ZetaEth");
const zetaToken = zetaFactory.attach(zetaTokenAddress);

await (await zetaToken.approve(args.contract, value)).wait();
{{/unless}}

const tx = await contract
.connect(signer)
.sendMessage(destination{{#each arguments.casts}}, param{{this.[0]}}{{/each}}{{#if arguments.feesNative}}, { value }{{else}}, value{{/if}});

const receipt = await tx.wait();
console.log(`✅ The transaction has been broadcasted to ${hre.network.name}
if (args.json) {
console.log(JSON.stringify(tx, null, 2));
} else {
console.log(`🔑 Using account: ${signer.address}\n`);
console.log(`✅ The transaction has been broadcasted to ${hre.network.name}
📝 Transaction hash: ${receipt.transactionHash}
`);
}
};

task("interact", "Sends a message from one chain to another.", main)
.addFlag("json", "Output JSON")
.addParam("contract", "Contract address")
.addParam("amount", "Token amount to send")
.addParam("destination", "Destination chain")
Expand Down
14 changes: 11 additions & 3 deletions templates/omnichain/tasks/deploy.ts.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,26 @@ const main = async (args: any, hre: HardhatRuntimeEnvironment) => {
`Wallet not found. Please, run "npx hardhat account --save" or set PRIVATE_KEY env variable (for example, in a .env file)`
);
}
console.log(`🔑 Using account: ${signer.address}\n`);

const systemContract = getAddress("systemContract", "zeta_testnet");

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

console.log(`🚀 Successfully deployed contract on ZetaChain.
if (args.json) {
console.log(JSON.stringify(contract));
} else {
console.log(`🔑 Using account: ${signer.address}

🚀 Successfully deployed contract on ZetaChain.
📜 Contract address: ${contract.address}
🌍 Explorer: https://athens3.explorer.zetachain.com/address/${contract.address}
`);
}
};

task("deploy", "Deploy the contract", main);
task("deploy", "Deploy the contract", main).addFlag(
"json",
"Output in JSON"
);
11 changes: 8 additions & 3 deletions templates/omnichain/tasks/interact.ts.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { prepareData } from "@zetachain/toolkit/helpers";

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

const data = prepareData(
args.contract,
Expand All @@ -18,15 +17,21 @@ const main = async (args: any, hre: HardhatRuntimeEnvironment) => {

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

console.log(`
🚀 Successfully broadcasted a token transfer transaction on ${hre.network.name} network.
if (args.json) {
console.log(JSON.stringify(tx, null, 2));
} else {
console.log(`🔑 Using account: ${signer.address}\n`);

console.log(`🚀 Successfully broadcasted a token transfer transaction on ${hre.network.name} network.
📝 Transaction hash: ${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")
.addFlag("json", "Output in JSON")
{{#each arguments.names}}
.addParam("{{this}}")
{{/each}}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ const _abi = [
] as const;

const _bytecode =
"0x610492610053600b82828239805160001a607314610046577f4e487b7100000000000000000000000000000000000000000000000000000000600052600060045260246000fd5b30600052607381538281f3fe73000000000000000000000000000000000000000030146080604052600436106100355760003560e01c8063c63585cc1461003a575b600080fd5b610054600480360381019061004f919061020d565b61006a565b6040516100619190610351565b60405180910390f35b600080600061007985856100dc565b915091508582826040516020016100919291906102e3565b604051602081830303815290604052805190602001206040516020016100b892919061030f565b6040516020818303038152906040528051906020012060001c925050509392505050565b6000808273ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff161415610145576040517fcb1e7cfe00000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b8273ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff161061017f578284610182565b83835b8092508193505050600073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff1614156101f1576040517f78b507da00000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b9250929050565b60008135905061020781610445565b92915050565b600080600060608486031215610226576102256103e1565b5b6000610234868287016101f8565b9350506020610245868287016101f8565b9250506040610256868287016101f8565b9150509250925092565b61026981610377565b82525050565b61028061027b82610377565b6103b3565b82525050565b61029761029282610389565b6103c5565b82525050565b60006102aa60208361036c565b91506102b5826103f3565b602082019050919050565b60006102cd60018361036c565b91506102d88261041c565b600182019050919050565b60006102ef828561026f565b6014820191506102ff828461026f565b6014820191508190509392505050565b600061031a826102c0565b9150610326828561026f565b6014820191506103368284610286565b6020820191506103458261029d565b91508190509392505050565b60006020820190506103666000830184610260565b92915050565b600081905092915050565b600061038282610393565b9050919050565b6000819050919050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b60006103be826103cf565b9050919050565b6000819050919050565b60006103da826103e6565b9050919050565b600080fd5b60008160601b9050919050565b7f96e8ac4277198ff8b6f785478aa9a39f403cb768dd02cbee326c3e7da348845f600082015250565b7fff00000000000000000000000000000000000000000000000000000000000000600082015250565b61044e81610377565b811461045957600080fd5b5056fea26469706673582212207bd375fc5a1b24cef144931d045cf41aab2227d9ac8d0ca0aead08a64a2b492864736f6c63430008070033";
"0x610492610053600b82828239805160001a607314610046577f4e487b7100000000000000000000000000000000000000000000000000000000600052600060045260246000fd5b30600052607381538281f3fe73000000000000000000000000000000000000000030146080604052600436106100355760003560e01c8063c63585cc1461003a575b600080fd5b610054600480360381019061004f919061020d565b61006a565b6040516100619190610351565b60405180910390f35b600080600061007985856100dc565b915091508582826040516020016100919291906102e3565b604051602081830303815290604052805190602001206040516020016100b892919061030f565b6040516020818303038152906040528051906020012060001c925050509392505050565b6000808273ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff161415610145576040517fcb1e7cfe00000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b8273ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff161061017f578284610182565b83835b8092508193505050600073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff1614156101f1576040517f78b507da00000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b9250929050565b60008135905061020781610445565b92915050565b600080600060608486031215610226576102256103e1565b5b6000610234868287016101f8565b9350506020610245868287016101f8565b9250506040610256868287016101f8565b9150509250925092565b61026981610377565b82525050565b61028061027b82610377565b6103b3565b82525050565b61029761029282610389565b6103c5565b82525050565b60006102aa60208361036c565b91506102b5826103f3565b602082019050919050565b60006102cd60018361036c565b91506102d88261041c565b600182019050919050565b60006102ef828561026f565b6014820191506102ff828461026f565b6014820191508190509392505050565b600061031a826102c0565b9150610326828561026f565b6014820191506103368284610286565b6020820191506103458261029d565b91508190509392505050565b60006020820190506103666000830184610260565b92915050565b600081905092915050565b600061038282610393565b9050919050565b6000819050919050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b60006103be826103cf565b9050919050565b6000819050919050565b60006103da826103e6565b9050919050565b600080fd5b60008160601b9050919050565b7f96e8ac4277198ff8b6f785478aa9a39f403cb768dd02cbee326c3e7da348845f600082015250565b7fff00000000000000000000000000000000000000000000000000000000000000600082015250565b61044e81610377565b811461045957600080fd5b5056fea2646970667358221220827821eff9c6c7e0b37f2bc10841b7e77efac4f01f5290fd4ae69062e24e2bfd64736f6c63430008070033";

type SwapHelperLibConstructorParams =
| [signer?: Signer]
Expand Down

0 comments on commit cd1637d

Please sign in to comment.