@scallop-io/sui-kit 0.35.2 → 0.36.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/README.md +83 -108
- package/dist/index.d.ts +6 -9
- package/dist/index.js +183 -137
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +194 -138
- package/dist/index.mjs.map +1 -1
- package/dist/{lib/sui-account-manager/sui-account-manager.d.ts → libs/suiAccountManager/index.d.ts} +1 -6
- package/dist/{lib/sui-account-manager → libs/suiAccountManager}/keypair.d.ts +1 -5
- package/dist/libs/suiAccountManager/types.d.ts +9 -0
- package/dist/{lib/sui-account-manager → libs/suiAccountManager}/util.d.ts +1 -1
- package/dist/{lib/sui-rpc-provider/default-chain-configs.d.ts → libs/suiRpcProvider/defaultChainConfigs.d.ts} +2 -2
- package/dist/{lib/sui-rpc-provider/sui-rpc-provider.d.ts → libs/suiRpcProvider/index.d.ts} +3 -16
- package/dist/libs/suiRpcProvider/types.d.ts +14 -0
- package/dist/{lib/sui-tx-builder/sui-tx-block.d.ts → libs/suiTxBuilder/index.d.ts} +6 -15
- package/dist/libs/suiTxBuilder/types.d.ts +12 -0
- package/dist/libs/suiTxBuilder/util.d.ts +76 -0
- package/dist/{lib/sui-kit.d.ts → suiKit.d.ts} +6 -28
- package/dist/types/index.d.ts +11 -0
- package/package.json +90 -8
- package/src/index.ts +10 -10
- package/src/libs/suiAccountManager/crypto.ts +7 -0
- package/src/{lib/sui-account-manager/sui-account-manager.ts → libs/suiAccountManager/index.ts} +12 -13
- package/src/{lib/sui-account-manager → libs/suiAccountManager}/keypair.ts +16 -11
- package/src/libs/suiAccountManager/types.ts +10 -0
- package/src/{lib/sui-account-manager → libs/suiAccountManager}/util.ts +14 -11
- package/src/{lib/sui-rpc-provider/default-chain-configs.ts → libs/suiRpcProvider/defaultChainConfigs.ts} +13 -12
- package/src/libs/suiRpcProvider/faucet.ts +57 -0
- package/src/{lib/sui-rpc-provider/sui-rpc-provider.ts → libs/suiRpcProvider/index.ts} +33 -28
- package/src/libs/suiRpcProvider/types.ts +17 -0
- package/src/libs/suiTxBuilder/index.ts +245 -0
- package/src/libs/suiTxBuilder/types.ts +32 -0
- package/src/libs/suiTxBuilder/util.ts +84 -0
- package/src/{lib/sui-kit.ts → suiKit.ts} +121 -40
- package/src/types/index.ts +17 -0
- package/dist/lib/sui-account-manager/index.d.ts +0 -2
- package/dist/lib/sui-rpc-provider/index.d.ts +0 -2
- package/dist/lib/sui-tx-builder/index.d.ts +0 -2
- package/dist/lib/sui-tx-builder/util.d.ts +0 -6
- package/src/lib/sui-account-manager/crypto.ts +0 -7
- package/src/lib/sui-account-manager/index.ts +0 -2
- package/src/lib/sui-rpc-provider/faucet.ts +0 -43
- package/src/lib/sui-rpc-provider/index.ts +0 -2
- package/src/lib/sui-tx-builder/index.ts +0 -2
- package/src/lib/sui-tx-builder/sui-tx-block.ts +0 -265
- package/src/lib/sui-tx-builder/util.ts +0 -26
- /package/dist/{lib/sui-account-manager → libs/suiAccountManager}/crypto.d.ts +0 -0
- /package/dist/{lib/sui-rpc-provider → libs/suiRpcProvider}/faucet.d.ts +0 -0
package/README.md
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
# Tookit for interacting with SUI network
|
|
2
2
|
|
|
3
|
-
[中文文档](./README_cn.md)
|
|
3
|
+
[中文文档](./document/README_cn.md)
|
|
4
4
|
|
|
5
5
|
## Features
|
|
6
|
+
|
|
6
7
|
- [x] Transfer SUI, Custom Coin and objects.
|
|
7
8
|
- [x] Move call
|
|
8
9
|
- [x] Programmable transaction
|
|
9
|
-
- [x] Query on-chain data
|
|
10
|
+
- [x] Query on-chain data
|
|
10
11
|
- [x] HD wallet multi-accounts
|
|
11
|
-
- [x] Publish & upgrade Move packages
|
|
12
12
|
|
|
13
13
|
## Pre-requisites
|
|
14
14
|
|
|
@@ -16,7 +16,6 @@
|
|
|
16
16
|
npm install @scallop-io/sui-kit
|
|
17
17
|
```
|
|
18
18
|
|
|
19
|
-
|
|
20
19
|
## How to use
|
|
21
20
|
|
|
22
21
|
### Init SuiKit
|
|
@@ -31,16 +30,13 @@ import { SuiKit } from '@scallop-io/sui-kit';
|
|
|
31
30
|
const secretKey = '<Secret key>';
|
|
32
31
|
const suiKit1 = new SuiKit({ secretKey });
|
|
33
32
|
|
|
34
|
-
|
|
35
33
|
// 12 or 24 words mnemonics
|
|
36
34
|
const mnemonics = '<Mnemonics>';
|
|
37
35
|
const suiKit2 = new SuiKit({ mnemonics });
|
|
38
36
|
|
|
39
|
-
|
|
40
|
-
// It will create a HD wallet with a random mnemonics
|
|
37
|
+
// It will create a HD wallet with a random mnemonics
|
|
41
38
|
const suiKit3 = new SuiKit();
|
|
42
39
|
|
|
43
|
-
|
|
44
40
|
// Override options
|
|
45
41
|
const suiKit = new SuiKit({
|
|
46
42
|
mnemonics: '<Mnemonics>',
|
|
@@ -49,16 +45,15 @@ const suiKit = new SuiKit({
|
|
|
49
45
|
// the fullnode url, default is the preconfig fullnode url for the given network type
|
|
50
46
|
fullnodeUrl: '<SUI fullnode>',
|
|
51
47
|
// the faucet url, default is the preconfig faucet url for the given network type
|
|
52
|
-
faucetUrl: '<SUI faucet url>'
|
|
48
|
+
faucetUrl: '<SUI faucet url>',
|
|
53
49
|
});
|
|
54
50
|
```
|
|
55
51
|
|
|
52
|
+
### Transfer
|
|
56
53
|
|
|
57
|
-
### Transfer
|
|
58
54
|
You can use SuiKit to transfer SUI, custom coins, and any objects.
|
|
59
55
|
|
|
60
56
|
```typescript
|
|
61
|
-
|
|
62
57
|
const recipient1 = '0x123'; // repace with real address
|
|
63
58
|
const recipient2 = '0x456'; // repace with real address
|
|
64
59
|
|
|
@@ -71,7 +66,11 @@ const coinType = '<pkgId>::custom_coin::CUSTOM_COIN';
|
|
|
71
66
|
// Transfer custom coin to single recipient
|
|
72
67
|
await suiKit.transferCoin(recipient1, 1000, coinType);
|
|
73
68
|
// Transfer custom coin to multiple recipients
|
|
74
|
-
await suiKit.transferCoinToMany(
|
|
69
|
+
await suiKit.transferCoinToMany(
|
|
70
|
+
[recipient1, recipient2],
|
|
71
|
+
[1000, 2000],
|
|
72
|
+
coinType
|
|
73
|
+
);
|
|
75
74
|
|
|
76
75
|
// Transfer objects
|
|
77
76
|
const objectIds = ['<objId1>', '<objId2>'];
|
|
@@ -79,6 +78,7 @@ await suiKit.transferObjects(objectIds, recipient1);
|
|
|
79
78
|
```
|
|
80
79
|
|
|
81
80
|
### Stake SUI
|
|
81
|
+
|
|
82
82
|
You can use SuiKit to stake SUI.
|
|
83
83
|
|
|
84
84
|
```typescript
|
|
@@ -93,6 +93,7 @@ suiKit.stakeSui(stakeAmount, validatorAddress).then(() => {
|
|
|
93
93
|
```
|
|
94
94
|
|
|
95
95
|
### Move call
|
|
96
|
+
|
|
96
97
|
You can use SuiKit to call move functions.
|
|
97
98
|
|
|
98
99
|
```typescript
|
|
@@ -101,11 +102,12 @@ const res = await suiKit.moveCall({
|
|
|
101
102
|
arguments: [coin0, coin1],
|
|
102
103
|
typeArguments: [coinType],
|
|
103
104
|
});
|
|
104
|
-
console.log(res)
|
|
105
|
+
console.log(res);
|
|
105
106
|
```
|
|
106
107
|
|
|
107
|
-
How to pass arguments?
|
|
108
|
+
How to pass arguments?
|
|
108
109
|
Suppose you have a move function like this:
|
|
110
|
+
|
|
109
111
|
```move
|
|
110
112
|
public entry fun test_args(
|
|
111
113
|
addrs: vector<address>,
|
|
@@ -118,29 +120,37 @@ public entry fun test_args(
|
|
|
118
120
|
// ...
|
|
119
121
|
}
|
|
120
122
|
```
|
|
123
|
+
|
|
121
124
|
You can pass the arguments like this:
|
|
125
|
+
|
|
122
126
|
```typescript
|
|
123
|
-
const addr1 =
|
|
124
|
-
|
|
125
|
-
const
|
|
126
|
-
|
|
127
|
+
const addr1 =
|
|
128
|
+
'0x656b875c9c072a465048fc10643470a39ba331727719df46c004973fcfb53c95';
|
|
129
|
+
const addr2 =
|
|
130
|
+
'0x10651e50cdbb4944a8fd77665d5af27f8abde6eb76a12b97444809ae4ddb1aad';
|
|
131
|
+
const coin1 =
|
|
132
|
+
'0xd4a01b597b87986b04b65e04049499b445c0ee901fe8ba310b1cf29feaa86876';
|
|
133
|
+
const coin2 =
|
|
134
|
+
'0x4d4a01b597b87986b04b65e04049499b445c0ee901fe8ba310b1cf29feaa8687';
|
|
127
135
|
suiKit.moveCall({
|
|
128
136
|
target: `${pkgId}::module::test_args`,
|
|
129
137
|
arguments: [
|
|
130
138
|
// pass vector<address>, need to specify the vecType as 'address'
|
|
131
|
-
{value: [addr1, addr2], vecType: 'address'},
|
|
139
|
+
{ value: [addr1, addr2], vecType: 'address' },
|
|
132
140
|
// pass vector<u8>, need to specify the vecType as 'u8'
|
|
133
|
-
{value: [10, 20], vecType: 'u8'},
|
|
141
|
+
{ value: [10, 20], vecType: 'u8' },
|
|
134
142
|
// pass vector<u64>, default vecType for number array is 'u64', so no need to specify
|
|
135
143
|
[34324, 234234],
|
|
136
144
|
// pass vector<bool>, default vecType for boolean array is 'bool', so no need to specify
|
|
137
145
|
[true, false],
|
|
138
146
|
// pass vector<Coin<SUI>>, no need to specify the vecType for object array
|
|
139
147
|
[coin1, coin2],
|
|
140
|
-
]
|
|
148
|
+
],
|
|
141
149
|
});
|
|
142
150
|
```
|
|
151
|
+
|
|
143
152
|
All the supported types are:
|
|
153
|
+
|
|
144
154
|
- address
|
|
145
155
|
- u8
|
|
146
156
|
- u16
|
|
@@ -152,58 +162,56 @@ All the supported types are:
|
|
|
152
162
|
- object
|
|
153
163
|
|
|
154
164
|
### Programmable transaction
|
|
165
|
+
|
|
155
166
|
With programmable transaction, you can send a transaction with multiple actions.
|
|
156
167
|
The following is an example using flashloan to make arbitrage.
|
|
157
168
|
(check [here](./examples/sample_move/custom_coin/sources/dex.move) for the corresponding Move contract code)
|
|
158
169
|
|
|
159
|
-
|
|
160
170
|
```typescript
|
|
161
|
-
import { SuiKit, SuiTxBlock } from
|
|
162
|
-
import * as process from
|
|
163
|
-
import * as dotenv from
|
|
171
|
+
import { SuiKit, SuiTxBlock } from '@scallop-io/sui-kit';
|
|
172
|
+
import * as process from 'process';
|
|
173
|
+
import * as dotenv from 'dotenv';
|
|
164
174
|
dotenv.config();
|
|
165
175
|
|
|
166
|
-
const treasuryA =
|
|
167
|
-
|
|
168
|
-
const
|
|
176
|
+
const treasuryA =
|
|
177
|
+
'0xe5042357d2c2bb928f37e4d12eac594e6d02327d565e801eaf9aca4c7340c28c';
|
|
178
|
+
const treasuryB =
|
|
179
|
+
'0xdd2f53171b8c886fad20e0bfecf1d4eede9d6c75762f169a9f3c3022e5ce7293';
|
|
180
|
+
const dexPool =
|
|
181
|
+
'0x8a13859a8d930f3238ddd31180a5f0914e5b8dbaa31e18387066b61a563fedf9';
|
|
169
182
|
|
|
170
|
-
const pkgId =
|
|
183
|
+
const pkgId =
|
|
184
|
+
'0x3c316b6af0586343ce8e6b4be890305a1f83b7e196366f6435b22b6e3fc8e3d9';
|
|
171
185
|
|
|
172
|
-
(async() => {
|
|
186
|
+
(async () => {
|
|
173
187
|
const mnemonics = process.env.MNEMONICS;
|
|
174
188
|
const suiKit = new SuiKit({ mnemonics });
|
|
175
189
|
const sender = suiKit.currentAddress();
|
|
176
|
-
|
|
190
|
+
|
|
177
191
|
const tx = new SuiTxBlock();
|
|
178
192
|
// 1. Make a flash loan for coinB
|
|
179
|
-
const[coinB, loan] = tx.moveCall(
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
);
|
|
193
|
+
const [coinB, loan] = tx.moveCall(`${pkgId}::custom_coin_b::flash_loan`, [
|
|
194
|
+
treasuryB,
|
|
195
|
+
10 ** 9,
|
|
196
|
+
]);
|
|
183
197
|
// 2. Swap from coinB to coinA, ratio is 1:1
|
|
184
|
-
const coinA = tx.moveCall(
|
|
185
|
-
`${pkgId}::dex::swap_a`,
|
|
186
|
-
[dexPool, coinB],
|
|
187
|
-
);
|
|
198
|
+
const coinA = tx.moveCall(`${pkgId}::dex::swap_a`, [dexPool, coinB]);
|
|
188
199
|
// 3. Swap from coinA back to coinB, ratio is 1:2
|
|
189
|
-
const coinB2 = tx.moveCall(
|
|
190
|
-
`${pkgId}::dex::swap_b`,
|
|
191
|
-
[dexPool, coinA]
|
|
192
|
-
);
|
|
200
|
+
const coinB2 = tx.moveCall(`${pkgId}::dex::swap_b`, [dexPool, coinA]);
|
|
193
201
|
// 4. Repay flash loan
|
|
194
202
|
const [paybackCoinB] = tx.splitCoins(coinB2, [10 ** 9]);
|
|
195
|
-
tx.moveCall(
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
203
|
+
tx.moveCall(`${pkgId}::custom_coin_b::payback_loan`, [
|
|
204
|
+
treasuryB,
|
|
205
|
+
paybackCoinB,
|
|
206
|
+
loan,
|
|
207
|
+
]);
|
|
199
208
|
// 4. Transfer profits to sender
|
|
200
209
|
tx.transferObjects([coinB2], sender);
|
|
201
|
-
|
|
210
|
+
|
|
202
211
|
// 5. Execute transaction
|
|
203
212
|
const res = await suiKit.signAndSendTxn(tx);
|
|
204
213
|
console.log(res);
|
|
205
214
|
})();
|
|
206
|
-
|
|
207
215
|
```
|
|
208
216
|
|
|
209
217
|
### Multi-accounts
|
|
@@ -219,76 +227,43 @@ import { SuiKit } from '@scallop-io/sui-kit';
|
|
|
219
227
|
|
|
220
228
|
async function checkAccounts(suiKit: SuiKit) {
|
|
221
229
|
const displayAccounts = async (suiKit: SuiKit, accountIndex: number) => {
|
|
222
|
-
const coinType = '0x2::sui::SUI'
|
|
223
|
-
const addr = suiKit.getAddress({accountIndex})
|
|
224
|
-
const balance = (await suiKit.getBalance(coinType, {accountIndex}))
|
|
225
|
-
|
|
226
|
-
|
|
230
|
+
const coinType = '0x2::sui::SUI';
|
|
231
|
+
const addr = suiKit.getAddress({ accountIndex });
|
|
232
|
+
const balance = (await suiKit.getBalance(coinType, { accountIndex }))
|
|
233
|
+
.totalBalance;
|
|
234
|
+
console.log(`Account ${accountIndex}: ${addr} has ${balance} SUI`);
|
|
235
|
+
};
|
|
227
236
|
// log the first 10 accounts
|
|
228
|
-
const numAccounts = 10
|
|
237
|
+
const numAccounts = 10;
|
|
229
238
|
for (let i = 0; i < numAccounts; i++) {
|
|
230
|
-
await displayAccounts(suiKit, i)
|
|
239
|
+
await displayAccounts(suiKit, i);
|
|
231
240
|
}
|
|
232
241
|
}
|
|
233
242
|
|
|
234
|
-
async function internalTransferSui(
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
243
|
+
async function internalTransferSui(
|
|
244
|
+
suiKit: SuiKit,
|
|
245
|
+
fromAccountIndex: number,
|
|
246
|
+
toAccountIndex: number,
|
|
247
|
+
amount: number
|
|
248
|
+
) {
|
|
249
|
+
const toAddr = suiKit.getAddress({ accountIndex: toAccountIndex });
|
|
250
|
+
console.log(
|
|
251
|
+
`Transfer ${amount} SUI from account ${fromAccountIndex} to account ${toAccountIndex}`
|
|
252
|
+
);
|
|
253
|
+
return await suiKit.transferSui(toAddr, amount, {
|
|
254
|
+
accountIndex: fromAccountIndex,
|
|
255
|
+
});
|
|
238
256
|
}
|
|
239
257
|
|
|
240
258
|
const mnemonics = process.env.MNEMONICS;
|
|
241
|
-
const suiKit = new SuiKit({ mnemonics })
|
|
242
|
-
checkAccounts(suiKit).then(() => {})
|
|
259
|
+
const suiKit = new SuiKit({ mnemonics });
|
|
260
|
+
checkAccounts(suiKit).then(() => {});
|
|
243
261
|
// transfer 1000 SUI from account 0 to account 1
|
|
244
|
-
internalTransferSui(suiKit, 0, 1, 1000).then(() => {})
|
|
262
|
+
internalTransferSui(suiKit, 0, 1, 1000).then(() => {});
|
|
245
263
|
```
|
|
246
264
|
|
|
247
265
|
### Publish & upgrade Move package
|
|
248
|
-
We have a standalone npm package to help you publish and upgrade Move package with typescript.
|
|
249
266
|
|
|
250
|
-
|
|
251
|
-
```bash
|
|
252
|
-
npm install @scallop-io/sui-package-kit
|
|
253
|
-
```
|
|
254
|
-
|
|
255
|
-
2. Install SUI cli
|
|
256
|
-
Please refer to the official documentation: [How to install SUI cli](https://docs.sui.io/devnet/build/install)
|
|
267
|
+
We have a standalone npm package to help you publish and upgrade Move package based on sui-kit.
|
|
257
268
|
|
|
258
|
-
|
|
259
|
-
/**
|
|
260
|
-
* This is an example of using SuiKit to publish a move package
|
|
261
|
-
*/
|
|
262
|
-
import { SuiKit } from "@scallop-io/sui-kit";
|
|
263
|
-
import { SuiPackagePublisher } from "@scallop-dao/sui-package-kit";
|
|
264
|
-
|
|
265
|
-
(async() => {
|
|
266
|
-
const mnemonics = '<Your mnemonics>';
|
|
267
|
-
const suiKit = new SuiKit({ mnemonics, networkType: 'devnet' });
|
|
268
|
-
|
|
269
|
-
const packagePath = path.join(__dirname, './sample_move/package_a');
|
|
270
|
-
const publisher = new SuiPackagePublisher();
|
|
271
|
-
const result = await publisher.publishPackage(packagePath, suiKit.getSigner());
|
|
272
|
-
console.log('packageId: ' + result.packageId);
|
|
273
|
-
})();
|
|
274
|
-
```
|
|
275
|
-
|
|
276
|
-
```typescript
|
|
277
|
-
/**
|
|
278
|
-
* This is an example of using SuiKit to upgrade a move package
|
|
279
|
-
*/
|
|
280
|
-
import { SuiKit } from "@scallop-io/sui-kit";
|
|
281
|
-
import { SuiPackagePublisher } from "@scallop-dao/sui-package-kit";
|
|
282
|
-
|
|
283
|
-
(async() => {
|
|
284
|
-
const mnemonics = '<Your mnemonics>';
|
|
285
|
-
const suiKit = new SuiKit({ mnemonics, networkType: 'devnet' });
|
|
286
|
-
|
|
287
|
-
const upgradeCapId = '<Package upgrade cap id>';
|
|
288
|
-
// Rember to set the 'published-at' in the package manifest
|
|
289
|
-
const packagePath = path.join(__dirname, './sample_move/package_a_upgrade');
|
|
290
|
-
const publisher = new SuiPackagePublisher();
|
|
291
|
-
const result = await publisher.upgradePackage(packagePath, upgradeCapId, { skipFetchLatestGitDeps: true });
|
|
292
|
-
console.log(result);
|
|
293
|
-
})();
|
|
294
|
-
```
|
|
269
|
+
Please refer to the repository: [sui-package-kit](https://docs.sui.io/devnet/build/install)
|
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
|
-
export { TransactionBlock, SUI_CLOCK_OBJECT_ID, SUI_SYSTEM_STATE_OBJECT_ID } from '@mysten/sui.js';
|
|
2
|
-
export { SuiKit } from './
|
|
3
|
-
export
|
|
4
|
-
export {
|
|
5
|
-
export
|
|
6
|
-
export
|
|
7
|
-
export type { SuiTxArg, SuiObjectArg } from './lib/sui-tx-builder';
|
|
8
|
-
export { SuiRpcProvider } from './lib/sui-rpc-provider';
|
|
9
|
-
export type { NetworkType } from './lib/sui-rpc-provider';
|
|
1
|
+
export { TransactionBlock, SUI_CLOCK_OBJECT_ID, SUI_SYSTEM_STATE_OBJECT_ID, } from '@mysten/sui.js';
|
|
2
|
+
export { SuiKit } from './suiKit';
|
|
3
|
+
export { SuiAccountManager } from './libs/suiAccountManager';
|
|
4
|
+
export { SuiTxBlock } from './libs/suiTxBuilder';
|
|
5
|
+
export { SuiRpcProvider } from './libs/suiRpcProvider';
|
|
6
|
+
export type * from './types';
|