Skip to content

Commit

Permalink
chore: viem experiment
Browse files Browse the repository at this point in the history
  • Loading branch information
jaybuidl committed Jan 9, 2024
1 parent be7d75c commit 1b5a16a
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 26 deletions.
5 changes: 5 additions & 0 deletions contracts/.npmignore
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
25 changes: 0 additions & 25 deletions contracts/scripts/viem-test.ts

This file was deleted.

53 changes: 53 additions & 0 deletions contracts/scripts/viemTest.ts
Original file line number Diff line number Diff line change
@@ -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<typeof disputeKit.abi, "disputes">;
type Result = AbiParametersToPrimitiveTypes<DelayedStakesFunction["outputs"]>;
// -> readonly [bigint, boolean, `0x${string}`]
// Ideally we would get an object instead of a tuple

// Human-readable
type FormattedFunction = FormatAbiItem<DelayedStakesFunction>;
// -> "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);
});
16 changes: 16 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 1b5a16a

Please sign in to comment.