-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(world): add TS helpers to encode system calls (#1745)
- Loading branch information
Showing
7 changed files
with
96 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>), | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>), | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.