@thalalabs/surf 0.0.16 → 1.0.0

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 (94) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/README.md +44 -45
  3. package/build/cjs/core/Client.js +55 -41
  4. package/build/cjs/core/Client.js.map +1 -1
  5. package/build/cjs/core/WalletClient.js +20 -31
  6. package/build/cjs/core/WalletClient.js.map +1 -1
  7. package/build/cjs/core/createEntryPayload.js +9 -91
  8. package/build/cjs/core/createEntryPayload.js.map +1 -1
  9. package/build/cjs/core/createViewPayload.js +26 -52
  10. package/build/cjs/core/createViewPayload.js.map +1 -1
  11. package/build/cjs/core/index.js +2 -2
  12. package/build/cjs/core/index.js.map +1 -1
  13. package/build/cjs/ensureTypes.js.map +1 -1
  14. package/build/cjs/hooks/index.js.map +1 -1
  15. package/build/cjs/hooks/useSubmitTransaction.js +25 -46
  16. package/build/cjs/hooks/useSubmitTransaction.js.map +1 -1
  17. package/build/cjs/hooks/useWalletClient.js +2 -2
  18. package/build/cjs/hooks/useWalletClient.js.map +1 -1
  19. package/build/cjs/index.js +2 -2
  20. package/build/cjs/index.js.map +1 -1
  21. package/build/esm/core/Client.js +53 -39
  22. package/build/esm/core/Client.js.map +1 -1
  23. package/build/esm/core/WalletClient.js +20 -31
  24. package/build/esm/core/WalletClient.js.map +1 -1
  25. package/build/esm/core/createEntryPayload.js +9 -91
  26. package/build/esm/core/createEntryPayload.js.map +1 -1
  27. package/build/esm/core/createViewPayload.js +26 -52
  28. package/build/esm/core/createViewPayload.js.map +1 -1
  29. package/build/esm/core/index.js +1 -1
  30. package/build/esm/core/index.js.map +1 -1
  31. package/build/esm/ensureTypes.js.map +1 -1
  32. package/build/esm/hooks/index.js.map +1 -1
  33. package/build/esm/hooks/useSubmitTransaction.js +25 -46
  34. package/build/esm/hooks/useSubmitTransaction.js.map +1 -1
  35. package/build/esm/hooks/useWalletClient.js +2 -2
  36. package/build/esm/hooks/useWalletClient.js.map +1 -1
  37. package/build/esm/index.js +1 -1
  38. package/build/esm/index.js.map +1 -1
  39. package/build/types/core/Client.d.ts +61 -48
  40. package/build/types/core/Client.d.ts.map +1 -1
  41. package/build/types/core/WalletClient.d.ts +3 -5
  42. package/build/types/core/WalletClient.d.ts.map +1 -1
  43. package/build/types/core/createEntryPayload.d.ts +8 -8
  44. package/build/types/core/createEntryPayload.d.ts.map +1 -1
  45. package/build/types/core/createViewPayload.d.ts +4 -4
  46. package/build/types/core/createViewPayload.d.ts.map +1 -1
  47. package/build/types/core/index.d.ts +1 -1
  48. package/build/types/core/index.d.ts.map +1 -1
  49. package/build/types/ensureTypes.d.ts.map +1 -1
  50. package/build/types/hooks/index.d.ts +1 -1
  51. package/build/types/hooks/index.d.ts.map +1 -1
  52. package/build/types/hooks/useSubmitTransaction.d.ts +3 -11
  53. package/build/types/hooks/useSubmitTransaction.d.ts.map +1 -1
  54. package/build/types/hooks/useWalletClient.d.ts +1 -3
  55. package/build/types/hooks/useWalletClient.d.ts.map +1 -1
  56. package/build/types/index.d.ts +1 -1
  57. package/build/types/index.d.ts.map +1 -1
  58. package/build/types/types/abi.d.ts.map +1 -1
  59. package/build/types/types/client/abiClient.d.ts +12 -16
  60. package/build/types/types/client/abiClient.d.ts.map +1 -1
  61. package/build/types/types/client/client.d.ts +11 -23
  62. package/build/types/types/client/client.d.ts.map +1 -1
  63. package/build/types/types/convertor/argsConvertor.d.ts +1 -1
  64. package/build/types/types/convertor/argsConvertor.d.ts.map +1 -1
  65. package/build/types/types/convertor/genericConvertor.d.ts.map +1 -1
  66. package/build/types/types/convertor/returnConvertor.d.ts +1 -1
  67. package/build/types/types/index.d.ts +1 -1
  68. package/build/types/types/index.d.ts.map +1 -1
  69. package/package.json +8 -9
  70. package/src/core/Client.ts +110 -114
  71. package/src/core/WalletClient.ts +21 -31
  72. package/src/core/__tests__/accountResource.test.ts +56 -54
  73. package/src/core/__tests__/createEntryPayload.test.ts +34 -34
  74. package/src/core/__tests__/createViewPayload.test.ts +94 -111
  75. package/src/core/__tests__/option.test.ts +38 -31
  76. package/src/core/__tests__/submitTransaction.test.ts +58 -26
  77. package/src/core/__tests__/useABI.test.ts +38 -38
  78. package/src/core/__tests__/view.test.ts +36 -30
  79. package/src/core/__tests__/view_vector.test.ts +52 -50
  80. package/src/core/createEntryPayload.ts +24 -143
  81. package/src/core/createViewPayload.ts +40 -68
  82. package/src/core/index.ts +1 -1
  83. package/src/ensureTypes.ts +1 -0
  84. package/src/hooks/index.ts +0 -1
  85. package/src/hooks/useSubmitTransaction.ts +35 -68
  86. package/src/hooks/useWalletClient.ts +2 -2
  87. package/src/index.ts +1 -1
  88. package/src/types/abi.ts +1 -0
  89. package/src/types/client/abiClient.ts +12 -16
  90. package/src/types/client/client.ts +11 -25
  91. package/src/types/convertor/argsConvertor.ts +1 -1
  92. package/src/types/convertor/genericConvertor.ts +2 -0
  93. package/src/types/convertor/returnConvertor.ts +3 -3
  94. package/src/types/index.ts +0 -2
@@ -1,5 +1,3 @@
1
- import { BCS, TxnBuilderTypes, TypeTagParser } from 'aptos';
2
- import { ensureBigInt, ensureBoolean, ensureNumber } from '../ensureTypes.js';
3
1
  import type {
4
2
  ABIRoot,
5
3
  EntryPayload,
@@ -16,27 +14,33 @@ import { EntryFunctionName } from '../types/extractor/functionExtractor.js';
16
14
  * @param payload.type_arguments The generic type arguments for function.
17
15
  * @returns The payload object to be used in `simulateTransaction` or `submitTransaction` method.
18
16
  * @example
19
- * const entryPayload = createEntryPayload(COIN_ABI, {
20
- * function: 'transfer',
21
- * arguments: ['0x1', 1],
22
- * type_arguments: ['0x1::aptos_coin::AptosCoin'],
17
+ * const payload = createEntryPayload(COIN_ABI, {
18
+ * function: 'transfer',
19
+ * functionArguments: ['0x1', 1],
20
+ * typeArguments: ['0x1::aptos_coin::AptosCoin'],
23
21
  * });
24
22
  *
25
- * const { hash } = await client.submitTransaction(
26
- * entryPayload,
27
- * { account },
28
- * );
23
+ * const result = await client.submitTransaction({
24
+ * payload,
25
+ * signer: account,
26
+ * });
29
27
  */
30
28
  export function createEntryPayload<
31
29
  T extends ABIRoot,
32
30
  TFuncName extends EntryFunctionName<T>,
33
31
  >(abi: T, payload: EntryRequestPayload<T, TFuncName>): EntryPayload {
34
- // TODO: remove unused variables
35
32
  const fnAbi = abi.exposed_functions.filter(
36
33
  (f) => f.name === payload.function,
37
- )[0]!;
38
- const typeArguments: string[] = payload.type_arguments as any[];
39
- const valArguments: any[] = payload.arguments as any[];
34
+ )[0];
35
+
36
+ if (fnAbi === undefined)
37
+ throw new Error(`Function ${payload.function} not found in ABI`);
38
+
39
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
40
+ const typeArguments: string[] = payload.typeArguments as any[];
41
+
42
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
43
+ const valArguments: any[] = payload.functionArguments as any[];
40
44
  const abiArgs =
41
45
  fnAbi.params[0] === '&signer'
42
46
  ? (fnAbi.params as string[]).slice(1)
@@ -47,140 +51,17 @@ export function createEntryPayload<
47
51
  throw new Error(`Function ${payload.function} not found in ABI`);
48
52
  if (abiArgs.length !== valArguments.length)
49
53
  throw new Error(
50
- `Function ${payload.function} expects ${fnAbi.params.length} arguments, but ${payload.arguments.length} were provided`,
54
+ `Function ${payload.function} expects ${fnAbi.params.length} arguments, but ${payload.functionArguments.length} were provided`,
51
55
  );
52
56
  if (fnAbi.generic_type_params.length !== typeArguments.length)
53
57
  throw new Error(
54
- `Function ${payload.function} expects ${fnAbi.generic_type_params.length} type arguments, but ${payload.type_arguments.length} were provided`,
58
+ `Function ${payload.function} expects ${fnAbi.generic_type_params.length} type arguments, but ${payload.typeArguments.length} were provided`,
55
59
  );
56
60
 
57
- // TODO: make entryRequest lazy
58
61
  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));
73
-
74
- // Use the TypeTagParser to parse the string literal into a TypeTagStruct
75
- const typeTagStruct = new TypeTagParser(
76
- arg,
77
- ).parseTypeTag() as TxnBuilderTypes.TypeTagStruct;
78
-
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
- ),
62
+ typeArguments: payload.typeArguments,
63
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
64
+ functionArguments: payload.functionArguments as any,
65
+ function: `${abi.address}::${abi.name}::${payload.function}`,
99
66
  };
100
67
  }
101
-
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
- serializer.serializeBytes(arg);
111
- return;
112
- }
113
-
114
- if (typeof arg === 'string') {
115
- serializer.serializeStr(arg);
116
- return;
117
- }
118
- }
119
-
120
- if (!Array.isArray(arg)) {
121
- throw new Error('Invalid vector args.');
122
- }
123
-
124
- serializer.serializeU32AsUleb128(arg.length);
125
-
126
- arg.forEach((arg) => argToBCS(innerType, arg, serializer));
127
- return;
128
- }
129
-
130
- const objectRegex = /0x1::object::Object<([^]+)>/;
131
- const objectMatch = type.match(objectRegex);
132
- if (objectMatch) {
133
- // It's 0x1::object::Object
134
- TxnBuilderTypes.AccountAddress.fromHex(arg as string).serialize(serializer);
135
- return;
136
- }
137
-
138
- const optionRegex = /0x1::option::Option<([^]+)>/;
139
- const optionMatch = type.match(optionRegex);
140
- if (optionMatch) {
141
- // It's 0x1::option::Option
142
- const innerType = optionMatch[1]!;
143
- serializer.serializeU32AsUleb128(arg.length);
144
- if (!(arg instanceof Array) || arg.length > 1) {
145
- throw new Error('Invalid input value for 0x1::option::Option.');
146
- }
147
- arg.forEach((arg) => argToBCS(innerType, arg, serializer));
148
- return;
149
- }
150
-
151
- switch (
152
- type // It's primitive
153
- ) {
154
- case 'bool':
155
- serializer.serializeBool(ensureBoolean(arg));
156
- break;
157
- case 'address':
158
- TxnBuilderTypes.AccountAddress.fromHex(arg as string).serialize(
159
- serializer,
160
- );
161
- break;
162
- case 'u8':
163
- serializer.serializeU8(ensureNumber(arg));
164
- break;
165
- case 'u16':
166
- serializer.serializeU16(ensureNumber(arg));
167
- break;
168
- case 'u32':
169
- serializer.serializeU32(ensureNumber(arg));
170
- break;
171
- case 'u64':
172
- serializer.serializeU64(ensureBigInt(arg));
173
- break;
174
- case 'u128':
175
- serializer.serializeU128(ensureBigInt(arg));
176
- break;
177
- case 'u256':
178
- serializer.serializeU256(ensureBigInt(arg));
179
- break;
180
- case '0x1::string::String':
181
- serializer.serializeStr(arg as string);
182
- break;
183
- default:
184
- throw new Error(`type "${type}" not supported`);
185
- }
186
- }
@@ -1,4 +1,3 @@
1
- import { HexString } from 'aptos';
2
1
  import {
3
2
  ABIRoot,
4
3
  ExtractReturnType,
@@ -7,9 +6,8 @@ import {
7
6
  ViewRequestPayload,
8
7
  } from '../types/index.js';
9
8
  import { ensureNumber } from '../ensureTypes.js';
9
+ import { MoveStructId } from '@aptos-labs/ts-sdk';
10
10
 
11
- // TODO: support vector<u8> input with Uint8Array
12
- // TODO: support vector<u8> input with string
13
11
  /**
14
12
  * Create a payload for calling a view function.
15
13
  *
@@ -19,12 +17,12 @@ import { ensureNumber } from '../ensureTypes.js';
19
17
  * @param payload.type_arguments The generic type arguments for function.
20
18
  * @returns The payload object to be used in `view` method.
21
19
  * @example
22
- * const viewPayload = createViewPayload(COIN_ABI, {
20
+ * const payload = createViewPayload(COIN_ABI, {
23
21
  * function: 'balance',
24
- * arguments: ['0x1'],
25
- * type_arguments: ['0x1::aptos_coin::AptosCoin'],
22
+ * functionArguments: ['0x1'],
23
+ * typeArguments: ['0x1::aptos_coin::AptosCoin'],
26
24
  * });
27
- * const [balance] = await client.view(viewPayload);
25
+ * const [balance] = await client.view({ payload });
28
26
  */
29
27
  export function createViewPayload<
30
28
  T extends ABIRoot,
@@ -36,24 +34,25 @@ export function createViewPayload<
36
34
  const fnAbi = abi.exposed_functions.filter(
37
35
  (f) => f.name === payload.function,
38
36
  )[0];
39
- const type_arguments: string[] = payload.type_arguments;
40
- const val_arguments: any[] = payload.arguments;
37
+ const type_arguments: string[] = payload.typeArguments;
38
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
39
+ const val_arguments: any[] = payload.functionArguments;
41
40
 
42
41
  // Validations
43
42
  if (fnAbi === undefined)
44
43
  throw new Error(`Function ${payload.function} not found in ABI`);
45
44
  if (fnAbi.params.length !== val_arguments.length)
46
45
  throw new Error(
47
- `Function ${payload.function} expects ${fnAbi.params.length} arguments, but ${payload.arguments.length} were provided`,
46
+ `Function ${payload.function} expects ${fnAbi.params.length} arguments, but ${payload.functionArguments.length} were provided`,
48
47
  );
49
48
  if (fnAbi.generic_type_params.length !== type_arguments.length)
50
49
  throw new Error(
51
- `Function ${payload.function} expects ${fnAbi.generic_type_params.length} type arguments, but ${payload.type_arguments.length} were provided`,
50
+ `Function ${payload.function} expects ${fnAbi.generic_type_params.length} type arguments, but ${payload.functionArguments.length} were provided`,
52
51
  );
53
52
 
54
53
  // TODO: do serialization here
55
54
  const args = fnAbi.params.map((type, i) => {
56
- const arg = payload.arguments[i] as unknown;
55
+ const arg = payload.functionArguments[i] as unknown;
57
56
  if (['u8', 'u16', 'u32'].includes(type)) {
58
57
  return ensureNumber(arg as number);
59
58
  } else if (['u64', 'u128', 'u256'].includes(type)) {
@@ -62,6 +61,7 @@ export function createViewPayload<
62
61
  }
63
62
  return arg.toString();
64
63
  } else if (type.includes('vector')) {
64
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
65
65
  return encodeVector(type, arg as any[]);
66
66
  } else {
67
67
  // string or address
@@ -69,75 +69,35 @@ export function createViewPayload<
69
69
  }
70
70
  });
71
71
 
72
- // used to decode the return value in response
73
- const decoders = fnAbi.return.map((type) => {
74
- if (['u64', 'u128', 'u256'].includes(type)) {
75
- return decodeBigint;
76
- } else if (type.includes('vector')) {
77
- return (value: any[]) => decodeVector(type, value);
78
- } else {
79
- return null;
80
- }
81
- });
82
-
83
72
  return {
84
- viewRequest: {
85
- function: `${abi.address}::${abi.name}::${payload.function}`,
86
- arguments: args,
87
- type_arguments: payload.type_arguments as string[],
88
- },
89
- decoders,
73
+ function: `${abi.address}::${abi.name}::${payload.function}`,
74
+ functionArguments: args,
75
+ typeArguments: payload.typeArguments as Array<MoveStructId>,
90
76
  };
91
77
  }
92
78
 
93
- function decodeBigint(value: string): bigint {
94
- return BigInt(value);
95
- }
96
-
97
- function decodeVector(type: string, value: any[]) {
79
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
80
+ function encodeVector(type: string, value: any) {
98
81
  const regex = /vector<([^]+)>/;
99
82
  const match = type.match(regex);
100
83
  if (!match) {
101
84
  // Should never happen
102
85
  throw new Error(`Unsupported type: ${type}`);
103
86
  }
104
- const innerType = match[1]!;
105
-
106
- if (['address', 'bool', 'u8', 'u16', 'u32'].includes(innerType)) {
107
- return value;
108
- } else if (['u64', 'u128', 'u256'].includes(innerType)) {
109
- return value.map((v: string) => BigInt(v));
110
- } else {
111
- // TODO: Figure out how to decode Vector of vector
112
- return value;
113
- }
114
- }
115
-
116
- function encodeVector(type: string, value: any[]) {
117
- const regex = /vector<([^]+)>/;
118
- const match = type.match(regex);
119
- if (!match) {
120
- // Should never happen
87
+ const innerType = match[1];
88
+ if(!innerType) {
121
89
  throw new Error(`Unsupported type: ${type}`);
122
90
  }
123
- const innerType = match[1]!;
91
+
124
92
  if (innerType === 'u8') {
125
- return (
126
- HexString.fromUint8Array(
127
- typeof value === 'string' ?
128
- new TextEncoder().encode(value) :
129
- value instanceof Uint8Array ?
130
- value :
131
- new Uint8Array(
132
- value.map((v: number) => {
133
- const result = ensureNumber(v);
134
- if (result < 0 || result > 255)
135
- throw new Error(`Invalid u8 value: ${result}`);
136
- return result;
137
- }),
138
- ),
139
- ) as any
140
- ).hexString;
93
+ if (typeof value === 'string' || value instanceof Uint8Array)
94
+ return value;
95
+ if (Array.isArray(value)) {
96
+ return arrayToHex(value)
97
+ }
98
+
99
+ throw new Error(`Invalid u8 value: ${value}`)
100
+
141
101
  } else if (['bool', 'u16', 'u32'].includes(innerType)) {
142
102
  return value;
143
103
  } else if (['u64', 'u128', 'u256'].includes(innerType)) {
@@ -148,3 +108,15 @@ function encodeVector(type: string, value: any[]) {
148
108
  return value;
149
109
  }
150
110
  }
111
+
112
+ const arrayToHex = (array: (string | number)[]): string => {
113
+ let result = "0x";
114
+ array.forEach((item) => {
115
+ const n = ensureNumber(item);
116
+ if (n < 0 || n > 255)
117
+ throw new Error(`Invalid u8 value: ${n}`);
118
+ result += n.toString(16).padStart(2, '0');
119
+ })
120
+
121
+ return result;
122
+ };
package/src/core/index.ts CHANGED
@@ -1,3 +1,3 @@
1
- export { Client as MoveTsClient, createClient } from './Client.js';
1
+ export { Client as MoveTsClient, createSurfClient } from './Client.js';
2
2
  export { createViewPayload } from './createViewPayload.js';
3
3
  export { createEntryPayload } from './createEntryPayload.js';
@@ -1,3 +1,4 @@
1
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1
2
  function assertType(val: any, types: string[] | string, message?: string) {
2
3
  if (!types?.includes(typeof val)) {
3
4
  throw new Error(
@@ -1,5 +1,4 @@
1
1
  export {
2
- submitTransactionOptions,
3
2
  useSubmitTransaction,
4
3
  } from './useSubmitTransaction.js';
5
4
  export { useWalletClient } from './useWalletClient.js';
@@ -1,28 +1,16 @@
1
1
  import { useState, useRef } from 'react';
2
2
  import { useWallet } from '@aptos-labs/wallet-adapter-react';
3
- import { AptosClient, Types } from 'aptos';
4
3
  import type { EntryPayload } from '../types/index.js';
5
-
6
- export type submitTransactionOptions = {
7
- nodeUrl: string;
8
- onSuccess?: (result: Types.Transaction_UserTransaction) => void;
9
- onFailed?: (error: Error) => void;
10
- max_gas_amount?: string;
11
- };
12
-
13
- function isRawPayload(
14
- payload: EntryPayload | Types.TransactionPayload_EntryFunctionPayload,
15
- ): payload is EntryPayload {
16
- return (<EntryPayload>payload).rawPayload !== undefined;
17
- }
4
+ import { InputGenerateTransactionOptions } from '@aptos-labs/ts-sdk';
18
5
 
19
6
  // TODO: add test for this
20
7
  export const useSubmitTransaction = () => {
21
- const { signAndSubmitTransaction } = useWallet();
8
+ const { connected, signAndSubmitTransaction, account } = useWallet();
22
9
 
23
10
  const [isIdle, setIsIdle] = useState(true);
24
11
  const [isLoading, setIsLoading] = useState(false);
25
- const [data, setResult] = useState<Types.Transaction_UserTransaction>();
12
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
13
+ const [data, setResult] = useState<any>();
26
14
  const [error, setError] = useState<Error>();
27
15
  const idRef = useRef<number>(0);
28
16
 
@@ -35,10 +23,15 @@ export const useSubmitTransaction = () => {
35
23
  }
36
24
 
37
25
  async function submitTransaction(
38
- payload: EntryPayload | Types.TransactionPayload_EntryFunctionPayload,
39
- { nodeUrl, onSuccess, onFailed, max_gas_amount }: submitTransactionOptions,
40
- ): Promise<void> {
26
+ payload: EntryPayload,
27
+ options?: InputGenerateTransactionOptions,
28
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
29
+ ): Promise<any> {
41
30
  // TODO: remove this
31
+ if (connected === false) {
32
+ throw new Error('Wallet is not connected');
33
+ }
34
+
42
35
  if (signAndSubmitTransaction === undefined) {
43
36
  return;
44
37
  }
@@ -50,71 +43,45 @@ export const useSubmitTransaction = () => {
50
43
  setIsLoading(true);
51
44
  setIsIdle(false);
52
45
 
53
- // TODO: make it better. This way to determine the type is tricky.
54
- const request = isRawPayload(payload) ? payload.rawPayload : payload;
55
-
46
+ let result
56
47
  try {
57
- // TODO: use the BCS API instead
58
- const { hash } = await signAndSubmitTransaction(
48
+ result = await signAndSubmitTransaction(
59
49
  {
60
- type: 'entry_function_payload',
61
- ...request,
62
- arguments: request.arguments.map((arg: any) => {
63
- if (Array.isArray(arg)) {
64
- // TODO: support nested array, or use the BCS API instead
65
- return arg.map((item: any) => item.toString());
66
- } else if (typeof arg === 'object') {
67
- throw new Error(
68
- `a value of struct type: ${arg} is not supported`,
69
- );
70
- } else {
71
- return arg.toString();
72
- }
73
- }),
50
+ sender: account?.address ?? "",
51
+ data: {
52
+ ...payload,
53
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
54
+ functionArguments: payload.functionArguments.map((arg: any) => {
55
+ if (Array.isArray(arg)) {
56
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
57
+ return arg.map((item: any) => item.toString());
58
+ } else if (typeof arg === 'object') {
59
+ throw new Error(
60
+ `a value of struct type: ${arg} is not supported`,
61
+ );
62
+ } else {
63
+ return arg.toString();
64
+ }
65
+ }),
66
+ }
74
67
  },
75
- { max_gas_amount },
68
+ options,
76
69
  );
77
70
 
78
- const client = new AptosClient(nodeUrl);
79
- const result = (await client.waitForTransactionWithResult(hash, {
80
- checkSuccess: true,
81
- })) as Types.Transaction_UserTransaction;
82
-
83
71
  // Only update the status if the request is not stale.
84
72
  if (id === idRef.current) {
85
- try {
86
- onSuccess?.(result);
87
- } catch (e) {
88
- // error from user's callback
89
- console.error(e);
90
- }
91
73
  setResult(result);
92
74
  }
93
75
  } catch (e) {
94
- // Only update the status if the request is not stale.
95
- if (id === idRef.current) {
96
- const error =
97
- e instanceof Error
98
- ? e
99
- : e
100
- ? new Error(String(e))
101
- : new Error('unknown error');
102
-
103
- try {
104
- onFailed?.(error);
105
- } catch (e2) {
106
- // error from user's callback
107
- console.error(e2);
108
- }
109
-
110
- setError(error);
111
- }
76
+ setError(error);
112
77
  } finally {
113
78
  // Only update the status if the request is not stale.
114
79
  if (id === idRef.current) {
115
80
  setIsLoading(false);
116
81
  }
117
82
  }
83
+
84
+ return result;
118
85
  }
119
86
 
120
87
  return { submitTransaction, reset, isLoading, isIdle, data, error };
@@ -1,12 +1,12 @@
1
1
  import { useWallet } from '@aptos-labs/wallet-adapter-react';
2
2
  import { WalletClient } from '../core/WalletClient.js';
3
3
 
4
- export const useWalletClient = ({ nodeUrl }: { nodeUrl: string }) => {
4
+ export const useWalletClient = () => {
5
5
  const wallet = useWallet();
6
6
  return {
7
7
  connected: wallet.connected,
8
8
  client: wallet.connected
9
- ? new WalletClient({ wallet, nodeUrl })
9
+ ? new WalletClient({ wallet, })
10
10
  : undefined,
11
11
  };
12
12
  };
package/src/index.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  export {
2
2
  createViewPayload,
3
3
  createEntryPayload,
4
- createClient,
4
+ createSurfClient,
5
5
  } from './core/index.js';
6
6
 
7
7
  export type {
package/src/types/abi.ts CHANGED
@@ -21,6 +21,7 @@ export interface ABIFunction {
21
21
  }
22
22
 
23
23
  export interface ABIFunctionGenericTypeParam {
24
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
24
25
  constraints: readonly any[];
25
26
  }
26
27
 
@@ -2,8 +2,6 @@
2
2
  * The types for `client.useABI` API.
3
3
  */
4
4
 
5
- import type { AptosAccount } from 'aptos';
6
- import { TransactionResponse } from './client.js';
7
5
  import { ABIRoot } from '../abi.js';
8
6
  import { ABITable } from '../defaultABITable.js';
9
7
  import {
@@ -19,31 +17,29 @@ import {
19
17
  ExtractStructGenericArgsType,
20
18
  ExtractStructType,
21
19
  } from '../extractor/structExtractor.js';
20
+ import { Account, AccountAddressInput, CommittedTransactionResponse } from '@aptos-labs/ts-sdk';
22
21
 
23
22
  export type ABIViewClient<T extends ABIRoot> = {
24
23
  [TFuncName in ViewFunctionName<T>]: (payload: {
25
- type_arguments: ExtractGenericArgsType<T, TFuncName>;
26
- arguments: ExtractArgsType<T, TFuncName>;
27
- ledger_version?: string;
24
+ typeArguments: ExtractGenericArgsType<T, TFuncName>;
25
+ functionArguments: ExtractArgsType<T, TFuncName>;
26
+ ledgerVersion?: string;
28
27
  }) => Promise<ExtractReturnType<T, TFuncName>>;
29
28
  };
30
29
 
31
30
  export type ABIEntryClient<T extends ABIRoot> = {
32
31
  [TFuncName in EntryFunctionName<T>]: (payload: {
33
- type_arguments: ExtractGenericArgsType<T, TFuncName>;
34
- arguments: ExtractArgsTypeOmitSigner<T, TFuncName>;
35
- account: AptosAccount;
32
+ typeArguments: ExtractGenericArgsType<T, TFuncName>;
33
+ functionArguments: ExtractArgsTypeOmitSigner<T, TFuncName>;
34
+ account: Account;
36
35
  isSimulation?: boolean;
37
- }) => Promise<TransactionResponse>;
36
+ }) => Promise<CommittedTransactionResponse>;
38
37
  };
39
38
 
40
39
  export type ABIResourceClient<TABITable extends ABITable, T extends ABIRoot> = {
41
40
  [TStructName in ResourceStructName<T>]: (payload: {
42
- type_arguments: ExtractStructGenericArgsType<T, TStructName>;
43
- account: `0x${string}`;
44
- ledger_version?: string;
45
- }) => Promise<{
46
- data: ExtractStructType<TABITable, T, TStructName>;
47
- type: string;
48
- }>;
41
+ typeArguments: ExtractStructGenericArgsType<T, TStructName>;
42
+ account: AccountAddressInput;
43
+ ledgerVersion?: string;
44
+ }) => Promise<ExtractStructType<TABITable, T, TStructName>>;
49
45
  };