Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update toolkit version #74

Merged
merged 32 commits into from
Oct 10, 2023
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
16 changes: 3 additions & 13 deletions messaging/counter/contracts/Counter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,16 @@ import "@openzeppelin/contracts/access/Ownable.sol";
import "@zetachain/protocol-contracts/contracts/evm/tools/ZetaInteractor.sol";
import "@zetachain/protocol-contracts/contracts/evm/interfaces/ZetaInterfaces.sol";

interface CounterErrors {
contract Counter is ZetaInteractor, ZetaReceiver {
error InvalidMessageType();
// highlight-next-line
error DecrementOverflow();
}

contract Counter is ZetaInteractor, ZetaReceiver, CounterErrors {
bytes32 public constant COUNTER_MESSAGE_TYPE =
keccak256("CROSS_CHAIN_COUNTER");

event CounterEvent(address);
event CounterRevertedEvent(address);
mapping(address => uint256) public counter;

bytes32 public constant COUNTER_MESSAGE_TYPE =
keccak256("CROSS_CHAIN_COUNTER");
ZetaTokenConsumer private immutable _zetaConsumer;
IERC20 internal immutable _zetaToken;

Expand All @@ -32,7 +28,6 @@ contract Counter is ZetaInteractor, ZetaReceiver, CounterErrors {
_zetaConsumer = ZetaTokenConsumer(zetaConsumerAddress);
}

// highlight-next-line
function sendMessage(uint256 destinationChainId) external payable {
if (!_isValidChainId(destinationChainId))
revert InvalidDestinationChainId();
Expand All @@ -43,13 +38,11 @@ contract Counter is ZetaInteractor, ZetaReceiver, CounterErrors {
}(address(this), crossChainGas);
_zetaToken.approve(address(connector), zetaValueAndGas);

counter[msg.sender]++;
connector.send(
ZetaInterfaces.SendInput({
destinationChainId: destinationChainId,
destinationAddress: interactorsByChainId[destinationChainId],
destinationGasLimit: 300000,
// highlight-next-line
message: abi.encode(COUNTER_MESSAGE_TYPE, msg.sender),
zetaValueAndGas: zetaValueAndGas,
zetaParams: abi.encode("")
Expand All @@ -67,7 +60,6 @@ contract Counter is ZetaInteractor, ZetaReceiver, CounterErrors {

if (messageType != COUNTER_MESSAGE_TYPE) revert InvalidMessageType();

// highlight-next-line
counter[from]++;
emit CounterEvent(from);
}
Expand All @@ -82,10 +74,8 @@ contract Counter is ZetaInteractor, ZetaReceiver, CounterErrors {

if (messageType != COUNTER_MESSAGE_TYPE) revert InvalidMessageType();

// highlight-start
if (counter[from] <= 0) revert DecrementOverflow();
counter[from]--;
// highlight-end
emit CounterRevertedEvent(from);
}
}
2 changes: 1 addition & 1 deletion messaging/counter/hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import "./tasks/interact";
import "./tasks/deploy";
import "./tasks/counter_show";
import "./tasks/counter_show.ts";
import "@nomicfoundation/hardhat-toolbox";
import "@zetachain/toolkit/tasks";

Expand Down
2 changes: 1 addition & 1 deletion messaging/counter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"@types/node": ">=12.0.0",
"@typescript-eslint/eslint-plugin": "^5.59.9",
"@typescript-eslint/parser": "^5.59.9",
"@zetachain/toolkit": "^2.1.2",
"@zetachain/toolkit": "^3.0.0",
"axios": "^1.3.6",
"chai": "^4.2.0",
"dotenv": "^16.0.3",
Expand Down
2 changes: 1 addition & 1 deletion messaging/counter/tasks/counter_show.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { task } from "hardhat/config";
import { HardhatRuntimeEnvironment } from "hardhat/types";

const contractName = "Counter";
const contractName = "CrossChainCounter";

const main = async (args: any, hre: HardhatRuntimeEnvironment) => {
const [signer] = await hre.ethers.getSigners();
Expand Down
76 changes: 42 additions & 34 deletions messaging/counter/tasks/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,34 @@
import { getAddress } from "@zetachain/protocol-contracts";
import { ethers } from "ethers";
import { task } from "hardhat/config";
import { task, types } from "hardhat/config";
import { HardhatRuntimeEnvironment } from "hardhat/types";
import { getSupportedNetworks } from "@zetachain/networks";

const contractName = "Counter";

const main = async (args: any, hre: HardhatRuntimeEnvironment) => {
const networks = args.networks.split(",");
// A mapping between network names and deployed contract addresses.
const contracts: { [key: string]: string } = {};
await Promise.all(
networks.map(async (networkName: string) => {
contracts[networkName] = await deployContract(hre, networkName);
contracts[networkName] = await deployContract(
hre,
networkName,
args.json,
args.gasLimit
);
})
);

for (const source in contracts) {
await setInteractors(hre, source, contracts);
await setInteractors(hre, source, contracts, args.json, args.gasLimit);
}

if (args.json) {
console.log(JSON.stringify(contracts, null, 2));
}
};

// Initialize a wallet using a network configuration and a private key from
// environment variables.
const initWallet = (hre: HardhatRuntimeEnvironment, networkName: string) => {
const { url } = hre.config.networks[networkName] as any;
const provider = new ethers.providers.JsonRpcProvider(url);
Expand All @@ -31,12 +37,11 @@ const initWallet = (hre: HardhatRuntimeEnvironment, networkName: string) => {
return wallet;
};

// Deploy the contract on the specified network. deployContract reads the
// contract artifact, creates a contract factory, and deploys the contract using
// that factory.
const deployContract = async (
hre: HardhatRuntimeEnvironment,
networkName: string
networkName: string,
json: boolean = false,
gasLimit: number
) => {
const wallet = initWallet(hre, networkName);

Expand All @@ -53,39 +58,35 @@ const deployContract = async (

const { abi, bytecode } = await hre.artifacts.readArtifact(contractName);
const factory = new ethers.ContractFactory(abi, bytecode, wallet);
const contract = await factory.deploy(
connector,
zetaToken,
zetaTokenConsumerUniV2 || zetaTokenConsumerUniV3
);
const contract = await factory.deploy(connector, zetaToken, zetaTokenConsumerUniV2 || zetaTokenConsumerUniV3, { gasLimit });

await contract.deployed();
console.log(`
if (!json) {
console.log(`
🚀 Successfully deployed contract on ${networkName}.
📜 Contract address: ${contract.address}`);
}
return contract.address;
};

// Set interactors for a contract. setInteractors attaches to the contract
// deployed at the specified address, and for every other network, sets the
// deployed contract's address as an interactor.
const setInteractors = async (
hre: HardhatRuntimeEnvironment,
source: string,
contracts: { [key: string]: string }
contracts: { [key: string]: string },
json: boolean = false,
gasLimit: number
) => {
console.log(`
if (!json) {
console.log(`
🔗 Setting interactors for a contract on ${source}`);
}
const wallet = initWallet(hre, source);

const { abi, bytecode } = await hre.artifacts.readArtifact(contractName);
const factory = new ethers.ContractFactory(abi, bytecode, wallet);
const contract = factory.attach(contracts[source]);

for (const counterparty in contracts) {
// Skip the destination network if it's the same as the source network.
// For example, we don't need to set an interactor for a contract on
// Goerli if the destination network is also Goerli.
if (counterparty === source) continue;

const counterpartyContract = hre.ethers.utils.solidityPack(
Expand All @@ -94,17 +95,24 @@ const setInteractors = async (
);
const chainId = hre.config.networks[counterparty].chainId;
await (
await contract.setInteractorByChainId(chainId, counterpartyContract)
await contract.setInteractorByChainId(chainId, counterpartyContract, {
gasLimit,
})
).wait();
console.log(
`✅ Interactor address for ${chainId} (${counterparty}) is set to ${counterpartyContract}`
);
if (!json) {
console.log(
`✅ Interactor address for ${chainId} (${counterparty}) is set to ${counterpartyContract}`
);
}
}
};

task("deploy", "Deploy the contract", main).addParam(
"networks",
`Comma separated list of networks to deploy to (e.g. ${getSupportedNetworks(
"ccm"
)})`
);
task("deploy", "Deploy the contract", main)
.addParam(
"networks",
`Comma separated list of networks to deploy to (e.g. ${getSupportedNetworks(
"ccm"
)})`
)
.addOptionalParam("gasLimit", "Gas limit", 10000000, types.int)
.addFlag("json", "Output JSON");
21 changes: 11 additions & 10 deletions messaging/counter/tasks/interact.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,35 @@
import { task } from "hardhat/config";
import { HardhatRuntimeEnvironment } from "hardhat/types";
import { parseEther } from "@ethersproject/units";
import { trackCCTX } from "@zetachain/toolkit/helpers";

const contractName = "Counter";

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("Counter");
const contract = factory.attach(args.contract);

const destination = hre.config.networks[args.destination]?.chainId;
if (destination === undefined) {
throw new Error(`${args.destination} is not a valid destination chain`);
}

const tx = await contract
.connect(signer)
.sendMessage(destination, { value: parseEther(args.amount) });
const value = parseEther(args.amount);

const tx = await contract.connect(signer).sendMessage(destination, { value });

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}
`);
await trackCCTX(tx.hash);
}
};

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");
8 changes: 4 additions & 4 deletions messaging/counter/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1750,10 +1750,10 @@
resolved "https://registry.yarnpkg.com/@zetachain/protocol-contracts/-/protocol-contracts-2.1.0.tgz#775b4eee7c85d115232dece121cbfc798fde6b63"
integrity sha512-xmG6p8DizIk0h7Tr8Lt6UMG0ejrfRrPx0qH9ze3enwblo7W+eGP12oQ7djanYu50B0pNhh9z7xy/IYxKa9wD0Q==

"@zetachain/toolkit@^2.1.2":
version "2.1.2"
resolved "https://registry.yarnpkg.com/@zetachain/toolkit/-/toolkit-2.1.2.tgz#f83b57d323eab2f54961565b8bd15279eab964e7"
integrity sha512-eKZDUKWCf/h5RRvQsP0rG2qB0kKaw9GvpSylZwetx8+NckhKwQi52sUisX7sjHVqjDziTSdsagkrl4HmiPJ0pQ==
"@zetachain/toolkit@^3.0.0":
version "3.0.0"
resolved "https://registry.yarnpkg.com/@zetachain/toolkit/-/toolkit-3.0.0.tgz#eb7ac93b1e20bc772ba82ef57db6466bea176f11"
integrity sha512-aCSkNdJ4qxBz1ApY5ODYeGr7EmFpmM3h8uQ+OxIxABgQEPpLfo9RHvSCkcfBnFGbhQckJlUWw8+NQVg6A/f9Wg==
dependencies:
"@inquirer/prompts" "^2.1.1"
"@inquirer/select" "1.1.3"
Expand Down
13 changes: 3 additions & 10 deletions messaging/message/contracts/CrossChainMessage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,14 @@ import "@openzeppelin/contracts/access/Ownable.sol";
import "@zetachain/protocol-contracts/contracts/evm/tools/ZetaInteractor.sol";
import "@zetachain/protocol-contracts/contracts/evm/interfaces/ZetaInterfaces.sol";

interface CrossChainMessageErrors {
contract CrossChainMessage is ZetaInteractor, ZetaReceiver {
error InvalidMessageType();
}

contract CrossChainMessage is
ZetaInteractor,
ZetaReceiver,
CrossChainMessageErrors
{
bytes32 public constant CROSS_CHAIN_MESSAGE_MESSAGE_TYPE =
keccak256("CROSS_CHAIN_CROSS_CHAIN_MESSAGE");

event CrossChainMessageEvent(string);
event CrossChainMessageRevertedEvent(string);

bytes32 public constant CROSS_CHAIN_MESSAGE_MESSAGE_TYPE =
keccak256("CROSS_CHAIN_CROSS_CHAIN_MESSAGE");
ZetaTokenConsumer private immutable _zetaConsumer;
IERC20 internal immutable _zetaToken;

Expand Down
4 changes: 4 additions & 0 deletions messaging/message/hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import { HardhatUserConfig } from "hardhat/config";
const config: HardhatUserConfig = {
networks: {
...getHardhatConfigNetworks(),
bsc_testnet: {
...getHardhatConfigNetworks().bsc_testnet,
url: "https://bsc-testnet.blockpi.network/v1/rpc/public",
},
},
solidity: "0.8.7",
};
Expand Down
5 changes: 3 additions & 2 deletions messaging/message/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
"@types/chai": "^4.2.0",
"@types/mocha": ">=9.1.0",
"@types/node": ">=12.0.0",
"@uniswap/v2-periphery": "^1.1.0-beta.0",
"@zetachain/toolkit": "^2.1.2",
"@typescript-eslint/eslint-plugin": "^5.59.9",
"@typescript-eslint/parser": "^5.59.9",
"@zetachain/toolkit": "^3.0.0",
"axios": "^1.3.6",
"chai": "^4.2.0",
"dotenv": "^16.0.3",
Expand Down
Loading
Loading