-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
40 additions
and
6 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 |
---|---|---|
@@ -1,24 +1,41 @@ | ||
import { mapObject } from "@latticexyz/utils"; | ||
import { Data, EncodedData } from "./types"; | ||
import { encodeAbiParameters, toHex, encodePacked, numberToHex } from "viem"; | ||
import { encodeAbiParameters, toHex, numberToHex } from "viem"; | ||
import { Schema, encodeRecord } from "@latticexyz/protocol-parser"; | ||
import config from "../../contracts/mud.config"; | ||
import { DynamicAbiType, SchemaAbiType, StaticAbiType, isDynamicAbiType } from "@latticexyz/schema-type"; | ||
|
||
/** | ||
* Turns the typed data into encoded data in the format expected by `world.setRecord` | ||
*/ | ||
export function encodeTestData(testData: Data) { | ||
return mapObject(testData, (records, table) => | ||
records | ||
? records.map((record) => ({ | ||
key: Object.entries(record.key).map(([keyName, keyValue]) => { | ||
? records.map((record) => { | ||
const valueSchema = abiTypeArrayToSchema(Object.values(config.tables[table].schema)); | ||
const value = encodeRecord(valueSchema, Object.values(record.value)); | ||
const key = Object.entries(record.key).map(([keyName, keyValue]) => { | ||
const signed = config.tables[table].keySchema[keyName]?.startsWith("int"); | ||
return encodeAbiParameters( | ||
[{ type: "bytes32" }], | ||
[signed ? numberToHex(keyValue as any, { size: 32, signed }) : toHex(keyValue as any, { size: 32 })] | ||
); | ||
}), | ||
value: encodePacked(Object.values(config.tables[table].schema), Object.values(record.value)), | ||
})) | ||
}); | ||
return { | ||
key, | ||
value, | ||
}; | ||
}) | ||
: undefined | ||
) as EncodedData<typeof testData>; | ||
} | ||
|
||
function abiTypeArrayToSchema(abiTypes: SchemaAbiType[]): Schema { | ||
const staticFields: StaticAbiType[] = []; | ||
const dynamicFields: DynamicAbiType[] = []; | ||
for (const abiType of abiTypes) { | ||
if (isDynamicAbiType(abiType)) dynamicFields.push(abiType); | ||
else staticFields.push(abiType); | ||
} | ||
return { staticFields, dynamicFields }; | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.