From 49c3e6cce25b16dfb410891f2bcfdc50511fc065 Mon Sep 17 00:00:00 2001 From: Kevin Ingersoll Date: Wed, 11 Oct 2023 20:50:15 +0100 Subject: [PATCH] feat(world): add TS helpers to encode system calls (#1745) --- packages/world/package.json | 1 + packages/world/ts/encodeSystemCall.ts | 33 ++++++++++++++++++++++ packages/world/ts/encodeSystemCallFrom.ts | 26 +++++++++++++++++ packages/world/ts/encodeSystemCalls.ts | 11 ++++++++ packages/world/ts/encodeSystemCallsFrom.ts | 14 +++++++++ packages/world/ts/index.ts | 5 ++++ pnpm-lock.yaml | 9 ++++-- 7 files changed, 96 insertions(+), 3 deletions(-) create mode 100644 packages/world/ts/encodeSystemCall.ts create mode 100644 packages/world/ts/encodeSystemCallFrom.ts create mode 100644 packages/world/ts/encodeSystemCalls.ts create mode 100644 packages/world/ts/encodeSystemCallsFrom.ts diff --git a/packages/world/package.json b/packages/world/package.json index 880f047610..071d7b6646 100644 --- a/packages/world/package.json +++ b/packages/world/package.json @@ -51,6 +51,7 @@ "@latticexyz/schema-type": "workspace:*", "@latticexyz/store": "workspace:*", "abitype": "0.9.8", + "viem": "1.14.0", "zod": "^3.21.4" }, "devDependencies": { diff --git a/packages/world/ts/encodeSystemCall.ts b/packages/world/ts/encodeSystemCall.ts new file mode 100644 index 0000000000..20d5fb53d2 --- /dev/null +++ b/packages/world/ts/encodeSystemCall.ts @@ -0,0 +1,33 @@ +import { + Abi, + EncodeFunctionDataParameters, + ContractFunctionConfig, + GetFunctionArgs, + Hex, + encodeFunctionData, +} from "viem"; +import IWorldCallAbi from "../out/IWorldKernel.sol/IWorldCall.abi.json"; + +export type SystemCall = Pick< + ContractFunctionConfig, + "abi" | "functionName" | "args" +> & { + readonly systemId: Hex; +}; + +/** Encode a system call to be passed as arguments into `World.call` */ +export function encodeSystemCall({ + abi, + systemId, + functionName, + args, +}: SystemCall): GetFunctionArgs["args"] { + return [ + systemId, + encodeFunctionData({ + abi, + functionName, + args, + } as unknown as EncodeFunctionDataParameters), + ]; +} diff --git a/packages/world/ts/encodeSystemCallFrom.ts b/packages/world/ts/encodeSystemCallFrom.ts new file mode 100644 index 0000000000..1688b8e0be --- /dev/null +++ b/packages/world/ts/encodeSystemCallFrom.ts @@ -0,0 +1,26 @@ +import { Abi, EncodeFunctionDataParameters, GetFunctionArgs, encodeFunctionData, Address } from "viem"; +import IWorldCallAbi from "../out/IWorldKernel.sol/IWorldCall.abi.json"; +import { SystemCall } from "./encodeSystemCall"; + +export type SystemCallFrom = SystemCall & { + readonly from: Address; +}; + +/** Encode a system call to be passed as arguments into `World.callFrom` */ +export function encodeSystemCallFrom({ + abi, + from, + systemId, + functionName, + args, +}: SystemCallFrom): GetFunctionArgs["args"] { + return [ + from, + systemId, + encodeFunctionData({ + abi, + functionName, + args, + } as unknown as EncodeFunctionDataParameters), + ]; +} diff --git a/packages/world/ts/encodeSystemCalls.ts b/packages/world/ts/encodeSystemCalls.ts new file mode 100644 index 0000000000..6336822e0b --- /dev/null +++ b/packages/world/ts/encodeSystemCalls.ts @@ -0,0 +1,11 @@ +import { Abi, GetFunctionArgs } from "viem"; +import IWorldCallAbi from "../out/IWorldKernel.sol/IWorldCall.abi.json"; +import { SystemCall, encodeSystemCall } from "./encodeSystemCall"; + +/** Encode system calls to be passed as arguments into `World.batchCall` */ +export function encodeSystemCalls( + abi: abi, + systemCalls: readonly Omit, "abi">[] +): GetFunctionArgs["args"][] { + return systemCalls.map((systemCall) => encodeSystemCall({ ...systemCall, abi } as SystemCall)); +} diff --git a/packages/world/ts/encodeSystemCallsFrom.ts b/packages/world/ts/encodeSystemCallsFrom.ts new file mode 100644 index 0000000000..e63cc43bd0 --- /dev/null +++ b/packages/world/ts/encodeSystemCallsFrom.ts @@ -0,0 +1,14 @@ +import { Abi, Address, GetFunctionArgs } from "viem"; +import IWorldCallAbi from "../out/IWorldKernel.sol/IWorldCall.abi.json"; +import { SystemCallFrom, encodeSystemCallFrom } from "./encodeSystemCallFrom"; + +/** Encode system calls to be passed as arguments into `World.batchCallFrom` */ +export function encodeSystemCallsFrom( + abi: abi, + from: Address, + systemCalls: readonly Omit, "abi" | "from">[] +): GetFunctionArgs["args"][] { + return systemCalls.map((systemCall) => + encodeSystemCallFrom({ ...systemCall, abi, from } as SystemCallFrom) + ); +} diff --git a/packages/world/ts/index.ts b/packages/world/ts/index.ts index f5fa677ff8..e7abeed4de 100644 --- a/packages/world/ts/index.ts +++ b/packages/world/ts/index.ts @@ -2,4 +2,9 @@ export * from "./config/defaults"; export * from "./config/resolveWorldConfig"; export * from "./config/types"; export * from "./config/worldConfig"; + +export * from "./encodeSystemCall"; +export * from "./encodeSystemCallFrom"; +export * from "./encodeSystemCalls"; +export * from "./encodeSystemCallsFrom"; export * from "./worldEvents"; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 5c996a9239..8e9c734ee4 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1004,6 +1004,9 @@ importers: abitype: specifier: 0.9.8 version: 0.9.8(typescript@5.1.6)(zod@3.21.4) + viem: + specifier: 1.14.0 + version: 1.14.0(typescript@5.1.6)(zod@3.21.4) zod: specifier: ^3.21.4 version: 3.21.4 @@ -2948,17 +2951,17 @@ packages: /@scure/base@1.1.1: resolution: {integrity: sha512-ZxOhsSyxYwLJj3pLZCefNitxsj093tb2vq90mp2txoYeBqbcjDjqFhyM8eUjq/uFm6zJ+mUuqxlS2FkuSY1MTA==} + dev: false /@scure/base@1.1.3: resolution: {integrity: sha512-/+SgoRjLq7Xlf0CWuLHq2LUZeL/w65kfzAPG5NH9pcmBhs+nunQTn4gvdwgMTIXnt9b2C/1SeL2XiysZEyIC9Q==} - dev: false /@scure/bip32@1.1.5: resolution: {integrity: sha512-XyNh1rB0SkEqd3tXcXMi+Xe1fvg+kUIcoRIEujP1Jgv7DqW2r9lg3Ah0NkFaCs9sTkQAQA8kw7xiRXzENi9Rtw==} dependencies: '@noble/hashes': 1.2.0 '@noble/secp256k1': 1.7.1 - '@scure/base': 1.1.1 + '@scure/base': 1.1.3 dev: true /@scure/bip32@1.3.2: @@ -2973,7 +2976,7 @@ packages: resolution: {integrity: sha512-t+wDck2rVkh65Hmv280fYdVdY25J9YeEUIgn2LG1WM6gxFkGzcksoDiUkWVpVp3Oex9xGC68JU2dSbUfwZ2jPg==} dependencies: '@noble/hashes': 1.2.0 - '@scure/base': 1.1.1 + '@scure/base': 1.1.3 dev: true /@scure/bip39@1.2.1: