@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
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -33,19 +33,17 @@
|
|
|
33
33
|
## Overview
|
|
34
34
|
|
|
35
35
|
```TypeScript
|
|
36
|
-
const client =
|
|
37
|
-
nodeUrl: 'https://fullnode.testnet.aptoslabs.com/v1',
|
|
38
|
-
});
|
|
36
|
+
const client = createSurfClient(new Aptos());
|
|
39
37
|
|
|
40
|
-
await client.useABI(COIN_ABI).entry.transfer({
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
38
|
+
const result = await client.useABI(COIN_ABI).entry.transfer({
|
|
39
|
+
functionArguments: ['0x1', 1],
|
|
40
|
+
typeArguments: ['0x1::aptos_coin::AptosCoin'],
|
|
41
|
+
account: Account.fromPrivateKey(...),
|
|
44
42
|
});
|
|
45
43
|
|
|
46
44
|
const [balance] = await client.useABI(COIN_ABI).view.balance({
|
|
47
|
-
|
|
48
|
-
|
|
45
|
+
functionArguments: ['0x1'],
|
|
46
|
+
typeArguments: ['0x1::aptos_coin::AptosCoin'],
|
|
49
47
|
});
|
|
50
48
|
```
|
|
51
49
|
|
|
@@ -58,21 +56,22 @@ When you input `client.useABI(COIN_ABI).view.` into your IDE, the auto-completio
|
|
|
58
56
|
### Installation
|
|
59
57
|
|
|
60
58
|
```shell
|
|
61
|
-
npm i @thalalabs/surf aptos
|
|
59
|
+
npm i @thalalabs/surf @aptos-labs/ts-sdk
|
|
62
60
|
```
|
|
63
61
|
|
|
64
|
-
If you want to use the React Hooks, install the `@aptos-labs/wallet-adapter-react` additionally. Those React Hooks will be moved to a separate package in near future.
|
|
62
|
+
If you want to use the React Hooks, install the `@aptos-labs/wallet-adapter-react@^2.0.0` additionally. Those React Hooks will be moved to a separate package in near future.
|
|
65
63
|
|
|
66
64
|
### Start
|
|
67
65
|
|
|
68
66
|
Create the client:
|
|
69
67
|
|
|
70
68
|
```TypeScript
|
|
71
|
-
import {
|
|
69
|
+
import { createSurfClient } from '@thalalabs/surf';
|
|
70
|
+
import { Aptos, Network, AptosConfig } from '@aptos-labs/ts-sdk';
|
|
72
71
|
|
|
73
|
-
const client =
|
|
74
|
-
|
|
75
|
-
|
|
72
|
+
const client = createSurfClient(new Aptos(
|
|
73
|
+
new AptosConfig({ network: Network.TESTNET })
|
|
74
|
+
));
|
|
76
75
|
```
|
|
77
76
|
|
|
78
77
|
Surf infers types from ABI to give you the end-to-end type-safety from your Move contract to your frontend. So firstly, you need to prepare the ABI json object of your contract in TypeScript (You can get ABI from Aptos Explorer, for example: [0x1::coin](https://explorer.aptoslabs.com/account/0x1/modules/code/coin?network=testnet)):
|
|
@@ -90,22 +89,22 @@ There are two ways to call a view function with the client:
|
|
|
90
89
|
```typescript
|
|
91
90
|
// Option 1. Use the `useABI` interface
|
|
92
91
|
const [balance] = await client.useABI(COIN_ABI).view.balance({
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
92
|
+
functionArguments: ['0x1'],
|
|
93
|
+
typeArguments: ['0x1::aptos_coin::AptosCoin'],
|
|
94
|
+
ledgerVersion: '562606728', // ledger_version is optional
|
|
96
95
|
});
|
|
97
96
|
|
|
98
97
|
// Option 2. Create payload and use the `view` interface
|
|
99
98
|
import { createViewPayload } from '@thalalabs/surf';
|
|
100
|
-
const
|
|
99
|
+
const payload = createViewPayload(COIN_ABI, {
|
|
101
100
|
function: 'balance',
|
|
102
|
-
|
|
103
|
-
|
|
101
|
+
functionArguments: ['0x1'],
|
|
102
|
+
typeArguments: ['0x1::aptos_coin::AptosCoin'],
|
|
103
|
+
});
|
|
104
|
+
const [balance] = await client.view({
|
|
105
|
+
payload,
|
|
106
|
+
options: { ledgerVersion: '562606728' }, // ledger_version is optional
|
|
104
107
|
});
|
|
105
|
-
const [balance] = await client.view(
|
|
106
|
-
viewPayload,
|
|
107
|
-
{ ledger_version: '562606728' }, // ledger_version is optional
|
|
108
|
-
);
|
|
109
108
|
```
|
|
110
109
|
|
|
111
110
|
Both of the interfaces can provide type safety.
|
|
@@ -122,23 +121,22 @@ const account = /* your AptosAccount */;
|
|
|
122
121
|
|
|
123
122
|
// Option 1. Use the `useABI` interface
|
|
124
123
|
const { hash } = await client.useABI(COIN_ABI).entry.transfer({
|
|
125
|
-
|
|
126
|
-
|
|
124
|
+
functionArguments: ['0x1', 1],
|
|
125
|
+
typeArguments: ['0x1::aptos_coin::AptosCoin'],
|
|
127
126
|
account,
|
|
128
127
|
});
|
|
129
128
|
|
|
130
129
|
// Option 2. Create payload and use the `submitTransaction` interface
|
|
131
130
|
import { createEntryPayload } from "@thalalabs/surf";
|
|
132
|
-
const
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
131
|
+
const payload = createEntryPayload(COIN_ABI, {
|
|
132
|
+
function: 'transfer',
|
|
133
|
+
functionArguments: ['0x1', 1],
|
|
134
|
+
typeArguments: ['0x1::aptos_coin::AptosCoin'],
|
|
135
|
+
});
|
|
136
|
+
const result = await client.submitTransaction({
|
|
137
|
+
payload,
|
|
138
|
+
signer: account,
|
|
136
139
|
});
|
|
137
|
-
|
|
138
|
-
const { hash } = await client.submitTransaction(
|
|
139
|
-
entryPayload,
|
|
140
|
-
{ account },
|
|
141
|
-
);
|
|
142
140
|
```
|
|
143
141
|
|
|
144
142
|
You can also simulate a transaction:
|
|
@@ -163,10 +161,11 @@ const entryPayload = createEntryPayload(COIN_ABI, {
|
|
|
163
161
|
type_arguments: ['0x1::aptos_coin::AptosCoin'],
|
|
164
162
|
});
|
|
165
163
|
|
|
166
|
-
const { hash } = await client.simulateTransaction(
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
164
|
+
const { hash } = await client.simulateTransaction({
|
|
165
|
+
payload,
|
|
166
|
+
sender: account.accountAddress,
|
|
167
|
+
publicKey: account.publicKey,
|
|
168
|
+
});
|
|
170
169
|
```
|
|
171
170
|
|
|
172
171
|
### Get account resource
|
|
@@ -175,9 +174,8 @@ To get account resource with type safety:
|
|
|
175
174
|
|
|
176
175
|
```typescript
|
|
177
176
|
const { data } = await client.useABI(COIN_ABI).resource.CoinStore({
|
|
178
|
-
|
|
177
|
+
typeArguments: ['0x1::aptos_coin::AptosCoin'],
|
|
179
178
|
account: '0x1',
|
|
180
|
-
ledger_version: '562606728', // ledger_version is optional
|
|
181
179
|
});
|
|
182
180
|
|
|
183
181
|
// Get property in the struct with type safety
|
|
@@ -192,13 +190,14 @@ Some fields of a stuct may reference external modules.To inference the type of a
|
|
|
192
190
|
|
|
193
191
|
```TypeScript
|
|
194
192
|
import { DefaultABITable } from "@thalalabs/surf";
|
|
193
|
+
import { createSurfClient } from '@thalalabs/surf';
|
|
194
|
+
import { Aptos } from '@aptos-labs/ts-sdk';
|
|
195
|
+
|
|
195
196
|
type ABITAble = DefaultABITable & {
|
|
196
197
|
'0x4dcae85fc5559071906cd5c76b7420fcbb4b0a92f00ab40ffc394aadbbff5ee9::fixed_point64': typeof FIXED_POINT64_ABI,
|
|
197
198
|
};
|
|
198
199
|
|
|
199
|
-
const client =
|
|
200
|
-
nodeUrl: 'https://fullnode.mainnet.aptoslabs.com/v1',
|
|
201
|
-
});
|
|
200
|
+
const client = createSurfClient<ABITAble>(new Aptos());
|
|
202
201
|
```
|
|
203
202
|
|
|
204
203
|
With this customized `ABITAble`, Surf can inference the struct from `0x4dcae85fc5559071906cd5c76b7420fcbb4b0a92f00ab40ffc394aadbbff5ee9::fixed_point64`.
|
package/build/cjs/core/Client.js
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Client = exports.
|
|
4
|
-
const aptos_1 = require("aptos");
|
|
3
|
+
exports.Client = exports.createSurfClient = void 0;
|
|
5
4
|
const createViewPayload_js_1 = require("./createViewPayload.js");
|
|
6
5
|
const createEntryPayload_js_1 = require("./createEntryPayload.js");
|
|
7
|
-
function
|
|
8
|
-
return new Client(
|
|
6
|
+
function createSurfClient(aptosClient) {
|
|
7
|
+
return new Client(aptosClient);
|
|
9
8
|
}
|
|
10
|
-
exports.
|
|
9
|
+
exports.createSurfClient = createSurfClient;
|
|
11
10
|
class Client {
|
|
12
11
|
constructor(client) {
|
|
13
12
|
Object.defineProperty(this, "client", {
|
|
@@ -18,27 +17,32 @@ class Client {
|
|
|
18
17
|
});
|
|
19
18
|
this.client = client;
|
|
20
19
|
}
|
|
21
|
-
async view(
|
|
22
|
-
|
|
23
|
-
return result.map((value, i) => payload.decoders[i] ? payload.decoders[i](value) : value);
|
|
20
|
+
async view(args) {
|
|
21
|
+
return await this.client.view(args);
|
|
24
22
|
}
|
|
25
|
-
async submitTransaction(
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
23
|
+
async submitTransaction(args) {
|
|
24
|
+
const transaction = await this.client.build.transaction({
|
|
25
|
+
sender: args.signer.accountAddress.toString(),
|
|
26
|
+
data: args.payload,
|
|
27
|
+
});
|
|
28
|
+
const transactionRes = await this.client.signAndSubmitTransaction({
|
|
29
|
+
signer: args.signer,
|
|
30
|
+
transaction,
|
|
31
|
+
});
|
|
32
|
+
return await this.client.waitForTransaction({
|
|
33
|
+
transactionHash: transactionRes.hash,
|
|
34
|
+
options: args.options ?? {},
|
|
32
35
|
});
|
|
33
|
-
return transactionRes;
|
|
34
36
|
}
|
|
35
|
-
async simulateTransaction(
|
|
36
|
-
const
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
37
|
+
async simulateTransaction(args) {
|
|
38
|
+
const transaction = await this.client.build.transaction({
|
|
39
|
+
sender: args.sender,
|
|
40
|
+
data: args.payload,
|
|
41
|
+
});
|
|
42
|
+
return (await this.client.simulate.transaction({
|
|
43
|
+
signerPublicKey: args.publicKey,
|
|
44
|
+
transaction,
|
|
45
|
+
}))[0];
|
|
42
46
|
}
|
|
43
47
|
useABI(abi) {
|
|
44
48
|
return {
|
|
@@ -48,11 +52,14 @@ class Client {
|
|
|
48
52
|
return (...args) => {
|
|
49
53
|
const payload = (0, createViewPayload_js_1.createViewPayload)(abi, {
|
|
50
54
|
function: functionName,
|
|
51
|
-
|
|
52
|
-
|
|
55
|
+
typeArguments: args[0].typeArguments,
|
|
56
|
+
functionArguments: args[0].functionArguments,
|
|
53
57
|
});
|
|
54
|
-
return this.view(
|
|
55
|
-
|
|
58
|
+
return this.view({
|
|
59
|
+
payload,
|
|
60
|
+
options: {
|
|
61
|
+
ledgerVersion: args[0].ledgerVersion,
|
|
62
|
+
}
|
|
56
63
|
});
|
|
57
64
|
};
|
|
58
65
|
},
|
|
@@ -63,12 +70,20 @@ class Client {
|
|
|
63
70
|
return (...args) => {
|
|
64
71
|
const payload = (0, createEntryPayload_js_1.createEntryPayload)(abi, {
|
|
65
72
|
function: functionName,
|
|
66
|
-
|
|
67
|
-
|
|
73
|
+
typeArguments: args[0].typeArguments,
|
|
74
|
+
functionArguments: args[0].functionArguments,
|
|
68
75
|
});
|
|
76
|
+
const account = args[0].account;
|
|
69
77
|
return args[0].isSimulation
|
|
70
|
-
? this.simulateTransaction(
|
|
71
|
-
|
|
78
|
+
? this.simulateTransaction({
|
|
79
|
+
publicKey: account.publicKey,
|
|
80
|
+
sender: account.accountAddress.toString(),
|
|
81
|
+
payload,
|
|
82
|
+
})
|
|
83
|
+
: this.submitTransaction({
|
|
84
|
+
signer: args[0].account,
|
|
85
|
+
payload,
|
|
86
|
+
});
|
|
72
87
|
};
|
|
73
88
|
},
|
|
74
89
|
}),
|
|
@@ -76,23 +91,22 @@ class Client {
|
|
|
76
91
|
get: (_, prop) => {
|
|
77
92
|
let structName = prop.toString();
|
|
78
93
|
return (...args) => {
|
|
79
|
-
if (args[0].
|
|
80
|
-
structName += `<${args[0].
|
|
94
|
+
if (args[0].typeArguments.length !== 0) {
|
|
95
|
+
structName += `<${args[0].typeArguments.join(',')}>`;
|
|
81
96
|
}
|
|
82
|
-
|
|
83
|
-
|
|
97
|
+
const account = args[0].account;
|
|
98
|
+
return this.client.getAccountResource({
|
|
99
|
+
accountAddress: account,
|
|
100
|
+
resourceType: `${abi.address}::${abi.name}::${structName}`,
|
|
101
|
+
options: {
|
|
102
|
+
ledgerVersion: args[0].ledgerVersion,
|
|
103
|
+
}
|
|
84
104
|
});
|
|
85
105
|
};
|
|
86
106
|
},
|
|
87
107
|
}),
|
|
88
108
|
};
|
|
89
109
|
}
|
|
90
|
-
async generateRawTxn(payload, options) {
|
|
91
|
-
const { account } = options;
|
|
92
|
-
const entryFunctionPayload = new aptos_1.TxnBuilderTypes.TransactionPayloadEntryFunction(payload.entryRequest);
|
|
93
|
-
const rawTxn = await this.client.generateRawTransaction(account.address(), entryFunctionPayload);
|
|
94
|
-
return rawTxn;
|
|
95
|
-
}
|
|
96
110
|
}
|
|
97
111
|
exports.Client = Client;
|
|
98
112
|
//# sourceMappingURL=Client.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Client.js","sourceRoot":"","sources":["../../../src/core/Client.ts"],"names":[],"mappings":";;;AAAA,
|
|
1
|
+
{"version":3,"file":"Client.js","sourceRoot":"","sources":["../../../src/core/Client.ts"],"names":[],"mappings":";;;AAAA,iEAA2D;AAC3D,mEAA6D;AAqB7D,SAAgB,gBAAgB,CAE9B,WAAkB;IAClB,OAAO,IAAI,MAAM,CAAY,WAAW,CAAC,CAAC;AAC5C,CAAC;AAJD,4CAIC;AAC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C,cAAc,CAAC,QAAQ,EAAE;gCACzC,OAAO;6BACR,CAAC;4BACF,CAAC,CAAC,IAAI,CAAC,iBAAiB,CAAC;gCACvB,MAAM,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC,OAAO;gCACvB,OAAO;6BACR,CAAC,CAAC;oBACP,CAAC,CAAC;gBACJ,CAAC;aACF,CAAC;YAWF,QAAQ,EAAE,IAAI,KAAK,CAAC,EAAqC,EAAE;gBACzD,GAAG,EAAE,CAAC,CAAC,EAAE,IAAI,EAAE,EAAE;oBACf,IAAI,UAAU,GAAG,IAAI,CAAC,QAAQ,EAAE,CAAC;oBACjC,OAAO,CAAC,GAAG,IAAI,EAAE,EAAE;wBACjB,IAAI,IAAI,CAAC,CAAC,CAAC,CAAC,aAAa,CAAC,MAAM,KAAK,CAAC,EAAE;4BACtC,UAAU,IAAI,IAAI,IAAI,CAAC,CAAC,CAAC,CAAC,aAAa,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC;yBACtD;wBAED,MAAM,OAAO,GAAwB,IAAI,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC;wBACrD,OAAO,IAAI,CAAC,MAAM,CAAC,kBAAkB,CACnC;4BACE,cAAc,EAAE,OAAO;4BACvB,YAAY,EAAE,GAAG,GAAG,CAAC,OAAO,KAAK,GAAG,CAAC,IAAI,KAAK,UAAU,EAAE;4BAC1D,OAAO,EAAE;gCACP,aAAa,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC,aAAa;6BACrC;yBACF,CACF,CAAC;oBACJ,CAAC,CAAC;gBACJ,CAAC;aACF,CAAC;SACH,CAAC;IACJ,CAAC;CACF;AAvND,wBAuNC"}
|
|
@@ -1,46 +1,35 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.WalletClient = void 0;
|
|
4
|
-
const aptos_1 = require("aptos");
|
|
5
4
|
const createEntryPayload_js_1 = require("./createEntryPayload.js");
|
|
6
5
|
class WalletClient {
|
|
7
|
-
constructor({ wallet
|
|
6
|
+
constructor({ wallet }) {
|
|
8
7
|
Object.defineProperty(this, "wallet", {
|
|
9
8
|
enumerable: true,
|
|
10
9
|
configurable: true,
|
|
11
10
|
writable: true,
|
|
12
11
|
value: void 0
|
|
13
12
|
});
|
|
14
|
-
Object.defineProperty(this, "client", {
|
|
15
|
-
enumerable: true,
|
|
16
|
-
configurable: true,
|
|
17
|
-
writable: true,
|
|
18
|
-
value: void 0
|
|
19
|
-
});
|
|
20
13
|
this.wallet = wallet;
|
|
21
|
-
this.client = new aptos_1.AptosClient(nodeUrl);
|
|
22
14
|
}
|
|
23
|
-
async submitTransaction(payload
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
15
|
+
async submitTransaction(payload) {
|
|
16
|
+
return await this.wallet.signAndSubmitTransaction({
|
|
17
|
+
sender: this.wallet.account?.address ?? "",
|
|
18
|
+
data: {
|
|
19
|
+
...payload,
|
|
20
|
+
functionArguments: payload.functionArguments.map((arg) => {
|
|
21
|
+
if (Array.isArray(arg)) {
|
|
22
|
+
return arg.map((item) => item.toString());
|
|
23
|
+
}
|
|
24
|
+
else if (typeof arg === 'object') {
|
|
25
|
+
throw new Error(`a value of struct type: ${arg} is not supported`);
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
return arg.toString();
|
|
29
|
+
}
|
|
30
|
+
}),
|
|
31
|
+
}
|
|
39
32
|
});
|
|
40
|
-
const result = (await this.client.waitForTransactionWithResult(hash, {
|
|
41
|
-
checkSuccess: true,
|
|
42
|
-
}));
|
|
43
|
-
return result;
|
|
44
33
|
}
|
|
45
34
|
useABI(abi) {
|
|
46
35
|
return new Proxy({}, {
|
|
@@ -49,8 +38,8 @@ class WalletClient {
|
|
|
49
38
|
return (...args) => {
|
|
50
39
|
const payload = (0, createEntryPayload_js_1.createEntryPayload)(abi, {
|
|
51
40
|
function: functionName,
|
|
52
|
-
|
|
53
|
-
|
|
41
|
+
typeArguments: args[0].type_arguments,
|
|
42
|
+
functionArguments: args[0].arguments,
|
|
54
43
|
});
|
|
55
44
|
return this.submitTransaction(payload);
|
|
56
45
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"WalletClient.js","sourceRoot":"","sources":["../../../src/core/WalletClient.ts"],"names":[],"mappings":";;;AACA,
|
|
1
|
+
{"version":3,"file":"WalletClient.js","sourceRoot":"","sources":["../../../src/core/WalletClient.ts"],"names":[],"mappings":";;;AACA,mEAA6D;AAS7D,MAAa,YAAY;IAGvB,YAAY,EAAE,MAAM,EAAuB;QAFnC;;;;;WAAe;QAGrB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;IAEM,KAAK,CAAC,iBAAiB,CAC5B,OAAqB;QAIrB,OAAO,MAAM,IAAI,CAAC,MAAM,CAAC,wBAAwB,CAAC;YAChD,MAAM,EAAE,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,OAAO,IAAI,EAAE;YAC1C,IAAI,EAAE;gBACJ,GAAG,OAAO;gBAEV,iBAAiB,EAAE,OAAO,CAAC,iBAAiB,CAAC,GAAG,CAAC,CAAC,GAAQ,EAAE,EAAE;oBAC5D,IAAI,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE;wBAEtB,OAAO,GAAG,CAAC,GAAG,CAAC,CAAC,IAAS,EAAE,EAAE,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAC,CAAC;qBAChD;yBAAM,IAAI,OAAO,GAAG,KAAK,QAAQ,EAAE;wBAClC,MAAM,IAAI,KAAK,CAAC,2BAA2B,GAAG,mBAAmB,CAAC,CAAC;qBACpE;yBAAM;wBACL,OAAO,GAAG,CAAC,QAAQ,EAAE,CAAC;qBACvB;gBACH,CAAC,CAAC;aACH;SACF,CAAC,CAAC;IACL,CAAC;IAEM,MAAM,CAAoB,GAAM;QACrC,OAAO,IAAI,KAAK,CAAC,EAAwB,EAAE;YACzC,GAAG,EAAE,CAAC,CAAC,EAAE,IAAI,EAAE,EAAE;gBACf,MAAM,YAAY,GAAG,IAAI,CAAC,QAAQ,EAAE,CAAC;gBACrC,OAAO,CAAC,GAAG,IAAI,EAAE,EAAE;oBACjB,MAAM,OAAO,GAAG,IAAA,0CAAkB,EAAC,GAAG,EAAE;wBACtC,QAAQ,EAAE,YAAY;wBACtB,aAAa,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC,cAAc;wBACrC,iBAAiB,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC,SAAS;qBACrC,CAAC,CAAC;oBACH,OAAO,IAAI,CAAC,iBAAiB,CAAC,OAAO,CAAC,CAAC;gBACzC,CAAC,CAAC;YACJ,CAAC;SACF,CAAC,CAAC;IACL,CAAC;CACF;AA9CD,oCA8CC"}
|
|
@@ -1,108 +1,26 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.createEntryPayload = void 0;
|
|
4
|
-
const aptos_1 = require("aptos");
|
|
5
|
-
const ensureTypes_js_1 = require("../ensureTypes.js");
|
|
6
4
|
function createEntryPayload(abi, payload) {
|
|
7
5
|
const fnAbi = abi.exposed_functions.filter((f) => f.name === payload.function)[0];
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
if (fnAbi === undefined)
|
|
7
|
+
throw new Error(`Function ${payload.function} not found in ABI`);
|
|
8
|
+
const typeArguments = payload.typeArguments;
|
|
9
|
+
const valArguments = payload.functionArguments;
|
|
10
10
|
const abiArgs = fnAbi.params[0] === '&signer'
|
|
11
11
|
? fnAbi.params.slice(1)
|
|
12
12
|
: fnAbi.params;
|
|
13
13
|
if (fnAbi === undefined)
|
|
14
14
|
throw new Error(`Function ${payload.function} not found in ABI`);
|
|
15
15
|
if (abiArgs.length !== valArguments.length)
|
|
16
|
-
throw new Error(`Function ${payload.function} expects ${fnAbi.params.length} arguments, but ${payload.
|
|
16
|
+
throw new Error(`Function ${payload.function} expects ${fnAbi.params.length} arguments, but ${payload.functionArguments.length} were provided`);
|
|
17
17
|
if (fnAbi.generic_type_params.length !== typeArguments.length)
|
|
18
|
-
throw new Error(`Function ${payload.function} expects ${fnAbi.generic_type_params.length} type arguments, but ${payload.
|
|
18
|
+
throw new Error(`Function ${payload.function} expects ${fnAbi.generic_type_params.length} type arguments, but ${payload.typeArguments.length} were provided`);
|
|
19
19
|
return {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
},
|
|
24
|
-
entryRequest: aptos_1.TxnBuilderTypes.EntryFunction.natural(`${abi.address}::${abi.name}`, payload.function, typeArguments
|
|
25
|
-
.map((arg) => {
|
|
26
|
-
const typeTagStruct = new aptos_1.TypeTagParser(arg).parseTypeTag();
|
|
27
|
-
return new aptos_1.TxnBuilderTypes.TypeTagStruct(new aptos_1.TxnBuilderTypes.StructTag(typeTagStruct.value.address, typeTagStruct.value.module_name, typeTagStruct.value.name, typeTagStruct.value.type_args));
|
|
28
|
-
}), valArguments.map((arg, i) => {
|
|
29
|
-
const type = abiArgs[i];
|
|
30
|
-
const serializer = new aptos_1.BCS.Serializer();
|
|
31
|
-
argToBCS(type, arg, serializer);
|
|
32
|
-
return serializer.getBytes();
|
|
33
|
-
})),
|
|
20
|
+
typeArguments: payload.typeArguments,
|
|
21
|
+
functionArguments: payload.functionArguments,
|
|
22
|
+
function: `${abi.address}::${abi.name}::${payload.function}`,
|
|
34
23
|
};
|
|
35
24
|
}
|
|
36
25
|
exports.createEntryPayload = createEntryPayload;
|
|
37
|
-
function argToBCS(type, arg, serializer) {
|
|
38
|
-
const vectorRegex = /vector<([^]+)>/;
|
|
39
|
-
const vectorMatch = type.match(vectorRegex);
|
|
40
|
-
if (vectorMatch) {
|
|
41
|
-
const innerType = vectorMatch[1];
|
|
42
|
-
if (innerType === 'u8') {
|
|
43
|
-
if (arg instanceof Uint8Array) {
|
|
44
|
-
serializer.serializeBytes(arg);
|
|
45
|
-
return;
|
|
46
|
-
}
|
|
47
|
-
if (typeof arg === 'string') {
|
|
48
|
-
serializer.serializeStr(arg);
|
|
49
|
-
return;
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
if (!Array.isArray(arg)) {
|
|
53
|
-
throw new Error('Invalid vector args.');
|
|
54
|
-
}
|
|
55
|
-
serializer.serializeU32AsUleb128(arg.length);
|
|
56
|
-
arg.forEach((arg) => argToBCS(innerType, arg, serializer));
|
|
57
|
-
return;
|
|
58
|
-
}
|
|
59
|
-
const objectRegex = /0x1::object::Object<([^]+)>/;
|
|
60
|
-
const objectMatch = type.match(objectRegex);
|
|
61
|
-
if (objectMatch) {
|
|
62
|
-
aptos_1.TxnBuilderTypes.AccountAddress.fromHex(arg).serialize(serializer);
|
|
63
|
-
return;
|
|
64
|
-
}
|
|
65
|
-
const optionRegex = /0x1::option::Option<([^]+)>/;
|
|
66
|
-
const optionMatch = type.match(optionRegex);
|
|
67
|
-
if (optionMatch) {
|
|
68
|
-
const innerType = optionMatch[1];
|
|
69
|
-
serializer.serializeU32AsUleb128(arg.length);
|
|
70
|
-
if (!(arg instanceof Array) || arg.length > 1) {
|
|
71
|
-
throw new Error('Invalid input value for 0x1::option::Option.');
|
|
72
|
-
}
|
|
73
|
-
arg.forEach((arg) => argToBCS(innerType, arg, serializer));
|
|
74
|
-
return;
|
|
75
|
-
}
|
|
76
|
-
switch (type) {
|
|
77
|
-
case 'bool':
|
|
78
|
-
serializer.serializeBool((0, ensureTypes_js_1.ensureBoolean)(arg));
|
|
79
|
-
break;
|
|
80
|
-
case 'address':
|
|
81
|
-
aptos_1.TxnBuilderTypes.AccountAddress.fromHex(arg).serialize(serializer);
|
|
82
|
-
break;
|
|
83
|
-
case 'u8':
|
|
84
|
-
serializer.serializeU8((0, ensureTypes_js_1.ensureNumber)(arg));
|
|
85
|
-
break;
|
|
86
|
-
case 'u16':
|
|
87
|
-
serializer.serializeU16((0, ensureTypes_js_1.ensureNumber)(arg));
|
|
88
|
-
break;
|
|
89
|
-
case 'u32':
|
|
90
|
-
serializer.serializeU32((0, ensureTypes_js_1.ensureNumber)(arg));
|
|
91
|
-
break;
|
|
92
|
-
case 'u64':
|
|
93
|
-
serializer.serializeU64((0, ensureTypes_js_1.ensureBigInt)(arg));
|
|
94
|
-
break;
|
|
95
|
-
case 'u128':
|
|
96
|
-
serializer.serializeU128((0, ensureTypes_js_1.ensureBigInt)(arg));
|
|
97
|
-
break;
|
|
98
|
-
case 'u256':
|
|
99
|
-
serializer.serializeU256((0, ensureTypes_js_1.ensureBigInt)(arg));
|
|
100
|
-
break;
|
|
101
|
-
case '0x1::string::String':
|
|
102
|
-
serializer.serializeStr(arg);
|
|
103
|
-
break;
|
|
104
|
-
default:
|
|
105
|
-
throw new Error(`type "${type}" not supported`);
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
26
|
//# sourceMappingURL=createEntryPayload.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createEntryPayload.js","sourceRoot":"","sources":["../../../src/core/createEntryPayload.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"createEntryPayload.js","sourceRoot":"","sources":["../../../src/core/createEntryPayload.ts"],"names":[],"mappings":";;;AA2BA,SAAgB,kBAAkB,CAGhC,GAAM,EAAE,OAA0C;IAClD,MAAM,KAAK,GAAG,GAAG,CAAC,iBAAiB,CAAC,MAAM,CACxC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,OAAO,CAAC,QAAQ,CACnC,CAAC,CAAC,CAAC,CAAC;IAEL,IAAI,KAAK,KAAK,SAAS;QACrB,MAAM,IAAI,KAAK,CAAC,YAAY,OAAO,CAAC,QAAQ,mBAAmB,CAAC,CAAC;IAGnE,MAAM,aAAa,GAAa,OAAO,CAAC,aAAsB,CAAC;I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