Skip to content

Commit

Permalink
use built in client
Browse files Browse the repository at this point in the history
  • Loading branch information
holic committed Jun 5, 2024
1 parent 0958d9e commit 46a18a6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 26 deletions.
5 changes: 2 additions & 3 deletions packages/account-kit/src/useAppAccountClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,13 @@ export function useAppAccountClient(): UseQueryResult<AppAccountClient> {
// TODO: provide way to override this transport?
transport: transportObserver("app account (signer) client", http()),
})
.extend(() => publicActions(publicClient))
.extend(publicActions)
.extend(walletActions)
.extend(transactionQueue({ publicClient }))
.extend(transactionQueue())
.extend(
callFrom({
worldAddress,
delegatorAddress: userAddress,
publicClient,
}),
);
return appAccountClient;
Expand Down
47 changes: 24 additions & 23 deletions packages/world/ts/actions/callFrom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import {
type WalletActions,
type WriteContractReturnType,
type EncodeFunctionDataParameters,
type PublicClient,
Client,
PublicActions,
} from "viem";
import { getAction, encodeFunctionData } from "viem/utils";
import { writeContract } from "viem/actions";
import { readContract, writeContract } from "viem/actions";
import { readHex } from "@latticexyz/common";
import {
getKeySchema,
Expand All @@ -24,19 +24,11 @@ import {
import worldConfig from "../../mud.config";
import IStoreReadAbi from "../../out/IStoreRead.sol/IStoreRead.abi.json";

// Accepts either `worldFunctionToSystemFunction` or `publicClient`, but not both.
type CallFromParameters = CallFromFunctionParameters | CallFromClientParameters;
type CallFromBaseParameters = {
type CallFromParameters = {
worldAddress: Hex;
delegatorAddress: Hex;
};
type CallFromFunctionParameters = CallFromBaseParameters & {
worldFunctionToSystemFunction: (worldFunctionSelector: Hex) => Promise<SystemFunction>;
publicClient?: never;
};
type CallFromClientParameters = CallFromBaseParameters & {
worldFunctionToSystemFunction?: never;
publicClient: PublicClient;
worldFunctionToSystemFunction?: (worldFunctionSelector: Hex) => Promise<SystemFunction>;
publicClient?: Client;
};

type SystemFunction = { systemId: Hex; systemFunctionSelector: Hex };
Expand Down Expand Up @@ -79,7 +71,11 @@ export function callFrom<TChain extends Chain, TAccount extends Account>(
const worldFunctionSelector = slice(worldCalldata, 0, 4);

// Get the systemId and System's function selector.
const { systemId, systemFunctionSelector } = await worldFunctionToSystemFunction(params, worldFunctionSelector);
const { systemId, systemFunctionSelector } = await worldFunctionToSystemFunction({
...params,
publicClient: params.publicClient ?? client,
worldFunctionSelector,
});

// Construct the System's calldata by replacing the World's function selector with the System's.
// Use `readHex` instead of `slice` to prevent out-of-bounds errors with calldata that has no args.
Expand All @@ -100,28 +96,31 @@ export function callFrom<TChain extends Chain, TAccount extends Account>(

const systemFunctionCache = new Map<Hex, SystemFunction>();

async function worldFunctionToSystemFunction(
params: CallFromParameters,
worldFunctionSelector: Hex,
): Promise<SystemFunction> {
const cacheKey = concat([params.worldAddress, worldFunctionSelector]);
async function worldFunctionToSystemFunction(params: {
worldAddress: Hex;
delegatorAddress: Hex;
worldFunctionSelector: Hex;
worldFunctionToSystemFunction?: (worldFunctionSelector: Hex) => Promise<SystemFunction>;
publicClient: Client;
}): Promise<SystemFunction> {
const cacheKey = concat([params.worldAddress, params.worldFunctionSelector]);

// Use cache if the function has been called previously.
const cached = systemFunctionCache.get(cacheKey);
if (cached) return cached;

// If a mapping function is provided, use it. Otherwise, call the World contract.
const systemFunction = params.worldFunctionToSystemFunction
? await params.worldFunctionToSystemFunction(worldFunctionSelector)
: await retrieveSystemFunctionFromContract(params.publicClient, params.worldAddress, worldFunctionSelector);
? await params.worldFunctionToSystemFunction(params.worldFunctionSelector)
: await retrieveSystemFunctionFromContract(params.publicClient, params.worldAddress, params.worldFunctionSelector);

systemFunctionCache.set(cacheKey, systemFunction);

return systemFunction;
}

async function retrieveSystemFunctionFromContract(
publicClient: PublicClient,
publicClient: Client,
worldAddress: Hex,
worldFunctionSelector: Hex,
): Promise<SystemFunction> {
Expand All @@ -130,7 +129,9 @@ async function retrieveSystemFunctionFromContract(
const keySchema = getSchemaTypes(getKeySchema(table));
const valueSchema = getSchemaTypes(getValueSchema(table));

const [staticData, encodedLengths, dynamicData] = await publicClient.readContract({
const _readContract = getAction(publicClient, readContract as never, "readContract") as PublicActions["readContract"];

const [staticData, encodedLengths, dynamicData] = await _readContract({
address: worldAddress,
abi: IStoreReadAbi,
functionName: "getRecord",
Expand Down

0 comments on commit 46a18a6

Please sign in to comment.