@0xobelisk/client 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.
Files changed (38) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +1 -0
  3. package/dist/index.d.ts +6 -0
  4. package/dist/index.js +757 -0
  5. package/dist/index.js.map +1 -0
  6. package/dist/index.mjs +755 -0
  7. package/dist/index.mjs.map +1 -0
  8. package/dist/libs/suiAccountManager/crypto.d.ts +1 -0
  9. package/dist/libs/suiAccountManager/index.d.ts +35 -0
  10. package/dist/libs/suiAccountManager/keypair.d.ts +21 -0
  11. package/dist/libs/suiAccountManager/types.d.ts +9 -0
  12. package/dist/libs/suiAccountManager/util.d.ts +29 -0
  13. package/dist/libs/suiRpcProvider/defaultChainConfigs.d.ts +8 -0
  14. package/dist/libs/suiRpcProvider/faucet.d.ts +8 -0
  15. package/dist/libs/suiRpcProvider/index.d.ts +40 -0
  16. package/dist/libs/suiRpcProvider/types.d.ts +14 -0
  17. package/dist/libs/suiTxBuilder/index.d.ts +544 -0
  18. package/dist/libs/suiTxBuilder/types.d.ts +12 -0
  19. package/dist/libs/suiTxBuilder/util.d.ts +76 -0
  20. package/dist/obelisk.d.ts +2857 -0
  21. package/dist/test/tsconfig.tsbuildinfo +1 -0
  22. package/dist/types/index.d.ts +11 -0
  23. package/package.json +152 -0
  24. package/src/index.ts +10 -0
  25. package/src/libs/suiAccountManager/crypto.ts +7 -0
  26. package/src/libs/suiAccountManager/index.ts +72 -0
  27. package/src/libs/suiAccountManager/keypair.ts +38 -0
  28. package/src/libs/suiAccountManager/types.ts +10 -0
  29. package/src/libs/suiAccountManager/util.ts +70 -0
  30. package/src/libs/suiRpcProvider/defaultChainConfigs.ts +30 -0
  31. package/src/libs/suiRpcProvider/faucet.ts +57 -0
  32. package/src/libs/suiRpcProvider/index.ts +114 -0
  33. package/src/libs/suiRpcProvider/types.ts +17 -0
  34. package/src/libs/suiTxBuilder/index.ts +245 -0
  35. package/src/libs/suiTxBuilder/types.ts +32 -0
  36. package/src/libs/suiTxBuilder/util.ts +84 -0
  37. package/src/obelisk.ts +297 -0
  38. package/src/types/index.ts +17 -0
package/src/obelisk.ts ADDED
@@ -0,0 +1,297 @@
1
+ /**
2
+ * @file src.ts
3
+ * @description This file is used to aggregate the tools that used to interact with SUI network.
4
+ * @author IceFox
5
+ * @version 0.1.0
6
+ */
7
+ import {
8
+ RawSigner,
9
+ TransactionBlock,
10
+ DevInspectResults,
11
+ SuiTransactionBlockResponse, JsonRpcProvider, testnetConnection, Ed25519Keypair,
12
+ } from '@mysten/sui.js';
13
+ import { SuiAccountManager } from './libs/suiAccountManager';
14
+ import { SuiRpcProvider } from './libs/suiRpcProvider';
15
+ import { SuiTxBlock } from './libs/suiTxBuilder';
16
+ import { SuiKitParams, DerivePathParams, SuiTxArg, SuiVecTxArg } from './types';
17
+
18
+ /**
19
+ * @class SuiKit
20
+ * @description This class is used to aggregate the tools that used to interact with SUI network.
21
+ */
22
+ export class Obelisk {
23
+ public accountManager: SuiAccountManager;
24
+ public rpcProvider: SuiRpcProvider;
25
+
26
+ /**
27
+ * Support the following ways to init the SuiToolkit:
28
+ * 1. mnemonics
29
+ * 2. secretKey (base64 or hex)
30
+ * If none of them is provided, will generate a random mnemonics with 24 words.
31
+ *
32
+ * @param mnemonics, 12 or 24 mnemonics words, separated by space
33
+ * @param secretKey, base64 or hex string, when mnemonics is provided, secretKey will be ignored
34
+ * @param networkType, 'testnet' | 'mainnet' | 'devnet' | 'localnet', default is 'devnet'
35
+ * @param fullnodeUrl, the fullnode url, default is the preconfig fullnode url for the given network type
36
+ * @param faucetUrl, the faucet url, default is the preconfig faucet url for the given network type
37
+ */
38
+ constructor({
39
+ mnemonics,
40
+ secretKey,
41
+ networkType,
42
+ fullnodeUrl,
43
+ faucetUrl,
44
+ }: SuiKitParams = {}) {
45
+ // Init the account manager
46
+ this.accountManager = new SuiAccountManager({ mnemonics, secretKey });
47
+ // Init the rpc provider
48
+ this.rpcProvider = new SuiRpcProvider({
49
+ fullnodeUrl,
50
+ faucetUrl,
51
+ networkType,
52
+ });
53
+ }
54
+
55
+ /**
56
+ * if derivePathParams is not provided or mnemonics is empty, it will return the currentSigner.
57
+ * else:
58
+ * it will generate signer from the mnemonic with the given derivePathParams.
59
+ * @param derivePathParams, such as { accountIndex: 2, isExternal: false, addressIndex: 10 }, comply with the BIP44 standard
60
+ */
61
+ getSigner(derivePathParams?: DerivePathParams) {
62
+ const keyPair = this.accountManager.getKeyPair(derivePathParams);
63
+ return new RawSigner(keyPair, this.rpcProvider.provider);
64
+ }
65
+
66
+ /**
67
+ * @description Switch the current account with the given derivePathParams
68
+ * @param derivePathParams, such as { accountIndex: 2, isExternal: false, addressIndex: 10 }, comply with the BIP44 standard
69
+ */
70
+ switchAccount(derivePathParams: DerivePathParams) {
71
+ this.accountManager.switchAccount(derivePathParams);
72
+ }
73
+
74
+ /**
75
+ * @description Get the address of the account for the given derivePathParams
76
+ * @param derivePathParams, such as { accountIndex: 2, isExternal: false, addressIndex: 10 }, comply with the BIP44 standard
77
+ */
78
+ getAddress(derivePathParams?: DerivePathParams) {
79
+ return this.accountManager.getAddress(derivePathParams);
80
+ }
81
+ currentAddress() {
82
+ return this.accountManager.currentAddress;
83
+ }
84
+
85
+ provider() {
86
+ return this.rpcProvider.provider;
87
+ }
88
+
89
+ /**
90
+ * Request some SUI from faucet
91
+ * @Returns {Promise<boolean>}, true if the request is successful, false otherwise.
92
+ */
93
+ async requestFaucet(derivePathParams?: DerivePathParams) {
94
+ const addr = this.accountManager.getAddress(derivePathParams);
95
+ return this.rpcProvider.requestFaucet(addr);
96
+ }
97
+
98
+ async getBalance(coinType?: string, derivePathParams?: DerivePathParams) {
99
+ const owner = this.accountManager.getAddress(derivePathParams);
100
+ return this.rpcProvider.getBalance(owner, coinType);
101
+ }
102
+
103
+ async getObjects(objectIds: string[]) {
104
+ return this.rpcProvider.getObjects(objectIds);
105
+ }
106
+
107
+ async signTxn(
108
+ tx: Uint8Array | TransactionBlock | SuiTxBlock,
109
+ derivePathParams?: DerivePathParams
110
+ ) {
111
+ tx = tx instanceof SuiTxBlock ? tx.txBlock : tx;
112
+ const signer = this.getSigner(derivePathParams);
113
+ return signer.signTransactionBlock({ transactionBlock: tx });
114
+ }
115
+
116
+ async signAndSendTxn(
117
+ tx: Uint8Array | TransactionBlock | SuiTxBlock,
118
+ derivePathParams?: DerivePathParams
119
+ ): Promise<SuiTransactionBlockResponse> {
120
+ tx = tx instanceof SuiTxBlock ? tx.txBlock : tx;
121
+ const signer = this.getSigner(derivePathParams);
122
+ return signer.signAndExecuteTransactionBlock({
123
+ transactionBlock: tx,
124
+ options: {
125
+ showEffects: true,
126
+ showEvents: true,
127
+ showObjectChanges: true,
128
+ },
129
+ });
130
+ }
131
+
132
+ /**
133
+ * Transfer the given amount of SUI to the recipient
134
+ * @param recipient
135
+ * @param amount
136
+ * @param derivePathParams
137
+ */
138
+ async transferSui(
139
+ recipient: string,
140
+ amount: number,
141
+ derivePathParams?: DerivePathParams
142
+ ) {
143
+ const tx = new SuiTxBlock();
144
+ tx.transferSui(recipient, amount);
145
+ return this.signAndSendTxn(tx, derivePathParams);
146
+ }
147
+
148
+ /**
149
+ * Transfer to mutliple recipients
150
+ * @param recipients the recipients addresses
151
+ * @param amounts the amounts of SUI to transfer to each recipient, the length of amounts should be the same as the length of recipients
152
+ * @param derivePathParams
153
+ */
154
+ async transferSuiToMany(
155
+ recipients: string[],
156
+ amounts: number[],
157
+ derivePathParams?: DerivePathParams
158
+ ) {
159
+ const tx = new SuiTxBlock();
160
+ tx.transferSuiToMany(recipients, amounts);
161
+ return this.signAndSendTxn(tx, derivePathParams);
162
+ }
163
+
164
+ /**
165
+ * Transfer the given amounts of coin to multiple recipients
166
+ * @param recipients the list of recipient address
167
+ * @param amounts the amounts to transfer for each recipient
168
+ * @param coinType any custom coin type but not SUI
169
+ * @param derivePathParams the derive path params for the current signer
170
+ */
171
+ async transferCoinToMany(
172
+ recipients: string[],
173
+ amounts: number[],
174
+ coinType: string,
175
+ derivePathParams?: DerivePathParams
176
+ ) {
177
+ const tx = new SuiTxBlock();
178
+ const owner = this.accountManager.getAddress(derivePathParams);
179
+ const totalAmount = amounts.reduce((a, b) => a + b, 0);
180
+ const coins = await this.rpcProvider.selectCoins(
181
+ owner,
182
+ totalAmount,
183
+ coinType
184
+ );
185
+ tx.transferCoinToMany(
186
+ coins.map((c) => c.objectId),
187
+ owner,
188
+ recipients,
189
+ amounts
190
+ );
191
+ return this.signAndSendTxn(tx, derivePathParams);
192
+ }
193
+
194
+ async transferCoin(
195
+ recipient: string,
196
+ amount: number,
197
+ coinType: string,
198
+ derivePathParams?: DerivePathParams
199
+ ) {
200
+ return this.transferCoinToMany(
201
+ [recipient],
202
+ [amount],
203
+ coinType,
204
+ derivePathParams
205
+ );
206
+ }
207
+
208
+ async transferObjects(
209
+ objects: string[],
210
+ recipient: string,
211
+ derivePathParams?: DerivePathParams
212
+ ) {
213
+ const tx = new SuiTxBlock();
214
+ tx.transferObjects(objects, recipient);
215
+ return this.signAndSendTxn(tx, derivePathParams);
216
+ }
217
+
218
+ async moveCall(callParams: {
219
+ target: string;
220
+ arguments?: (SuiTxArg | SuiVecTxArg)[];
221
+ typeArguments?: string[];
222
+ derivePathParams?: DerivePathParams;
223
+ }) {
224
+ const {
225
+ target,
226
+ arguments: args = [],
227
+ typeArguments = [],
228
+ derivePathParams,
229
+ } = callParams;
230
+ const tx = new SuiTxBlock();
231
+ tx.moveCall(target, args, typeArguments);
232
+ return this.signAndSendTxn(tx, derivePathParams);
233
+ }
234
+
235
+ /**
236
+ * Select coins with the given amount and coin type, the total amount is greater than or equal to the given amount
237
+ * @param amount
238
+ * @param coinType
239
+ * @param owner
240
+ */
241
+ async selectCoinsWithAmount(
242
+ amount: number,
243
+ coinType: string,
244
+ owner?: string
245
+ ) {
246
+ owner = owner || this.accountManager.currentAddress;
247
+ const coins = await this.rpcProvider.selectCoins(owner, amount, coinType);
248
+ return coins.map((c) => c.objectId);
249
+ }
250
+
251
+ /**
252
+ * stake the given amount of SUI to the validator
253
+ * @param amount the amount of SUI to stake
254
+ * @param validatorAddr the validator address
255
+ * @param derivePathParams the derive path params for the current signer
256
+ */
257
+ async stakeSui(
258
+ amount: number,
259
+ validatorAddr: string,
260
+ derivePathParams?: DerivePathParams
261
+ ) {
262
+ const tx = new SuiTxBlock();
263
+ tx.stakeSui(amount, validatorAddr);
264
+ return this.signAndSendTxn(tx, derivePathParams);
265
+ }
266
+
267
+ /**
268
+ * Execute the transaction with on-chain data but without really submitting. Useful for querying the effects of a transaction.
269
+ * Since the transaction is not submitted, its gas cost is not charged.
270
+ * @param tx the transaction to execute
271
+ * @param derivePathParams the derive path params
272
+ * @returns the effects and events of the transaction, such as object changes, gas cost, event emitted.
273
+ */
274
+ async inspectTxn(
275
+ tx: Uint8Array | TransactionBlock | SuiTxBlock,
276
+ derivePathParams?: DerivePathParams
277
+ ): Promise<DevInspectResults> {
278
+ tx = tx instanceof SuiTxBlock ? tx.txBlock : tx;
279
+ return this.rpcProvider.provider.devInspectTransactionBlock({
280
+ transactionBlock: tx,
281
+ sender: this.getAddress(derivePathParams),
282
+ });
283
+ }
284
+
285
+ async call(world_id: string, system_name: string, counter: any,derivePathParams?: DerivePathParams) {
286
+ const tx = new TransactionBlock();
287
+ tx.moveCall({
288
+ target: `${world_id}::system::${system_name}`,
289
+ arguments: [
290
+ // txb.pure(manager),
291
+ tx.pure(counter),
292
+ ],
293
+ })
294
+ return this.signAndSendTxn(tx, derivePathParams);
295
+ }
296
+
297
+ }
@@ -0,0 +1,17 @@
1
+ import type { NetworkType as SuiNetworkType } from '../libs/suiRpcProvider/types';
2
+
3
+ export type { DerivePathParams } from '../libs/suiAccountManager/types';
4
+ export type {
5
+ SuiTxArg,
6
+ SuiVecTxArg,
7
+ SuiObjectArg,
8
+ } from '../libs/suiTxBuilder/types';
9
+
10
+ export type NetworkType = SuiNetworkType;
11
+ export type SuiKitParams = {
12
+ mnemonics?: string;
13
+ secretKey?: string;
14
+ fullnodeUrl?: string;
15
+ faucetUrl?: string;
16
+ networkType?: NetworkType;
17
+ };