1- import { Client , Abi , Address , hexToString , parseAbi , stringToHex } from "viem" ;
1+ import {
2+ Client ,
3+ Abi ,
4+ Address ,
5+ AbiItem ,
6+ AbiFunction ,
7+ getAddress ,
8+ toFunctionSelector ,
9+ hexToString ,
10+ parseAbi ,
11+ stringToHex ,
12+ } from "viem" ;
13+ import IBaseWorldAbi from "@latticexyz/world/out/IBaseWorld.sol/IBaseWorld.abi.json" assert { type : "json" } ;
214import metadataConfig from "@latticexyz/world-module-metadata/mud.config" ;
15+ import { functionSignatureToAbiItem } from "./functionSignatureToAbiItem" ;
16+ import { isDefined } from "@latticexyz/common/utils" ;
17+ import { getFunctions } from "./getFunctions" ;
318import { getRecords } from "../getRecords" ;
419
20+ function isAbiFunction ( abiItem : AbiItem ) : abiItem is AbiFunction {
21+ return abiItem . type === "function" ;
22+ }
23+
524export async function getWorldAbi ( {
625 client,
726 worldAddress,
@@ -17,19 +36,46 @@ export async function getWorldAbi({
1736 readonly indexerUrl ?: string ;
1837 readonly chainId ?: number ;
1938} ) : Promise < Abi > {
20- const { records } = await getRecords ( {
21- table : metadataConfig . tables . metadata__ResourceTag ,
22- worldAddress,
23- chainId,
24- indexerUrl,
25- client,
26- fromBlock,
27- toBlock,
28- } ) ;
39+ const [ worldFunctions , { records } ] = await Promise . all ( [
40+ getFunctions ( {
41+ client,
42+ worldAddress : getAddress ( worldAddress ) ,
43+ fromBlock,
44+ toBlock,
45+ indexerUrl,
46+ chainId,
47+ } ) ,
48+ getRecords ( {
49+ table : metadataConfig . tables . metadata__ResourceTag ,
50+ worldAddress,
51+ chainId,
52+ indexerUrl,
53+ client,
54+ fromBlock,
55+ toBlock,
56+ } ) ,
57+ ] ) ;
2958
30- const abi = records
59+ const metadataAbi = records
3160 . filter ( ( { tag } ) => tag === stringToHex ( "worldAbi" , { size : 32 } ) )
3261 . flatMap ( ( { value } ) => ( value === "0x" ? [ ] : parseAbi ( hexToString ( value ) . split ( "\n" ) ) ) ) ;
3362
34- return abi ;
63+ const metadataFunctionSelectors = metadataAbi . filter ( isAbiFunction ) . map ( toFunctionSelector ) ;
64+ const baseFunctionSelectors = ( IBaseWorldAbi as Abi ) . filter ( isAbiFunction ) . map ( toFunctionSelector ) ;
65+ const worldFunctionsAbi = worldFunctions
66+ . map ( ( func ) => {
67+ try {
68+ return functionSignatureToAbiItem ( func . signature ) ;
69+ } catch ( error ) {
70+ console . error ( error ) ;
71+ }
72+ } )
73+ . filter ( isDefined )
74+ . filter (
75+ ( abiItem ) =>
76+ ! baseFunctionSelectors . includes ( toFunctionSelector ( abiItem ) ) &&
77+ ! metadataFunctionSelectors . includes ( toFunctionSelector ( abiItem ) ) ,
78+ ) ;
79+
80+ return [ ...( IBaseWorldAbi as Abi ) , ...metadataAbi , ...worldFunctionsAbi ] ;
3581}
0 commit comments