Skip to content

Commit

Permalink
fix world ts issues
Browse files Browse the repository at this point in the history
  • Loading branch information
holic committed Jan 8, 2025
1 parent 8575637 commit ce07d43
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 12 deletions.
38 changes: 36 additions & 2 deletions packages/world/ts/actions/callFrom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {
encodeKey,
} from "@latticexyz/protocol-parser/internal";
import worldConfig from "../../mud.config";
import IStoreReadAbi from "../../out/IStoreRead.sol/IStoreRead.abi.json";

type CallFromParameters = {
worldAddress: Hex;
Expand Down Expand Up @@ -134,7 +133,42 @@ async function retrieveSystemFunctionFromContract(

const [staticData, encodedLengths, dynamicData] = await _readContract({
address: worldAddress,
abi: IStoreReadAbi,
abi: [
{
type: "function",
name: "getRecord",
inputs: [
{
name: "tableId",
type: "bytes32",
internalType: "ResourceId",
},
{
name: "keyTuple",
type: "bytes32[]",
internalType: "bytes32[]",
},
],
outputs: [
{
name: "staticData",
type: "bytes",
internalType: "bytes",
},
{
name: "encodedLengths",
type: "bytes32",
internalType: "EncodedLengths",
},
{
name: "dynamicData",
type: "bytes",
internalType: "bytes",
},
],
stateMutability: "view",
},
],
functionName: "getRecord",
args: [table.tableId, encodeKey(keySchema, { worldFunctionSelector })],
});
Expand Down
6 changes: 2 additions & 4 deletions packages/world/ts/encodeSystemCall.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Abi, EncodeFunctionDataParameters, Hex, encodeFunctionData, type ContractFunctionName } from "viem";
import type { AbiParametersToPrimitiveTypes, ExtractAbiFunction } from "abitype";
import IWorldCallAbi from "../out/IWorldKernel.sol/IWorldCall.abi.json";
import { worldCallAbi } from "./worldCallAbi";

export type SystemCall<abi extends Abi, functionName extends ContractFunctionName<abi>> = EncodeFunctionDataParameters<
abi,
Expand All @@ -15,9 +15,7 @@ export function encodeSystemCall<abi extends Abi, functionName extends ContractF
systemId,
functionName,
args,
}: SystemCall<abi, functionName>): AbiParametersToPrimitiveTypes<
ExtractAbiFunction<typeof IWorldCallAbi, "call">["inputs"]
> {
}: SystemCall<abi, functionName>): AbiParametersToPrimitiveTypes<ExtractAbiFunction<worldCallAbi, "call">["inputs"]> {
return [
systemId,
encodeFunctionData<abi, functionName>({
Expand Down
4 changes: 2 additions & 2 deletions packages/world/ts/encodeSystemCallFrom.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Abi, EncodeFunctionDataParameters, encodeFunctionData, Address, type ContractFunctionName } from "viem";
import type { AbiParametersToPrimitiveTypes, ExtractAbiFunction } from "abitype";
import IWorldCallAbi from "../out/IWorldKernel.sol/IWorldCall.abi.json";
import { SystemCall } from "./encodeSystemCall";
import { worldCallAbi } from "./worldCallAbi";

export type SystemCallFrom<abi extends Abi, functionName extends ContractFunctionName<abi>> = SystemCall<
abi,
Expand All @@ -18,7 +18,7 @@ export function encodeSystemCallFrom<abi extends Abi, functionName extends Contr
functionName,
args,
}: SystemCallFrom<abi, functionName>): AbiParametersToPrimitiveTypes<
ExtractAbiFunction<typeof IWorldCallAbi, "callFrom">["inputs"]
ExtractAbiFunction<worldCallAbi, "callFrom">["inputs"]
> {
return [
from,
Expand Down
4 changes: 2 additions & 2 deletions packages/world/ts/encodeSystemCalls.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Abi, type ContractFunctionName } from "viem";
import IWorldCallAbi from "../out/IWorldKernel.sol/IWorldCall.abi.json";
import { SystemCall, encodeSystemCall } from "./encodeSystemCall";
import type { AbiParametersToPrimitiveTypes, ExtractAbiFunction } from "abitype";
import { worldCallAbi } from "./worldCallAbi";

/** Encode system calls to be passed as arguments into `World.batchCall` */
export function encodeSystemCalls<abi extends Abi, functionName extends ContractFunctionName<abi>>(
abi: abi,
systemCalls: readonly Omit<SystemCall<abi, functionName>, "abi">[],
): AbiParametersToPrimitiveTypes<ExtractAbiFunction<typeof IWorldCallAbi, "call">["inputs"]>[] {
): AbiParametersToPrimitiveTypes<ExtractAbiFunction<worldCallAbi, "call">["inputs"]>[] {
return systemCalls.map((systemCall) => encodeSystemCall({ ...systemCall, abi } as SystemCall<abi, functionName>));
}
4 changes: 2 additions & 2 deletions packages/world/ts/encodeSystemCallsFrom.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Abi, Address, type ContractFunctionName } from "viem";
import IWorldCallAbi from "../out/IWorldKernel.sol/IWorldCall.abi.json";
import { SystemCallFrom, encodeSystemCallFrom } from "./encodeSystemCallFrom";
import type { AbiParametersToPrimitiveTypes, ExtractAbiFunction } from "abitype";
import { worldCallAbi } from "./worldCallAbi";

/** Encode system calls to be passed as arguments into `World.batchCallFrom` */
export function encodeSystemCallsFrom<abi extends Abi, functionName extends ContractFunctionName<abi>>(
abi: abi,
from: Address,
systemCalls: readonly Omit<SystemCallFrom<abi, functionName>, "abi" | "from">[],
): AbiParametersToPrimitiveTypes<ExtractAbiFunction<typeof IWorldCallAbi, "callFrom">["inputs"]>[] {
): AbiParametersToPrimitiveTypes<ExtractAbiFunction<worldCallAbi, "callFrom">["inputs"]>[] {
return systemCalls.map((systemCall) =>
encodeSystemCallFrom({ ...systemCall, abi, from } as SystemCallFrom<abi, functionName>),
);
Expand Down
60 changes: 60 additions & 0 deletions packages/world/ts/worldCallAbi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// TODO: replace this with abi-ts generated files once we move to
// generating full TS files rather than DTS

export const worldCallAbi = [
{
type: "function",
name: "call",
inputs: [
{
name: "systemId",
type: "bytes32",
internalType: "ResourceId",
},
{
name: "callData",
type: "bytes",
internalType: "bytes",
},
],
outputs: [
{
name: "",
type: "bytes",
internalType: "bytes",
},
],
stateMutability: "payable",
},
{
type: "function",
name: "callFrom",
inputs: [
{
name: "delegator",
type: "address",
internalType: "address",
},
{
name: "systemId",
type: "bytes32",
internalType: "ResourceId",
},
{
name: "callData",
type: "bytes",
internalType: "bytes",
},
],
outputs: [
{
name: "",
type: "bytes",
internalType: "bytes",
},
],
stateMutability: "payable",
},
] as const;

export type worldCallAbi = typeof worldCallAbi;

0 comments on commit ce07d43

Please sign in to comment.