@thalalabs/surf 0.0.1
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/LICENSE +21 -0
- package/README.md +196 -0
- package/build/cjs/core/Client.js +80 -0
- package/build/cjs/core/Client.js.map +1 -0
- package/build/cjs/core/WalletClient.js +52 -0
- package/build/cjs/core/WalletClient.js.map +1 -0
- package/build/cjs/core/createEntryPayload.js +91 -0
- package/build/cjs/core/createEntryPayload.js.map +1 -0
- package/build/cjs/core/createViewPayload.js +78 -0
- package/build/cjs/core/createViewPayload.js.map +1 -0
- package/build/cjs/core/index.js +13 -0
- package/build/cjs/core/index.js.map +1 -0
- package/build/cjs/ensureTypes.js +40 -0
- package/build/cjs/ensureTypes.js.map +1 -0
- package/build/cjs/hooks/index.js +8 -0
- package/build/cjs/hooks/index.js.map +1 -0
- package/build/cjs/hooks/useSubmitTransaction.js +88 -0
- package/build/cjs/hooks/useSubmitTransaction.js.map +1 -0
- package/build/cjs/hooks/useWalletClient.js +14 -0
- package/build/cjs/hooks/useWalletClient.js.map +1 -0
- package/build/cjs/index.js +8 -0
- package/build/cjs/index.js.map +1 -0
- package/build/cjs/package.json +1 -0
- package/build/cjs/types/abi.js +3 -0
- package/build/cjs/types/abi.js.map +1 -0
- package/build/cjs/types/abiClient.js +3 -0
- package/build/cjs/types/abiClient.js.map +1 -0
- package/build/cjs/types/client.js +3 -0
- package/build/cjs/types/client.js.map +1 -0
- package/build/cjs/types/common.js +3 -0
- package/build/cjs/types/common.js.map +1 -0
- package/build/cjs/types/index.js +3 -0
- package/build/cjs/types/index.js.map +1 -0
- package/build/cjs/types/walletClient.js +3 -0
- package/build/cjs/types/walletClient.js.map +1 -0
- package/build/esm/core/Client.js +75 -0
- package/build/esm/core/Client.js.map +1 -0
- package/build/esm/core/WalletClient.js +48 -0
- package/build/esm/core/WalletClient.js.map +1 -0
- package/build/esm/core/createEntryPayload.js +87 -0
- package/build/esm/core/createEntryPayload.js.map +1 -0
- package/build/esm/core/createViewPayload.js +74 -0
- package/build/esm/core/createViewPayload.js.map +1 -0
- package/build/esm/core/index.js +5 -0
- package/build/esm/core/index.js.map +1 -0
- package/build/esm/ensureTypes.js +34 -0
- package/build/esm/ensureTypes.js.map +1 -0
- package/build/esm/hooks/index.js +3 -0
- package/build/esm/hooks/index.js.map +1 -0
- package/build/esm/hooks/useSubmitTransaction.js +84 -0
- package/build/esm/hooks/useSubmitTransaction.js.map +1 -0
- package/build/esm/hooks/useWalletClient.js +10 -0
- package/build/esm/hooks/useWalletClient.js.map +1 -0
- package/build/esm/index.js +2 -0
- package/build/esm/index.js.map +1 -0
- package/build/esm/package.json +1 -0
- package/build/esm/types/abi.js +2 -0
- package/build/esm/types/abi.js.map +1 -0
- package/build/esm/types/abiClient.js +2 -0
- package/build/esm/types/abiClient.js.map +1 -0
- package/build/esm/types/client.js +2 -0
- package/build/esm/types/client.js.map +1 -0
- package/build/esm/types/common.js +2 -0
- package/build/esm/types/common.js.map +1 -0
- package/build/esm/types/index.js +2 -0
- package/build/esm/types/index.js.map +1 -0
- package/build/esm/types/walletClient.js +2 -0
- package/build/esm/types/walletClient.js.map +1 -0
- package/build/types/core/Client.d.ts +20 -0
- package/build/types/core/Client.d.ts.map +1 -0
- package/build/types/core/WalletClient.d.ts +17 -0
- package/build/types/core/WalletClient.d.ts.map +1 -0
- package/build/types/core/createEntryPayload.d.ts +4 -0
- package/build/types/core/createEntryPayload.d.ts.map +1 -0
- package/build/types/core/createViewPayload.d.ts +4 -0
- package/build/types/core/createViewPayload.d.ts.map +1 -0
- package/build/types/core/index.d.ts +5 -0
- package/build/types/core/index.d.ts.map +1 -0
- package/build/types/ensureTypes.d.ts +4 -0
- package/build/types/ensureTypes.d.ts.map +1 -0
- package/build/types/hooks/index.d.ts +3 -0
- package/build/types/hooks/index.d.ts.map +1 -0
- package/build/types/hooks/useSubmitTransaction.d.ts +19 -0
- package/build/types/hooks/useSubmitTransaction.d.ts.map +1 -0
- package/build/types/hooks/useWalletClient.d.ts +8 -0
- package/build/types/hooks/useWalletClient.d.ts.map +1 -0
- package/build/types/index.d.ts +3 -0
- package/build/types/index.d.ts.map +1 -0
- package/build/types/types/abi.d.ts +31 -0
- package/build/types/types/abi.d.ts.map +1 -0
- package/build/types/types/abiClient.d.ts +18 -0
- package/build/types/types/abiClient.d.ts.map +1 -0
- package/build/types/types/client.d.ts +24 -0
- package/build/types/types/client.d.ts.map +1 -0
- package/build/types/types/common.d.ts +60 -0
- package/build/types/types/common.d.ts.map +1 -0
- package/build/types/types/index.d.ts +5 -0
- package/build/types/types/index.d.ts.map +1 -0
- package/build/types/types/walletClient.d.ts +9 -0
- package/build/types/types/walletClient.d.ts.map +1 -0
- package/package.json +78 -0
- package/src/core/Client.ts +120 -0
- package/src/core/WalletClient.ts +64 -0
- package/src/core/__tests__/createEntryPayload.test.ts +262 -0
- package/src/core/__tests__/createViewPayload.test.ts +297 -0
- package/src/core/__tests__/submitTransaction.test.ts +112 -0
- package/src/core/__tests__/useABI.test.ts +139 -0
- package/src/core/__tests__/view.test.ts +145 -0
- package/src/core/createEntryPayload.ts +128 -0
- package/src/core/createViewPayload.ts +100 -0
- package/src/core/index.ts +4 -0
- package/src/ensureTypes.ts +42 -0
- package/src/hooks/index.ts +2 -0
- package/src/hooks/useSubmitTransaction.ts +117 -0
- package/src/hooks/useWalletClient.ts +10 -0
- package/src/index.ts +10 -0
- package/src/types/abi.ts +35 -0
- package/src/types/abiClient.ts +19 -0
- package/src/types/client.ts +32 -0
- package/src/types/common.ts +157 -0
- package/src/types/index.ts +9 -0
- package/src/types/walletClient.ts +10 -0
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { useWallet } from "@aptos-labs/wallet-adapter-react";
|
|
2
|
+
import { AptosClient, Types } from "aptos";
|
|
3
|
+
import { createEntryPayload } from "./createEntryPayload";
|
|
4
|
+
import { ABIRoot, ABIWalletClient, EntryOptions, EntryPayload } from "../types";
|
|
5
|
+
|
|
6
|
+
type Wallet = ReturnType<typeof useWallet>;
|
|
7
|
+
|
|
8
|
+
export class WalletClient {
|
|
9
|
+
private wallet: Wallet;
|
|
10
|
+
private client: AptosClient;
|
|
11
|
+
|
|
12
|
+
constructor({ wallet, nodeUrl }: {
|
|
13
|
+
wallet: Wallet,
|
|
14
|
+
nodeUrl: string
|
|
15
|
+
}) {
|
|
16
|
+
this.wallet = wallet;
|
|
17
|
+
this.client = new AptosClient(nodeUrl);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
public async submitTransaction(
|
|
21
|
+
payload: EntryPayload,
|
|
22
|
+
_: EntryOptions | undefined = undefined
|
|
23
|
+
): Promise<{ hash: string }> { // TODO: make { hash: string } a individual type.
|
|
24
|
+
const request = payload.rawPayload;
|
|
25
|
+
|
|
26
|
+
// TODO: use the BCS API instead
|
|
27
|
+
const { hash } = await this.wallet.signAndSubmitTransaction({
|
|
28
|
+
type: "entry_function_payload",
|
|
29
|
+
...request,
|
|
30
|
+
arguments: request.arguments.map((arg: any) => {
|
|
31
|
+
if (Array.isArray(arg)) {
|
|
32
|
+
// TODO: support nested array, or use the BCS API instead
|
|
33
|
+
return arg.map((item: any) => item.toString());
|
|
34
|
+
} else if (typeof arg === "object") {
|
|
35
|
+
throw new Error(`a value of struct type: ${arg} is not supported`);
|
|
36
|
+
} else {
|
|
37
|
+
return arg.toString();
|
|
38
|
+
}
|
|
39
|
+
}),
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
const result = (await this.client.waitForTransactionWithResult(hash, {
|
|
43
|
+
checkSuccess: true,
|
|
44
|
+
})) as Types.Transaction_UserTransaction;
|
|
45
|
+
|
|
46
|
+
return result;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
public useABI<T extends ABIRoot>(abi: T) {
|
|
50
|
+
return new Proxy({} as ABIWalletClient<T>, {
|
|
51
|
+
get: (_, prop) => {
|
|
52
|
+
const functionName = prop.toString();
|
|
53
|
+
return (...args) => {
|
|
54
|
+
const payload = createEntryPayload(abi, {
|
|
55
|
+
function: functionName,
|
|
56
|
+
type_arguments: args[0].type_arguments,
|
|
57
|
+
arguments: args[0].arguments,
|
|
58
|
+
});
|
|
59
|
+
return this.submitTransaction(payload);
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
}
|
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
import { COIN_ABI } from "../../abi/coin";
|
|
2
|
+
import { createEntryPayload } from "../createEntryPayload";
|
|
3
|
+
|
|
4
|
+
// TODO: add struct, vector of vector, vector
|
|
5
|
+
describe('createEntryPayload', () => {
|
|
6
|
+
// Act before assertions
|
|
7
|
+
beforeAll(async () => { });
|
|
8
|
+
|
|
9
|
+
// Teardown (cleanup) after assertions
|
|
10
|
+
afterAll(() => { });
|
|
11
|
+
|
|
12
|
+
it('basic type checking', async () => {
|
|
13
|
+
// no need to run, type check only.
|
|
14
|
+
() => {
|
|
15
|
+
createEntryPayload(COIN_ABI, {
|
|
16
|
+
// @ts-expect-error abc is not a function
|
|
17
|
+
function: 'abc',
|
|
18
|
+
arguments: ['0x1', 1],
|
|
19
|
+
type_arguments: ['0x1::aptos_coin::AptosCoin'],
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
createEntryPayload(COIN_ABI, {
|
|
23
|
+
// @ts-expect-error balance is not a view function
|
|
24
|
+
function: 'balance',
|
|
25
|
+
arguments: ['0x1', 1],
|
|
26
|
+
type_arguments: ['0x1::aptos_coin::AptosCoin'],
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
createEntryPayload(COIN_ABI, {
|
|
30
|
+
function: 'transfer',
|
|
31
|
+
// @ts-expect-error require a address type
|
|
32
|
+
arguments: [1, 1],
|
|
33
|
+
type_arguments: ['0x1::aptos_coin::AptosCoin'],
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
createEntryPayload(COIN_ABI, {
|
|
37
|
+
function: 'transfer',
|
|
38
|
+
// @ts-expect-error require 2 args
|
|
39
|
+
arguments: ['0x1', 1, 1],
|
|
40
|
+
type_arguments: ['0x1::aptos_coin::AptosCoin'],
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
createEntryPayload(COIN_ABI, {
|
|
44
|
+
function: 'transfer',
|
|
45
|
+
arguments: ['0x1', 1],
|
|
46
|
+
// @ts-expect-error require a type_argument
|
|
47
|
+
type_arguments: [],
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
createEntryPayload(COIN_ABI, {
|
|
51
|
+
function: 'transfer',
|
|
52
|
+
arguments: ['0x1', 1],
|
|
53
|
+
type_arguments: ['0x1::aptos_coin::AptosCoin'],
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('number', async () => {
|
|
59
|
+
createEntryPayload(TEST_ABI, {
|
|
60
|
+
function: 'number_as_input',
|
|
61
|
+
arguments: [1, 2, 3, BigInt(4), BigInt(5), BigInt(6)],
|
|
62
|
+
type_arguments: [],
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
// no need to run, type check only.
|
|
66
|
+
() => {
|
|
67
|
+
createEntryPayload(TEST_ABI, {
|
|
68
|
+
function: 'number_as_input',
|
|
69
|
+
arguments: [
|
|
70
|
+
// @ts-expect-error the input type for u8 should be number, not bigint
|
|
71
|
+
BigInt(1),
|
|
72
|
+
// @ts-expect-error the input type for u16 should be number, not bigint
|
|
73
|
+
BigInt(2),
|
|
74
|
+
// @ts-expect-error the input type for u32 should be number, not bigint
|
|
75
|
+
BigInt(2),
|
|
76
|
+
BigInt(4), BigInt(5), BigInt(6)],
|
|
77
|
+
type_arguments: [],
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it('bool', async () => {
|
|
83
|
+
createEntryPayload(TEST_ABI, {
|
|
84
|
+
function: 'bool_as_input',
|
|
85
|
+
arguments: [true, false],
|
|
86
|
+
type_arguments: [],
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
// no need to run, type check only.
|
|
90
|
+
() => {
|
|
91
|
+
createEntryPayload(TEST_ABI, {
|
|
92
|
+
function: 'bool_as_input',
|
|
93
|
+
arguments: [
|
|
94
|
+
// @ts-expect-error invalid bool
|
|
95
|
+
1,
|
|
96
|
+
// @ts-expect-error invalid bool
|
|
97
|
+
'true',
|
|
98
|
+
],
|
|
99
|
+
type_arguments: [],
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it('address', async () => {
|
|
106
|
+
createEntryPayload(TEST_ABI, {
|
|
107
|
+
function: 'address_as_input',
|
|
108
|
+
arguments: ['0x1', '0x2'],
|
|
109
|
+
type_arguments: [],
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
// no need to run, type check only.
|
|
113
|
+
() => {
|
|
114
|
+
createEntryPayload(TEST_ABI, {
|
|
115
|
+
function: 'address_as_input',
|
|
116
|
+
arguments: [
|
|
117
|
+
// @ts-expect-error invalid address
|
|
118
|
+
1,
|
|
119
|
+
// @ts-expect-error invalid address
|
|
120
|
+
'1',
|
|
121
|
+
],
|
|
122
|
+
type_arguments: [],
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it('vector', async () => {
|
|
128
|
+
createEntryPayload(TEST_ABI, {
|
|
129
|
+
function: 'vector_as_input',
|
|
130
|
+
arguments: [
|
|
131
|
+
[],
|
|
132
|
+
[1, 2, 3],
|
|
133
|
+
[BigInt(1), BigInt(2), BigInt(3)],
|
|
134
|
+
[true, false, true],
|
|
135
|
+
['0x1', '0x2', '0x3']
|
|
136
|
+
],
|
|
137
|
+
type_arguments: [],
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
// no need to run, type check only.
|
|
141
|
+
() => {
|
|
142
|
+
createEntryPayload(TEST_ABI, {
|
|
143
|
+
function: 'vector_as_input',
|
|
144
|
+
arguments: [
|
|
145
|
+
// @ts-expect-error invalid vector<u8>
|
|
146
|
+
[1, 2, '3'],
|
|
147
|
+
// @ts-expect-error invalid vector<u16>
|
|
148
|
+
[1, 2, '3'],
|
|
149
|
+
// @ts-expect-error invalid vector<u64>
|
|
150
|
+
true,
|
|
151
|
+
// @ts-expect-error invalid vector<bool>
|
|
152
|
+
[true, false, 1],
|
|
153
|
+
// @ts-expect-error invalid vector<address>
|
|
154
|
+
['0x1', '0x2', 1],
|
|
155
|
+
],
|
|
156
|
+
type_arguments: [],
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it('vector of vector', async () => {
|
|
162
|
+
createEntryPayload(TEST_ABI, {
|
|
163
|
+
function: 'vector_of_vector_as_input',
|
|
164
|
+
arguments: [
|
|
165
|
+
[[1, 2], [3, 4], [5, 6]],
|
|
166
|
+
[['0x1', '0x2'], ['0x3', '0x4'], ['0x5', '0x6']]
|
|
167
|
+
],
|
|
168
|
+
type_arguments: [],
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
// no need to run, type check only.
|
|
172
|
+
() => {
|
|
173
|
+
createEntryPayload(TEST_ABI, {
|
|
174
|
+
function: 'vector_of_vector_as_input',
|
|
175
|
+
arguments: [
|
|
176
|
+
// @ts-expect-error invalid vector<vector<u8>>
|
|
177
|
+
[[], 1, 2, '3'],
|
|
178
|
+
// @ts-expect-error invalid vector<vector<address>>
|
|
179
|
+
[[], 1, 2, '3'],
|
|
180
|
+
],
|
|
181
|
+
type_arguments: [],
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
});
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
const TEST_ABI = {
|
|
188
|
+
"address": "0x123",
|
|
189
|
+
"name": "test",
|
|
190
|
+
"friends": [],
|
|
191
|
+
"exposed_functions": [
|
|
192
|
+
{
|
|
193
|
+
"name": "address_as_input",
|
|
194
|
+
"visibility": "public",
|
|
195
|
+
"is_entry": true,
|
|
196
|
+
"is_view": false,
|
|
197
|
+
"generic_type_params": [],
|
|
198
|
+
"params": [
|
|
199
|
+
"address",
|
|
200
|
+
"address",
|
|
201
|
+
],
|
|
202
|
+
"return": []
|
|
203
|
+
},
|
|
204
|
+
{
|
|
205
|
+
"name": "bool_as_input",
|
|
206
|
+
"visibility": "public",
|
|
207
|
+
"is_entry": true,
|
|
208
|
+
"is_view": false,
|
|
209
|
+
"generic_type_params": [],
|
|
210
|
+
"params": [
|
|
211
|
+
"bool",
|
|
212
|
+
"bool",
|
|
213
|
+
],
|
|
214
|
+
"return": []
|
|
215
|
+
},
|
|
216
|
+
{
|
|
217
|
+
"name": "number_as_input",
|
|
218
|
+
"visibility": "public",
|
|
219
|
+
"is_entry": true,
|
|
220
|
+
"is_view": false,
|
|
221
|
+
"generic_type_params": [],
|
|
222
|
+
"params": [
|
|
223
|
+
"u8",
|
|
224
|
+
"u16",
|
|
225
|
+
"u32",
|
|
226
|
+
"u64",
|
|
227
|
+
"u128",
|
|
228
|
+
"u256",
|
|
229
|
+
],
|
|
230
|
+
"return": []
|
|
231
|
+
},
|
|
232
|
+
{
|
|
233
|
+
"name": "vector_as_input",
|
|
234
|
+
"visibility": "public",
|
|
235
|
+
"is_entry": true,
|
|
236
|
+
"is_view": false,
|
|
237
|
+
"generic_type_params": [],
|
|
238
|
+
"params": [
|
|
239
|
+
"vector<u8>",
|
|
240
|
+
"vector<u16>",
|
|
241
|
+
"vector<u64>",
|
|
242
|
+
"vector<bool>",
|
|
243
|
+
"vector<address>",
|
|
244
|
+
],
|
|
245
|
+
"return": []
|
|
246
|
+
},
|
|
247
|
+
{
|
|
248
|
+
"name": "vector_of_vector_as_input",
|
|
249
|
+
"visibility": "public",
|
|
250
|
+
"is_entry": true,
|
|
251
|
+
"is_view": false,
|
|
252
|
+
"generic_type_params": [],
|
|
253
|
+
"params": [
|
|
254
|
+
"vector<vector<u8>>",
|
|
255
|
+
"vector<vector<address>>",
|
|
256
|
+
],
|
|
257
|
+
"return": []
|
|
258
|
+
},
|
|
259
|
+
],
|
|
260
|
+
|
|
261
|
+
"structs": []
|
|
262
|
+
} as const;
|
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
import { COIN_ABI } from '../../abi/coin';
|
|
2
|
+
import { createViewPayload } from '../createViewPayload';
|
|
3
|
+
|
|
4
|
+
// TODO: add struct, vector of vector
|
|
5
|
+
describe('createViewPayload', () => {
|
|
6
|
+
// Act before assertions
|
|
7
|
+
beforeAll(async () => { });
|
|
8
|
+
|
|
9
|
+
// Teardown (cleanup) after assertions
|
|
10
|
+
afterAll(() => { });
|
|
11
|
+
|
|
12
|
+
it('basic type checking', async () => {
|
|
13
|
+
// no need to run, type check only.
|
|
14
|
+
() => {
|
|
15
|
+
createViewPayload(COIN_ABI, {
|
|
16
|
+
// @ts-expect-error abc is not a function
|
|
17
|
+
function: 'abc',
|
|
18
|
+
arguments: ['0x1'],
|
|
19
|
+
type_arguments: ['0x1::aptos_coin::AptosCoin'],
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
createViewPayload(COIN_ABI, {
|
|
23
|
+
// @ts-expect-error transfer is not a view function
|
|
24
|
+
function: 'transfer',
|
|
25
|
+
arguments: ['0x1'],
|
|
26
|
+
type_arguments: ['0x1::aptos_coin::AptosCoin'],
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
createViewPayload(COIN_ABI, {
|
|
30
|
+
function: 'balance',
|
|
31
|
+
// @ts-expect-error require a address type
|
|
32
|
+
arguments: [''],
|
|
33
|
+
type_arguments: ['0x1::aptos_coin::AptosCoin'],
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
createViewPayload(COIN_ABI, {
|
|
37
|
+
function: 'balance',
|
|
38
|
+
// @ts-expect-error require 1 args
|
|
39
|
+
arguments: ['0x1', 1],
|
|
40
|
+
type_arguments: ['0x1::aptos_coin::AptosCoin'],
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
createViewPayload(COIN_ABI, {
|
|
44
|
+
function: 'balance',
|
|
45
|
+
arguments: ['0x1'],
|
|
46
|
+
// @ts-expect-error require a type_argument
|
|
47
|
+
type_arguments: [],
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it('basic', async () => {
|
|
53
|
+
const payload = createViewPayload(COIN_ABI, {
|
|
54
|
+
function: 'balance',
|
|
55
|
+
arguments: ['0x1'],
|
|
56
|
+
type_arguments: ['0x1::aptos_coin::AptosCoin'],
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
expect(payload).toMatchInlineSnapshot(`
|
|
60
|
+
{
|
|
61
|
+
"decoders": [
|
|
62
|
+
[Function],
|
|
63
|
+
],
|
|
64
|
+
"viewRequest": {
|
|
65
|
+
"arguments": [
|
|
66
|
+
"0x1",
|
|
67
|
+
],
|
|
68
|
+
"function": "0x1::coin::balance",
|
|
69
|
+
"type_arguments": [
|
|
70
|
+
"0x1::aptos_coin::AptosCoin",
|
|
71
|
+
],
|
|
72
|
+
},
|
|
73
|
+
}
|
|
74
|
+
`);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it('bool', async () => {
|
|
78
|
+
// no need to run, type check only.
|
|
79
|
+
() => {
|
|
80
|
+
createViewPayload(TEST_ABI, {
|
|
81
|
+
function: 'bool_as_input',
|
|
82
|
+
arguments: [
|
|
83
|
+
// @ts-expect-error not a boolean type
|
|
84
|
+
1,
|
|
85
|
+
// @ts-expect-error not a boolean type
|
|
86
|
+
'false',
|
|
87
|
+
],
|
|
88
|
+
type_arguments: [],
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const payload = createViewPayload(TEST_ABI, {
|
|
93
|
+
function: 'bool_as_input',
|
|
94
|
+
arguments: [true, false],
|
|
95
|
+
type_arguments: [],
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
expect(payload).toMatchInlineSnapshot(`
|
|
99
|
+
{
|
|
100
|
+
"decoders": [],
|
|
101
|
+
"viewRequest": {
|
|
102
|
+
"arguments": [
|
|
103
|
+
true,
|
|
104
|
+
false,
|
|
105
|
+
],
|
|
106
|
+
"function": "0x123::test::bool_as_input",
|
|
107
|
+
"type_arguments": [],
|
|
108
|
+
},
|
|
109
|
+
}
|
|
110
|
+
`);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it('address', async () => {
|
|
114
|
+
// no need to run, type check only.
|
|
115
|
+
() => {
|
|
116
|
+
createViewPayload(TEST_ABI, {
|
|
117
|
+
function: 'address_as_input',
|
|
118
|
+
arguments: [
|
|
119
|
+
// @ts-expect-error not a address type
|
|
120
|
+
1,
|
|
121
|
+
// @ts-expect-error not a address type
|
|
122
|
+
'1',
|
|
123
|
+
],
|
|
124
|
+
type_arguments: [],
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const payload = createViewPayload(TEST_ABI, {
|
|
129
|
+
function: 'address_as_input',
|
|
130
|
+
arguments: ['0x1', '0x2'],
|
|
131
|
+
type_arguments: [],
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
expect(payload).toMatchInlineSnapshot(`
|
|
135
|
+
{
|
|
136
|
+
"decoders": [],
|
|
137
|
+
"viewRequest": {
|
|
138
|
+
"arguments": [
|
|
139
|
+
"0x1",
|
|
140
|
+
"0x2",
|
|
141
|
+
],
|
|
142
|
+
"function": "0x123::test::address_as_input",
|
|
143
|
+
"type_arguments": [],
|
|
144
|
+
},
|
|
145
|
+
}
|
|
146
|
+
`);
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it('number', async () => {
|
|
150
|
+
// no need to run, type check only.
|
|
151
|
+
() => {
|
|
152
|
+
createViewPayload(TEST_ABI, {
|
|
153
|
+
function: 'number_as_input',
|
|
154
|
+
arguments: [
|
|
155
|
+
// @ts-expect-error not a number type
|
|
156
|
+
'1',
|
|
157
|
+
// @ts-expect-error not a number type
|
|
158
|
+
true,
|
|
159
|
+
],
|
|
160
|
+
type_arguments: [],
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const payload = createViewPayload(TEST_ABI, {
|
|
165
|
+
function: 'number_as_input',
|
|
166
|
+
arguments: [1, 2, 3, BigInt(5), BigInt(6), BigInt(7)],
|
|
167
|
+
type_arguments: [],
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
expect(payload).toMatchInlineSnapshot(`
|
|
171
|
+
{
|
|
172
|
+
"decoders": [],
|
|
173
|
+
"viewRequest": {
|
|
174
|
+
"arguments": [
|
|
175
|
+
1,
|
|
176
|
+
2,
|
|
177
|
+
3,
|
|
178
|
+
"5",
|
|
179
|
+
"6",
|
|
180
|
+
"7",
|
|
181
|
+
],
|
|
182
|
+
"function": "0x123::test::number_as_input",
|
|
183
|
+
"type_arguments": [],
|
|
184
|
+
},
|
|
185
|
+
}
|
|
186
|
+
`);
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
it('vector', async () => {
|
|
190
|
+
const payload = createViewPayload(TEST_ABI, {
|
|
191
|
+
function: 'vector_as_input',
|
|
192
|
+
arguments: [
|
|
193
|
+
[1, 2, 3],
|
|
194
|
+
[4, 5, 6],
|
|
195
|
+
[BigInt(10000000000000000000001), BigInt(10000000000000000000001)],
|
|
196
|
+
[true, false],
|
|
197
|
+
['0x1', '0x2'],
|
|
198
|
+
],
|
|
199
|
+
type_arguments: [],
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
expect(payload).toMatchInlineSnapshot(`
|
|
203
|
+
{
|
|
204
|
+
"decoders": [],
|
|
205
|
+
"viewRequest": {
|
|
206
|
+
"arguments": [
|
|
207
|
+
"0x010203",
|
|
208
|
+
[
|
|
209
|
+
4,
|
|
210
|
+
5,
|
|
211
|
+
6,
|
|
212
|
+
],
|
|
213
|
+
[
|
|
214
|
+
"10000000000000000000000",
|
|
215
|
+
"10000000000000000000000",
|
|
216
|
+
],
|
|
217
|
+
[
|
|
218
|
+
true,
|
|
219
|
+
false,
|
|
220
|
+
],
|
|
221
|
+
[
|
|
222
|
+
"0x1",
|
|
223
|
+
"0x2",
|
|
224
|
+
],
|
|
225
|
+
],
|
|
226
|
+
"function": "0x123::test::vector_as_input",
|
|
227
|
+
"type_arguments": [],
|
|
228
|
+
},
|
|
229
|
+
}
|
|
230
|
+
`);
|
|
231
|
+
});
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
const TEST_ABI = {
|
|
235
|
+
"address": "0x123",
|
|
236
|
+
"name": "test",
|
|
237
|
+
"friends": [],
|
|
238
|
+
"exposed_functions": [
|
|
239
|
+
{
|
|
240
|
+
"name": "bool_as_input",
|
|
241
|
+
"visibility": "public",
|
|
242
|
+
"is_entry": false,
|
|
243
|
+
"is_view": true,
|
|
244
|
+
"generic_type_params": [],
|
|
245
|
+
"params": [
|
|
246
|
+
"bool",
|
|
247
|
+
"bool",
|
|
248
|
+
],
|
|
249
|
+
"return": []
|
|
250
|
+
},
|
|
251
|
+
{
|
|
252
|
+
"name": "number_as_input",
|
|
253
|
+
"visibility": "public",
|
|
254
|
+
"is_entry": false,
|
|
255
|
+
"is_view": true,
|
|
256
|
+
"generic_type_params": [],
|
|
257
|
+
"params": [
|
|
258
|
+
"u8",
|
|
259
|
+
"u16",
|
|
260
|
+
"u32",
|
|
261
|
+
"u64",
|
|
262
|
+
"u128",
|
|
263
|
+
"u256",
|
|
264
|
+
],
|
|
265
|
+
"return": []
|
|
266
|
+
},
|
|
267
|
+
{
|
|
268
|
+
"name": "address_as_input",
|
|
269
|
+
"visibility": "public",
|
|
270
|
+
"is_entry": false,
|
|
271
|
+
"is_view": true,
|
|
272
|
+
"generic_type_params": [],
|
|
273
|
+
"params": [
|
|
274
|
+
"address",
|
|
275
|
+
"address",
|
|
276
|
+
],
|
|
277
|
+
"return": []
|
|
278
|
+
},
|
|
279
|
+
{
|
|
280
|
+
"name": "vector_as_input",
|
|
281
|
+
"visibility": "public",
|
|
282
|
+
"is_entry": false,
|
|
283
|
+
"is_view": true,
|
|
284
|
+
"generic_type_params": [],
|
|
285
|
+
"params": [
|
|
286
|
+
"vector<u8>",
|
|
287
|
+
"vector<u16>",
|
|
288
|
+
"vector<u64>",
|
|
289
|
+
"vector<bool>",
|
|
290
|
+
"vector<address>",
|
|
291
|
+
],
|
|
292
|
+
"return": []
|
|
293
|
+
},
|
|
294
|
+
|
|
295
|
+
],
|
|
296
|
+
"structs": []
|
|
297
|
+
} as const;
|