Skip to content

Commit

Permalink
feat(world): add TS helpers to encode system calls (#1745)
Browse files Browse the repository at this point in the history
  • Loading branch information
holic authored Oct 11, 2023
1 parent 1d0f7e2 commit 49c3e6c
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/world/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"@latticexyz/schema-type": "workspace:*",
"@latticexyz/store": "workspace:*",
"abitype": "0.9.8",
"viem": "1.14.0",
"zod": "^3.21.4"
},
"devDependencies": {
Expand Down
33 changes: 33 additions & 0 deletions packages/world/ts/encodeSystemCall.ts
Original file line number Diff line number Diff line change
@@ -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<abi extends Abi, functionName extends string = string> = Pick<
ContractFunctionConfig<abi, functionName>,
"abi" | "functionName" | "args"
> & {
readonly systemId: Hex;
};

/** Encode a system call to be passed as arguments into `World.call` */
export function encodeSystemCall<abi extends Abi, functionName extends string = string>({
abi,
systemId,
functionName,
args,
}: SystemCall<abi, functionName>): GetFunctionArgs<typeof IWorldCallAbi, "call">["args"] {
return [
systemId,
encodeFunctionData({
abi,
functionName,
args,
} as unknown as EncodeFunctionDataParameters<abi, functionName>),
];
}
26 changes: 26 additions & 0 deletions packages/world/ts/encodeSystemCallFrom.ts
Original file line number Diff line number Diff line change
@@ -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<abi extends Abi, functionName extends string = string> = SystemCall<abi, functionName> & {
readonly from: Address;
};

/** Encode a system call to be passed as arguments into `World.callFrom` */
export function encodeSystemCallFrom<abi extends Abi, functionName extends string = string>({
abi,
from,
systemId,
functionName,
args,
}: SystemCallFrom<abi, functionName>): GetFunctionArgs<typeof IWorldCallAbi, "callFrom">["args"] {
return [
from,
systemId,
encodeFunctionData({
abi,
functionName,
args,
} as unknown as EncodeFunctionDataParameters<abi, functionName>),
];
}
11 changes: 11 additions & 0 deletions packages/world/ts/encodeSystemCalls.ts
Original file line number Diff line number Diff line change
@@ -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 extends Abi, functionName extends string = string>(
abi: abi,
systemCalls: readonly Omit<SystemCall<abi, functionName>, "abi">[]
): GetFunctionArgs<typeof IWorldCallAbi, "call">["args"][] {
return systemCalls.map((systemCall) => encodeSystemCall({ ...systemCall, abi } as SystemCall<abi, functionName>));
}
14 changes: 14 additions & 0 deletions packages/world/ts/encodeSystemCallsFrom.ts
Original file line number Diff line number Diff line change
@@ -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 extends Abi, functionName extends string = string>(
abi: abi,
from: Address,
systemCalls: readonly Omit<SystemCallFrom<abi, functionName>, "abi" | "from">[]
): GetFunctionArgs<typeof IWorldCallAbi, "callFrom">["args"][] {
return systemCalls.map((systemCall) =>
encodeSystemCallFrom({ ...systemCall, abi, from } as SystemCallFrom<abi, functionName>)
);
}
5 changes: 5 additions & 0 deletions packages/world/ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
9 changes: 6 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 49c3e6c

Please sign in to comment.