@thalalabs/surf 0.0.3 → 0.0.5

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 (77) hide show
  1. package/.changeset/README.md +4 -4
  2. package/CHANGELOG.md +12 -0
  3. package/README.md +12 -8
  4. package/build/cjs/core/Client.js +10 -5
  5. package/build/cjs/core/Client.js.map +1 -1
  6. package/build/cjs/core/WalletClient.js +14 -4
  7. package/build/cjs/core/WalletClient.js.map +1 -1
  8. package/build/cjs/core/createEntryPayload.js +8 -8
  9. package/build/cjs/core/createEntryPayload.js.map +1 -1
  10. package/build/cjs/core/createViewPayload.js +31 -12
  11. package/build/cjs/core/createViewPayload.js.map +1 -1
  12. package/build/cjs/core/index.js +9 -9
  13. package/build/cjs/core/index.js.map +1 -1
  14. package/build/cjs/hooks/index.js +4 -4
  15. package/build/cjs/hooks/index.js.map +1 -1
  16. package/build/cjs/hooks/useWalletClient.js +2 -2
  17. package/build/cjs/hooks/useWalletClient.js.map +1 -1
  18. package/build/cjs/index.js +4 -4
  19. package/build/cjs/index.js.map +1 -1
  20. package/build/esm/core/Client.js +8 -3
  21. package/build/esm/core/Client.js.map +1 -1
  22. package/build/esm/core/WalletClient.js +13 -3
  23. package/build/esm/core/WalletClient.js.map +1 -1
  24. package/build/esm/core/createEntryPayload.js +1 -1
  25. package/build/esm/core/createEntryPayload.js.map +1 -1
  26. package/build/esm/core/createViewPayload.js +30 -11
  27. package/build/esm/core/createViewPayload.js.map +1 -1
  28. package/build/esm/core/index.js +4 -4
  29. package/build/esm/core/index.js.map +1 -1
  30. package/build/esm/hooks/index.js +2 -2
  31. package/build/esm/hooks/index.js.map +1 -1
  32. package/build/esm/hooks/useWalletClient.js +1 -1
  33. package/build/esm/hooks/useWalletClient.js.map +1 -1
  34. package/build/esm/index.js +1 -1
  35. package/build/esm/index.js.map +1 -1
  36. package/build/types/core/Client.d.ts +2 -3
  37. package/build/types/core/Client.d.ts.map +1 -1
  38. package/build/types/core/WalletClient.d.ts +3 -4
  39. package/build/types/core/WalletClient.d.ts.map +1 -1
  40. package/build/types/core/createEntryPayload.d.ts +2 -2
  41. package/build/types/core/createEntryPayload.d.ts.map +1 -1
  42. package/build/types/core/createViewPayload.d.ts +2 -2
  43. package/build/types/core/createViewPayload.d.ts.map +1 -1
  44. package/build/types/core/index.d.ts +4 -4
  45. package/build/types/core/index.d.ts.map +1 -1
  46. package/build/types/hooks/index.d.ts +2 -2
  47. package/build/types/hooks/index.d.ts.map +1 -1
  48. package/build/types/hooks/useSubmitTransaction.d.ts +4 -4
  49. package/build/types/hooks/useSubmitTransaction.d.ts.map +1 -1
  50. package/build/types/hooks/useWalletClient.d.ts +2 -2
  51. package/build/types/hooks/useWalletClient.d.ts.map +1 -1
  52. package/build/types/index.d.ts +2 -2
  53. package/build/types/index.d.ts.map +1 -1
  54. package/build/types/types/abiClient.d.ts +3 -3
  55. package/build/types/types/abiClient.d.ts.map +1 -1
  56. package/build/types/types/common.d.ts +1 -1
  57. package/build/types/types/common.d.ts.map +1 -1
  58. package/build/types/types/index.d.ts +4 -4
  59. package/build/types/types/index.d.ts.map +1 -1
  60. package/build/types/types/walletClient.d.ts +2 -2
  61. package/build/types/types/walletClient.d.ts.map +1 -1
  62. package/package.json +2 -1
  63. package/src/core/Client.ts +4 -5
  64. package/src/core/WalletClient.ts +4 -3
  65. package/src/core/__tests__/view.test.ts +15 -70
  66. package/src/core/__tests__/view_vector.test.ts +223 -0
  67. package/src/core/createEntryPayload.ts +6 -6
  68. package/src/core/createViewPayload.ts +37 -16
  69. package/src/core/index.ts +4 -4
  70. package/src/hooks/index.ts +2 -2
  71. package/src/hooks/useSubmitTransaction.ts +1 -1
  72. package/src/hooks/useWalletClient.ts +1 -1
  73. package/src/index.ts +3 -3
  74. package/src/types/abiClient.ts +11 -3
  75. package/src/types/common.ts +1 -1
  76. package/src/types/index.ts +5 -5
  77. package/src/types/walletClient.ts +2 -2
@@ -0,0 +1,223 @@
1
+ /**
2
+ * These test cases depends on network, it call the real contract.
3
+ */
4
+
5
+ import { createClient } from '../Client';
6
+ import { createViewPayload } from '../createViewPayload';
7
+
8
+ // TODO: add vector<address>, vector<struct>
9
+ describe('call view functions for vector type', () => {
10
+ const client = createClient({
11
+ nodeUrl: 'https://fullnode.testnet.aptoslabs.com/v1',
12
+ });
13
+
14
+ // Act before assertions
15
+ beforeAll(async () => {});
16
+
17
+ // Teardown (cleanup) after assertions
18
+ afterAll(() => {});
19
+
20
+ it('vector_bool', async () => {
21
+ const viewPayload = createViewPayload(TEST_ABI, {
22
+ function: 'test_view_function_bool',
23
+ arguments: [[true, false, false, true, true]],
24
+ type_arguments: [],
25
+ });
26
+ const result = await client.view(viewPayload);
27
+ expect(result).toMatchInlineSnapshot(`
28
+ [
29
+ 3,
30
+ ]
31
+ `);
32
+ }, 60000);
33
+
34
+ it('vector_u8', async () => {
35
+ const viewPayload = createViewPayload(TEST_ABI, {
36
+ function: 'test_view_function_u8',
37
+ arguments: [[1, 2, 3, 10, 50]],
38
+ type_arguments: [],
39
+ });
40
+ const result = await client.view(viewPayload);
41
+ expect(result).toMatchInlineSnapshot(`
42
+ [
43
+ 66,
44
+ ]
45
+ `);
46
+ }, 60000);
47
+
48
+ it('vector_u16', async () => {
49
+ const viewPayload = createViewPayload(TEST_ABI, {
50
+ function: 'test_view_function_u16',
51
+ arguments: [[256, 100]],
52
+ type_arguments: [],
53
+ });
54
+ const result = await client.view(viewPayload);
55
+ expect(result).toMatchInlineSnapshot(`
56
+ [
57
+ 356,
58
+ ]
59
+ `);
60
+ }, 60000);
61
+
62
+ it('vector_u32', async () => {
63
+ const viewPayload = createViewPayload(TEST_ABI, {
64
+ function: 'test_view_function_u32',
65
+ arguments: [[70000, 100]],
66
+ type_arguments: [],
67
+ });
68
+ const result = await client.view(viewPayload);
69
+ expect(result).toMatchInlineSnapshot(`
70
+ [
71
+ 70100,
72
+ ]
73
+ `);
74
+ }, 60000);
75
+
76
+ it('vector_u64', async () => {
77
+ const viewPayload = createViewPayload(TEST_ABI, {
78
+ function: 'test_view_function_u64',
79
+ arguments: [[BigInt('4294967296'), 100]],
80
+ type_arguments: [],
81
+ });
82
+ const result = await client.view(viewPayload);
83
+ expect(result).toMatchInlineSnapshot(`
84
+ [
85
+ 4294967396n,
86
+ ]
87
+ `);
88
+ }, 60000);
89
+
90
+ it('vector_u256', async () => {
91
+ const viewPayload = createViewPayload(TEST_ABI, {
92
+ function: 'test_view_function_u256',
93
+ arguments: [[BigInt('4294967296'), 100]],
94
+ type_arguments: [],
95
+ });
96
+ const result = await client.view(viewPayload);
97
+ expect(result).toMatchInlineSnapshot(`
98
+ [
99
+ 4294967396n,
100
+ ]
101
+ `);
102
+ }, 60000);
103
+
104
+ it('return vector', async () => {
105
+ const viewPayload = createViewPayload(TEST_ABI, {
106
+ function: 'test_view_function_u64_return_vector',
107
+ arguments: [[BigInt('4294967296'), 100]],
108
+ type_arguments: [],
109
+ });
110
+ const result = await client.view(viewPayload);
111
+ expect(result).toMatchInlineSnapshot(`
112
+ [
113
+ [
114
+ 4294967296n,
115
+ 100n,
116
+ ],
117
+ ]
118
+ `);
119
+ }, 60000);
120
+ });
121
+
122
+ const TEST_ABI = {
123
+ address: '0xf1ab5cd814ef1480b8c36466310d9c21d7758b54f6121872d1fb43887a40e7d8',
124
+ name: 'test',
125
+ friends: [],
126
+ exposed_functions: [
127
+ {
128
+ name: 'test_run_function',
129
+ visibility: 'public',
130
+ is_entry: true,
131
+ is_view: false,
132
+ generic_type_params: [],
133
+ params: ['&signer', 'vector<u8>'],
134
+ return: [],
135
+ },
136
+ {
137
+ name: 'test_view_function_bool',
138
+ visibility: 'public',
139
+ is_entry: false,
140
+ is_view: true,
141
+ generic_type_params: [],
142
+ params: ['vector<bool>'],
143
+ return: ['u32'],
144
+ },
145
+ {
146
+ name: 'test_view_function_u128',
147
+ visibility: 'public',
148
+ is_entry: false,
149
+ is_view: true,
150
+ generic_type_params: [],
151
+ params: ['vector<u128>'],
152
+ return: ['u128'],
153
+ },
154
+ {
155
+ name: 'test_view_function_u16',
156
+ visibility: 'public',
157
+ is_entry: false,
158
+ is_view: true,
159
+ generic_type_params: [],
160
+ params: ['vector<u16>'],
161
+ return: ['u16'],
162
+ },
163
+ {
164
+ name: 'test_view_function_u256',
165
+ visibility: 'public',
166
+ is_entry: false,
167
+ is_view: true,
168
+ generic_type_params: [],
169
+ params: ['vector<u256>'],
170
+ return: ['u256'],
171
+ },
172
+ {
173
+ name: 'test_view_function_u32',
174
+ visibility: 'public',
175
+ is_entry: false,
176
+ is_view: true,
177
+ generic_type_params: [],
178
+ params: ['vector<u32>'],
179
+ return: ['u32'],
180
+ },
181
+ {
182
+ name: 'test_view_function_u64',
183
+ visibility: 'public',
184
+ is_entry: false,
185
+ is_view: true,
186
+ generic_type_params: [],
187
+ params: ['vector<u64>'],
188
+ return: ['u64'],
189
+ },
190
+ {
191
+ name: 'test_view_function_u64_return_vector',
192
+ visibility: 'public',
193
+ is_entry: false,
194
+ is_view: true,
195
+ generic_type_params: [],
196
+ params: ['vector<u64>'],
197
+ return: ['vector<u64>'],
198
+ },
199
+ {
200
+ name: 'test_view_function_u8',
201
+ visibility: 'public',
202
+ is_entry: false,
203
+ is_view: true,
204
+ generic_type_params: [],
205
+ params: ['vector<u8>'],
206
+ return: ['u8'],
207
+ },
208
+ ],
209
+ structs: [
210
+ {
211
+ name: 'RunFunctionStruct',
212
+ is_native: false,
213
+ abilities: ['key'],
214
+ generic_type_params: [],
215
+ fields: [
216
+ {
217
+ name: 'sum',
218
+ type: 'u8',
219
+ },
220
+ ],
221
+ },
222
+ ],
223
+ } as const;
@@ -1,7 +1,7 @@
1
1
  import { BCS, TxnBuilderTypes, TypeTagParser } from "aptos";
2
- import { ensureBigInt, ensureBoolean, ensureNumber } from "../ensureTypes";
3
- import { ABIRoot, EntryPayload } from "../types";
4
- import { EntryFunctionName, EntryRequestPayload } from "../types/common";
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";
5
5
 
6
6
  export function createEntryPayload<
7
7
  T extends ABIRoot,
@@ -11,7 +11,7 @@ export function createEntryPayload<
11
11
  payload: EntryRequestPayload<T, TFuncName>
12
12
  ): EntryPayload {
13
13
  // TODO: remove unused variables
14
- const fnAbi = abi.exposed_functions.filter(f => f.name === payload.function)[0];
14
+ const fnAbi = abi.exposed_functions.filter(f => f.name === payload.function)[0]!;
15
15
  const typeArguments: string[] = payload.type_arguments as any[];
16
16
  const valArguments: any[] = payload.arguments as any[];
17
17
  const abiArgs = fnAbi.params[0] === '&signer'
@@ -53,7 +53,7 @@ export function createEntryPayload<
53
53
  }),
54
54
  valArguments.map( // arguments
55
55
  (arg, i) => {
56
- const type = abiArgs[i];
56
+ const type = abiArgs[i]!;
57
57
  const serializer = new BCS.Serializer();
58
58
  argToBCS(type, arg, serializer);
59
59
  return serializer.getBytes();
@@ -69,7 +69,7 @@ function argToBCS(type: string, arg: any, serializer: BCS.Serializer) {
69
69
  const regex = /vector<([^]+)>/;
70
70
  const match = type.match(regex);
71
71
  if (match) { // It's vector
72
- const innerType = match[1];
72
+ const innerType = match[1]!;
73
73
  if (innerType === 'u8') {
74
74
  if (arg instanceof Uint8Array) { // TODO: add type support for Uint8Array
75
75
  serializer.serializeBytes(arg);
@@ -1,7 +1,7 @@
1
1
  import { HexString } from "aptos";
2
- import { ABIRoot, ViewPayload } from "../types";
3
- import { ExtractReturnType, ViewFunctionName, ViewRequestPayload } from "../types/common";
4
- import { ensureNumber } from "../ensureTypes";
2
+ import type { ABIRoot, ViewPayload } from "../types/index.js";
3
+ import type { ExtractReturnType, ViewFunctionName, ViewRequestPayload } from "../types/common.js";
4
+ import { ensureNumber } from "../ensureTypes.js";
5
5
 
6
6
  // TODO: support vector<u8> input with Uint8Array
7
7
  // TODO: support vector<u8> input with string
@@ -24,17 +24,18 @@ export function createViewPayload<
24
24
 
25
25
  // TODO: do serialization here
26
26
  const args = fnAbi.params.map((type, i) => {
27
+ const arg = payload.arguments[i]!;
27
28
  if (['u8', 'u16', 'u32'].includes(type)) {
28
- return ensureNumber(payload.arguments[i] as number);
29
+ return ensureNumber(arg as number);
29
30
  }
30
31
  else if (['u64', 'u128', 'u256'].includes(type)) {
31
- return payload.arguments[i].toString();
32
+ return arg.toString();
32
33
  }
33
34
  else if (type.includes("vector")) {
34
- return encodeVector(type, payload.arguments[i] as any[]);
35
+ return encodeVector(type, arg as any[]);
35
36
  }
36
37
  else {
37
- return payload.arguments[i];
38
+ return arg;
38
39
  }
39
40
  });
40
41
 
@@ -43,6 +44,9 @@ export function createViewPayload<
43
44
  if (['u64', 'u128', 'u256'].includes(type)) {
44
45
  return decodeBigint;
45
46
  }
47
+ else if (type.includes("vector")) {
48
+ return (value: any[]) => decodeVector(type, value)
49
+ }
46
50
  else {
47
51
  return null;
48
52
  }
@@ -62,15 +66,36 @@ function decodeBigint(value: string): bigint {
62
66
  return BigInt(value);
63
67
  }
64
68
 
65
- function encodeVector(type: string, value: any[]) {
69
+ function decodeVector(type: string, value: any[]) {
66
70
  const regex = /vector<([^]+)>/;
67
71
  const match = type.match(regex);
68
72
  if (!match) {
69
73
  // Should never happen
70
74
  throw new Error(`Unsupported type: ${type}`);
71
75
  }
76
+ const innerType = match[1]!;
77
+
78
+ if(["address", "bool", "u8", "u16", "u32"].includes(innerType)) {
79
+ return value;
80
+ }
81
+ else if (["u64", "u128", "u256"].includes(innerType)) {
82
+ return value.map((v: string) => BigInt(v));
83
+ } else {
84
+ // 1. TODO: Figure out how to decode Struct type
85
+ // 2. TODO: Figure out how to decode Vector of vector, vector of struct
86
+ return value;
87
+ }
88
+ }
72
89
 
73
- if (match[1] === "u8") {
90
+ function encodeVector(type: string, value: any[]) {
91
+ const regex = /vector<([^]+)>/;
92
+ const match = type.match(regex);
93
+ if (!match) {
94
+ // Should never happen
95
+ throw new Error(`Unsupported type: ${type}`);
96
+ }
97
+ const innerType = match[1]!;
98
+ if (innerType === "u8") {
74
99
  return (
75
100
  HexString.fromUint8Array(
76
101
  new Uint8Array(
@@ -83,18 +108,14 @@ function encodeVector(type: string, value: any[]) {
83
108
  ),
84
109
  ) as any
85
110
  ).hexString;
86
- } else if (["u16", "u32"].includes(match[1])) {
87
- // TODO: Figure out how to encode
111
+ } else if (["bool", "u16", "u32"].includes(innerType)) {
88
112
  return value;
89
- } else if (["u64", "u128", "u256"].includes(match[1])) {
90
- // TODO: Figure out how to encode
113
+ } else if (["u64", "u128", "u256"].includes(innerType)) {
91
114
  return value.map((v: bigint) => v.toString());
92
- } else if (match[1] === "bool") {
93
- // TODO: Figure out how to encode
94
- return value;
95
115
  } else {
96
116
  // 1. Address type no need to encode
97
117
  // 2. TODO: Figure out how to encode Struct type
118
+ // 3. TODO: Figure out how to encode Vector of vector, vector of struct
98
119
  return value;
99
120
  }
100
121
  }
package/src/core/index.ts CHANGED
@@ -1,4 +1,4 @@
1
- export { WalletClient as MoveTsWalletClient } from "./WalletClient";
2
- export { Client as MoveTsClient, createClient } from "./Client";
3
- export { createViewPayload } from "./createViewPayload";
4
- export { createEntryPayload } from "./createEntryPayload";
1
+ export { WalletClient as MoveTsWalletClient } from "./WalletClient.js";
2
+ export { Client as MoveTsClient, createClient } from "./Client.js";
3
+ export { createViewPayload } from "./createViewPayload.js";
4
+ export { createEntryPayload } from "./createEntryPayload.js";
@@ -1,2 +1,2 @@
1
- export { submitTransactionOptions, useSubmitTransaction } from "./useSubmitTransaction";
2
- export { useWalletClient } from "./useWalletClient";
1
+ export { submitTransactionOptions, useSubmitTransaction } from "./useSubmitTransaction.js";
2
+ export { useWalletClient } from "./useWalletClient.js";
@@ -1,7 +1,7 @@
1
1
  import { useState, useRef } from "react";
2
2
  import { useWallet } from "@aptos-labs/wallet-adapter-react";
3
3
  import { AptosClient, Types } from "aptos";
4
- import { EntryPayload } from "../types";
4
+ import type { EntryPayload } from "../types/index.js";
5
5
 
6
6
  export type submitTransactionOptions = {
7
7
  nodeUrl: string,
@@ -1,5 +1,5 @@
1
1
  import { useWallet } from "@aptos-labs/wallet-adapter-react";
2
- import { MoveTsWalletClient } from "../core";
2
+ import { MoveTsWalletClient } from "../core/index.js";
3
3
 
4
4
  export const useWalletClient = ({ nodeUrl }: { nodeUrl: string }) => {
5
5
  const wallet = useWallet();
package/src/index.ts CHANGED
@@ -2,9 +2,9 @@ export {
2
2
  createViewPayload,
3
3
  createEntryPayload,
4
4
  createClient
5
- } from "./core";
5
+ } from "./core/index.js";
6
6
 
7
- export {
7
+ export type {
8
8
  EntryPayload,
9
9
  ViewPayload,
10
- } from "./types";
10
+ } from "./types/index.js";
@@ -1,6 +1,14 @@
1
- import { AptosAccount } from "aptos";
2
- import { ABIRoot } from "./abi";
3
- import { EntryFunctionName, ExtractGenericParamsType, ExtractParamsType, ExtractParamsTypeOmitSigner, ExtractReturnType, TransactionResponse, ViewFunctionName } from "./common";
1
+ import type { AptosAccount } from "aptos";
2
+ import type { ABIRoot } from "./abi.js";
3
+ import type {
4
+ EntryFunctionName,
5
+ ExtractGenericParamsType,
6
+ ExtractParamsType,
7
+ ExtractParamsTypeOmitSigner,
8
+ ExtractReturnType,
9
+ TransactionResponse,
10
+ ViewFunctionName
11
+ } from "./common.js";
4
12
 
5
13
  export type ABIViewClient<T extends ABIRoot> = {
6
14
  [TFuncName in ViewFunctionName<T>]: (payload: {
@@ -1,4 +1,4 @@
1
- import { ABIRoot } from "./abi";
1
+ import type { ABIRoot } from "./abi.js";
2
2
 
3
3
  // TODO: rename this variable, not only primitive, but also struct and vector
4
4
  export type Primitive =
@@ -1,9 +1,9 @@
1
- export { ABIWalletClient } from "./walletClient";
2
- export { ABIViewClient, ABIEntryClient } from "./abiClient";
3
- export {
1
+ export type { ABIWalletClient } from "./walletClient.js";
2
+ export type { ABIViewClient, ABIEntryClient } from "./abiClient.js";
3
+ export type {
4
4
  ViewPayload,
5
5
  EntryPayload,
6
6
  ViewOptions,
7
7
  EntryOptions,
8
- } from "./client";
9
- export { ABIRoot } from "./abi";
8
+ } from "./client.js";
9
+ export type { ABIRoot } from "./abi.js";
@@ -1,5 +1,5 @@
1
- import { ABIRoot } from "./abi";
2
- import { EntryFunctionName, ExtractGenericParamsType, ExtractParamsTypeOmitSigner, TransactionResponse } from "./common";
1
+ import type { ABIRoot } from "./abi.js";
2
+ import { EntryFunctionName, ExtractGenericParamsType, ExtractParamsTypeOmitSigner, TransactionResponse } from "./common.js";
3
3
 
4
4
  export type ABIWalletClient<T extends ABIRoot> = {
5
5
  [TFuncName in EntryFunctionName<T>]: