Skip to content

Commit

Permalink
Support 0x1::object::Object
Browse files Browse the repository at this point in the history
  • Loading branch information
SamuelQZQ committed Jul 11, 2023
1 parent 2315de1 commit 64ea383
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/chatty-boxes-speak.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@thalalabs/surf': patch
---

Support 0x1::object::Object
19 changes: 19 additions & 0 deletions src/core/__tests__/createEntryPayload.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,14 @@ describe('createEntryPayload', () => {
});
}
});

it('0x1::object::Object', async () => {
createEntryPayload(TEST_ABI, {
function: 'object_as_input',
arguments: ["0x123456"],
type_arguments: [],
});
});
});

const TEST_ABI = {
Expand Down Expand Up @@ -256,6 +264,17 @@ const TEST_ABI = {
],
"return": []
},
{
"name": "object_as_input",
"visibility": "public",
"is_entry": true,
"is_view": false,
"generic_type_params": [],
"params": [
"0x1::object::Object<0xb8a4015d231899eaf1de8eb6dc6547f296f215b7ca46ea01b22b3d1ba24b6eb1::overflow::Overflow<T0, T1>>",
],
"return": []
},
],

"structs": []
Expand Down
15 changes: 11 additions & 4 deletions src/core/createEntryPayload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ export function createEntryPayload<

function argToBCS(type: string, arg: any, serializer: BCS.Serializer) {

const regex = /vector<([^]+)>/;
const match = type.match(regex);
if (match) { // It's vector
const innerType = match[1]!;
const vectorRegex = /vector<([^]+)>/;
const vectorMatch = type.match(vectorRegex);
if (vectorMatch) { // It's vector
const innerType = vectorMatch[1]!;
if (innerType === 'u8') {
if (arg instanceof Uint8Array) { // TODO: add type support for Uint8Array
serializer.serializeBytes(arg);
Expand All @@ -112,6 +112,13 @@ function argToBCS(type: string, arg: any, serializer: BCS.Serializer) {
return;
}

const objectRegex = /0x1::object::Object<([^]+)>/;
const objectMatch = type.match(objectRegex);
if (objectMatch) { // It's 0x1::object::Object
TxnBuilderTypes.AccountAddress.fromHex(arg as string).serialize(serializer);
return;
}

switch (type) { // It's primitive
case 'bool':
serializer.serializeBool(ensureBoolean(arg));
Expand Down
4 changes: 3 additions & 1 deletion src/types/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ export type Primitive =

type Vector = `vector<${string}>`;
type VectorOfVector = `vector<vector<${string}>>`;
type MoveObject = `0x1::object::Object<${string}>`

// TODO: support vector<vector<vector>>
export type AllTypes = Primitive | Vector | VectorOfVector;
export type AllTypes = Primitive | Vector | VectorOfVector | MoveObject;
type ConvertPrimitiveArgsType<T extends Primitive> =
T extends 'bool' ? boolean :
T extends 'u8' ? number :
Expand All @@ -35,6 +36,7 @@ type ConvertVectorArgsType<TInner> = TInner extends Primitive ? ConvertPrimitive
export type ConvertArgsType<T extends AllTypes> =
T extends Primitive ? ConvertPrimitiveArgsType<T> :
T extends `vector<${infer TInner}>` ? ConvertVectorArgsType<TInner> :
T extends `0x1::object::Object<${string}>` ? `0x${string}` :
Struct<T>;

//@ts-ignore TODO: remove this ignore
Expand Down

0 comments on commit 64ea383

Please sign in to comment.