@thalalabs/surf 0.0.10 → 0.0.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/README.md +5 -2
- package/build/cjs/core/Client.js +9 -11
- package/build/cjs/core/Client.js.map +1 -1
- package/build/cjs/core/WalletClient.js +3 -3
- package/build/cjs/core/WalletClient.js.map +1 -1
- package/build/cjs/core/createEntryPayload.js +25 -8
- package/build/cjs/core/createEntryPayload.js.map +1 -1
- package/build/cjs/core/createViewPayload.js +11 -8
- package/build/cjs/core/createViewPayload.js.map +1 -1
- package/build/cjs/hooks/index.js.map +1 -1
- package/build/cjs/hooks/useSubmitTransaction.js +8 -6
- package/build/cjs/hooks/useSubmitTransaction.js.map +1 -1
- package/build/cjs/hooks/useWalletClient.js +3 -1
- package/build/cjs/hooks/useWalletClient.js.map +1 -1
- package/build/cjs/types/client/abiClient.js.map +1 -0
- package/build/{esm/types → cjs/types/client}/client.js.map +1 -1
- package/build/cjs/types/client/walletClient.js.map +1 -0
- package/build/cjs/types/convertor/argsConvertor.js +3 -0
- package/build/cjs/types/convertor/argsConvertor.js.map +1 -0
- package/build/cjs/types/convertor/genericConvertor.js +3 -0
- package/build/cjs/types/convertor/genericConvertor.js.map +1 -0
- package/build/cjs/types/convertor/returnConvertor.js +3 -0
- package/build/cjs/types/convertor/returnConvertor.js.map +1 -0
- package/build/cjs/types/convertor/structConvertor.js +3 -0
- package/build/cjs/types/convertor/structConvertor.js.map +1 -0
- package/build/cjs/types/extractor/functionExtractor.js +3 -0
- package/build/cjs/types/extractor/functionExtractor.js.map +1 -0
- package/build/cjs/types/extractor/structExtractor.js +3 -0
- package/build/cjs/types/extractor/structExtractor.js.map +1 -0
- package/build/cjs/types/{struct.js → moveTypes.js} +1 -1
- package/build/cjs/types/moveTypes.js.map +1 -0
- package/build/esm/core/Client.js +12 -14
- package/build/esm/core/Client.js.map +1 -1
- package/build/esm/core/WalletClient.js +5 -5
- package/build/esm/core/WalletClient.js.map +1 -1
- package/build/esm/core/createEntryPayload.js +27 -10
- package/build/esm/core/createEntryPayload.js.map +1 -1
- package/build/esm/core/createViewPayload.js +13 -10
- package/build/esm/core/createViewPayload.js.map +1 -1
- package/build/esm/core/index.js +4 -4
- package/build/esm/hooks/index.js +2 -2
- package/build/esm/hooks/index.js.map +1 -1
- package/build/esm/hooks/useSubmitTransaction.js +11 -9
- package/build/esm/hooks/useSubmitTransaction.js.map +1 -1
- package/build/esm/hooks/useWalletClient.js +5 -3
- package/build/esm/hooks/useWalletClient.js.map +1 -1
- package/build/esm/types/client/abiClient.js.map +1 -0
- package/build/{cjs/types → esm/types/client}/client.js.map +1 -1
- package/build/esm/types/client/walletClient.js.map +1 -0
- package/build/esm/types/convertor/argsConvertor.js +2 -0
- package/build/esm/types/convertor/argsConvertor.js.map +1 -0
- package/build/esm/types/convertor/genericConvertor.js +2 -0
- package/build/esm/types/convertor/genericConvertor.js.map +1 -0
- package/build/esm/types/convertor/returnConvertor.js +2 -0
- package/build/esm/types/convertor/returnConvertor.js.map +1 -0
- package/build/esm/types/convertor/structConvertor.js +2 -0
- package/build/esm/types/convertor/structConvertor.js.map +1 -0
- package/build/esm/types/extractor/functionExtractor.js +2 -0
- package/build/esm/types/extractor/functionExtractor.js.map +1 -0
- package/build/esm/types/extractor/structExtractor.js +2 -0
- package/build/esm/types/extractor/structExtractor.js.map +1 -0
- package/build/esm/types/moveTypes.js +2 -0
- package/build/esm/types/moveTypes.js.map +1 -0
- package/build/types/core/Client.d.ts +3 -5
- package/build/types/core/Client.d.ts.map +1 -1
- package/build/types/core/WalletClient.d.ts +2 -3
- package/build/types/core/WalletClient.d.ts.map +1 -1
- package/build/types/core/createEntryPayload.d.ts +2 -2
- package/build/types/core/createEntryPayload.d.ts.map +1 -1
- package/build/types/core/createViewPayload.d.ts +1 -2
- package/build/types/core/createViewPayload.d.ts.map +1 -1
- package/build/types/core/index.d.ts +4 -4
- package/build/types/hooks/index.d.ts +2 -2
- package/build/types/hooks/index.d.ts.map +1 -1
- package/build/types/hooks/useSubmitTransaction.d.ts +2 -2
- package/build/types/hooks/useSubmitTransaction.d.ts.map +1 -1
- package/build/types/hooks/useWalletClient.d.ts +1 -1
- package/build/types/hooks/useWalletClient.d.ts.map +1 -1
- package/build/types/types/abi.d.ts +4 -1
- package/build/types/types/abi.d.ts.map +1 -1
- package/build/types/types/client/abiClient.d.ts +33 -0
- package/build/types/types/client/abiClient.d.ts.map +1 -0
- package/build/types/types/client/client.d.ts +54 -0
- package/build/types/types/client/client.d.ts.map +1 -0
- package/build/types/types/client/walletClient.d.ts +13 -0
- package/build/types/types/client/walletClient.d.ts.map +1 -0
- package/build/types/types/common.d.ts +7 -59
- package/build/types/types/common.d.ts.map +1 -1
- package/build/types/types/convertor/argsConvertor.d.ts +21 -0
- package/build/types/types/convertor/argsConvertor.d.ts.map +1 -0
- package/build/types/types/convertor/genericConvertor.d.ts +8 -0
- package/build/types/types/convertor/genericConvertor.d.ts.map +1 -0
- package/build/types/types/convertor/returnConvertor.d.ts +21 -0
- package/build/types/types/convertor/returnConvertor.d.ts.map +1 -0
- package/build/types/types/convertor/structConvertor.d.ts +19 -0
- package/build/types/types/convertor/structConvertor.d.ts.map +1 -0
- package/build/types/types/defaultABITable.d.ts +10 -6
- package/build/types/types/defaultABITable.d.ts.map +1 -1
- package/build/types/types/extractor/functionExtractor.d.ts +52 -0
- package/build/types/types/extractor/functionExtractor.d.ts.map +1 -0
- package/build/types/types/extractor/structExtractor.d.ts +35 -0
- package/build/types/types/extractor/structExtractor.d.ts.map +1 -0
- package/build/types/types/index.d.ts +6 -5
- package/build/types/types/index.d.ts.map +1 -1
- package/build/types/types/moveTypes.d.ts +9 -0
- package/build/types/types/moveTypes.d.ts.map +1 -0
- package/package.json +4 -4
- package/src/core/Client.ts +230 -223
- package/src/core/WalletClient.ts +57 -55
- package/src/core/__tests__/createEntryPayload.test.ts +19 -0
- package/src/core/__tests__/option.test.ts +144 -0
- package/src/core/createEntryPayload.ts +153 -112
- package/src/core/createViewPayload.ts +112 -100
- package/src/core/index.ts +4 -4
- package/src/hooks/index.ts +5 -2
- package/src/hooks/useSubmitTransaction.ts +101 -98
- package/src/hooks/useWalletClient.ts +10 -8
- package/src/types/abi.ts +24 -21
- package/src/types/client/abiClient.ts +47 -0
- package/src/types/client/client.ts +73 -0
- package/src/types/client/walletClient.ts +18 -0
- package/src/types/common.ts +13 -154
- package/src/types/convertor/argsConvertor.ts +59 -0
- package/src/types/convertor/genericConvertor.ts +9 -0
- package/src/types/convertor/returnConvertor.ts +55 -0
- package/src/types/convertor/structConvertor.ts +82 -0
- package/src/types/defaultABITable.ts +17 -12
- package/src/types/extractor/functionExtractor.ts +86 -0
- package/src/types/extractor/structExtractor.ts +67 -0
- package/src/types/index.ts +20 -9
- package/src/types/moveTypes.ts +22 -0
- package/build/cjs/types/abiClient.js.map +0 -1
- package/build/cjs/types/struct.js.map +0 -1
- package/build/cjs/types/walletClient.js.map +0 -1
- package/build/esm/types/abiClient.js.map +0 -1
- package/build/esm/types/struct.js +0 -2
- package/build/esm/types/struct.js.map +0 -1
- package/build/esm/types/walletClient.js.map +0 -1
- package/build/types/types/abiClient.d.ts +0 -28
- package/build/types/types/abiClient.d.ts.map +0 -1
- package/build/types/types/client.d.ts +0 -24
- package/build/types/types/client.d.ts.map +0 -1
- package/build/types/types/struct.d.ts +0 -30
- package/build/types/types/struct.d.ts.map +0 -1
- package/build/types/types/walletClient.d.ts +0 -9
- package/build/types/types/walletClient.d.ts.map +0 -1
- package/src/types/abiClient.ts +0 -38
- package/src/types/client.ts +0 -32
- package/src/types/struct.ts +0 -87
- package/src/types/walletClient.ts +0 -10
- /package/build/cjs/types/{abiClient.js → client/abiClient.js} +0 -0
- /package/build/cjs/types/{client.js → client/client.js} +0 -0
- /package/build/cjs/types/{walletClient.js → client/walletClient.js} +0 -0
- /package/build/esm/types/{abiClient.js → client/abiClient.js} +0 -0
- /package/build/esm/types/{client.js → client/client.js} +0 -0
- /package/build/esm/types/{walletClient.js → client/walletClient.js} +0 -0
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* These test cases depends on network, it call the real contract.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { AptosAccount } from 'aptos';
|
|
6
|
+
import { createClient } from '../Client';
|
|
7
|
+
import { createViewPayload } from '../createViewPayload';
|
|
8
|
+
import { createEntryPayload } from '../createEntryPayload';
|
|
9
|
+
|
|
10
|
+
describe('option type', () => {
|
|
11
|
+
const client = createClient({
|
|
12
|
+
nodeUrl: 'https://fullnode.testnet.aptoslabs.com/v1',
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
const account = new AptosAccount(
|
|
16
|
+
undefined,
|
|
17
|
+
'0xac914efd2367c7aa42c95d100592c099e487d2270bf0e0761e5fe93ff4016593',
|
|
18
|
+
);
|
|
19
|
+
|
|
20
|
+
// TODO: correctly encode option type for view function
|
|
21
|
+
it('view function some value', async () => {
|
|
22
|
+
const payload = createViewPayload(OPTION_ABI, {
|
|
23
|
+
function: 'test_option_view',
|
|
24
|
+
arguments: [{ vec: ['50'] } as any],
|
|
25
|
+
type_arguments: [],
|
|
26
|
+
});
|
|
27
|
+
const result = await client.view(payload);
|
|
28
|
+
expect(result).toMatchInlineSnapshot(`
|
|
29
|
+
[
|
|
30
|
+
50n,
|
|
31
|
+
]
|
|
32
|
+
`);
|
|
33
|
+
}, 60000);
|
|
34
|
+
|
|
35
|
+
it('view function none value', async () => {
|
|
36
|
+
const payload = createViewPayload(OPTION_ABI, {
|
|
37
|
+
function: 'test_option_view',
|
|
38
|
+
arguments: [{ vec: [] } as any],
|
|
39
|
+
type_arguments: [],
|
|
40
|
+
});
|
|
41
|
+
const result = await client.view(payload);
|
|
42
|
+
expect(result).toMatchInlineSnapshot(`
|
|
43
|
+
[
|
|
44
|
+
0n,
|
|
45
|
+
]
|
|
46
|
+
`);
|
|
47
|
+
}, 60000);
|
|
48
|
+
|
|
49
|
+
it('entry function none value', async () => {
|
|
50
|
+
const payload = createEntryPayload(OPTION_ABI, {
|
|
51
|
+
function: 'test_option_entry',
|
|
52
|
+
arguments: [[]],
|
|
53
|
+
type_arguments: [],
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
const result = await client.simulateTransaction(payload, { account });
|
|
57
|
+
|
|
58
|
+
expect(result.hash).toBeDefined();
|
|
59
|
+
expect((result as any).payload).toMatchInlineSnapshot(`
|
|
60
|
+
{
|
|
61
|
+
"arguments": [
|
|
62
|
+
{
|
|
63
|
+
"vec": [],
|
|
64
|
+
},
|
|
65
|
+
],
|
|
66
|
+
"function": "0xf1ab5cd814ef1480b8c36466310d9c21d7758b54f6121872d1fb43887a40e7d8::test_option::test_option_entry",
|
|
67
|
+
"type": "entry_function_payload",
|
|
68
|
+
"type_arguments": [],
|
|
69
|
+
}
|
|
70
|
+
`);
|
|
71
|
+
}, 60000);
|
|
72
|
+
|
|
73
|
+
it('entry function some value', async () => {
|
|
74
|
+
const payload = createEntryPayload(OPTION_ABI, {
|
|
75
|
+
function: 'test_option_entry',
|
|
76
|
+
arguments: [[50]],
|
|
77
|
+
type_arguments: [],
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
const result = await client.simulateTransaction(payload, { account });
|
|
81
|
+
|
|
82
|
+
expect(result.hash).toBeDefined();
|
|
83
|
+
expect((result as any).payload).toMatchInlineSnapshot(`
|
|
84
|
+
{
|
|
85
|
+
"arguments": [
|
|
86
|
+
{
|
|
87
|
+
"vec": [
|
|
88
|
+
"50",
|
|
89
|
+
],
|
|
90
|
+
},
|
|
91
|
+
],
|
|
92
|
+
"function": "0xf1ab5cd814ef1480b8c36466310d9c21d7758b54f6121872d1fb43887a40e7d8::test_option::test_option_entry",
|
|
93
|
+
"type": "entry_function_payload",
|
|
94
|
+
"type_arguments": [],
|
|
95
|
+
}
|
|
96
|
+
`);
|
|
97
|
+
}, 60000);
|
|
98
|
+
|
|
99
|
+
it('entry function incorrect type', async () => {
|
|
100
|
+
// no need to run, only for type check
|
|
101
|
+
() => {
|
|
102
|
+
createEntryPayload(OPTION_ABI, {
|
|
103
|
+
function: 'test_option_entry',
|
|
104
|
+
// @ts-expect-error should be a vector
|
|
105
|
+
arguments: [{}],
|
|
106
|
+
type_arguments: [],
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
createEntryPayload(OPTION_ABI, {
|
|
110
|
+
function: 'test_option_entry',
|
|
111
|
+
// @ts-expect-error should be a vector with length 0 or 1
|
|
112
|
+
arguments: [[1, 2]],
|
|
113
|
+
type_arguments: [],
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
}, 60000);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
const OPTION_ABI = {
|
|
120
|
+
address: '0xf1ab5cd814ef1480b8c36466310d9c21d7758b54f6121872d1fb43887a40e7d8',
|
|
121
|
+
name: 'test_option',
|
|
122
|
+
friends: [],
|
|
123
|
+
exposed_functions: [
|
|
124
|
+
{
|
|
125
|
+
name: 'test_option_entry',
|
|
126
|
+
visibility: 'public',
|
|
127
|
+
is_entry: true,
|
|
128
|
+
is_view: false,
|
|
129
|
+
generic_type_params: [],
|
|
130
|
+
params: ['0x1::option::Option<u64>'],
|
|
131
|
+
return: [],
|
|
132
|
+
},
|
|
133
|
+
{
|
|
134
|
+
name: 'test_option_view',
|
|
135
|
+
visibility: 'public',
|
|
136
|
+
is_entry: false,
|
|
137
|
+
is_view: true,
|
|
138
|
+
generic_type_params: [],
|
|
139
|
+
params: ['0x1::option::Option<u64>'],
|
|
140
|
+
return: ['u64'],
|
|
141
|
+
},
|
|
142
|
+
],
|
|
143
|
+
structs: [],
|
|
144
|
+
} as const;
|
|
@@ -1,11 +1,15 @@
|
|
|
1
|
-
import { BCS, TxnBuilderTypes, TypeTagParser } from
|
|
2
|
-
import { ensureBigInt, ensureBoolean, ensureNumber } from
|
|
3
|
-
import type {
|
|
4
|
-
|
|
1
|
+
import { BCS, TxnBuilderTypes, TypeTagParser } from 'aptos';
|
|
2
|
+
import { ensureBigInt, ensureBoolean, ensureNumber } from '../ensureTypes.js';
|
|
3
|
+
import type {
|
|
4
|
+
ABIRoot,
|
|
5
|
+
EntryPayload,
|
|
6
|
+
EntryRequestPayload,
|
|
7
|
+
} from '../types/index.js';
|
|
8
|
+
import { EntryFunctionName } from '../types/extractor/functionExtractor.js';
|
|
5
9
|
|
|
6
10
|
/**
|
|
7
11
|
* Create a payload for calling a entry function.
|
|
8
|
-
*
|
|
12
|
+
*
|
|
9
13
|
* @param abi The ABI JSON contains the entry function. For type inference and encoding/decoding purpose.
|
|
10
14
|
* @param payload.function The function name.
|
|
11
15
|
* @param payload.arguments The input arguments for function.
|
|
@@ -17,130 +21,167 @@ import type { EntryFunctionName, EntryRequestPayload } from "../types/common.js"
|
|
|
17
21
|
* arguments: ['0x1', 1],
|
|
18
22
|
* type_arguments: ['0x1::aptos_coin::AptosCoin'],
|
|
19
23
|
* });
|
|
20
|
-
*
|
|
24
|
+
*
|
|
21
25
|
* const { hash } = await client.submitTransaction(
|
|
22
|
-
* entryPayload,
|
|
26
|
+
* entryPayload,
|
|
23
27
|
* { account },
|
|
24
28
|
* );
|
|
25
29
|
*/
|
|
26
30
|
export function createEntryPayload<
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
>(
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
)
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
31
|
+
T extends ABIRoot,
|
|
32
|
+
TFuncName extends EntryFunctionName<T>,
|
|
33
|
+
>(abi: T, payload: EntryRequestPayload<T, TFuncName>): EntryPayload {
|
|
34
|
+
// TODO: remove unused variables
|
|
35
|
+
const fnAbi = abi.exposed_functions.filter(
|
|
36
|
+
(f) => f.name === payload.function,
|
|
37
|
+
)[0]!;
|
|
38
|
+
const typeArguments: string[] = payload.type_arguments as any[];
|
|
39
|
+
const valArguments: any[] = payload.arguments as any[];
|
|
40
|
+
const abiArgs =
|
|
41
|
+
fnAbi.params[0] === '&signer'
|
|
42
|
+
? (fnAbi.params as string[]).slice(1)
|
|
43
|
+
: (fnAbi.params as string[]);
|
|
40
44
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
+
// Validations
|
|
46
|
+
if (fnAbi === undefined)
|
|
47
|
+
throw new Error(`Function ${payload.function} not found in ABI`);
|
|
48
|
+
if (abiArgs.length !== valArguments.length)
|
|
49
|
+
throw new Error(
|
|
50
|
+
`Function ${payload.function} expects ${fnAbi.params.length} arguments, but ${payload.arguments.length} were provided`,
|
|
51
|
+
);
|
|
52
|
+
if (fnAbi.generic_type_params.length !== typeArguments.length)
|
|
53
|
+
throw new Error(
|
|
54
|
+
`Function ${payload.function} expects ${fnAbi.generic_type_params.length} type arguments, but ${payload.type_arguments.length} were provided`,
|
|
55
|
+
);
|
|
45
56
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
57
|
+
// TODO: make entryRequest lazy
|
|
58
|
+
return {
|
|
59
|
+
rawPayload: {
|
|
60
|
+
...payload,
|
|
61
|
+
function: `${abi.address}::${abi.name}::${payload.function}`,
|
|
62
|
+
} as any,
|
|
63
|
+
entryRequest: TxnBuilderTypes.EntryFunction.natural(
|
|
64
|
+
`${abi.address}::${abi.name}`, // module id
|
|
65
|
+
payload.function, // function name
|
|
66
|
+
typeArguments // type arguments
|
|
67
|
+
.map((arg) => {
|
|
68
|
+
// The StructTag.fromString not support nested struct tag before aptos@1.8.4.
|
|
69
|
+
// So we use the TypeTagParser to parse the string literal into a TypeTagStruct
|
|
70
|
+
// For better compatibility.
|
|
71
|
+
// The next line of code is simpler, but not compatible with aptos below 1.8.3.
|
|
72
|
+
// return new TxnBuilderTypes.TypeTagStruct(TxnBuilderTypes.StructTag.fromString(arg));
|
|
62
73
|
|
|
63
|
-
|
|
64
|
-
|
|
74
|
+
// Use the TypeTagParser to parse the string literal into a TypeTagStruct
|
|
75
|
+
const typeTagStruct = new TypeTagParser(
|
|
76
|
+
arg,
|
|
77
|
+
).parseTypeTag() as TxnBuilderTypes.TypeTagStruct;
|
|
65
78
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
79
|
+
// Convert and return as a StructTag
|
|
80
|
+
return new TxnBuilderTypes.TypeTagStruct(
|
|
81
|
+
new TxnBuilderTypes.StructTag(
|
|
82
|
+
typeTagStruct.value.address,
|
|
83
|
+
typeTagStruct.value.module_name,
|
|
84
|
+
typeTagStruct.value.name,
|
|
85
|
+
typeTagStruct.value.type_args,
|
|
86
|
+
),
|
|
87
|
+
);
|
|
88
|
+
}),
|
|
89
|
+
valArguments.map(
|
|
90
|
+
// arguments
|
|
91
|
+
(arg, i) => {
|
|
92
|
+
const type = abiArgs[i]!;
|
|
93
|
+
const serializer = new BCS.Serializer();
|
|
94
|
+
argToBCS(type, arg, serializer);
|
|
95
|
+
return serializer.getBytes();
|
|
96
|
+
},
|
|
97
|
+
),
|
|
98
|
+
),
|
|
99
|
+
};
|
|
84
100
|
}
|
|
85
101
|
|
|
86
|
-
|
|
87
102
|
function argToBCS(type: string, arg: any, serializer: BCS.Serializer) {
|
|
103
|
+
const vectorRegex = /vector<([^]+)>/;
|
|
104
|
+
const vectorMatch = type.match(vectorRegex);
|
|
105
|
+
if (vectorMatch) {
|
|
106
|
+
// It's vector
|
|
107
|
+
const innerType = vectorMatch[1]!;
|
|
108
|
+
if (innerType === 'u8') {
|
|
109
|
+
if (arg instanceof Uint8Array) {
|
|
110
|
+
// TODO: add type support for Uint8Array
|
|
111
|
+
serializer.serializeBytes(arg);
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (typeof arg === 'string') {
|
|
116
|
+
// TODO: add type support for string
|
|
117
|
+
serializer.serializeStr(arg);
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
88
121
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
const innerType = match[1]!;
|
|
93
|
-
if (innerType === 'u8') {
|
|
94
|
-
if (arg instanceof Uint8Array) { // TODO: add type support for Uint8Array
|
|
95
|
-
serializer.serializeBytes(arg);
|
|
96
|
-
return;
|
|
97
|
-
}
|
|
122
|
+
if (!Array.isArray(arg)) {
|
|
123
|
+
throw new Error('Invalid vector args.');
|
|
124
|
+
}
|
|
98
125
|
|
|
99
|
-
|
|
100
|
-
serializer.serializeStr(arg);
|
|
101
|
-
return;
|
|
102
|
-
}
|
|
103
|
-
}
|
|
126
|
+
serializer.serializeU32AsUleb128(arg.length);
|
|
104
127
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
128
|
+
arg.forEach((arg) => argToBCS(innerType, arg, serializer));
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
108
131
|
|
|
109
|
-
|
|
132
|
+
const objectRegex = /0x1::object::Object<([^]+)>/;
|
|
133
|
+
const objectMatch = type.match(objectRegex);
|
|
134
|
+
if (objectMatch) {
|
|
135
|
+
// It's 0x1::object::Object
|
|
136
|
+
TxnBuilderTypes.AccountAddress.fromHex(arg as string).serialize(serializer);
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
110
139
|
|
|
111
|
-
|
|
112
|
-
|
|
140
|
+
const optionRegex = /0x1::option::Option<([^]+)>/;
|
|
141
|
+
const optionMatch = type.match(optionRegex);
|
|
142
|
+
if (optionMatch) { // It's 0x1::option::Option
|
|
143
|
+
const innerType = optionMatch[1]!;
|
|
144
|
+
serializer.serializeU32AsUleb128(arg.length);
|
|
145
|
+
if(!(arg instanceof Array) || arg.length > 1) {
|
|
146
|
+
throw new Error('Invalid input value for 0x1::option::Option.');
|
|
113
147
|
}
|
|
148
|
+
arg.forEach((arg) => argToBCS(innerType, arg, serializer));
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
114
151
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
152
|
+
switch (
|
|
153
|
+
type // It's primitive
|
|
154
|
+
) {
|
|
155
|
+
case 'bool':
|
|
156
|
+
serializer.serializeBool(ensureBoolean(arg));
|
|
157
|
+
break;
|
|
158
|
+
case 'address':
|
|
159
|
+
TxnBuilderTypes.AccountAddress.fromHex(arg as string).serialize(
|
|
160
|
+
serializer,
|
|
161
|
+
);
|
|
162
|
+
break;
|
|
163
|
+
case 'u8':
|
|
164
|
+
serializer.serializeU8(ensureNumber(arg));
|
|
165
|
+
break;
|
|
166
|
+
case 'u16':
|
|
167
|
+
serializer.serializeU16(ensureNumber(arg));
|
|
168
|
+
break;
|
|
169
|
+
case 'u32':
|
|
170
|
+
serializer.serializeU32(ensureNumber(arg));
|
|
171
|
+
break;
|
|
172
|
+
case 'u64':
|
|
173
|
+
serializer.serializeU64(ensureBigInt(arg));
|
|
174
|
+
break;
|
|
175
|
+
case 'u128':
|
|
176
|
+
serializer.serializeU128(ensureBigInt(arg));
|
|
177
|
+
break;
|
|
178
|
+
case 'u256':
|
|
179
|
+
serializer.serializeU256(ensureBigInt(arg));
|
|
180
|
+
break;
|
|
181
|
+
case '0x1::string::String':
|
|
182
|
+
serializer.serializeStr(arg as string);
|
|
183
|
+
break;
|
|
184
|
+
default:
|
|
185
|
+
throw new Error(`type "${type}" not supported`);
|
|
186
|
+
}
|
|
146
187
|
}
|