From 1b5a16adcf3d70e9a03000ca177c7585d115a4a8 Mon Sep 17 00:00:00 2001 From: jaybuidl Date: Tue, 9 Jan 2024 14:40:09 +0000 Subject: [PATCH] chore: viem experiment --- contracts/.npmignore | 5 ++++ contracts/package.json | 3 +- contracts/scripts/viem-test.ts | 25 ---------------- contracts/scripts/viemTest.ts | 53 ++++++++++++++++++++++++++++++++++ yarn.lock | 16 ++++++++++ 5 files changed, 76 insertions(+), 26 deletions(-) delete mode 100644 contracts/scripts/viem-test.ts create mode 100644 contracts/scripts/viemTest.ts diff --git a/contracts/.npmignore b/contracts/.npmignore index 382ffca6e..ac1207574 100644 --- a/contracts/.npmignore +++ b/contracts/.npmignore @@ -1,2 +1,7 @@ # NOP, just force npm to disregard .gitignore # https://docs.npmjs.com/cli/v9/using-npm/developers#keeping-files-out-of-your-package + +.env* +.flaskenv* +!.env.project +!.env.vault \ No newline at end of file diff --git a/contracts/package.json b/contracts/package.json index 36dc89378..4b09fb6df 100644 --- a/contracts/package.json +++ b/contracts/package.json @@ -36,7 +36,7 @@ "export:devnet": "yarn hardhat export --export deployments/arbitrumSepoliaDevnet.ts --network arbitrumSepoliaDevnet", "export:testnet": "yarn hardhat export --export deployments/arbitrumSepolia.ts --network arbitrumSepolia", "export:mainnet": "yarn hardhat export --export deployments/arbitrum.ts --network arbitrum", - "viem:test": "NODE_NO_WARNINGS=1 NODE_OPTIONS=--experimental-fetch ts-node ./scripts/viem-test.ts", + "viem:test": "NODE_NO_WARNINGS=1 NODE_OPTIONS=--experimental-fetch ts-node ./scripts/viemTest.ts", "bot:keeper": "NODE_NO_WARNINGS=1 NODE_OPTIONS=--experimental-fetch hardhat run ./scripts/keeperBot.ts", "bot:relayer-from-chiado": "NODE_NO_WARNINGS=1 NODE_OPTIONS=--experimental-fetch hardhat run ./scripts/disputeRelayerBotFromChiado.ts", "bot:relayer-from-sepolia": "NODE_NO_WARNINGS=1 NODE_OPTIONS=--experimental-fetch hardhat run ./scripts/disputeRelayerBotFromSepolia.ts", @@ -70,6 +70,7 @@ "@types/mocha": "^10.0.6", "@types/node": "^16.18.68", "@wagmi/cli": "^1.5.2", + "abitype": "^0.10.3", "chai": "^4.3.10", "dotenv": "^16.3.1", "ethereumjs-util": "^7.1.5", diff --git a/contracts/scripts/viem-test.ts b/contracts/scripts/viem-test.ts deleted file mode 100644 index b019d3ebb..000000000 --- a/contracts/scripts/viem-test.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { createPublicClient, http, getContract } from "viem"; -import { arbitrumSepolia } from "viem/chains"; -import { disputeKitClassicConfig } from "../deployments/devnet.viem"; - -const main = async () => { - const client = createPublicClient({ - chain: arbitrumSepolia, - transport: http(), - }); - - const disputeKit = getContract({ - address: disputeKitClassicConfig.address[arbitrumSepolia.id], - abi: disputeKitClassicConfig.abi, - publicClient: client, - }); - - await disputeKit.read.governor().then(console.log); -}; - -main() - .then(() => process.exit(0)) - .catch((error) => { - console.error(error); - process.exit(1); - }); diff --git a/contracts/scripts/viemTest.ts b/contracts/scripts/viemTest.ts new file mode 100644 index 000000000..17c19d783 --- /dev/null +++ b/contracts/scripts/viemTest.ts @@ -0,0 +1,53 @@ +import { createPublicClient, http, getContract } from "viem"; +import { arbitrumSepolia } from "viem/chains"; +import { disputeKitClassicConfig } from "../deployments/devnet.viem"; +import { AbiFunction, AbiParametersToPrimitiveTypes, ExtractAbiFunction, FormatAbiItem } from "abitype"; + +const main = async () => { + const client = createPublicClient({ + chain: arbitrumSepolia, + transport: http(), + }); + + const disputeKit = getContract({ + address: disputeKitClassicConfig.address[arbitrumSepolia.id], + abi: disputeKitClassicConfig.abi, + publicClient: client, + }); + + await disputeKit.read.governor().then(console.log); + + // -------------------------------------------------- + + // Working around the "unknown tuple types" issue + // https://viem.sh/docs/faq.html#why-are-contract-function-args-with-fully-named-inputs-represented-as-unnamed-tuple-types-instead-of-object-types + + // Not human-readable + type DelayedStakesFunction = ExtractAbiFunction; + type Result = AbiParametersToPrimitiveTypes; + // -> readonly [bigint, boolean, `0x${string}`] + // Ideally we would get an object instead of a tuple + + // Human-readable + type FormattedFunction = FormatAbiItem; + // -> "function disputes(uint256) view returns (uint256 numberOfChoices, bool jumped, bytes extraData)" + + const getFunctionReturnParameterNames = (abi: AbiFunction[], name: string): string[] => { + const f = abi.filter((abi: AbiFunction) => abi.type === "function" && abi.name === name)[0]; // WARNING: overloaded functions confusion + return f.outputs.map((item) => item.name).filter(String) as string[]; + }; + + const createObject = (keys: string[], values: any[]) => Object.fromEntries(keys.map((k, i) => [k, values[i]])); + + const disputes = await disputeKit.read.disputes([BigInt(0)]); + const disputeParamNames = getFunctionReturnParameterNames(disputeKit.abi as unknown as AbiFunction[], "disputes"); // such type hack + const disputeObject = createObject(disputeParamNames, [...disputes]); + console.log("disputes: %O", disputeObject); +}; + +main() + .then(() => process.exit(0)) + .catch((error) => { + console.error(error); + process.exit(1); + }); diff --git a/yarn.lock b/yarn.lock index 00e4603b0..2c64cea3b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5468,6 +5468,7 @@ __metadata: "@types/mocha": ^10.0.6 "@types/node": ^16.18.68 "@wagmi/cli": ^1.5.2 + abitype: ^0.10.3 chai: ^4.3.10 dotenv: ^16.3.1 ethereumjs-util: ^7.1.5 @@ -11336,6 +11337,21 @@ __metadata: languageName: node linkType: hard +"abitype@npm:^0.10.3": + version: 0.10.3 + resolution: "abitype@npm:0.10.3" + peerDependencies: + typescript: ">=5.0.4" + zod: ^3 >=3.22.0 + peerDependenciesMeta: + typescript: + optional: true + zod: + optional: true + checksum: 3dccd692c2cf26bfa29926ef8cf5e9649d496327f77e693f953647e86ff15d70ff9d26a7296c27313ef34ca208faeaafc8cd4054dfc1bd075089e78a2d124e7d + languageName: node + linkType: hard + "abort-controller@npm:^3.0.0": version: 3.0.0 resolution: "abort-controller@npm:3.0.0"