@thalalabs/surf 0.0.11 → 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 (156) hide show
  1. package/CHANGELOG.md +6 -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 +15 -4
  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 +17 -6
  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 -60
  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 +1 -1
  109. package/src/core/Client.ts +230 -223
  110. package/src/core/WalletClient.ts +57 -55
  111. package/src/core/__tests__/option.test.ts +144 -0
  112. package/src/core/createEntryPayload.ts +152 -118
  113. package/src/core/createViewPayload.ts +112 -100
  114. package/src/core/index.ts +4 -4
  115. package/src/hooks/index.ts +5 -2
  116. package/src/hooks/useSubmitTransaction.ts +101 -98
  117. package/src/hooks/useWalletClient.ts +10 -8
  118. package/src/types/abi.ts +24 -21
  119. package/src/types/client/abiClient.ts +47 -0
  120. package/src/types/client/client.ts +73 -0
  121. package/src/types/client/walletClient.ts +18 -0
  122. package/src/types/common.ts +13 -156
  123. package/src/types/convertor/argsConvertor.ts +59 -0
  124. package/src/types/convertor/genericConvertor.ts +9 -0
  125. package/src/types/convertor/returnConvertor.ts +55 -0
  126. package/src/types/convertor/structConvertor.ts +82 -0
  127. package/src/types/defaultABITable.ts +17 -12
  128. package/src/types/extractor/functionExtractor.ts +86 -0
  129. package/src/types/extractor/structExtractor.ts +67 -0
  130. package/src/types/index.ts +20 -9
  131. package/src/types/moveTypes.ts +22 -0
  132. package/build/cjs/types/abiClient.js.map +0 -1
  133. package/build/cjs/types/struct.js.map +0 -1
  134. package/build/cjs/types/walletClient.js.map +0 -1
  135. package/build/esm/types/abiClient.js.map +0 -1
  136. package/build/esm/types/struct.js +0 -2
  137. package/build/esm/types/struct.js.map +0 -1
  138. package/build/esm/types/walletClient.js.map +0 -1
  139. package/build/types/types/abiClient.d.ts +0 -28
  140. package/build/types/types/abiClient.d.ts.map +0 -1
  141. package/build/types/types/client.d.ts +0 -24
  142. package/build/types/types/client.d.ts.map +0 -1
  143. package/build/types/types/struct.d.ts +0 -30
  144. package/build/types/types/struct.d.ts.map +0 -1
  145. package/build/types/types/walletClient.d.ts +0 -9
  146. package/build/types/types/walletClient.d.ts.map +0 -1
  147. package/src/types/abiClient.ts +0 -38
  148. package/src/types/client.ts +0 -32
  149. package/src/types/struct.ts +0 -87
  150. package/src/types/walletClient.ts +0 -10
  151. /package/build/cjs/types/{abiClient.js → client/abiClient.js} +0 -0
  152. /package/build/cjs/types/{client.js → client/client.js} +0 -0
  153. /package/build/cjs/types/{walletClient.js → client/walletClient.js} +0 -0
  154. /package/build/esm/types/{abiClient.js → client/abiClient.js} +0 -0
  155. /package/build/esm/types/{client.js → client/client.js} +0 -0
  156. /package/build/esm/types/{walletClient.js → client/walletClient.js} +0 -0
@@ -1,13 +1,18 @@
1
- import { HexString } from "aptos";
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";
1
+ import { HexString } from 'aptos';
2
+ import {
3
+ ABIRoot,
4
+ ExtractReturnType,
5
+ ViewFunctionName,
6
+ ViewPayload,
7
+ ViewRequestPayload,
8
+ } from '../types/index.js';
9
+ import { ensureNumber } from '../ensureTypes.js';
5
10
 
6
11
  // TODO: support vector<u8> input with Uint8Array
7
12
  // TODO: support vector<u8> input with string
8
13
  /**
9
14
  * Create a payload for calling a view function.
10
- *
15
+ *
11
16
  * @param abi The ABI JSON contains the view function. For type inference and encoding/decoding purpose.
12
17
  * @param payload.function The function name.
13
18
  * @param payload.arguments The input arguments for function.
@@ -22,113 +27,120 @@ import { ensureNumber } from "../ensureTypes.js";
22
27
  * const [balance] = await client.view(viewPayload);
23
28
  */
24
29
  export function createViewPayload<
25
- T extends ABIRoot,
26
- TFuncName extends ViewFunctionName<T>
30
+ T extends ABIRoot,
31
+ TFuncName extends ViewFunctionName<T>,
27
32
  >(
28
- abi: T,
29
- payload: ViewRequestPayload<T, TFuncName>
30
- ):
31
- ViewPayload<ExtractReturnType<T, TFuncName>> {
32
- const fnAbi = abi.exposed_functions.filter(f => f.name === payload.function)[0];
33
- const type_arguments: string[] = payload.type_arguments as any[];
34
- const val_arguments: any[] = payload.arguments as any[];
33
+ abi: T,
34
+ payload: ViewRequestPayload<T, TFuncName>,
35
+ ): ViewPayload<ExtractReturnType<T, TFuncName>> {
36
+ const fnAbi = abi.exposed_functions.filter(
37
+ (f) => f.name === payload.function,
38
+ )[0];
39
+ const type_arguments: string[] = payload.type_arguments;
40
+ const val_arguments: any[] = payload.arguments;
35
41
 
36
- // Validations
37
- if (fnAbi === undefined) throw new Error(`Function ${payload.function} not found in ABI`);
38
- if (fnAbi.params.length !== val_arguments.length) throw new Error(`Function ${payload.function} expects ${fnAbi.params.length} arguments, but ${payload.arguments.length} were provided`);
39
- if (fnAbi.generic_type_params.length !== type_arguments.length) throw new Error(`Function ${payload.function} expects ${fnAbi.generic_type_params.length} type arguments, but ${payload.type_arguments.length} were provided`);
42
+ // Validations
43
+ if (fnAbi === undefined)
44
+ throw new Error(`Function ${payload.function} not found in ABI`);
45
+ if (fnAbi.params.length !== val_arguments.length)
46
+ throw new Error(
47
+ `Function ${payload.function} expects ${fnAbi.params.length} arguments, but ${payload.arguments.length} were provided`,
48
+ );
49
+ if (fnAbi.generic_type_params.length !== type_arguments.length)
50
+ throw new Error(
51
+ `Function ${payload.function} expects ${fnAbi.generic_type_params.length} type arguments, but ${payload.type_arguments.length} were provided`,
52
+ );
40
53
 
41
- // TODO: do serialization here
42
- const args = fnAbi.params.map((type, i) => {
43
- const arg = payload.arguments[i]!;
44
- if (['u8', 'u16', 'u32'].includes(type)) {
45
- return ensureNumber(arg as number);
46
- }
47
- else if (['u64', 'u128', 'u256'].includes(type)) {
48
- return arg.toString();
49
- }
50
- else if (type.includes("vector")) {
51
- return encodeVector(type, arg as any[]);
52
- }
53
- else { // string or address
54
- return arg;
55
- }
56
- });
54
+ // TODO: do serialization here
55
+ const args = fnAbi.params.map((type, i) => {
56
+ const arg = payload.arguments[i] as unknown;
57
+ if (['u8', 'u16', 'u32'].includes(type)) {
58
+ return ensureNumber(arg as number);
59
+ } else if (['u64', 'u128', 'u256'].includes(type)) {
60
+ if (!arg?.toString) {
61
+ throw new Error(`Expecting a bigint, but got ${arg}`);
62
+ }
63
+ return arg.toString();
64
+ } else if (type.includes('vector')) {
65
+ return encodeVector(type, arg as any[]);
66
+ } else {
67
+ // string or address
68
+ return arg;
69
+ }
70
+ });
57
71
 
58
- // used to decode the return value in response
59
- const decoders = fnAbi.return.map((type) => {
60
- if (['u64', 'u128', 'u256'].includes(type)) {
61
- return decodeBigint;
62
- }
63
- else if (type.includes("vector")) {
64
- return (value: any[]) => decodeVector(type, value)
65
- }
66
- else {
67
- return null;
68
- }
69
- });
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
+ });
70
82
 
71
- return {
72
- viewRequest: {
73
- function: `${abi.address}::${abi.name}::${payload.function}`,
74
- arguments: args,
75
- type_arguments: payload.type_arguments as string[],
76
- },
77
- decoders,
78
- };
83
+ 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,
90
+ };
79
91
  }
80
92
 
81
93
  function decodeBigint(value: string): bigint {
82
- return BigInt(value);
94
+ return BigInt(value);
83
95
  }
84
96
 
85
97
  function decodeVector(type: string, value: any[]) {
86
- const regex = /vector<([^]+)>/;
87
- const match = type.match(regex);
88
- if (!match) {
89
- // Should never happen
90
- throw new Error(`Unsupported type: ${type}`);
91
- }
92
- const innerType = match[1]!;
98
+ const regex = /vector<([^]+)>/;
99
+ const match = type.match(regex);
100
+ if (!match) {
101
+ // Should never happen
102
+ throw new Error(`Unsupported type: ${type}`);
103
+ }
104
+ const innerType = match[1]!;
93
105
 
94
- if(["address", "bool", "u8", "u16", "u32"].includes(innerType)) {
95
- return value;
96
- }
97
- else if (["u64", "u128", "u256"].includes(innerType)) {
98
- return value.map((v: string) => BigInt(v));
99
- } else {
100
- // TODO: Figure out how to decode Vector of vector
101
- return value;
102
- }
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
+ }
103
114
  }
104
115
 
105
116
  function encodeVector(type: string, value: any[]) {
106
- const regex = /vector<([^]+)>/;
107
- const match = type.match(regex);
108
- if (!match) {
109
- // Should never happen
110
- throw new Error(`Unsupported type: ${type}`);
111
- }
112
- const innerType = match[1]!;
113
- if (innerType === "u8") {
114
- return (
115
- HexString.fromUint8Array(
116
- new Uint8Array(
117
- value.map((v: number) => {
118
- const result = ensureNumber(v);
119
- if (result < 0 || result > 255)
120
- throw new Error(`Invalid u8 value: ${result}`);
121
- return result;
122
- }),
123
- ),
124
- ) as any
125
- ).hexString;
126
- } else if (["bool", "u16", "u32"].includes(innerType)) {
127
- return value;
128
- } else if (["u64", "u128", "u256"].includes(innerType)) {
129
- return value.map((v: bigint) => v.toString());
130
- } else { // string or address
131
- // TODO: encode for Vector of vector
132
- return value;
133
- }
134
- }
117
+ const regex = /vector<([^]+)>/;
118
+ const match = type.match(regex);
119
+ if (!match) {
120
+ // Should never happen
121
+ throw new Error(`Unsupported type: ${type}`);
122
+ }
123
+ const innerType = match[1]!;
124
+ if (innerType === 'u8') {
125
+ return (
126
+ HexString.fromUint8Array(
127
+ new Uint8Array(
128
+ value.map((v: number) => {
129
+ const result = ensureNumber(v);
130
+ if (result < 0 || result > 255)
131
+ throw new Error(`Invalid u8 value: ${result}`);
132
+ return result;
133
+ }),
134
+ ),
135
+ ) as any
136
+ ).hexString;
137
+ } else if (['bool', 'u16', 'u32'].includes(innerType)) {
138
+ return value;
139
+ } else if (['u64', 'u128', 'u256'].includes(innerType)) {
140
+ return value.map((v: bigint) => v.toString());
141
+ } else {
142
+ // string or address
143
+ // TODO: encode for Vector of vector
144
+ return value;
145
+ }
146
+ }
package/src/core/index.ts CHANGED
@@ -1,4 +1,4 @@
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
+ 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,5 @@
1
- export { submitTransactionOptions, useSubmitTransaction } from "./useSubmitTransaction.js";
2
- export { useWalletClient } from "./useWalletClient.js";
1
+ export {
2
+ submitTransactionOptions,
3
+ useSubmitTransaction,
4
+ } from './useSubmitTransaction.js';
5
+ export { useWalletClient } from './useWalletClient.js';
@@ -1,118 +1,121 @@
1
- import { useState, useRef } from "react";
2
- import { useWallet } from "@aptos-labs/wallet-adapter-react";
3
- import { AptosClient, Types } from "aptos";
4
- import type { EntryPayload } from "../types/index.js";
1
+ import { useState, useRef } from 'react';
2
+ import { useWallet } from '@aptos-labs/wallet-adapter-react';
3
+ import { AptosClient, Types } from 'aptos';
4
+ import type { EntryPayload } from '../types/index.js';
5
5
 
6
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
- }
7
+ nodeUrl: string;
8
+ onSuccess?: (result: Types.Transaction_UserTransaction) => void;
9
+ onFailed?: (error: Error) => void;
10
+ max_gas_amount?: string;
11
+ };
12
12
 
13
- function isRawPayload(payload: EntryPayload | Types.TransactionPayload_EntryFunctionPayload,): payload is EntryPayload {
14
- return (<EntryPayload>payload).rawPayload !== undefined;
13
+ function isRawPayload(
14
+ payload: EntryPayload | Types.TransactionPayload_EntryFunctionPayload,
15
+ ): payload is EntryPayload {
16
+ return (<EntryPayload>payload).rawPayload !== undefined;
15
17
  }
16
18
 
17
19
  // TODO: add test for this
18
20
  export const useSubmitTransaction = () => {
19
- const { signAndSubmitTransaction } = useWallet();
21
+ const { signAndSubmitTransaction } = useWallet();
20
22
 
21
- const [isIdle, setIsIdle] = useState(true);
22
- const [isLoading, setIsLoading] = useState(false);
23
- const [data, setResult] = useState<Types.Transaction_UserTransaction>();
24
- const [error, setError] = useState<Error>();
25
- const idRef = useRef<number>(0);
23
+ const [isIdle, setIsIdle] = useState(true);
24
+ const [isLoading, setIsLoading] = useState(false);
25
+ const [data, setResult] = useState<Types.Transaction_UserTransaction>();
26
+ const [error, setError] = useState<Error>();
27
+ const idRef = useRef<number>(0);
26
28
 
27
- function reset() {
28
- setIsIdle(true);
29
- setIsLoading(false);
30
- setResult(undefined);
31
- setError(undefined);
32
- idRef.current = idRef.current + 1;
33
- }
29
+ function reset() {
30
+ setIsIdle(true);
31
+ setIsLoading(false);
32
+ setResult(undefined);
33
+ setError(undefined);
34
+ idRef.current = idRef.current + 1;
35
+ }
34
36
 
35
- async function submitTransaction(
36
- payload: EntryPayload | Types.TransactionPayload_EntryFunctionPayload,
37
- {
38
- nodeUrl,
39
- onSuccess,
40
- onFailed,
41
- max_gas_amount
42
- }: submitTransactionOptions
43
- ): Promise<void> {
44
- // TODO: remove this
45
- if (signAndSubmitTransaction === undefined) {
46
- return;
47
- }
48
- const id = idRef.current;
37
+ async function submitTransaction(
38
+ payload: EntryPayload | Types.TransactionPayload_EntryFunctionPayload,
39
+ { nodeUrl, onSuccess, onFailed, max_gas_amount }: submitTransactionOptions,
40
+ ): Promise<void> {
41
+ // TODO: remove this
42
+ if (signAndSubmitTransaction === undefined) {
43
+ return;
44
+ }
45
+ const id = idRef.current;
49
46
 
50
- if (!isIdle) {
51
- throw new Error("Transaction is already in progress");
52
- }
53
- setIsLoading(true);
54
- setIsIdle(false);
47
+ if (!isIdle) {
48
+ throw new Error('Transaction is already in progress');
49
+ }
50
+ setIsLoading(true);
51
+ setIsIdle(false);
55
52
 
56
- // TODO: make it better. This way to determine the type is tricky.
57
- const request = isRawPayload(payload) ? payload.rawPayload : payload;
53
+ // TODO: make it better. This way to determine the type is tricky.
54
+ const request = isRawPayload(payload) ? payload.rawPayload : payload;
58
55
 
59
- try {
60
- // TODO: use the BCS API instead
61
- const { hash } = await signAndSubmitTransaction({
62
- type: "entry_function_payload",
63
- ...request,
64
- arguments: request.arguments.map((arg: any) => {
65
- if (Array.isArray(arg)) {
66
- // TODO: support nested array, or use the BCS API instead
67
- return arg.map((item: any) => item.toString());
68
- } else if (typeof arg === "object") {
69
- throw new Error(`a value of struct type: ${arg} is not supported`);
70
- } else {
71
- return arg.toString();
72
- }
73
- }),
74
- }, { max_gas_amount });
56
+ try {
57
+ // TODO: use the BCS API instead
58
+ const { hash } = await signAndSubmitTransaction(
59
+ {
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
+ }),
74
+ },
75
+ { max_gas_amount },
76
+ );
75
77
 
76
- const client = new AptosClient(nodeUrl);
77
- const result = (await client.waitForTransactionWithResult(hash, {
78
- checkSuccess: true,
79
- })) as Types.Transaction_UserTransaction;
78
+ const client = new AptosClient(nodeUrl);
79
+ const result = (await client.waitForTransactionWithResult(hash, {
80
+ checkSuccess: true,
81
+ })) as Types.Transaction_UserTransaction;
80
82
 
81
- // Only update the status if the request is not stale.
82
- if (id === idRef.current) {
83
- try {
84
- onSuccess?.(result);
85
- }
86
- catch (e) {
87
- // error from user's callback
88
- console.error(e);
89
- }
90
- setResult(result);
91
- }
83
+ // Only update the status if the request is not stale.
84
+ if (id === idRef.current) {
85
+ try {
86
+ onSuccess?.(result);
92
87
  } catch (e) {
93
- // Only update the status if the request is not stale.
94
- if (id === idRef.current) {
95
- const error = e instanceof Error ? e :
96
- e ? new Error(String(e)) :
97
- new Error("unknown error");
98
-
99
- try {
100
- onFailed?.(error);
101
- }
102
- catch (e2) {
103
- // error from user's callback
104
- console.error(e2);
105
- }
88
+ // error from user's callback
89
+ console.error(e);
90
+ }
91
+ setResult(result);
92
+ }
93
+ } 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');
106
102
 
107
- setError(error);
108
- }
109
- } finally {
110
- // Only update the status if the request is not stale.
111
- if (id === idRef.current) {
112
- setIsLoading(false);
113
- }
103
+ try {
104
+ onFailed?.(error);
105
+ } catch (e2) {
106
+ // error from user's callback
107
+ console.error(e2);
114
108
  }
109
+
110
+ setError(error);
111
+ }
112
+ } finally {
113
+ // Only update the status if the request is not stale.
114
+ if (id === idRef.current) {
115
+ setIsLoading(false);
116
+ }
115
117
  }
118
+ }
116
119
 
117
- return { submitTransaction, reset, isLoading, isIdle, data, error };
118
- };
120
+ return { submitTransaction, reset, isLoading, isIdle, data, error };
121
+ };
@@ -1,10 +1,12 @@
1
- import { useWallet } from "@aptos-labs/wallet-adapter-react";
2
- import { MoveTsWalletClient } from "../core/index.js";
1
+ import { useWallet } from '@aptos-labs/wallet-adapter-react';
2
+ import { MoveTsWalletClient } from '../core/index.js';
3
3
 
4
4
  export const useWalletClient = ({ nodeUrl }: { nodeUrl: string }) => {
5
- const wallet = useWallet();
6
- return {
7
- connected: wallet.connected,
8
- client: wallet.connected ? new MoveTsWalletClient({ wallet, nodeUrl }) : undefined
9
- };
10
- }
5
+ const wallet = useWallet();
6
+ return {
7
+ connected: wallet.connected,
8
+ client: wallet.connected
9
+ ? new MoveTsWalletClient({ wallet, nodeUrl })
10
+ : undefined,
11
+ };
12
+ };
package/src/types/abi.ts CHANGED
@@ -1,35 +1,38 @@
1
+ /**
2
+ * The ABI JSON related types.
3
+ */
1
4
 
2
5
  export interface ABIRoot {
3
- address: string;
4
- name: string;
5
- friends: readonly string[];
6
- exposed_functions: readonly ABIFunction[];
7
- structs: readonly ABIStruct[];
6
+ address: string;
7
+ name: string;
8
+ friends: readonly string[];
9
+ exposed_functions: readonly ABIFunction[];
10
+ structs: readonly ABIStruct[];
8
11
  }
9
12
 
10
13
  export interface ABIFunction {
11
- name: string;
12
- visibility: "friend" | "public";
13
- is_entry: boolean;
14
- is_view: boolean;
15
- generic_type_params: readonly ABIFunctionGenericTypeParam[];
16
- params: readonly string[];
17
- return: readonly string[];
14
+ name: string;
15
+ visibility: 'friend' | 'public';
16
+ is_entry: boolean;
17
+ is_view: boolean;
18
+ generic_type_params: readonly ABIFunctionGenericTypeParam[];
19
+ params: readonly string[];
20
+ return: readonly string[];
18
21
  }
19
22
 
20
23
  export interface ABIFunctionGenericTypeParam {
21
- constraints: readonly any[];
24
+ constraints: readonly any[];
22
25
  }
23
26
 
24
27
  export interface ABIStruct {
25
- name: string;
26
- is_native: boolean;
27
- abilities: readonly string[];
28
- generic_type_params: readonly ABIFunctionGenericTypeParam[];
29
- fields: readonly ABIStructField[];
28
+ name: string;
29
+ is_native: boolean;
30
+ abilities: readonly string[];
31
+ generic_type_params: readonly ABIFunctionGenericTypeParam[];
32
+ fields: readonly ABIStructField[];
30
33
  }
31
34
 
32
35
  export interface ABIStructField {
33
- name: string;
34
- type: string;
35
- }
36
+ name: string;
37
+ type: string;
38
+ }
@@ -0,0 +1,47 @@
1
+ /**
2
+ * The types for `client.useABI` API.
3
+ */
4
+
5
+ import type { AptosAccount } from 'aptos';
6
+ import { TransactionResponse } from './client.js';
7
+ import { ABIRoot } from '../abi.js';
8
+ import { ABITable } from '../defaultABITable.js';
9
+ import {
10
+ EntryFunctionName,
11
+ ExtractArgsType,
12
+ ExtractArgsTypeOmitSigner,
13
+ ExtractGenericArgsType,
14
+ ExtractReturnType,
15
+ ViewFunctionName,
16
+ } from '../extractor/functionExtractor.js';
17
+ import {
18
+ ResourceStructName,
19
+ ExtractStructGenericArgsType,
20
+ ExtractStructType,
21
+ } from '../extractor/structExtractor.js';
22
+
23
+ export type ABIViewClient<T extends ABIRoot> = {
24
+ [TFuncName in ViewFunctionName<T>]: (payload: {
25
+ type_arguments: ExtractGenericArgsType<T, TFuncName>;
26
+ arguments: ExtractArgsType<T, TFuncName>;
27
+ }) => Promise<ExtractReturnType<T, TFuncName>>;
28
+ };
29
+
30
+ export type ABIEntryClient<T extends ABIRoot> = {
31
+ [TFuncName in EntryFunctionName<T>]: (payload: {
32
+ type_arguments: ExtractGenericArgsType<T, TFuncName>;
33
+ arguments: ExtractArgsTypeOmitSigner<T, TFuncName>;
34
+ account: AptosAccount;
35
+ isSimulation?: boolean;
36
+ }) => Promise<TransactionResponse>;
37
+ };
38
+
39
+ export type ABIResourceClient<TABITable extends ABITable, T extends ABIRoot> = {
40
+ [TStructName in ResourceStructName<T>]: (payload: {
41
+ type_arguments: ExtractStructGenericArgsType<T, TStructName>;
42
+ account: `0x${string}`;
43
+ }) => Promise<{
44
+ data: ExtractStructType<TABITable, T, TStructName>;
45
+ type: string;
46
+ }>;
47
+ };