@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.
- package/CHANGELOG.md +6 -0
- package/README.md +44 -45
- package/build/cjs/core/Client.js +55 -41
- package/build/cjs/core/Client.js.map +1 -1
- package/build/cjs/core/WalletClient.js +20 -31
- package/build/cjs/core/WalletClient.js.map +1 -1
- package/build/cjs/core/createEntryPayload.js +9 -91
- package/build/cjs/core/createEntryPayload.js.map +1 -1
- package/build/cjs/core/createViewPayload.js +26 -52
- package/build/cjs/core/createViewPayload.js.map +1 -1
- package/build/cjs/core/index.js +2 -2
- package/build/cjs/core/index.js.map +1 -1
- package/build/cjs/ensureTypes.js.map +1 -1
- package/build/cjs/hooks/index.js.map +1 -1
- package/build/cjs/hooks/useSubmitTransaction.js +25 -46
- package/build/cjs/hooks/useSubmitTransaction.js.map +1 -1
- package/build/cjs/hooks/useWalletClient.js +2 -2
- package/build/cjs/hooks/useWalletClient.js.map +1 -1
- package/build/cjs/index.js +2 -2
- package/build/cjs/index.js.map +1 -1
- package/build/esm/core/Client.js +53 -39
- package/build/esm/core/Client.js.map +1 -1
- package/build/esm/core/WalletClient.js +20 -31
- package/build/esm/core/WalletClient.js.map +1 -1
- package/build/esm/core/createEntryPayload.js +9 -91
- package/build/esm/core/createEntryPayload.js.map +1 -1
- package/build/esm/core/createViewPayload.js +26 -52
- package/build/esm/core/createViewPayload.js.map +1 -1
- package/build/esm/core/index.js +1 -1
- package/build/esm/core/index.js.map +1 -1
- package/build/esm/ensureTypes.js.map +1 -1
- package/build/esm/hooks/index.js.map +1 -1
- package/build/esm/hooks/useSubmitTransaction.js +25 -46
- package/build/esm/hooks/useSubmitTransaction.js.map +1 -1
- package/build/esm/hooks/useWalletClient.js +2 -2
- package/build/esm/hooks/useWalletClient.js.map +1 -1
- package/build/esm/index.js +1 -1
- package/build/esm/index.js.map +1 -1
- package/build/types/core/Client.d.ts +61 -48
- package/build/types/core/Client.d.ts.map +1 -1
- package/build/types/core/WalletClient.d.ts +3 -5
- package/build/types/core/WalletClient.d.ts.map +1 -1
- package/build/types/core/createEntryPayload.d.ts +8 -8
- package/build/types/core/createEntryPayload.d.ts.map +1 -1
- package/build/types/core/createViewPayload.d.ts +4 -4
- package/build/types/core/createViewPayload.d.ts.map +1 -1
- package/build/types/core/index.d.ts +1 -1
- package/build/types/core/index.d.ts.map +1 -1
- package/build/types/ensureTypes.d.ts.map +1 -1
- package/build/types/hooks/index.d.ts +1 -1
- package/build/types/hooks/index.d.ts.map +1 -1
- package/build/types/hooks/useSubmitTransaction.d.ts +3 -11
- package/build/types/hooks/useSubmitTransaction.d.ts.map +1 -1
- package/build/types/hooks/useWalletClient.d.ts +1 -3
- package/build/types/hooks/useWalletClient.d.ts.map +1 -1
- package/build/types/index.d.ts +1 -1
- package/build/types/index.d.ts.map +1 -1
- package/build/types/types/abi.d.ts.map +1 -1
- package/build/types/types/client/abiClient.d.ts +12 -16
- package/build/types/types/client/abiClient.d.ts.map +1 -1
- package/build/types/types/client/client.d.ts +11 -23
- package/build/types/types/client/client.d.ts.map +1 -1
- package/build/types/types/convertor/argsConvertor.d.ts +1 -1
- package/build/types/types/convertor/argsConvertor.d.ts.map +1 -1
- package/build/types/types/convertor/genericConvertor.d.ts.map +1 -1
- package/build/types/types/convertor/returnConvertor.d.ts +1 -1
- package/build/types/types/index.d.ts +1 -1
- package/build/types/types/index.d.ts.map +1 -1
- package/package.json +8 -9
- package/src/core/Client.ts +110 -114
- package/src/core/WalletClient.ts +21 -31
- package/src/core/__tests__/accountResource.test.ts +56 -54
- package/src/core/__tests__/createEntryPayload.test.ts +34 -34
- package/src/core/__tests__/createViewPayload.test.ts +94 -111
- package/src/core/__tests__/option.test.ts +38 -31
- package/src/core/__tests__/submitTransaction.test.ts +58 -26
- package/src/core/__tests__/useABI.test.ts +38 -38
- package/src/core/__tests__/view.test.ts +36 -30
- package/src/core/__tests__/view_vector.test.ts +52 -50
- package/src/core/createEntryPayload.ts +24 -143
- package/src/core/createViewPayload.ts +40 -68
- package/src/core/index.ts +1 -1
- package/src/ensureTypes.ts +1 -0
- package/src/hooks/index.ts +0 -1
- package/src/hooks/useSubmitTransaction.ts +35 -68
- package/src/hooks/useWalletClient.ts +2 -2
- package/src/index.ts +1 -1
- package/src/types/abi.ts +1 -0
- package/src/types/client/abiClient.ts +12 -16
- package/src/types/client/client.ts +11 -25
- package/src/types/convertor/argsConvertor.ts +1 -1
- package/src/types/convertor/genericConvertor.ts +2 -0
- package/src/types/convertor/returnConvertor.ts +3 -3
- 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
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
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
|
|
26
|
-
*
|
|
27
|
-
*
|
|
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
|
-
|
|
39
|
-
|
|
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.
|
|
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.
|
|
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
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
}
|
|
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
|
|
20
|
+
* const payload = createViewPayload(COIN_ABI, {
|
|
23
21
|
* function: 'balance',
|
|
24
|
-
*
|
|
25
|
-
*
|
|
22
|
+
* functionArguments: ['0x1'],
|
|
23
|
+
* typeArguments: ['0x1::aptos_coin::AptosCoin'],
|
|
26
24
|
* });
|
|
27
|
-
* const [balance] = await client.view(
|
|
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.
|
|
40
|
-
|
|
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.
|
|
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.
|
|
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.
|
|
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
|
-
|
|
85
|
-
|
|
86
|
-
|
|
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
|
-
|
|
94
|
-
|
|
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
|
-
|
|
91
|
+
|
|
124
92
|
if (innerType === 'u8') {
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
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
package/src/ensureTypes.ts
CHANGED
package/src/hooks/index.ts
CHANGED
|
@@ -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
|
-
|
|
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
|
|
39
|
-
|
|
40
|
-
|
|
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
|
-
|
|
54
|
-
const request = isRawPayload(payload) ? payload.rawPayload : payload;
|
|
55
|
-
|
|
46
|
+
let result
|
|
56
47
|
try {
|
|
57
|
-
|
|
58
|
-
const { hash } = await signAndSubmitTransaction(
|
|
48
|
+
result = await signAndSubmitTransaction(
|
|
59
49
|
{
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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 = (
|
|
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,
|
|
9
|
+
? new WalletClient({ wallet, })
|
|
10
10
|
: undefined,
|
|
11
11
|
};
|
|
12
12
|
};
|
package/src/index.ts
CHANGED
package/src/types/abi.ts
CHANGED
|
@@ -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
|
-
|
|
26
|
-
|
|
27
|
-
|
|
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
|
-
|
|
34
|
-
|
|
35
|
-
account:
|
|
32
|
+
typeArguments: ExtractGenericArgsType<T, TFuncName>;
|
|
33
|
+
functionArguments: ExtractArgsTypeOmitSigner<T, TFuncName>;
|
|
34
|
+
account: Account;
|
|
36
35
|
isSimulation?: boolean;
|
|
37
|
-
}) => Promise<
|
|
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
|
-
|
|
43
|
-
account:
|
|
44
|
-
|
|
45
|
-
|
|
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
|
};
|