@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.
- package/CHANGELOG.md +6 -0
- package/README.md +5 -2
- package/build/cjs/core/Client.js +9 -11
- package/build/cjs/core/Client.js.map +1 -1
- package/build/cjs/core/WalletClient.js +3 -3
- package/build/cjs/core/WalletClient.js.map +1 -1
- package/build/cjs/core/createEntryPayload.js +15 -4
- package/build/cjs/core/createEntryPayload.js.map +1 -1
- package/build/cjs/core/createViewPayload.js +11 -8
- package/build/cjs/core/createViewPayload.js.map +1 -1
- package/build/cjs/hooks/index.js.map +1 -1
- package/build/cjs/hooks/useSubmitTransaction.js +8 -6
- package/build/cjs/hooks/useSubmitTransaction.js.map +1 -1
- package/build/cjs/hooks/useWalletClient.js +3 -1
- package/build/cjs/hooks/useWalletClient.js.map +1 -1
- package/build/cjs/types/client/abiClient.js.map +1 -0
- package/build/{esm/types → cjs/types/client}/client.js.map +1 -1
- package/build/cjs/types/client/walletClient.js.map +1 -0
- package/build/cjs/types/convertor/argsConvertor.js +3 -0
- package/build/cjs/types/convertor/argsConvertor.js.map +1 -0
- package/build/cjs/types/convertor/genericConvertor.js +3 -0
- package/build/cjs/types/convertor/genericConvertor.js.map +1 -0
- package/build/cjs/types/convertor/returnConvertor.js +3 -0
- package/build/cjs/types/convertor/returnConvertor.js.map +1 -0
- package/build/cjs/types/convertor/structConvertor.js +3 -0
- package/build/cjs/types/convertor/structConvertor.js.map +1 -0
- package/build/cjs/types/extractor/functionExtractor.js +3 -0
- package/build/cjs/types/extractor/functionExtractor.js.map +1 -0
- package/build/cjs/types/extractor/structExtractor.js +3 -0
- package/build/cjs/types/extractor/structExtractor.js.map +1 -0
- package/build/cjs/types/{struct.js → moveTypes.js} +1 -1
- package/build/cjs/types/moveTypes.js.map +1 -0
- package/build/esm/core/Client.js +12 -14
- package/build/esm/core/Client.js.map +1 -1
- package/build/esm/core/WalletClient.js +5 -5
- package/build/esm/core/WalletClient.js.map +1 -1
- package/build/esm/core/createEntryPayload.js +17 -6
- package/build/esm/core/createEntryPayload.js.map +1 -1
- package/build/esm/core/createViewPayload.js +13 -10
- package/build/esm/core/createViewPayload.js.map +1 -1
- package/build/esm/core/index.js +4 -4
- package/build/esm/hooks/index.js +2 -2
- package/build/esm/hooks/index.js.map +1 -1
- package/build/esm/hooks/useSubmitTransaction.js +11 -9
- package/build/esm/hooks/useSubmitTransaction.js.map +1 -1
- package/build/esm/hooks/useWalletClient.js +5 -3
- package/build/esm/hooks/useWalletClient.js.map +1 -1
- package/build/esm/types/client/abiClient.js.map +1 -0
- package/build/{cjs/types → esm/types/client}/client.js.map +1 -1
- package/build/esm/types/client/walletClient.js.map +1 -0
- package/build/esm/types/convertor/argsConvertor.js +2 -0
- package/build/esm/types/convertor/argsConvertor.js.map +1 -0
- package/build/esm/types/convertor/genericConvertor.js +2 -0
- package/build/esm/types/convertor/genericConvertor.js.map +1 -0
- package/build/esm/types/convertor/returnConvertor.js +2 -0
- package/build/esm/types/convertor/returnConvertor.js.map +1 -0
- package/build/esm/types/convertor/structConvertor.js +2 -0
- package/build/esm/types/convertor/structConvertor.js.map +1 -0
- package/build/esm/types/extractor/functionExtractor.js +2 -0
- package/build/esm/types/extractor/functionExtractor.js.map +1 -0
- package/build/esm/types/extractor/structExtractor.js +2 -0
- package/build/esm/types/extractor/structExtractor.js.map +1 -0
- package/build/esm/types/moveTypes.js +2 -0
- package/build/esm/types/moveTypes.js.map +1 -0
- package/build/types/core/Client.d.ts +3 -5
- package/build/types/core/Client.d.ts.map +1 -1
- package/build/types/core/WalletClient.d.ts +2 -3
- package/build/types/core/WalletClient.d.ts.map +1 -1
- package/build/types/core/createEntryPayload.d.ts +2 -2
- package/build/types/core/createEntryPayload.d.ts.map +1 -1
- package/build/types/core/createViewPayload.d.ts +1 -2
- package/build/types/core/createViewPayload.d.ts.map +1 -1
- package/build/types/core/index.d.ts +4 -4
- package/build/types/hooks/index.d.ts +2 -2
- package/build/types/hooks/index.d.ts.map +1 -1
- package/build/types/hooks/useSubmitTransaction.d.ts +2 -2
- package/build/types/hooks/useSubmitTransaction.d.ts.map +1 -1
- package/build/types/hooks/useWalletClient.d.ts +1 -1
- package/build/types/hooks/useWalletClient.d.ts.map +1 -1
- package/build/types/types/abi.d.ts +4 -1
- package/build/types/types/abi.d.ts.map +1 -1
- package/build/types/types/client/abiClient.d.ts +33 -0
- package/build/types/types/client/abiClient.d.ts.map +1 -0
- package/build/types/types/client/client.d.ts +54 -0
- package/build/types/types/client/client.d.ts.map +1 -0
- package/build/types/types/client/walletClient.d.ts +13 -0
- package/build/types/types/client/walletClient.d.ts.map +1 -0
- package/build/types/types/common.d.ts +7 -60
- package/build/types/types/common.d.ts.map +1 -1
- package/build/types/types/convertor/argsConvertor.d.ts +21 -0
- package/build/types/types/convertor/argsConvertor.d.ts.map +1 -0
- package/build/types/types/convertor/genericConvertor.d.ts +8 -0
- package/build/types/types/convertor/genericConvertor.d.ts.map +1 -0
- package/build/types/types/convertor/returnConvertor.d.ts +21 -0
- package/build/types/types/convertor/returnConvertor.d.ts.map +1 -0
- package/build/types/types/convertor/structConvertor.d.ts +19 -0
- package/build/types/types/convertor/structConvertor.d.ts.map +1 -0
- package/build/types/types/defaultABITable.d.ts +10 -6
- package/build/types/types/defaultABITable.d.ts.map +1 -1
- package/build/types/types/extractor/functionExtractor.d.ts +52 -0
- package/build/types/types/extractor/functionExtractor.d.ts.map +1 -0
- package/build/types/types/extractor/structExtractor.d.ts +35 -0
- package/build/types/types/extractor/structExtractor.d.ts.map +1 -0
- package/build/types/types/index.d.ts +6 -5
- package/build/types/types/index.d.ts.map +1 -1
- package/build/types/types/moveTypes.d.ts +9 -0
- package/build/types/types/moveTypes.d.ts.map +1 -0
- package/package.json +1 -1
- package/src/core/Client.ts +230 -223
- package/src/core/WalletClient.ts +57 -55
- package/src/core/__tests__/option.test.ts +144 -0
- package/src/core/createEntryPayload.ts +152 -118
- package/src/core/createViewPayload.ts +112 -100
- package/src/core/index.ts +4 -4
- package/src/hooks/index.ts +5 -2
- package/src/hooks/useSubmitTransaction.ts +101 -98
- package/src/hooks/useWalletClient.ts +10 -8
- package/src/types/abi.ts +24 -21
- package/src/types/client/abiClient.ts +47 -0
- package/src/types/client/client.ts +73 -0
- package/src/types/client/walletClient.ts +18 -0
- package/src/types/common.ts +13 -156
- package/src/types/convertor/argsConvertor.ts +59 -0
- package/src/types/convertor/genericConvertor.ts +9 -0
- package/src/types/convertor/returnConvertor.ts +55 -0
- package/src/types/convertor/structConvertor.ts +82 -0
- package/src/types/defaultABITable.ts +17 -12
- package/src/types/extractor/functionExtractor.ts +86 -0
- package/src/types/extractor/structExtractor.ts +67 -0
- package/src/types/index.ts +20 -9
- package/src/types/moveTypes.ts +22 -0
- package/build/cjs/types/abiClient.js.map +0 -1
- package/build/cjs/types/struct.js.map +0 -1
- package/build/cjs/types/walletClient.js.map +0 -1
- package/build/esm/types/abiClient.js.map +0 -1
- package/build/esm/types/struct.js +0 -2
- package/build/esm/types/struct.js.map +0 -1
- package/build/esm/types/walletClient.js.map +0 -1
- package/build/types/types/abiClient.d.ts +0 -28
- package/build/types/types/abiClient.d.ts.map +0 -1
- package/build/types/types/client.d.ts +0 -24
- package/build/types/types/client.d.ts.map +0 -1
- package/build/types/types/struct.d.ts +0 -30
- package/build/types/types/struct.d.ts.map +0 -1
- package/build/types/types/walletClient.d.ts +0 -9
- package/build/types/types/walletClient.d.ts.map +0 -1
- package/src/types/abiClient.ts +0 -38
- package/src/types/client.ts +0 -32
- package/src/types/struct.ts +0 -87
- package/src/types/walletClient.ts +0 -10
- /package/build/cjs/types/{abiClient.js → client/abiClient.js} +0 -0
- /package/build/cjs/types/{client.js → client/client.js} +0 -0
- /package/build/cjs/types/{walletClient.js → client/walletClient.js} +0 -0
- /package/build/esm/types/{abiClient.js → client/abiClient.js} +0 -0
- /package/build/esm/types/{client.js → client/client.js} +0 -0
- /package/build/esm/types/{walletClient.js → client/walletClient.js} +0 -0
|
@@ -1,13 +1,18 @@
|
|
|
1
|
-
import { HexString } from
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
|
|
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
|
-
|
|
26
|
-
|
|
30
|
+
T extends ABIRoot,
|
|
31
|
+
TFuncName extends ViewFunctionName<T>,
|
|
27
32
|
>(
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
):
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
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
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
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
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
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
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
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
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
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
|
-
|
|
94
|
+
return BigInt(value);
|
|
83
95
|
}
|
|
84
96
|
|
|
85
97
|
function decodeVector(type: string, value: any[]) {
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
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
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
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
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
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
|
|
2
|
-
export { Client as MoveTsClient, createClient } from
|
|
3
|
-
export { createViewPayload } from
|
|
4
|
-
export { createEntryPayload } from
|
|
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';
|
package/src/hooks/index.ts
CHANGED
|
@@ -1,2 +1,5 @@
|
|
|
1
|
-
export {
|
|
2
|
-
|
|
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
|
|
2
|
-
import { useWallet } from
|
|
3
|
-
import { AptosClient, Types } from
|
|
4
|
-
import type { EntryPayload } from
|
|
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
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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(
|
|
14
|
-
|
|
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
|
-
|
|
21
|
+
const { signAndSubmitTransaction } = useWallet();
|
|
20
22
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
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
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
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
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
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
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
47
|
+
if (!isIdle) {
|
|
48
|
+
throw new Error('Transaction is already in progress');
|
|
49
|
+
}
|
|
50
|
+
setIsLoading(true);
|
|
51
|
+
setIsIdle(false);
|
|
55
52
|
|
|
56
|
-
|
|
57
|
-
|
|
53
|
+
// TODO: make it better. This way to determine the type is tricky.
|
|
54
|
+
const request = isRawPayload(payload) ? payload.rawPayload : payload;
|
|
58
55
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
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
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
78
|
+
const client = new AptosClient(nodeUrl);
|
|
79
|
+
const result = (await client.waitForTransactionWithResult(hash, {
|
|
80
|
+
checkSuccess: true,
|
|
81
|
+
})) as Types.Transaction_UserTransaction;
|
|
80
82
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
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
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
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
|
-
|
|
108
|
-
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
|
|
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
|
-
|
|
118
|
-
};
|
|
120
|
+
return { submitTransaction, reset, isLoading, isIdle, data, error };
|
|
121
|
+
};
|
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
import { useWallet } from
|
|
2
|
-
import { MoveTsWalletClient } from
|
|
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
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
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
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
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
|
-
|
|
24
|
+
constraints: readonly any[];
|
|
22
25
|
}
|
|
23
26
|
|
|
24
27
|
export interface ABIStruct {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
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
|
-
|
|
34
|
-
|
|
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
|
+
};
|