@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.
Files changed (157) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/README.md +5 -2
  3. package/build/cjs/core/Client.js +9 -11
  4. package/build/cjs/core/Client.js.map +1 -1
  5. package/build/cjs/core/WalletClient.js +3 -3
  6. package/build/cjs/core/WalletClient.js.map +1 -1
  7. package/build/cjs/core/createEntryPayload.js +25 -8
  8. package/build/cjs/core/createEntryPayload.js.map +1 -1
  9. package/build/cjs/core/createViewPayload.js +11 -8
  10. package/build/cjs/core/createViewPayload.js.map +1 -1
  11. package/build/cjs/hooks/index.js.map +1 -1
  12. package/build/cjs/hooks/useSubmitTransaction.js +8 -6
  13. package/build/cjs/hooks/useSubmitTransaction.js.map +1 -1
  14. package/build/cjs/hooks/useWalletClient.js +3 -1
  15. package/build/cjs/hooks/useWalletClient.js.map +1 -1
  16. package/build/cjs/types/client/abiClient.js.map +1 -0
  17. package/build/{esm/types → cjs/types/client}/client.js.map +1 -1
  18. package/build/cjs/types/client/walletClient.js.map +1 -0
  19. package/build/cjs/types/convertor/argsConvertor.js +3 -0
  20. package/build/cjs/types/convertor/argsConvertor.js.map +1 -0
  21. package/build/cjs/types/convertor/genericConvertor.js +3 -0
  22. package/build/cjs/types/convertor/genericConvertor.js.map +1 -0
  23. package/build/cjs/types/convertor/returnConvertor.js +3 -0
  24. package/build/cjs/types/convertor/returnConvertor.js.map +1 -0
  25. package/build/cjs/types/convertor/structConvertor.js +3 -0
  26. package/build/cjs/types/convertor/structConvertor.js.map +1 -0
  27. package/build/cjs/types/extractor/functionExtractor.js +3 -0
  28. package/build/cjs/types/extractor/functionExtractor.js.map +1 -0
  29. package/build/cjs/types/extractor/structExtractor.js +3 -0
  30. package/build/cjs/types/extractor/structExtractor.js.map +1 -0
  31. package/build/cjs/types/{struct.js → moveTypes.js} +1 -1
  32. package/build/cjs/types/moveTypes.js.map +1 -0
  33. package/build/esm/core/Client.js +12 -14
  34. package/build/esm/core/Client.js.map +1 -1
  35. package/build/esm/core/WalletClient.js +5 -5
  36. package/build/esm/core/WalletClient.js.map +1 -1
  37. package/build/esm/core/createEntryPayload.js +27 -10
  38. package/build/esm/core/createEntryPayload.js.map +1 -1
  39. package/build/esm/core/createViewPayload.js +13 -10
  40. package/build/esm/core/createViewPayload.js.map +1 -1
  41. package/build/esm/core/index.js +4 -4
  42. package/build/esm/hooks/index.js +2 -2
  43. package/build/esm/hooks/index.js.map +1 -1
  44. package/build/esm/hooks/useSubmitTransaction.js +11 -9
  45. package/build/esm/hooks/useSubmitTransaction.js.map +1 -1
  46. package/build/esm/hooks/useWalletClient.js +5 -3
  47. package/build/esm/hooks/useWalletClient.js.map +1 -1
  48. package/build/esm/types/client/abiClient.js.map +1 -0
  49. package/build/{cjs/types → esm/types/client}/client.js.map +1 -1
  50. package/build/esm/types/client/walletClient.js.map +1 -0
  51. package/build/esm/types/convertor/argsConvertor.js +2 -0
  52. package/build/esm/types/convertor/argsConvertor.js.map +1 -0
  53. package/build/esm/types/convertor/genericConvertor.js +2 -0
  54. package/build/esm/types/convertor/genericConvertor.js.map +1 -0
  55. package/build/esm/types/convertor/returnConvertor.js +2 -0
  56. package/build/esm/types/convertor/returnConvertor.js.map +1 -0
  57. package/build/esm/types/convertor/structConvertor.js +2 -0
  58. package/build/esm/types/convertor/structConvertor.js.map +1 -0
  59. package/build/esm/types/extractor/functionExtractor.js +2 -0
  60. package/build/esm/types/extractor/functionExtractor.js.map +1 -0
  61. package/build/esm/types/extractor/structExtractor.js +2 -0
  62. package/build/esm/types/extractor/structExtractor.js.map +1 -0
  63. package/build/esm/types/moveTypes.js +2 -0
  64. package/build/esm/types/moveTypes.js.map +1 -0
  65. package/build/types/core/Client.d.ts +3 -5
  66. package/build/types/core/Client.d.ts.map +1 -1
  67. package/build/types/core/WalletClient.d.ts +2 -3
  68. package/build/types/core/WalletClient.d.ts.map +1 -1
  69. package/build/types/core/createEntryPayload.d.ts +2 -2
  70. package/build/types/core/createEntryPayload.d.ts.map +1 -1
  71. package/build/types/core/createViewPayload.d.ts +1 -2
  72. package/build/types/core/createViewPayload.d.ts.map +1 -1
  73. package/build/types/core/index.d.ts +4 -4
  74. package/build/types/hooks/index.d.ts +2 -2
  75. package/build/types/hooks/index.d.ts.map +1 -1
  76. package/build/types/hooks/useSubmitTransaction.d.ts +2 -2
  77. package/build/types/hooks/useSubmitTransaction.d.ts.map +1 -1
  78. package/build/types/hooks/useWalletClient.d.ts +1 -1
  79. package/build/types/hooks/useWalletClient.d.ts.map +1 -1
  80. package/build/types/types/abi.d.ts +4 -1
  81. package/build/types/types/abi.d.ts.map +1 -1
  82. package/build/types/types/client/abiClient.d.ts +33 -0
  83. package/build/types/types/client/abiClient.d.ts.map +1 -0
  84. package/build/types/types/client/client.d.ts +54 -0
  85. package/build/types/types/client/client.d.ts.map +1 -0
  86. package/build/types/types/client/walletClient.d.ts +13 -0
  87. package/build/types/types/client/walletClient.d.ts.map +1 -0
  88. package/build/types/types/common.d.ts +7 -59
  89. package/build/types/types/common.d.ts.map +1 -1
  90. package/build/types/types/convertor/argsConvertor.d.ts +21 -0
  91. package/build/types/types/convertor/argsConvertor.d.ts.map +1 -0
  92. package/build/types/types/convertor/genericConvertor.d.ts +8 -0
  93. package/build/types/types/convertor/genericConvertor.d.ts.map +1 -0
  94. package/build/types/types/convertor/returnConvertor.d.ts +21 -0
  95. package/build/types/types/convertor/returnConvertor.d.ts.map +1 -0
  96. package/build/types/types/convertor/structConvertor.d.ts +19 -0
  97. package/build/types/types/convertor/structConvertor.d.ts.map +1 -0
  98. package/build/types/types/defaultABITable.d.ts +10 -6
  99. package/build/types/types/defaultABITable.d.ts.map +1 -1
  100. package/build/types/types/extractor/functionExtractor.d.ts +52 -0
  101. package/build/types/types/extractor/functionExtractor.d.ts.map +1 -0
  102. package/build/types/types/extractor/structExtractor.d.ts +35 -0
  103. package/build/types/types/extractor/structExtractor.d.ts.map +1 -0
  104. package/build/types/types/index.d.ts +6 -5
  105. package/build/types/types/index.d.ts.map +1 -1
  106. package/build/types/types/moveTypes.d.ts +9 -0
  107. package/build/types/types/moveTypes.d.ts.map +1 -0
  108. package/package.json +4 -4
  109. package/src/core/Client.ts +230 -223
  110. package/src/core/WalletClient.ts +57 -55
  111. package/src/core/__tests__/createEntryPayload.test.ts +19 -0
  112. package/src/core/__tests__/option.test.ts +144 -0
  113. package/src/core/createEntryPayload.ts +153 -112
  114. package/src/core/createViewPayload.ts +112 -100
  115. package/src/core/index.ts +4 -4
  116. package/src/hooks/index.ts +5 -2
  117. package/src/hooks/useSubmitTransaction.ts +101 -98
  118. package/src/hooks/useWalletClient.ts +10 -8
  119. package/src/types/abi.ts +24 -21
  120. package/src/types/client/abiClient.ts +47 -0
  121. package/src/types/client/client.ts +73 -0
  122. package/src/types/client/walletClient.ts +18 -0
  123. package/src/types/common.ts +13 -154
  124. package/src/types/convertor/argsConvertor.ts +59 -0
  125. package/src/types/convertor/genericConvertor.ts +9 -0
  126. package/src/types/convertor/returnConvertor.ts +55 -0
  127. package/src/types/convertor/structConvertor.ts +82 -0
  128. package/src/types/defaultABITable.ts +17 -12
  129. package/src/types/extractor/functionExtractor.ts +86 -0
  130. package/src/types/extractor/structExtractor.ts +67 -0
  131. package/src/types/index.ts +20 -9
  132. package/src/types/moveTypes.ts +22 -0
  133. package/build/cjs/types/abiClient.js.map +0 -1
  134. package/build/cjs/types/struct.js.map +0 -1
  135. package/build/cjs/types/walletClient.js.map +0 -1
  136. package/build/esm/types/abiClient.js.map +0 -1
  137. package/build/esm/types/struct.js +0 -2
  138. package/build/esm/types/struct.js.map +0 -1
  139. package/build/esm/types/walletClient.js.map +0 -1
  140. package/build/types/types/abiClient.d.ts +0 -28
  141. package/build/types/types/abiClient.d.ts.map +0 -1
  142. package/build/types/types/client.d.ts +0 -24
  143. package/build/types/types/client.d.ts.map +0 -1
  144. package/build/types/types/struct.d.ts +0 -30
  145. package/build/types/types/struct.d.ts.map +0 -1
  146. package/build/types/types/walletClient.d.ts +0 -9
  147. package/build/types/types/walletClient.d.ts.map +0 -1
  148. package/src/types/abiClient.ts +0 -38
  149. package/src/types/client.ts +0 -32
  150. package/src/types/struct.ts +0 -87
  151. package/src/types/walletClient.ts +0 -10
  152. /package/build/cjs/types/{abiClient.js → client/abiClient.js} +0 -0
  153. /package/build/cjs/types/{client.js → client/client.js} +0 -0
  154. /package/build/cjs/types/{walletClient.js → client/walletClient.js} +0 -0
  155. /package/build/esm/types/{abiClient.js → client/abiClient.js} +0 -0
  156. /package/build/esm/types/{client.js → client/client.js} +0 -0
  157. /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 "aptos";
2
- import { ensureBigInt, ensureBoolean, ensureNumber } from "../ensureTypes.js";
3
- import type { ABIRoot, EntryPayload } from "../types/index.js";
4
- import type { EntryFunctionName, EntryRequestPayload } from "../types/common.js";
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
- T extends ABIRoot,
28
- TFuncName extends EntryFunctionName<T>
29
- >(
30
- abi: T,
31
- payload: EntryRequestPayload<T, TFuncName>
32
- ): EntryPayload {
33
- // TODO: remove unused variables
34
- const fnAbi = abi.exposed_functions.filter(f => f.name === payload.function)[0]!;
35
- const typeArguments: string[] = payload.type_arguments as any[];
36
- const valArguments: any[] = payload.arguments as any[];
37
- const abiArgs = fnAbi.params[0] === '&signer'
38
- ? (fnAbi.params as string[]).slice(1)
39
- : fnAbi.params as string[];
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
- // Validations
42
- if (fnAbi === undefined) throw new Error(`Function ${payload.function} not found in ABI`);
43
- if (abiArgs.length !== valArguments.length) throw new Error(`Function ${payload.function} expects ${fnAbi.params.length} arguments, but ${payload.arguments.length} were provided`);
44
- if (fnAbi.generic_type_params.length !== typeArguments.length) throw new Error(`Function ${payload.function} expects ${fnAbi.generic_type_params.length} type arguments, but ${payload.type_arguments.length} were provided`);
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
- // TODO: make entryRequest lazy
47
- return {
48
- rawPayload: {
49
- ...payload,
50
- function: `${abi.address}::${abi.name}::${payload.function}`,
51
- } as any,
52
- entryRequest: TxnBuilderTypes.EntryFunction.natural(
53
- `${abi.address}::${abi.name}`, // module id
54
- payload.function, // function name
55
- typeArguments // type arguments
56
- .map((arg) => {
57
- // The StructTag.fromString not support nested struct tag before aptos@1.8.4.
58
- // So we use the TypeTagParser to parse the string literal into a TypeTagStruct
59
- // For better compatibility.
60
- // The next line of code is simpler, but not compatible with aptos below 1.8.3.
61
- // return new TxnBuilderTypes.TypeTagStruct(TxnBuilderTypes.StructTag.fromString(arg));
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
- // Use the TypeTagParser to parse the string literal into a TypeTagStruct
64
- const typeTagStruct = new TypeTagParser(arg).parseTypeTag() as TxnBuilderTypes.TypeTagStruct;
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
- // Convert and return as a StructTag
67
- return new TxnBuilderTypes.TypeTagStruct(new TxnBuilderTypes.StructTag(
68
- typeTagStruct.value.address,
69
- typeTagStruct.value.module_name,
70
- typeTagStruct.value.name,
71
- typeTagStruct.value.type_args,
72
- ));
73
- }),
74
- valArguments.map( // arguments
75
- (arg, i) => {
76
- const type = abiArgs[i]!;
77
- const serializer = new BCS.Serializer();
78
- argToBCS(type, arg, serializer);
79
- return serializer.getBytes();
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
- const regex = /vector<([^]+)>/;
90
- const match = type.match(regex);
91
- if (match) { // It's vector
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
- if (typeof arg === "string") { // TODO: add type support for string
100
- serializer.serializeStr(arg);
101
- return;
102
- }
103
- }
126
+ serializer.serializeU32AsUleb128(arg.length);
104
127
 
105
- if (!Array.isArray(arg)) {
106
- throw new Error("Invalid vector args.");
107
- }
128
+ arg.forEach((arg) => argToBCS(innerType, arg, serializer));
129
+ return;
130
+ }
108
131
 
109
- serializer.serializeU32AsUleb128(arg.length);
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
- arg.forEach((arg) => argToBCS(innerType, arg, serializer));
112
- return;
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
- switch (type) { // It's primitive
116
- case 'bool':
117
- serializer.serializeBool(ensureBoolean(arg));
118
- break;
119
- case 'address':
120
- TxnBuilderTypes.AccountAddress.fromHex(arg as string).serialize(serializer);
121
- break;
122
- case 'u8':
123
- serializer.serializeU8(ensureNumber(arg));
124
- break;
125
- case 'u16':
126
- serializer.serializeU16(ensureNumber(arg));
127
- break;
128
- case 'u32':
129
- serializer.serializeU32(ensureNumber(arg));
130
- break;
131
- case 'u64':
132
- serializer.serializeU64(ensureBigInt(arg));
133
- break;
134
- case 'u128':
135
- serializer.serializeU128(ensureBigInt(arg));
136
- break;
137
- case 'u256':
138
- serializer.serializeU256(ensureBigInt(arg));
139
- break;
140
- case '0x1::string::String':
141
- serializer.serializeStr(arg as string);
142
- break;
143
- default:
144
- throw new Error(`type "${type}" not supported`);
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
  }