@0xobelisk/client 0.3.3 → 0.3.4
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/dist/index.js +1281 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +1263 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +1 -1
package/dist/index.js
ADDED
|
@@ -0,0 +1,1281 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
21
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
22
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
23
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
24
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
25
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
26
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
27
|
+
mod
|
|
28
|
+
));
|
|
29
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
30
|
+
var __accessCheck = (obj, member, msg) => {
|
|
31
|
+
if (!member.has(obj))
|
|
32
|
+
throw TypeError("Cannot " + msg);
|
|
33
|
+
};
|
|
34
|
+
var __privateGet = (obj, member, getter) => {
|
|
35
|
+
__accessCheck(obj, member, "read from private field");
|
|
36
|
+
return getter ? getter.call(obj) : member.get(obj);
|
|
37
|
+
};
|
|
38
|
+
var __privateAdd = (obj, member, value) => {
|
|
39
|
+
if (member.has(obj))
|
|
40
|
+
throw TypeError("Cannot add the same private member more than once");
|
|
41
|
+
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
// src/index.ts
|
|
45
|
+
var src_exports = {};
|
|
46
|
+
__export(src_exports, {
|
|
47
|
+
Ed25519Keypair: () => import_ed25519.Ed25519Keypair,
|
|
48
|
+
Obelisk: () => Obelisk,
|
|
49
|
+
SuiAccountManager: () => SuiAccountManager,
|
|
50
|
+
SuiContractFactory: () => SuiContractFactory,
|
|
51
|
+
SuiTxBlock: () => SuiTxBlock,
|
|
52
|
+
getMetadata: () => getMetadata
|
|
53
|
+
});
|
|
54
|
+
module.exports = __toCommonJS(src_exports);
|
|
55
|
+
__reExport(src_exports, require("@mysten/sui.js"), module.exports);
|
|
56
|
+
var import_ed25519 = require("@mysten/sui.js/keypairs/ed25519");
|
|
57
|
+
__reExport(src_exports, require("@mysten/bcs"), module.exports);
|
|
58
|
+
|
|
59
|
+
// src/obelisk.ts
|
|
60
|
+
var import_sui9 = require("@mysten/sui.js");
|
|
61
|
+
|
|
62
|
+
// src/libs/suiAccountManager/index.ts
|
|
63
|
+
var import_sui3 = require("@mysten/sui.js");
|
|
64
|
+
|
|
65
|
+
// src/libs/suiAccountManager/keypair.ts
|
|
66
|
+
var import_sui = require("@mysten/sui.js");
|
|
67
|
+
var getDerivePathForSUI = (derivePathParams = {}) => {
|
|
68
|
+
const {
|
|
69
|
+
accountIndex = 0,
|
|
70
|
+
isExternal = false,
|
|
71
|
+
addressIndex = 0
|
|
72
|
+
} = derivePathParams;
|
|
73
|
+
return `m/44'/784'/${accountIndex}'/${isExternal ? 1 : 0}'/${addressIndex}'`;
|
|
74
|
+
};
|
|
75
|
+
var getKeyPair = (mnemonics, derivePathParams = {}) => {
|
|
76
|
+
const derivePath = getDerivePathForSUI(derivePathParams);
|
|
77
|
+
return import_sui.Ed25519Keypair.deriveKeypair(mnemonics, derivePath);
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
// src/libs/suiAccountManager/util.ts
|
|
81
|
+
var import_sui2 = require("@mysten/sui.js");
|
|
82
|
+
var isHex = (str) => /^0x[0-9a-fA-F]+$|^[0-9a-fA-F]+$/.test(str);
|
|
83
|
+
var isBase64 = (str) => /^[a-zA-Z0-9+/]+={0,2}$/g.test(str);
|
|
84
|
+
var fromHEX = (hexStr) => {
|
|
85
|
+
if (!hexStr) {
|
|
86
|
+
throw new Error("cannot parse empty string to Uint8Array");
|
|
87
|
+
}
|
|
88
|
+
const intArr = hexStr.replace("0x", "").match(/.{1,2}/g)?.map((byte) => parseInt(byte, 16));
|
|
89
|
+
if (!intArr || intArr.length === 0) {
|
|
90
|
+
throw new Error(`Unable to parse HEX: ${hexStr}`);
|
|
91
|
+
}
|
|
92
|
+
return Uint8Array.from(intArr);
|
|
93
|
+
};
|
|
94
|
+
var hexOrBase64ToUint8Array = (str) => {
|
|
95
|
+
if (isHex(str)) {
|
|
96
|
+
return fromHEX(str);
|
|
97
|
+
} else if (isBase64(str)) {
|
|
98
|
+
return (0, import_sui2.fromB64)(str);
|
|
99
|
+
} else {
|
|
100
|
+
throw new Error("The string is not a valid hex or base64 string.");
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
var PRIVATE_KEY_SIZE = 32;
|
|
104
|
+
var LEGACY_PRIVATE_KEY_SIZE = 64;
|
|
105
|
+
var normalizePrivateKey = (key) => {
|
|
106
|
+
if (key.length === LEGACY_PRIVATE_KEY_SIZE) {
|
|
107
|
+
key = key.slice(0, PRIVATE_KEY_SIZE);
|
|
108
|
+
} else if (key.length === PRIVATE_KEY_SIZE + 1 && key[0] === 0) {
|
|
109
|
+
return key.slice(1);
|
|
110
|
+
} else if (key.length === PRIVATE_KEY_SIZE) {
|
|
111
|
+
return key;
|
|
112
|
+
}
|
|
113
|
+
throw new Error("invalid secret key");
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
// src/libs/suiAccountManager/crypto.ts
|
|
117
|
+
var import_bip39 = require("@scure/bip39");
|
|
118
|
+
var import_english = require("@scure/bip39/wordlists/english");
|
|
119
|
+
var generateMnemonic = (numberOfWords = 24) => {
|
|
120
|
+
const strength = numberOfWords === 12 ? 128 : 256;
|
|
121
|
+
return (0, import_bip39.generateMnemonic)(import_english.wordlist, strength);
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
// src/libs/suiAccountManager/index.ts
|
|
125
|
+
var SuiAccountManager = class {
|
|
126
|
+
/**
|
|
127
|
+
* Support the following ways to init the SuiToolkit:
|
|
128
|
+
* 1. mnemonics
|
|
129
|
+
* 2. secretKey (base64 or hex)
|
|
130
|
+
* If none of them is provided, will generate a random mnemonics with 24 words.
|
|
131
|
+
*
|
|
132
|
+
* @param mnemonics, 12 or 24 mnemonics words, separated by space
|
|
133
|
+
* @param secretKey, base64 or hex string, when mnemonics is provided, secretKey will be ignored
|
|
134
|
+
*/
|
|
135
|
+
constructor({ mnemonics, secretKey } = {}) {
|
|
136
|
+
this.mnemonics = mnemonics || "";
|
|
137
|
+
this.secretKey = secretKey || "";
|
|
138
|
+
if (!this.mnemonics && !this.secretKey) {
|
|
139
|
+
this.mnemonics = generateMnemonic(24);
|
|
140
|
+
}
|
|
141
|
+
this.currentKeyPair = this.secretKey ? import_sui3.Ed25519Keypair.fromSecretKey(
|
|
142
|
+
normalizePrivateKey(hexOrBase64ToUint8Array(this.secretKey))
|
|
143
|
+
) : getKeyPair(this.mnemonics);
|
|
144
|
+
this.currentAddress = this.currentKeyPair.getPublicKey().toSuiAddress();
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* if derivePathParams is not provided or mnemonics is empty, it will return the currentKeyPair.
|
|
148
|
+
* else:
|
|
149
|
+
* it will generate keyPair from the mnemonic with the given derivePathParams.
|
|
150
|
+
*/
|
|
151
|
+
getKeyPair(derivePathParams) {
|
|
152
|
+
if (!derivePathParams || !this.mnemonics)
|
|
153
|
+
return this.currentKeyPair;
|
|
154
|
+
return getKeyPair(this.mnemonics, derivePathParams);
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* if derivePathParams is not provided or mnemonics is empty, it will return the currentAddress.
|
|
158
|
+
* else:
|
|
159
|
+
* it will generate address from the mnemonic with the given derivePathParams.
|
|
160
|
+
*/
|
|
161
|
+
getAddress(derivePathParams) {
|
|
162
|
+
if (!derivePathParams || !this.mnemonics)
|
|
163
|
+
return this.currentAddress;
|
|
164
|
+
return getKeyPair(this.mnemonics, derivePathParams).getPublicKey().toSuiAddress();
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Switch the current account with the given derivePathParams.
|
|
168
|
+
* This is only useful when the mnemonics is provided. For secretKey mode, it will always use the same account.
|
|
169
|
+
*/
|
|
170
|
+
switchAccount(derivePathParams) {
|
|
171
|
+
if (this.mnemonics) {
|
|
172
|
+
this.currentKeyPair = getKeyPair(this.mnemonics, derivePathParams);
|
|
173
|
+
this.currentAddress = this.currentKeyPair.getPublicKey().toSuiAddress();
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
// src/libs/suiTxBuilder/index.ts
|
|
179
|
+
var import_sui5 = require("@mysten/sui.js");
|
|
180
|
+
|
|
181
|
+
// src/libs/suiTxBuilder/util.ts
|
|
182
|
+
var import_sui4 = require("@mysten/sui.js");
|
|
183
|
+
var getDefaultSuiInputType = (value) => {
|
|
184
|
+
if (typeof value === "string" && value.startsWith("0x")) {
|
|
185
|
+
return "object";
|
|
186
|
+
} else if (typeof value === "number" || typeof value === "bigint") {
|
|
187
|
+
return "u64";
|
|
188
|
+
} else if (typeof value === "boolean") {
|
|
189
|
+
return "bool";
|
|
190
|
+
} else {
|
|
191
|
+
return "object";
|
|
192
|
+
}
|
|
193
|
+
};
|
|
194
|
+
function makeVecParam(txBlock, args, type) {
|
|
195
|
+
if (args.length === 0)
|
|
196
|
+
throw new Error("Transaction builder error: Empty array is not allowed");
|
|
197
|
+
const defaultSuiType = getDefaultSuiInputType(args[0]);
|
|
198
|
+
if (type === "object" || !type && defaultSuiType === "object") {
|
|
199
|
+
const objects = args.map(
|
|
200
|
+
(arg) => typeof arg === "string" ? txBlock.object((0, import_sui4.normalizeSuiObjectId)(arg)) : arg
|
|
201
|
+
);
|
|
202
|
+
return txBlock.makeMoveVec({ objects });
|
|
203
|
+
} else {
|
|
204
|
+
const vecType = type || defaultSuiType;
|
|
205
|
+
return txBlock.pure(args, `vector<${vecType}>`);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
function isMoveVecArg(arg) {
|
|
209
|
+
const isFullMoveVecArg = arg && arg.value && Array.isArray(arg.value) && arg.vecType;
|
|
210
|
+
const isSimpleMoveVecArg = Array.isArray(arg);
|
|
211
|
+
return isFullMoveVecArg || isSimpleMoveVecArg;
|
|
212
|
+
}
|
|
213
|
+
function convertArgs(txBlock, args) {
|
|
214
|
+
return args.map((arg) => {
|
|
215
|
+
if (typeof arg === "string" && arg.startsWith("0x")) {
|
|
216
|
+
return txBlock.object((0, import_sui4.normalizeSuiObjectId)(arg));
|
|
217
|
+
} else if (isMoveVecArg(arg)) {
|
|
218
|
+
const vecType = arg.vecType || void 0;
|
|
219
|
+
return vecType ? makeVecParam(txBlock, arg.value, vecType) : makeVecParam(txBlock, arg);
|
|
220
|
+
} else if (typeof arg !== "object") {
|
|
221
|
+
return txBlock.pure(arg);
|
|
222
|
+
} else {
|
|
223
|
+
return arg;
|
|
224
|
+
}
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// src/libs/suiTxBuilder/index.ts
|
|
229
|
+
var SuiTxBlock = class {
|
|
230
|
+
constructor(transaction) {
|
|
231
|
+
this.txBlock = new import_sui5.TransactionBlock(transaction);
|
|
232
|
+
}
|
|
233
|
+
//======== override methods of TransactionBlock ============
|
|
234
|
+
address(value) {
|
|
235
|
+
return this.txBlock.pure(value, "address");
|
|
236
|
+
}
|
|
237
|
+
pure(value, type) {
|
|
238
|
+
return this.txBlock.pure(value, type);
|
|
239
|
+
}
|
|
240
|
+
object(value) {
|
|
241
|
+
return this.txBlock.object(value);
|
|
242
|
+
}
|
|
243
|
+
objectRef(ref) {
|
|
244
|
+
return this.txBlock.objectRef(ref);
|
|
245
|
+
}
|
|
246
|
+
sharedObjectRef(ref) {
|
|
247
|
+
return this.txBlock.sharedObjectRef(ref);
|
|
248
|
+
}
|
|
249
|
+
setSender(sender) {
|
|
250
|
+
return this.txBlock.setSender(sender);
|
|
251
|
+
}
|
|
252
|
+
setSenderIfNotSet(sender) {
|
|
253
|
+
return this.txBlock.setSenderIfNotSet(sender);
|
|
254
|
+
}
|
|
255
|
+
setExpiration(expiration) {
|
|
256
|
+
return this.txBlock.setExpiration(expiration);
|
|
257
|
+
}
|
|
258
|
+
setGasPrice(price) {
|
|
259
|
+
return this.txBlock.setGasPrice(price);
|
|
260
|
+
}
|
|
261
|
+
setGasBudget(budget) {
|
|
262
|
+
return this.txBlock.setGasBudget(budget);
|
|
263
|
+
}
|
|
264
|
+
setGasOwner(owner) {
|
|
265
|
+
return this.txBlock.setGasOwner(owner);
|
|
266
|
+
}
|
|
267
|
+
setGasPayment(payments) {
|
|
268
|
+
return this.txBlock.setGasPayment(payments);
|
|
269
|
+
}
|
|
270
|
+
add(transaction) {
|
|
271
|
+
return this.txBlock.add(transaction);
|
|
272
|
+
}
|
|
273
|
+
serialize() {
|
|
274
|
+
return this.txBlock.serialize();
|
|
275
|
+
}
|
|
276
|
+
build(params = {}) {
|
|
277
|
+
return this.txBlock.build(params);
|
|
278
|
+
}
|
|
279
|
+
getDigest({ provider } = {}) {
|
|
280
|
+
return this.txBlock.getDigest({ provider });
|
|
281
|
+
}
|
|
282
|
+
get gas() {
|
|
283
|
+
return this.txBlock.gas;
|
|
284
|
+
}
|
|
285
|
+
get blockData() {
|
|
286
|
+
return this.txBlock.blockData;
|
|
287
|
+
}
|
|
288
|
+
transferObjects(objects, recipient) {
|
|
289
|
+
const tx = this.txBlock;
|
|
290
|
+
tx.transferObjects(convertArgs(this.txBlock, objects), tx.pure(recipient));
|
|
291
|
+
return this;
|
|
292
|
+
}
|
|
293
|
+
splitCoins(coin, amounts) {
|
|
294
|
+
const tx = this.txBlock;
|
|
295
|
+
const coinObject = convertArgs(this.txBlock, [coin])[0];
|
|
296
|
+
const res = tx.splitCoins(
|
|
297
|
+
coinObject,
|
|
298
|
+
amounts.map((m) => tx.pure(m))
|
|
299
|
+
);
|
|
300
|
+
return amounts.map((_, i) => res[i]);
|
|
301
|
+
}
|
|
302
|
+
mergeCoins(destination, sources) {
|
|
303
|
+
const destinationObject = convertArgs(this.txBlock, [destination])[0];
|
|
304
|
+
const sourceObjects = convertArgs(this.txBlock, sources);
|
|
305
|
+
return this.txBlock.mergeCoins(destinationObject, sourceObjects);
|
|
306
|
+
}
|
|
307
|
+
publish(...args) {
|
|
308
|
+
return this.txBlock.publish(...args);
|
|
309
|
+
}
|
|
310
|
+
upgrade(...args) {
|
|
311
|
+
return this.txBlock.upgrade(...args);
|
|
312
|
+
}
|
|
313
|
+
makeMoveVec(...args) {
|
|
314
|
+
return this.txBlock.makeMoveVec(...args);
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* @description Move call
|
|
318
|
+
* @param target `${string}::${string}::${string}`, e.g. `0x3::sui_system::request_add_stake`
|
|
319
|
+
* @param args the arguments of the move call, such as `['0x1', '0x2']`
|
|
320
|
+
* @param typeArgs the type arguments of the move call, such as `['0x2::sui::SUI']`
|
|
321
|
+
*/
|
|
322
|
+
moveCall(target, args = [], typeArgs = []) {
|
|
323
|
+
const regex = /(?<package>[a-zA-Z0-9]+)::(?<module>[a-zA-Z0-9_]+)::(?<function>[a-zA-Z0-9_]+)/;
|
|
324
|
+
const match = target.match(regex);
|
|
325
|
+
if (match === null)
|
|
326
|
+
throw new Error(
|
|
327
|
+
"Invalid target format. Expected `${string}::${string}::${string}`"
|
|
328
|
+
);
|
|
329
|
+
const convertedArgs = convertArgs(this.txBlock, args);
|
|
330
|
+
const tx = this.txBlock;
|
|
331
|
+
return tx.moveCall({
|
|
332
|
+
target,
|
|
333
|
+
arguments: convertedArgs,
|
|
334
|
+
typeArguments: typeArgs
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
//======== enhance methods ============
|
|
338
|
+
transferSuiToMany(recipients, amounts) {
|
|
339
|
+
if (recipients.length !== amounts.length) {
|
|
340
|
+
throw new Error(
|
|
341
|
+
"transferSuiToMany: recipients.length !== amounts.length"
|
|
342
|
+
);
|
|
343
|
+
}
|
|
344
|
+
const tx = this.txBlock;
|
|
345
|
+
const coins = tx.splitCoins(
|
|
346
|
+
tx.gas,
|
|
347
|
+
amounts.map((amount) => tx.pure(amount))
|
|
348
|
+
);
|
|
349
|
+
recipients.forEach((recipient, index) => {
|
|
350
|
+
tx.transferObjects([coins[index]], tx.pure(recipient));
|
|
351
|
+
});
|
|
352
|
+
return this;
|
|
353
|
+
}
|
|
354
|
+
transferSui(recipient, amount) {
|
|
355
|
+
return this.transferSuiToMany([recipient], [amount]);
|
|
356
|
+
}
|
|
357
|
+
takeAmountFromCoins(coins, amount) {
|
|
358
|
+
const tx = this.txBlock;
|
|
359
|
+
const coinObjects = convertArgs(this.txBlock, coins);
|
|
360
|
+
const mergedCoin = coinObjects[0];
|
|
361
|
+
if (coins.length > 1) {
|
|
362
|
+
tx.mergeCoins(mergedCoin, coinObjects.slice(1));
|
|
363
|
+
}
|
|
364
|
+
const [sendCoin] = tx.splitCoins(mergedCoin, [tx.pure(amount)]);
|
|
365
|
+
return [sendCoin, mergedCoin];
|
|
366
|
+
}
|
|
367
|
+
splitSUIFromGas(amounts) {
|
|
368
|
+
const tx = this.txBlock;
|
|
369
|
+
return tx.splitCoins(
|
|
370
|
+
tx.gas,
|
|
371
|
+
amounts.map((m) => tx.pure(m))
|
|
372
|
+
);
|
|
373
|
+
}
|
|
374
|
+
splitMultiCoins(coins, amounts) {
|
|
375
|
+
const tx = this.txBlock;
|
|
376
|
+
const coinObjects = convertArgs(this.txBlock, coins);
|
|
377
|
+
const mergedCoin = coinObjects[0];
|
|
378
|
+
if (coins.length > 1) {
|
|
379
|
+
tx.mergeCoins(mergedCoin, coinObjects.slice(1));
|
|
380
|
+
}
|
|
381
|
+
const splitedCoins = tx.splitCoins(
|
|
382
|
+
mergedCoin,
|
|
383
|
+
amounts.map((m) => tx.pure(m))
|
|
384
|
+
);
|
|
385
|
+
return { splitedCoins, mergedCoin };
|
|
386
|
+
}
|
|
387
|
+
transferCoinToMany(inputCoins, sender, recipients, amounts) {
|
|
388
|
+
if (recipients.length !== amounts.length) {
|
|
389
|
+
throw new Error(
|
|
390
|
+
"transferSuiToMany: recipients.length !== amounts.length"
|
|
391
|
+
);
|
|
392
|
+
}
|
|
393
|
+
const tx = this.txBlock;
|
|
394
|
+
const { splitedCoins, mergedCoin } = this.splitMultiCoins(
|
|
395
|
+
inputCoins,
|
|
396
|
+
amounts
|
|
397
|
+
);
|
|
398
|
+
recipients.forEach((recipient, index) => {
|
|
399
|
+
tx.transferObjects([splitedCoins[index]], tx.pure(recipient));
|
|
400
|
+
});
|
|
401
|
+
tx.transferObjects([mergedCoin], tx.pure(sender));
|
|
402
|
+
return this;
|
|
403
|
+
}
|
|
404
|
+
transferCoin(inputCoins, sender, recipient, amount) {
|
|
405
|
+
return this.transferCoinToMany(inputCoins, sender, [recipient], [amount]);
|
|
406
|
+
}
|
|
407
|
+
stakeSui(amount, validatorAddr) {
|
|
408
|
+
const tx = this.txBlock;
|
|
409
|
+
const [stakeCoin] = tx.splitCoins(tx.gas, [tx.pure(amount)]);
|
|
410
|
+
tx.moveCall({
|
|
411
|
+
target: "0x3::sui_system::request_add_stake",
|
|
412
|
+
arguments: [
|
|
413
|
+
tx.object(import_sui5.SUI_SYSTEM_STATE_OBJECT_ID),
|
|
414
|
+
stakeCoin,
|
|
415
|
+
tx.pure(validatorAddr)
|
|
416
|
+
]
|
|
417
|
+
});
|
|
418
|
+
return tx;
|
|
419
|
+
}
|
|
420
|
+
};
|
|
421
|
+
|
|
422
|
+
// src/libs/suiInteractor/suiInteractor.ts
|
|
423
|
+
var import_sui7 = require("@mysten/sui.js");
|
|
424
|
+
var import_faucet = require("@mysten/sui.js/faucet");
|
|
425
|
+
|
|
426
|
+
// src/libs/suiModel/suiOwnedObject.ts
|
|
427
|
+
var import_sui6 = require("@mysten/sui.js");
|
|
428
|
+
var SuiOwnedObject = class {
|
|
429
|
+
constructor(param) {
|
|
430
|
+
this.objectId = param.objectId;
|
|
431
|
+
this.version = param.version;
|
|
432
|
+
this.digest = param.digest;
|
|
433
|
+
}
|
|
434
|
+
/**
|
|
435
|
+
* Check if the object is fully initialized.
|
|
436
|
+
* So that when it's used as an input, it won't be necessary to fetch from fullnode again.
|
|
437
|
+
* Which can save time when sending transactions.
|
|
438
|
+
*/
|
|
439
|
+
isFullObject() {
|
|
440
|
+
return !!this.version && !!this.digest;
|
|
441
|
+
}
|
|
442
|
+
asCallArg() {
|
|
443
|
+
if (!this.version || !this.digest) {
|
|
444
|
+
return this.objectId;
|
|
445
|
+
}
|
|
446
|
+
return {
|
|
447
|
+
Object: {
|
|
448
|
+
ImmOrOwned: {
|
|
449
|
+
objectId: this.objectId,
|
|
450
|
+
version: this.version,
|
|
451
|
+
digest: this.digest
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
};
|
|
455
|
+
}
|
|
456
|
+
/**
|
|
457
|
+
* Update object version & digest based on the transaction response.
|
|
458
|
+
* @param txResponse
|
|
459
|
+
*/
|
|
460
|
+
updateFromTxResponse(txResponse) {
|
|
461
|
+
const changes = (0, import_sui6.getObjectChanges)(txResponse);
|
|
462
|
+
if (!changes) {
|
|
463
|
+
throw new Error("Bad transaction response!");
|
|
464
|
+
}
|
|
465
|
+
for (const change of changes) {
|
|
466
|
+
if (change.type === "mutated" && change.objectId === this.objectId) {
|
|
467
|
+
this.digest = change.digest;
|
|
468
|
+
this.version = change.version;
|
|
469
|
+
return;
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
throw new Error("Could not find object in transaction response!");
|
|
473
|
+
}
|
|
474
|
+
};
|
|
475
|
+
|
|
476
|
+
// src/libs/suiModel/suiSharedObject.ts
|
|
477
|
+
var SuiSharedObject = class {
|
|
478
|
+
constructor(param) {
|
|
479
|
+
this.objectId = param.objectId;
|
|
480
|
+
this.initialSharedVersion = param.initialSharedVersion;
|
|
481
|
+
}
|
|
482
|
+
asCallArg(mutable = false) {
|
|
483
|
+
if (!this.initialSharedVersion) {
|
|
484
|
+
return this.objectId;
|
|
485
|
+
}
|
|
486
|
+
return {
|
|
487
|
+
Object: {
|
|
488
|
+
Shared: {
|
|
489
|
+
objectId: this.objectId,
|
|
490
|
+
initialSharedVersion: this.initialSharedVersion,
|
|
491
|
+
mutable
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
};
|
|
495
|
+
}
|
|
496
|
+
};
|
|
497
|
+
|
|
498
|
+
// src/libs/suiInteractor/util.ts
|
|
499
|
+
var delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
500
|
+
|
|
501
|
+
// src/libs/suiInteractor/suiInteractor.ts
|
|
502
|
+
var SuiInteractor = class {
|
|
503
|
+
constructor(fullNodeUrls, network) {
|
|
504
|
+
if (fullNodeUrls.length === 0)
|
|
505
|
+
throw new Error("fullNodeUrls must not be empty");
|
|
506
|
+
this.providers = fullNodeUrls.map(
|
|
507
|
+
(url) => new import_sui7.JsonRpcProvider(new import_sui7.Connection({ fullnode: url }))
|
|
508
|
+
);
|
|
509
|
+
this.currentProvider = this.providers[0];
|
|
510
|
+
this.network = network;
|
|
511
|
+
}
|
|
512
|
+
switchToNextProvider() {
|
|
513
|
+
const currentProviderIdx = this.providers.indexOf(this.currentProvider);
|
|
514
|
+
this.currentProvider = this.providers[(currentProviderIdx + 1) % this.providers.length];
|
|
515
|
+
}
|
|
516
|
+
async sendTx(transactionBlock, signature) {
|
|
517
|
+
const txResOptions = {
|
|
518
|
+
showEvents: true,
|
|
519
|
+
showEffects: true,
|
|
520
|
+
showObjectChanges: true,
|
|
521
|
+
showBalanceChanges: true
|
|
522
|
+
};
|
|
523
|
+
for (const provider of this.providers) {
|
|
524
|
+
try {
|
|
525
|
+
const res = await provider.executeTransactionBlock({
|
|
526
|
+
transactionBlock,
|
|
527
|
+
signature,
|
|
528
|
+
options: txResOptions
|
|
529
|
+
});
|
|
530
|
+
return res;
|
|
531
|
+
} catch (err) {
|
|
532
|
+
console.warn(
|
|
533
|
+
`Failed to send transaction with fullnode ${provider.connection.fullnode}: ${err}`
|
|
534
|
+
);
|
|
535
|
+
await delay(2e3);
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
throw new Error("Failed to send transaction with all fullnodes");
|
|
539
|
+
}
|
|
540
|
+
async getObjects(ids) {
|
|
541
|
+
const options = {
|
|
542
|
+
showContent: true,
|
|
543
|
+
showDisplay: true,
|
|
544
|
+
showType: true,
|
|
545
|
+
showOwner: true
|
|
546
|
+
};
|
|
547
|
+
for (const provider of this.providers) {
|
|
548
|
+
try {
|
|
549
|
+
const objects = await provider.multiGetObjects({ ids, options });
|
|
550
|
+
const parsedObjects = objects.map((object) => {
|
|
551
|
+
const objectId = (0, import_sui7.getObjectId)(object);
|
|
552
|
+
const objectType = (0, import_sui7.getObjectType)(object);
|
|
553
|
+
const objectVersion = (0, import_sui7.getObjectVersion)(object);
|
|
554
|
+
const objectDigest = object.data ? object.data.digest : void 0;
|
|
555
|
+
const initialSharedVersion = (0, import_sui7.getSharedObjectInitialVersion)(object);
|
|
556
|
+
const objectFields = (0, import_sui7.getObjectFields)(object);
|
|
557
|
+
const objectDisplay = (0, import_sui7.getObjectDisplay)(object);
|
|
558
|
+
return {
|
|
559
|
+
objectId,
|
|
560
|
+
objectType,
|
|
561
|
+
objectVersion,
|
|
562
|
+
objectDigest,
|
|
563
|
+
objectFields,
|
|
564
|
+
objectDisplay,
|
|
565
|
+
initialSharedVersion
|
|
566
|
+
};
|
|
567
|
+
});
|
|
568
|
+
return parsedObjects;
|
|
569
|
+
} catch (err) {
|
|
570
|
+
await delay(2e3);
|
|
571
|
+
console.warn(
|
|
572
|
+
`Failed to get objects with fullnode ${provider.connection.fullnode}: ${err}`
|
|
573
|
+
);
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
throw new Error("Failed to get objects with all fullnodes");
|
|
577
|
+
}
|
|
578
|
+
async getObject(id) {
|
|
579
|
+
const objects = await this.getObjects([id]);
|
|
580
|
+
return objects[0];
|
|
581
|
+
}
|
|
582
|
+
async getEntitiesObjects(ids) {
|
|
583
|
+
const options = {
|
|
584
|
+
showContent: true,
|
|
585
|
+
showType: true
|
|
586
|
+
};
|
|
587
|
+
for (const provider of this.providers) {
|
|
588
|
+
try {
|
|
589
|
+
const objects = await provider.multiGetObjects({ ids, options });
|
|
590
|
+
const parsedObjects = objects.map((object) => {
|
|
591
|
+
const objectId = (0, import_sui7.getObjectId)(object);
|
|
592
|
+
const objectFields = (0, import_sui7.getObjectFields)(object);
|
|
593
|
+
const index = objectFields.name;
|
|
594
|
+
const key = objectFields.value;
|
|
595
|
+
return {
|
|
596
|
+
objectId,
|
|
597
|
+
index,
|
|
598
|
+
key
|
|
599
|
+
};
|
|
600
|
+
});
|
|
601
|
+
return parsedObjects;
|
|
602
|
+
} catch (err) {
|
|
603
|
+
await delay(2e3);
|
|
604
|
+
console.warn(
|
|
605
|
+
`Failed to get objects with fullnode ${provider.connection.fullnode}: ${err}`
|
|
606
|
+
);
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
throw new Error("Failed to get objects with all fullnodes");
|
|
610
|
+
}
|
|
611
|
+
async getDynamicFieldObject(parentId, name) {
|
|
612
|
+
for (const provider of this.providers) {
|
|
613
|
+
try {
|
|
614
|
+
return provider.getDynamicFieldObject({ parentId, name });
|
|
615
|
+
} catch (err) {
|
|
616
|
+
await delay(2e3);
|
|
617
|
+
console.warn(
|
|
618
|
+
`Failed to get objects with fullnode ${provider.connection.fullnode}: ${err}`
|
|
619
|
+
);
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
throw new Error("Failed to get objects with all fullnodes");
|
|
623
|
+
}
|
|
624
|
+
async getDynamicFields(parentId, cursor, limit) {
|
|
625
|
+
for (const provider of this.providers) {
|
|
626
|
+
try {
|
|
627
|
+
return provider.getDynamicFields({ parentId, cursor, limit });
|
|
628
|
+
} catch (err) {
|
|
629
|
+
await delay(2e3);
|
|
630
|
+
console.warn(
|
|
631
|
+
`Failed to get objects with fullnode ${provider.connection.fullnode}: ${err}`
|
|
632
|
+
);
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
throw new Error("Failed to get objects with all fullnodes");
|
|
636
|
+
}
|
|
637
|
+
async getOwnedObjects(owner, cursor, limit) {
|
|
638
|
+
for (const provider of this.providers) {
|
|
639
|
+
try {
|
|
640
|
+
return await provider.getOwnedObjects({ owner, cursor, limit });
|
|
641
|
+
} catch (err) {
|
|
642
|
+
await delay(2e3);
|
|
643
|
+
console.warn(
|
|
644
|
+
`Failed to get objects with fullnode ${provider.connection.fullnode}: ${err}`
|
|
645
|
+
);
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
throw new Error("Failed to get objects with all fullnodes");
|
|
649
|
+
}
|
|
650
|
+
async getNormalizedMoveModulesByPackage(packageId) {
|
|
651
|
+
for (const provider of this.providers) {
|
|
652
|
+
try {
|
|
653
|
+
return provider.getNormalizedMoveModulesByPackage({
|
|
654
|
+
package: packageId
|
|
655
|
+
});
|
|
656
|
+
} catch (err) {
|
|
657
|
+
await delay(2e3);
|
|
658
|
+
console.warn(
|
|
659
|
+
`Failed to get objects with fullnode ${provider.connection.fullnode}: ${err}`
|
|
660
|
+
);
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
throw new Error("Failed to get objects with all fullnodes");
|
|
664
|
+
}
|
|
665
|
+
/**
|
|
666
|
+
* @description Update objects in a batch
|
|
667
|
+
* @param suiObjects
|
|
668
|
+
*/
|
|
669
|
+
async updateObjects(suiObjects) {
|
|
670
|
+
const objectIds = suiObjects.map((obj) => obj.objectId);
|
|
671
|
+
const objects = await this.getObjects(objectIds);
|
|
672
|
+
for (const object of objects) {
|
|
673
|
+
const suiObject = suiObjects.find(
|
|
674
|
+
(obj) => obj.objectId === object.objectId
|
|
675
|
+
);
|
|
676
|
+
if (suiObject instanceof SuiSharedObject) {
|
|
677
|
+
suiObject.initialSharedVersion = object.initialSharedVersion;
|
|
678
|
+
} else if (suiObject instanceof SuiOwnedObject) {
|
|
679
|
+
suiObject.version = object.objectVersion;
|
|
680
|
+
suiObject.digest = object.objectDigest;
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
/**
|
|
685
|
+
* @description Select coins that add up to the given amount.
|
|
686
|
+
* @param addr the address of the owner
|
|
687
|
+
* @param amount the amount that is needed for the coin
|
|
688
|
+
* @param coinType the coin type, default is '0x2::SUI::SUI'
|
|
689
|
+
*/
|
|
690
|
+
async selectCoins(addr, amount, coinType = "0x2::SUI::SUI") {
|
|
691
|
+
const selectedCoins = [];
|
|
692
|
+
let totalAmount = 0;
|
|
693
|
+
let hasNext = true, nextCursor = null;
|
|
694
|
+
while (hasNext && totalAmount < amount) {
|
|
695
|
+
const coins = await this.currentProvider.getCoins({
|
|
696
|
+
owner: addr,
|
|
697
|
+
coinType,
|
|
698
|
+
cursor: nextCursor
|
|
699
|
+
});
|
|
700
|
+
coins.data.sort((a, b) => parseInt(b.balance) - parseInt(a.balance));
|
|
701
|
+
for (const coinData of coins.data) {
|
|
702
|
+
selectedCoins.push({
|
|
703
|
+
objectId: coinData.coinObjectId,
|
|
704
|
+
digest: coinData.digest,
|
|
705
|
+
version: coinData.version
|
|
706
|
+
});
|
|
707
|
+
totalAmount = totalAmount + parseInt(coinData.balance);
|
|
708
|
+
if (totalAmount >= amount) {
|
|
709
|
+
break;
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
nextCursor = coins.nextCursor;
|
|
713
|
+
hasNext = coins.hasNextPage;
|
|
714
|
+
}
|
|
715
|
+
if (!selectedCoins.length) {
|
|
716
|
+
throw new Error("No valid coins found for the transaction.");
|
|
717
|
+
}
|
|
718
|
+
return selectedCoins;
|
|
719
|
+
}
|
|
720
|
+
async requestFaucet(address, network) {
|
|
721
|
+
await (0, import_faucet.requestSuiFromFaucetV0)({
|
|
722
|
+
host: (0, import_faucet.getFaucetHost)(network),
|
|
723
|
+
recipient: address
|
|
724
|
+
});
|
|
725
|
+
}
|
|
726
|
+
};
|
|
727
|
+
|
|
728
|
+
// src/libs/suiInteractor/defaultConfig.ts
|
|
729
|
+
var import_sui8 = require("@mysten/sui.js");
|
|
730
|
+
var defaultGasBudget = 10 ** 8;
|
|
731
|
+
var getDefaultConnection = (networkType = "devnet") => {
|
|
732
|
+
switch (networkType) {
|
|
733
|
+
case "localnet":
|
|
734
|
+
return import_sui8.localnetConnection;
|
|
735
|
+
case "devnet":
|
|
736
|
+
return import_sui8.devnetConnection;
|
|
737
|
+
case "testnet":
|
|
738
|
+
return import_sui8.testnetConnection;
|
|
739
|
+
case "mainnet":
|
|
740
|
+
return import_sui8.mainnetConnection;
|
|
741
|
+
default:
|
|
742
|
+
return import_sui8.devnetConnection;
|
|
743
|
+
}
|
|
744
|
+
};
|
|
745
|
+
|
|
746
|
+
// src/libs/suiContractFactory/index.ts
|
|
747
|
+
var SuiContractFactory = class {
|
|
748
|
+
// readonly #query: MapMessageQuery<ApiTypes> = {};
|
|
749
|
+
// readonly #tx: MapMessageTx<ApiTypes> = {};
|
|
750
|
+
/**
|
|
751
|
+
* Support the following ways to init the SuiToolkit:
|
|
752
|
+
* 1. mnemonics
|
|
753
|
+
* 2. secretKey (base64 or hex)
|
|
754
|
+
* If none of them is provided, will generate a random mnemonics with 24 words.
|
|
755
|
+
*
|
|
756
|
+
* @param mnemonics, 12 or 24 mnemonics words, separated by space
|
|
757
|
+
* @param secretKey, base64 or hex string, when mnemonics is provided, secretKey will be ignored
|
|
758
|
+
*/
|
|
759
|
+
constructor({ packageId, metadata } = {}) {
|
|
760
|
+
this.packageId = packageId || "";
|
|
761
|
+
this.metadata = metadata || void 0;
|
|
762
|
+
}
|
|
763
|
+
getFuncByModuleName(moduleName) {
|
|
764
|
+
Object.values(this.metadata).forEach(
|
|
765
|
+
(value) => {
|
|
766
|
+
const data = value;
|
|
767
|
+
console.log(`moudle name: ${data.name}`);
|
|
768
|
+
Object.entries(data.exposedFunctions).forEach(([key, value2]) => {
|
|
769
|
+
console.log(` func name: ${key}`);
|
|
770
|
+
Object.values(value2.parameters).forEach((values) => {
|
|
771
|
+
});
|
|
772
|
+
});
|
|
773
|
+
}
|
|
774
|
+
);
|
|
775
|
+
}
|
|
776
|
+
getAllFunc() {
|
|
777
|
+
Object.values(this.metadata).forEach(
|
|
778
|
+
(value) => {
|
|
779
|
+
const data = value;
|
|
780
|
+
console.log(`moudle name: ${data.name}`);
|
|
781
|
+
Object.entries(data.exposedFunctions).forEach(([key, value2]) => {
|
|
782
|
+
console.log(` func name: ${key}`);
|
|
783
|
+
console.log(` ${value2.parameters.length}`);
|
|
784
|
+
Object.values(value2.parameters).forEach((values) => {
|
|
785
|
+
console.log(` args: ${values}`);
|
|
786
|
+
});
|
|
787
|
+
});
|
|
788
|
+
}
|
|
789
|
+
);
|
|
790
|
+
}
|
|
791
|
+
getAllModule() {
|
|
792
|
+
Object.values(this.metadata).forEach(
|
|
793
|
+
(value, index) => {
|
|
794
|
+
const data = value;
|
|
795
|
+
console.log(`${index}. ${data.name}`);
|
|
796
|
+
}
|
|
797
|
+
);
|
|
798
|
+
}
|
|
799
|
+
// async call(arguments: ({
|
|
800
|
+
// kind: "Input";
|
|
801
|
+
// index: number;
|
|
802
|
+
// type?: "object" | "pure" | undefined;
|
|
803
|
+
// value?: any;
|
|
804
|
+
// } | {
|
|
805
|
+
// kind: "GasCoin";
|
|
806
|
+
// } | {
|
|
807
|
+
// kind: "Result";
|
|
808
|
+
// index: number;
|
|
809
|
+
// } | {
|
|
810
|
+
// kind: "NestedResult";
|
|
811
|
+
// index: number;
|
|
812
|
+
// resultIndex: number;
|
|
813
|
+
// })[], derivePathParams?: DerivePathParams) {
|
|
814
|
+
// const tx = new TransactionBlock();
|
|
815
|
+
// tx.moveCall({
|
|
816
|
+
// target: `${this.packageId}::${}::${}`,
|
|
817
|
+
// arguments,
|
|
818
|
+
// })
|
|
819
|
+
// return ;
|
|
820
|
+
// }
|
|
821
|
+
};
|
|
822
|
+
|
|
823
|
+
// src/utils/index.ts
|
|
824
|
+
function normalizeHexAddress(input) {
|
|
825
|
+
const hexRegex = /^(0x)?[0-9a-fA-F]{64}$/;
|
|
826
|
+
if (hexRegex.test(input)) {
|
|
827
|
+
if (input.startsWith("0x")) {
|
|
828
|
+
return input;
|
|
829
|
+
} else {
|
|
830
|
+
return "0x" + input;
|
|
831
|
+
}
|
|
832
|
+
} else {
|
|
833
|
+
return null;
|
|
834
|
+
}
|
|
835
|
+
}
|
|
836
|
+
function numberToAddressHex(num) {
|
|
837
|
+
const hex = num.toString(16);
|
|
838
|
+
const paddedHex = "0x" + hex.padStart(64, "0");
|
|
839
|
+
return paddedHex;
|
|
840
|
+
}
|
|
841
|
+
|
|
842
|
+
// src/obelisk.ts
|
|
843
|
+
var import_keccak256 = __toESM(require("keccak256"));
|
|
844
|
+
var import_bcs = require("@mysten/bcs");
|
|
845
|
+
function isUndefined(value) {
|
|
846
|
+
return value === void 0;
|
|
847
|
+
}
|
|
848
|
+
function withMeta(meta, creator) {
|
|
849
|
+
creator.meta = meta;
|
|
850
|
+
return creator;
|
|
851
|
+
}
|
|
852
|
+
function createQuery(meta, fn) {
|
|
853
|
+
return withMeta(
|
|
854
|
+
meta,
|
|
855
|
+
async (tx, params, isRaw) => {
|
|
856
|
+
const result = await fn(tx, params, isRaw);
|
|
857
|
+
return result;
|
|
858
|
+
}
|
|
859
|
+
);
|
|
860
|
+
}
|
|
861
|
+
function createTx(meta, fn) {
|
|
862
|
+
return withMeta(
|
|
863
|
+
meta,
|
|
864
|
+
async (tx, params, isRaw) => {
|
|
865
|
+
const result = await fn(tx, params, isRaw);
|
|
866
|
+
return result;
|
|
867
|
+
}
|
|
868
|
+
);
|
|
869
|
+
}
|
|
870
|
+
var _query, _tx, _exec, _read;
|
|
871
|
+
var Obelisk = class {
|
|
872
|
+
/**
|
|
873
|
+
* Support the following ways to init the ObeliskClient:
|
|
874
|
+
* 1. mnemonics
|
|
875
|
+
* 2. secretKey (base64 or hex)
|
|
876
|
+
* If none of them is provided, will generate a random mnemonics with 24 words.
|
|
877
|
+
*
|
|
878
|
+
* @param mnemonics, 12 or 24 mnemonics words, separated by space
|
|
879
|
+
* @param secretKey, base64 or hex string, when mnemonics is provided, secretKey will be ignored
|
|
880
|
+
* @param networkType, 'testnet' | 'mainnet' | 'devnet' | 'localnet', default is 'devnet'
|
|
881
|
+
* @param fullnodeUrl, the fullnode url, default is the preconfig fullnode url for the given network type
|
|
882
|
+
* @param packageId
|
|
883
|
+
*/
|
|
884
|
+
constructor({
|
|
885
|
+
mnemonics,
|
|
886
|
+
secretKey,
|
|
887
|
+
networkType,
|
|
888
|
+
fullnodeUrls,
|
|
889
|
+
packageId,
|
|
890
|
+
metadata
|
|
891
|
+
} = {}) {
|
|
892
|
+
__privateAdd(this, _query, {});
|
|
893
|
+
__privateAdd(this, _tx, {});
|
|
894
|
+
__privateAdd(this, _exec, async (meta, tx, params, isRaw) => {
|
|
895
|
+
tx.moveCall({
|
|
896
|
+
target: `${this.contractFactory.packageId}::${meta.moudleName}::${meta.funcName}`,
|
|
897
|
+
arguments: params
|
|
898
|
+
});
|
|
899
|
+
if (isRaw === true) {
|
|
900
|
+
return tx;
|
|
901
|
+
}
|
|
902
|
+
return await this.signAndSendTxn(tx);
|
|
903
|
+
});
|
|
904
|
+
__privateAdd(this, _read, async (meta, tx, params, isRaw) => {
|
|
905
|
+
tx.moveCall({
|
|
906
|
+
target: `${this.contractFactory.packageId}::${meta.moudleName}::${meta.funcName}`,
|
|
907
|
+
arguments: params
|
|
908
|
+
});
|
|
909
|
+
if (isRaw === true) {
|
|
910
|
+
return tx;
|
|
911
|
+
}
|
|
912
|
+
return await this.inspectTxn(tx);
|
|
913
|
+
});
|
|
914
|
+
this.accountManager = new SuiAccountManager({ mnemonics, secretKey });
|
|
915
|
+
fullnodeUrls = fullnodeUrls || [getDefaultConnection(networkType).fullnode];
|
|
916
|
+
this.suiInteractor = new SuiInteractor(fullnodeUrls, networkType);
|
|
917
|
+
this.packageId = packageId;
|
|
918
|
+
if (metadata !== void 0) {
|
|
919
|
+
this.metadata = metadata;
|
|
920
|
+
Object.values(metadata).forEach((value) => {
|
|
921
|
+
const data = value;
|
|
922
|
+
const moduleName = data.name;
|
|
923
|
+
Object.entries(data.exposedFunctions).forEach(([funcName, value2]) => {
|
|
924
|
+
const meta = value2;
|
|
925
|
+
meta.moudleName = moduleName;
|
|
926
|
+
meta.funcName = funcName;
|
|
927
|
+
if (isUndefined(__privateGet(this, _query)[moduleName])) {
|
|
928
|
+
__privateGet(this, _query)[moduleName] = {};
|
|
929
|
+
}
|
|
930
|
+
if (isUndefined(__privateGet(this, _query)[moduleName][funcName])) {
|
|
931
|
+
__privateGet(this, _query)[moduleName][funcName] = createQuery(
|
|
932
|
+
meta,
|
|
933
|
+
(tx, p, isRaw) => __privateGet(this, _read).call(this, meta, tx, p, isRaw)
|
|
934
|
+
);
|
|
935
|
+
}
|
|
936
|
+
if (isUndefined(__privateGet(this, _tx)[moduleName])) {
|
|
937
|
+
__privateGet(this, _tx)[moduleName] = {};
|
|
938
|
+
}
|
|
939
|
+
if (isUndefined(__privateGet(this, _tx)[moduleName][funcName])) {
|
|
940
|
+
__privateGet(this, _tx)[moduleName][funcName] = createTx(
|
|
941
|
+
meta,
|
|
942
|
+
(tx, p, isRaw) => __privateGet(this, _exec).call(this, meta, tx, p, isRaw)
|
|
943
|
+
);
|
|
944
|
+
}
|
|
945
|
+
});
|
|
946
|
+
});
|
|
947
|
+
}
|
|
948
|
+
this.contractFactory = new SuiContractFactory({
|
|
949
|
+
packageId,
|
|
950
|
+
metadata
|
|
951
|
+
});
|
|
952
|
+
}
|
|
953
|
+
get query() {
|
|
954
|
+
return __privateGet(this, _query);
|
|
955
|
+
}
|
|
956
|
+
get tx() {
|
|
957
|
+
return __privateGet(this, _tx);
|
|
958
|
+
}
|
|
959
|
+
/**
|
|
960
|
+
* if derivePathParams is not provided or mnemonics is empty, it will return the currentSigner.
|
|
961
|
+
* else:
|
|
962
|
+
* it will generate signer from the mnemonic with the given derivePathParams.
|
|
963
|
+
* @param derivePathParams, such as { accountIndex: 2, isExternal: false, addressIndex: 10 }, comply with the BIP44 standard
|
|
964
|
+
*/
|
|
965
|
+
getSigner(derivePathParams) {
|
|
966
|
+
const keyPair = this.accountManager.getKeyPair(derivePathParams);
|
|
967
|
+
return new import_sui9.RawSigner(keyPair, this.suiInteractor.currentProvider);
|
|
968
|
+
}
|
|
969
|
+
/**
|
|
970
|
+
* @description Switch the current account with the given derivePathParams
|
|
971
|
+
* @param derivePathParams, such as { accountIndex: 2, isExternal: false, addressIndex: 10 }, comply with the BIP44 standard
|
|
972
|
+
*/
|
|
973
|
+
switchAccount(derivePathParams) {
|
|
974
|
+
this.accountManager.switchAccount(derivePathParams);
|
|
975
|
+
}
|
|
976
|
+
/**
|
|
977
|
+
* @description Get the address of the account for the given derivePathParams
|
|
978
|
+
* @param derivePathParams, such as { accountIndex: 2, isExternal: false, addressIndex: 10 }, comply with the BIP44 standard
|
|
979
|
+
*/
|
|
980
|
+
getAddress(derivePathParams) {
|
|
981
|
+
return this.accountManager.getAddress(derivePathParams);
|
|
982
|
+
}
|
|
983
|
+
currentAddress() {
|
|
984
|
+
return this.accountManager.currentAddress;
|
|
985
|
+
}
|
|
986
|
+
provider() {
|
|
987
|
+
return this.suiInteractor.currentProvider;
|
|
988
|
+
}
|
|
989
|
+
getPackageId() {
|
|
990
|
+
return this.contractFactory.packageId;
|
|
991
|
+
}
|
|
992
|
+
getMetadata() {
|
|
993
|
+
return this.contractFactory.metadata;
|
|
994
|
+
}
|
|
995
|
+
/**
|
|
996
|
+
* Request some SUI from faucet
|
|
997
|
+
* @Returns {Promise<boolean>}, true if the request is successful, false otherwise.
|
|
998
|
+
*/
|
|
999
|
+
async requestFaucet(address, network) {
|
|
1000
|
+
return this.suiInteractor.requestFaucet(address, network);
|
|
1001
|
+
}
|
|
1002
|
+
async getBalance(coinType, derivePathParams) {
|
|
1003
|
+
const owner = this.accountManager.getAddress(derivePathParams);
|
|
1004
|
+
return this.suiInteractor.currentProvider.getBalance({ owner, coinType });
|
|
1005
|
+
}
|
|
1006
|
+
async getObject(objectId) {
|
|
1007
|
+
return this.suiInteractor.getObject(objectId);
|
|
1008
|
+
}
|
|
1009
|
+
async getObjects(objectIds) {
|
|
1010
|
+
return this.suiInteractor.getObjects(objectIds);
|
|
1011
|
+
}
|
|
1012
|
+
async signTxn(tx, derivePathParams) {
|
|
1013
|
+
tx = tx instanceof SuiTxBlock ? tx.txBlock : tx;
|
|
1014
|
+
const signer = this.getSigner(derivePathParams);
|
|
1015
|
+
return signer.signTransactionBlock({ transactionBlock: tx });
|
|
1016
|
+
}
|
|
1017
|
+
async signAndSendTxn(tx, derivePathParams) {
|
|
1018
|
+
const { transactionBlockBytes, signature } = await this.signTxn(
|
|
1019
|
+
tx,
|
|
1020
|
+
derivePathParams
|
|
1021
|
+
);
|
|
1022
|
+
return this.suiInteractor.sendTx(transactionBlockBytes, signature);
|
|
1023
|
+
}
|
|
1024
|
+
/**
|
|
1025
|
+
* Transfer the given amount of SUI to the recipient
|
|
1026
|
+
* @param recipient
|
|
1027
|
+
* @param amount
|
|
1028
|
+
* @param derivePathParams
|
|
1029
|
+
*/
|
|
1030
|
+
async transferSui(recipient, amount, derivePathParams) {
|
|
1031
|
+
const tx = new SuiTxBlock();
|
|
1032
|
+
tx.transferSui(recipient, amount);
|
|
1033
|
+
return this.signAndSendTxn(tx, derivePathParams);
|
|
1034
|
+
}
|
|
1035
|
+
/**
|
|
1036
|
+
* Transfer to mutliple recipients
|
|
1037
|
+
* @param recipients the recipients addresses
|
|
1038
|
+
* @param amounts the amounts of SUI to transfer to each recipient, the length of amounts should be the same as the length of recipients
|
|
1039
|
+
* @param derivePathParams
|
|
1040
|
+
*/
|
|
1041
|
+
async transferSuiToMany(recipients, amounts, derivePathParams) {
|
|
1042
|
+
const tx = new SuiTxBlock();
|
|
1043
|
+
tx.transferSuiToMany(recipients, amounts);
|
|
1044
|
+
return this.signAndSendTxn(tx, derivePathParams);
|
|
1045
|
+
}
|
|
1046
|
+
/**
|
|
1047
|
+
* Transfer the given amounts of coin to multiple recipients
|
|
1048
|
+
* @param recipients the list of recipient address
|
|
1049
|
+
* @param amounts the amounts to transfer for each recipient
|
|
1050
|
+
* @param coinType any custom coin type but not SUI
|
|
1051
|
+
* @param derivePathParams the derive path params for the current signer
|
|
1052
|
+
*/
|
|
1053
|
+
async transferCoinToMany(recipients, amounts, coinType, derivePathParams) {
|
|
1054
|
+
const tx = new SuiTxBlock();
|
|
1055
|
+
const owner = this.accountManager.getAddress(derivePathParams);
|
|
1056
|
+
const totalAmount = amounts.reduce((a, b) => a + b, 0);
|
|
1057
|
+
const coins = await this.suiInteractor.selectCoins(
|
|
1058
|
+
owner,
|
|
1059
|
+
totalAmount,
|
|
1060
|
+
coinType
|
|
1061
|
+
);
|
|
1062
|
+
tx.transferCoinToMany(
|
|
1063
|
+
coins.map((c) => c.objectId),
|
|
1064
|
+
owner,
|
|
1065
|
+
recipients,
|
|
1066
|
+
amounts
|
|
1067
|
+
);
|
|
1068
|
+
return this.signAndSendTxn(tx, derivePathParams);
|
|
1069
|
+
}
|
|
1070
|
+
async transferCoin(recipient, amount, coinType, derivePathParams) {
|
|
1071
|
+
return this.transferCoinToMany(
|
|
1072
|
+
[recipient],
|
|
1073
|
+
[amount],
|
|
1074
|
+
coinType,
|
|
1075
|
+
derivePathParams
|
|
1076
|
+
);
|
|
1077
|
+
}
|
|
1078
|
+
async transferObjects(objects, recipient, derivePathParams) {
|
|
1079
|
+
const tx = new SuiTxBlock();
|
|
1080
|
+
tx.transferObjects(objects, recipient);
|
|
1081
|
+
return this.signAndSendTxn(tx, derivePathParams);
|
|
1082
|
+
}
|
|
1083
|
+
async moveCall(callParams) {
|
|
1084
|
+
const {
|
|
1085
|
+
target,
|
|
1086
|
+
arguments: args = [],
|
|
1087
|
+
typeArguments = [],
|
|
1088
|
+
derivePathParams
|
|
1089
|
+
} = callParams;
|
|
1090
|
+
const tx = new SuiTxBlock();
|
|
1091
|
+
tx.moveCall(target, args, typeArguments);
|
|
1092
|
+
return this.signAndSendTxn(tx, derivePathParams);
|
|
1093
|
+
}
|
|
1094
|
+
/**
|
|
1095
|
+
* Select coins with the given amount and coin type, the total amount is greater than or equal to the given amount
|
|
1096
|
+
* @param amount
|
|
1097
|
+
* @param coinType
|
|
1098
|
+
* @param owner
|
|
1099
|
+
*/
|
|
1100
|
+
async selectCoinsWithAmount(amount, coinType, owner) {
|
|
1101
|
+
owner = owner || this.accountManager.currentAddress;
|
|
1102
|
+
const coins = await this.suiInteractor.selectCoins(owner, amount, coinType);
|
|
1103
|
+
return coins.map((c) => c.objectId);
|
|
1104
|
+
}
|
|
1105
|
+
/**
|
|
1106
|
+
* stake the given amount of SUI to the validator
|
|
1107
|
+
* @param amount the amount of SUI to stake
|
|
1108
|
+
* @param validatorAddr the validator address
|
|
1109
|
+
* @param derivePathParams the derive path params for the current signer
|
|
1110
|
+
*/
|
|
1111
|
+
async stakeSui(amount, validatorAddr, derivePathParams) {
|
|
1112
|
+
const tx = new SuiTxBlock();
|
|
1113
|
+
tx.stakeSui(amount, validatorAddr);
|
|
1114
|
+
return this.signAndSendTxn(tx, derivePathParams);
|
|
1115
|
+
}
|
|
1116
|
+
/**
|
|
1117
|
+
* Execute the transaction with on-chain data but without really submitting. Useful for querying the effects of a transaction.
|
|
1118
|
+
* Since the transaction is not submitted, its gas cost is not charged.
|
|
1119
|
+
* @param tx the transaction to execute
|
|
1120
|
+
* @param derivePathParams the derive path params
|
|
1121
|
+
* @returns the effects and events of the transaction, such as object changes, gas cost, event emitted.
|
|
1122
|
+
*/
|
|
1123
|
+
async inspectTxn(tx, derivePathParams) {
|
|
1124
|
+
tx = tx instanceof SuiTxBlock ? tx.txBlock : tx;
|
|
1125
|
+
return this.suiInteractor.currentProvider.devInspectTransactionBlock({
|
|
1126
|
+
transactionBlock: tx,
|
|
1127
|
+
sender: this.getAddress(derivePathParams)
|
|
1128
|
+
});
|
|
1129
|
+
}
|
|
1130
|
+
async getWorld(worldObjectId) {
|
|
1131
|
+
return this.suiInteractor.getObject(worldObjectId);
|
|
1132
|
+
}
|
|
1133
|
+
async listSchemaNames(worldId) {
|
|
1134
|
+
const worldObject = await this.getObject(worldId);
|
|
1135
|
+
const newObjectContent = worldObject.objectFields;
|
|
1136
|
+
return newObjectContent["schemaNames"];
|
|
1137
|
+
}
|
|
1138
|
+
async getEntity(worldId, schemaName, entityId) {
|
|
1139
|
+
const schemaMoudleName = `${schemaName}_schema`;
|
|
1140
|
+
const tx = new import_sui9.TransactionBlock();
|
|
1141
|
+
const params = [tx.pure(worldId)];
|
|
1142
|
+
if (entityId !== void 0) {
|
|
1143
|
+
params.push(tx.pure(entityId));
|
|
1144
|
+
}
|
|
1145
|
+
const getResult = await this.query[schemaMoudleName].get(
|
|
1146
|
+
tx,
|
|
1147
|
+
params
|
|
1148
|
+
);
|
|
1149
|
+
const returnValue = [];
|
|
1150
|
+
if (getResult.effects.status.status === "success") {
|
|
1151
|
+
const resultList = getResult.results[0].returnValues;
|
|
1152
|
+
for (const res of resultList) {
|
|
1153
|
+
const bcs = new import_bcs.BCS((0, import_bcs.getSuiMoveConfig)());
|
|
1154
|
+
const value = Uint8Array.from(res[0]);
|
|
1155
|
+
const data = bcs.de(res[1], value);
|
|
1156
|
+
returnValue.push(data);
|
|
1157
|
+
}
|
|
1158
|
+
return returnValue;
|
|
1159
|
+
} else {
|
|
1160
|
+
return void 0;
|
|
1161
|
+
}
|
|
1162
|
+
}
|
|
1163
|
+
async containEntity(worldId, schemaName, entityId) {
|
|
1164
|
+
const schemaMoudleName = `${schemaName}_schema`;
|
|
1165
|
+
const tx = new import_sui9.TransactionBlock();
|
|
1166
|
+
const params = [tx.pure(worldId)];
|
|
1167
|
+
if (entityId !== void 0) {
|
|
1168
|
+
params.push(tx.pure(entityId));
|
|
1169
|
+
}
|
|
1170
|
+
const getResult = await this.query[schemaMoudleName].contains(
|
|
1171
|
+
tx,
|
|
1172
|
+
params
|
|
1173
|
+
);
|
|
1174
|
+
if (getResult.effects.status.status === "success") {
|
|
1175
|
+
const res = getResult.results[0].returnValues[0];
|
|
1176
|
+
const bcs = new import_bcs.BCS((0, import_bcs.getSuiMoveConfig)());
|
|
1177
|
+
const value = Uint8Array.from(res[0]);
|
|
1178
|
+
return bcs.de(res[1], value);
|
|
1179
|
+
} else {
|
|
1180
|
+
return void 0;
|
|
1181
|
+
}
|
|
1182
|
+
}
|
|
1183
|
+
// async getEntities(
|
|
1184
|
+
// worldId: string,
|
|
1185
|
+
// schemaName: string,
|
|
1186
|
+
// cursor?: string,
|
|
1187
|
+
// limit?: number
|
|
1188
|
+
// ) {
|
|
1189
|
+
// let schemaMoudleName = `${schemaName}_schema`;
|
|
1190
|
+
// const tx = new TransactionBlock();
|
|
1191
|
+
// let params = [tx.pure(worldId)] as SuiTxArgument[];
|
|
1192
|
+
// const tableResult = (await this.query[schemaonentMoudleName].entities(
|
|
1193
|
+
// tx,
|
|
1194
|
+
// params
|
|
1195
|
+
// )) as DevInspectResults;
|
|
1196
|
+
// const entities = tableResult.results as SuiReturnValues;
|
|
1197
|
+
// const bcs = new BCS(getSuiMoveConfig());
|
|
1198
|
+
// let value = Uint8Array.from(entities[0].returnValues[0][0]);
|
|
1199
|
+
// let tableId = '0x' + bcs.de('address', value);
|
|
1200
|
+
// let dynamicFields = await this.suiInteractor.getDynamicFields(
|
|
1201
|
+
// tableId,
|
|
1202
|
+
// cursor,
|
|
1203
|
+
// limit
|
|
1204
|
+
// );
|
|
1205
|
+
// let objectIds = dynamicFields.data.map((field) => field.objectId);
|
|
1206
|
+
// let objectDatas = await this.suiInteractor.getEntitiesObjects(objectIds);
|
|
1207
|
+
// return {
|
|
1208
|
+
// data: objectDatas,
|
|
1209
|
+
// nextCursor: dynamicFields.nextCursor,
|
|
1210
|
+
// hasNextPage: dynamicFields.hasNextPage,
|
|
1211
|
+
// };
|
|
1212
|
+
// }
|
|
1213
|
+
async getOwnedObjects(owner, cursor, limit) {
|
|
1214
|
+
const ownedObjects = await this.suiInteractor.getOwnedObjects(
|
|
1215
|
+
owner,
|
|
1216
|
+
cursor,
|
|
1217
|
+
limit
|
|
1218
|
+
);
|
|
1219
|
+
const ownedObjectsRes = [];
|
|
1220
|
+
for (const object of ownedObjects.data) {
|
|
1221
|
+
const objectDetail = await this.getObject(object.data.objectId);
|
|
1222
|
+
if (objectDetail.objectType.split("::")[0] === this.contractFactory.packageId) {
|
|
1223
|
+
ownedObjectsRes.push(objectDetail);
|
|
1224
|
+
}
|
|
1225
|
+
}
|
|
1226
|
+
return ownedObjectsRes;
|
|
1227
|
+
}
|
|
1228
|
+
async entity_key_from_object(objectId) {
|
|
1229
|
+
const checkObjectId = normalizeHexAddress(objectId);
|
|
1230
|
+
if (checkObjectId !== null) {
|
|
1231
|
+
objectId = checkObjectId;
|
|
1232
|
+
return objectId;
|
|
1233
|
+
} else {
|
|
1234
|
+
return void 0;
|
|
1235
|
+
}
|
|
1236
|
+
}
|
|
1237
|
+
async entity_key_from_bytes(bytes) {
|
|
1238
|
+
const hashBytes = (0, import_keccak256.default)(bytes);
|
|
1239
|
+
const hashU8Array = Array.from(hashBytes);
|
|
1240
|
+
const bcs = new import_bcs.BCS((0, import_bcs.getSuiMoveConfig)());
|
|
1241
|
+
const value = Uint8Array.from(hashU8Array);
|
|
1242
|
+
const data = bcs.de("address", value);
|
|
1243
|
+
return "0x" + data;
|
|
1244
|
+
}
|
|
1245
|
+
async entity_key_from_u256(x) {
|
|
1246
|
+
return numberToAddressHex(x);
|
|
1247
|
+
}
|
|
1248
|
+
async formatData(type, value) {
|
|
1249
|
+
const bcs = new import_bcs.BCS((0, import_bcs.getSuiMoveConfig)());
|
|
1250
|
+
const u8Value = Uint8Array.from(value);
|
|
1251
|
+
return bcs.de(type, u8Value);
|
|
1252
|
+
}
|
|
1253
|
+
};
|
|
1254
|
+
_query = new WeakMap();
|
|
1255
|
+
_tx = new WeakMap();
|
|
1256
|
+
_exec = new WeakMap();
|
|
1257
|
+
_read = new WeakMap();
|
|
1258
|
+
|
|
1259
|
+
// src/metadata/index.ts
|
|
1260
|
+
async function getMetadata(networkType, packageId) {
|
|
1261
|
+
const fullnodeUrls = [getDefaultConnection(networkType).fullnode];
|
|
1262
|
+
const suiInteractor = new SuiInteractor(fullnodeUrls);
|
|
1263
|
+
if (packageId !== void 0) {
|
|
1264
|
+
const jsonData = await suiInteractor.getNormalizedMoveModulesByPackage(packageId);
|
|
1265
|
+
return jsonData;
|
|
1266
|
+
} else {
|
|
1267
|
+
console.error("please set your package id.");
|
|
1268
|
+
}
|
|
1269
|
+
}
|
|
1270
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
1271
|
+
0 && (module.exports = {
|
|
1272
|
+
Ed25519Keypair,
|
|
1273
|
+
Obelisk,
|
|
1274
|
+
SuiAccountManager,
|
|
1275
|
+
SuiContractFactory,
|
|
1276
|
+
SuiTxBlock,
|
|
1277
|
+
getMetadata,
|
|
1278
|
+
...require("@mysten/sui.js"),
|
|
1279
|
+
...require("@mysten/bcs")
|
|
1280
|
+
});
|
|
1281
|
+
//# sourceMappingURL=index.js.map
|