Skip to content

Commit

Permalink
feat(templating): support types in ccm interact task (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
fadeev authored Aug 17, 2023
1 parent 02c093f commit f9e1d24
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 12 deletions.
62 changes: 57 additions & 5 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,74 @@ import * as fs from "fs";
import * as handlebars from "handlebars";
import * as path from "path";

const numberTypes = [
"int",
"int8",
"int16",
"int128",
"int256",
"uint",
"uint8",
"uint16",
"uint128",
"uint256",
];
const addressTypes = ["address"];
const boolTypes = ["bool"];
const bytesTypes = ["bytes32"];
const allTypes = [...numberTypes, ...addressTypes, ...boolTypes, ...bytesTypes];

const capitalizeFirstChar = (input: string): string => {
if (input.length === 0) {
return input;
}

const firstChar = input.charAt(0).toUpperCase();
const restOfTheString = input.slice(1);

return firstChar + restOfTheString;
};

const prepareData = (args: any) => {
const argsList = args.arguments || [];
const names = argsList.map((i: string) => i.split(":")[0]);
const types = argsList.map((i: string) => {
let parts = i.split(":");
// If there's a type and it's not empty, use it; if not, default to "bytes32"
let t =
parts.length > 1 && parts[1].trim() !== "" ? parts[1].trim() : "bytes32";
let t = i.split(":")[1];
if (t === undefined) {
return "bytes32";
}
if (!allTypes.includes(t)) {
throw new Error(
`Invalid type "${t}", must be one of ${allTypes.join(", ")}`
);
}
return t;
});
const pairs = names.map((v: string, i: string) => [v, types[i]]);
const contractName = sanitizeSolidityFunctionName(args.name);
const casts = pairs.map((p: any) => {
const n = capitalizeFirstChar(p[0]);
const type = p[1];

if (numberTypes.includes(type)) {
return [n, `hre.ethers.BigNumber.from(args.${p[0]})`];
}

if (addressTypes.includes(type)) {
return [n, `hre.ethers.utils.getAddress(args.${p[0]})`];
}

if (boolTypes.includes(type)) {
return [n, `JSON.parse(args.${p[0]})`];
}

// Default case is "bytes32" and other unexpected cases.
return [n, `hre.ethers.utils.toUtf8Bytes(args.${p[0]})`];
});

return {
args,
arguments: { names, pairs, types },
arguments: { casts, names, pairs, types },
contractName,
contractNameUnderscore: camelToUnderscoreUpper(contractName),
};
Expand Down
2 changes: 1 addition & 1 deletion tasks/messaging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as path from "path";
import { processTemplates } from "../lib";

const main = async (args: any, hre: HardhatRuntimeEnvironment) => {
processTemplates("messaging", args);
await processTemplates("messaging", args);

const configPath = path.resolve(process.cwd(), "hardhat.config.ts");
let hardhatConfigContents = fs.readFileSync(configPath, "utf8");
Expand Down
21 changes: 15 additions & 6 deletions templates/messaging/tasks/interact.ts.hbs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { task } from "hardhat/config";
import { HardhatRuntimeEnvironment } from "hardhat/types";
import { parseEther } from "@ethersproject/units";
import { trackCCTX } from "@zetachain/toolkit/helpers";

const contractName = "{{contractName}}";

Expand All @@ -11,21 +12,29 @@ const main = async (args: any, hre: HardhatRuntimeEnvironment) => {
const factory = await hre.ethers.getContractFactory(contractName);
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`);
}

{{#each arguments.casts}}const param{{this.[0]}} = {{this.[1]}};
{{/each}}

const tx = await contract
.connect(signer)
.sendMessage(args.destination, { value: parseEther(args.amount) });
.sendMessage(destination, {{#each arguments.casts}} param{{this.[0]}}, {{/each}} { value: parseEther(args.amount) });

const receipt = await tx.wait();
console.log(`✅ The transaction has been broadcasted to ${hre.network.name}
📝 Transaction hash: ${receipt.transactionHash}

Please, refer to ZetaChain's explorer for updates on the progress of the cross-chain transaction.

🌍 Explorer: https://athens3.explorer.zetachain.com/cc/tx/${receipt.transactionHash}
`);
await trackCCTX(tx.hash);
};

task("interact", "Sends a message from one chain to another.", main)
.addParam("contract", "Contract address")
.addParam("amount", "Token amount to send")
.addParam("destination", "Destination chain ID (integer)");
.addParam("destination", "Destination chain")
{{#each arguments.pairs}}
.addParam("{{this.[0]}}", "{{this.[1]}}")
{{/each}}

0 comments on commit f9e1d24

Please sign in to comment.