Skip to content

Commit

Permalink
fix types
Browse files Browse the repository at this point in the history
  • Loading branch information
holic committed Feb 6, 2025
1 parent 7997c41 commit 087f6c3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 24 deletions.
28 changes: 15 additions & 13 deletions packages/world/ts/encodeSystemCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,21 @@ import { worldCallAbi } from "./worldCallAbi";
import { internal_normalizeSystemFunctionName } from "./normalizeSystemFunctionName";

export type SystemCall<abi extends Abi, functionName extends ContractFunctionName<abi>> = {
/**
* System ABI
*/
readonly abi: abi;
/**
* System's resource ID
*/
readonly systemId: Hex;
/**
* System function name to call
*/
readonly functionName: functionName;
} & Pick<EncodeFunctionDataParameters<abi, functionName>, "args">;
[k in ContractFunctionName<abi>]: {
/**
* System's resource ID
*/
readonly systemId: Hex;
/**
* System ABI
*/
readonly abi: abi;
/**
* System function name to call
*/
readonly functionName: k;
} & Pick<EncodeFunctionDataParameters<abi, k>, "args">;
}[functionName];

/** Encode a system call to be passed as arguments into `World.call` */
export function encodeSystemCall<abi extends Abi, functionName extends ContractFunctionName<abi>>({
Expand Down
14 changes: 9 additions & 5 deletions packages/world/ts/encodeSystemCalls.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
import { Abi, EncodeFunctionDataParameters, encodeFunctionData, type ContractFunctionName } from "viem";
import { Abi, encodeFunctionData, type ContractFunctionName } from "viem";
import { SystemCall } from "./encodeSystemCall";
import type { AbiParametersToPrimitiveTypes, ExtractAbiFunction } from "abitype";
import { worldCallAbi } from "./worldCallAbi";
import { internal_normalizeSystemFunctionName } from "./normalizeSystemFunctionName";

export type SystemCalls<abis extends readonly Abi[]> = {
[k in keyof abis]: SystemCall<abis[k], ContractFunctionName<abis[k]>>;
};

/** Encode system calls to be passed as arguments into `World.batchCall` */
export function encodeSystemCalls<abi extends Abi, functionName extends ContractFunctionName<abi>>(
systemCalls: readonly SystemCall<abi, functionName>[],
export function encodeSystemCalls<abis extends readonly Abi[]>(
systemCalls: SystemCalls<abis>,
): AbiParametersToPrimitiveTypes<ExtractAbiFunction<worldCallAbi, "batchCall">["inputs"]> {
return [
systemCalls.map(({ abi, systemId, functionName, args }) => ({
systemId,
callData: encodeFunctionData<abi, functionName>({
callData: encodeFunctionData({
abi,
functionName: internal_normalizeSystemFunctionName(systemId, functionName),
args,
} as EncodeFunctionDataParameters<abi, functionName>),
}),
})),
];
}
12 changes: 6 additions & 6 deletions packages/world/ts/encodeSystemCallsFrom.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import { Abi, Address, EncodeFunctionDataParameters, encodeFunctionData, type ContractFunctionName } from "viem";
import { SystemCallFrom } from "./encodeSystemCallFrom";
import { Abi, Address, encodeFunctionData } from "viem";
import type { AbiParametersToPrimitiveTypes, ExtractAbiFunction } from "abitype";
import { worldCallAbi } from "./worldCallAbi";
import { internal_normalizeSystemFunctionName } from "./normalizeSystemFunctionName";
import { SystemCalls } from "./encodeSystemCalls";

/** Encode system calls to be passed as arguments into `World.batchCallFrom` */
export function encodeSystemCallsFrom<abi extends Abi, functionName extends ContractFunctionName<abi>>(
export function encodeSystemCallsFrom<abis extends readonly Abi[]>(
from: Address,
systemCalls: readonly Omit<SystemCallFrom<abi, functionName>, "from">[],
systemCalls: SystemCalls<abis>,
): AbiParametersToPrimitiveTypes<ExtractAbiFunction<worldCallAbi, "batchCallFrom">["inputs"]> {
return [
systemCalls.map(({ abi, systemId, functionName, args }) => ({
from,
systemId,
callData: encodeFunctionData<abi, functionName>({
callData: encodeFunctionData({
abi,
functionName: internal_normalizeSystemFunctionName(systemId, functionName),
args,
} as EncodeFunctionDataParameters<abi, functionName>),
}),
})),
];
}

0 comments on commit 087f6c3

Please sign in to comment.