@0xobelisk/client 0.0.8 → 0.1.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/dist/index.d.ts +0 -1
- package/dist/index.js +339 -292
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +338 -308
- package/dist/index.mjs.map +1 -1
- package/dist/libs/suiAccountManager/index.d.ts +1 -1
- package/dist/libs/suiAccountManager/keypair.d.ts +1 -1
- package/dist/libs/suiContractFactory/index.d.ts +0 -1
- package/dist/libs/suiInteractor/defaultConfig.d.ts +10 -0
- package/dist/libs/suiInteractor/index.d.ts +2 -0
- package/dist/libs/suiInteractor/suiInteractor.d.ts +204 -0
- package/dist/libs/suiInteractor/util.d.ts +1 -0
- package/dist/libs/suiModel/index.d.ts +2 -0
- package/dist/libs/suiModel/suiOwnedObject.d.ts +24 -0
- package/dist/libs/suiModel/suiSharedObject.d.ts +12 -0
- package/dist/libs/suiTxBuilder/index.d.ts +2 -2
- package/dist/libs/suiTxBuilder/util.d.ts +8 -8
- package/dist/metadata/index.d.ts +1 -1
- package/dist/obelisk.d.ts +122 -331
- package/dist/types/index.d.ts +48 -6
- package/package.json +3 -3
- package/src/index.ts +0 -1
- package/src/libs/suiAccountManager/index.ts +1 -1
- package/src/libs/suiAccountManager/keypair.ts +1 -1
- package/src/libs/suiContractFactory/index.ts +0 -1
- package/src/libs/{suiRpcProvider/defaultChainConfigs.ts → suiInteractor/defaultConfig.ts} +4 -2
- package/src/libs/suiInteractor/index.ts +2 -0
- package/src/libs/suiInteractor/suiInteractor.ts +269 -0
- package/src/libs/suiInteractor/util.ts +2 -0
- package/src/libs/suiModel/index.ts +2 -0
- package/src/libs/suiModel/suiOwnedObject.ts +62 -0
- package/src/libs/suiModel/suiSharedObject.ts +33 -0
- package/src/libs/suiTxBuilder/index.ts +1 -1
- package/src/libs/suiTxBuilder/util.ts +1 -1
- package/src/metadata/index.ts +7 -7
- package/src/obelisk.ts +39 -132
- package/src/types/index.ts +92 -10
- package/src/utils/index.ts +1 -1
- package/src/libs/suiAccountManager/types.ts +0 -10
- package/src/libs/suiRpcProvider/faucet.ts +0 -57
- package/src/libs/suiRpcProvider/index.ts +0 -150
- package/src/libs/suiRpcProvider/types.ts +0 -17
- package/src/libs/suiTxBuilder/types.ts +0 -32
package/dist/index.mjs
CHANGED
|
@@ -1,10 +1,3 @@
|
|
|
1
|
-
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
2
|
-
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
3
|
-
}) : x)(function(x) {
|
|
4
|
-
if (typeof require !== "undefined")
|
|
5
|
-
return require.apply(this, arguments);
|
|
6
|
-
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
7
|
-
});
|
|
8
1
|
var __accessCheck = (obj, member, msg) => {
|
|
9
2
|
if (!member.has(obj))
|
|
10
3
|
throw TypeError("Cannot " + msg);
|
|
@@ -147,193 +140,6 @@ var SuiAccountManager = class {
|
|
|
147
140
|
}
|
|
148
141
|
};
|
|
149
142
|
|
|
150
|
-
// src/libs/suiRpcProvider/index.ts
|
|
151
|
-
import {
|
|
152
|
-
Connection,
|
|
153
|
-
JsonRpcProvider as JsonRpcProvider2,
|
|
154
|
-
getObjectType,
|
|
155
|
-
getObjectId,
|
|
156
|
-
getObjectFields,
|
|
157
|
-
getObjectDisplay,
|
|
158
|
-
getObjectVersion
|
|
159
|
-
} from "@mysten/sui.js";
|
|
160
|
-
|
|
161
|
-
// src/libs/suiRpcProvider/faucet.ts
|
|
162
|
-
import {
|
|
163
|
-
FaucetRateLimitError,
|
|
164
|
-
assert,
|
|
165
|
-
FaucetResponse
|
|
166
|
-
} from "@mysten/sui.js";
|
|
167
|
-
import { retry } from "ts-retry-promise";
|
|
168
|
-
var requestFaucet = async (address, provider) => {
|
|
169
|
-
console.log("\nRequesting SUI from faucet for address: ", address);
|
|
170
|
-
const headers = {
|
|
171
|
-
authority: "faucet.testnet.sui.io",
|
|
172
|
-
method: "POST",
|
|
173
|
-
path: "/gas",
|
|
174
|
-
scheme: "https",
|
|
175
|
-
accept: "*/*",
|
|
176
|
-
"accept-encoding": "gzip, deflate, br",
|
|
177
|
-
"accept-language": "zh-CN,zh;q=0.9,en;q=0.8,ja;q=0.7",
|
|
178
|
-
"content-length": "105",
|
|
179
|
-
"content-type": "application/json",
|
|
180
|
-
origin: "chrome-extension://opcgpfmipidbgpenhmajoajpbobppdil",
|
|
181
|
-
cookie: '_ga=GA1.1.2092533979.1664032306; sui_io_cookie={"level":["necessary","analytics"],"revision":0,"data":null,"rfc_cookie":false}; _ga_YKP53WJMB0=GS1.1.1680531285.31.0.1680531334.11.0.0; _ga_0GW4F97GFL=GS1.1.1680826187.125.0.1680826187.60.0.0; __cf_bm=6rPjXUwuzUPy4yDlZuXgDj0v7xLPpUd5z0CFGCoN_YI-1680867579-0-AZMhU7/mKUUbUlOa27LmfW6eIFkBkXsPKqYgWjpjWpj2XzDckgUsRu/pxSRGfvXCspn3w7Df+uO1MR/b+XikJU0=; _cfuvid=zjwCXMmu19KBIVo_L9Qbq4TqFXJpophG3.EvFTxqdf4-1680867579342-0-604800000',
|
|
182
|
-
"sec-ch-ua": '"Google Chrome";v="111", "Not(A:Brand";v="8", "Chromium";v="111"',
|
|
183
|
-
"sec-ch-ua-mobile": "?0",
|
|
184
|
-
"sec-ch-ua-platform": "macOS",
|
|
185
|
-
"sec-fetch-dest": "empty",
|
|
186
|
-
"sec-fetch-mode": "cors",
|
|
187
|
-
"sec-fetch-site": "none",
|
|
188
|
-
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36"
|
|
189
|
-
};
|
|
190
|
-
const resp = await retry(
|
|
191
|
-
() => provider.requestSuiFromFaucet(address, headers),
|
|
192
|
-
{
|
|
193
|
-
backoff: "EXPONENTIAL",
|
|
194
|
-
// overall timeout in 60 seconds
|
|
195
|
-
timeout: 1e3 * 60,
|
|
196
|
-
// skip retry if we hit the rate-limit error
|
|
197
|
-
retryIf: (error) => !(error instanceof FaucetRateLimitError),
|
|
198
|
-
logger: (msg) => console.warn(`Retry requesting faucet: ${msg}`)
|
|
199
|
-
}
|
|
200
|
-
);
|
|
201
|
-
assert(resp, FaucetResponse, "Request faucet failed\n");
|
|
202
|
-
console.log("Request faucet success\n");
|
|
203
|
-
};
|
|
204
|
-
|
|
205
|
-
// src/libs/suiRpcProvider/defaultChainConfigs.ts
|
|
206
|
-
import {
|
|
207
|
-
localnetConnection,
|
|
208
|
-
devnetConnection,
|
|
209
|
-
testnetConnection,
|
|
210
|
-
mainnetConnection
|
|
211
|
-
} from "@mysten/sui.js";
|
|
212
|
-
var getDefaultNetworkParams = (networkType = "devnet") => {
|
|
213
|
-
switch (networkType) {
|
|
214
|
-
case "localnet":
|
|
215
|
-
return localnetConnection;
|
|
216
|
-
case "devnet":
|
|
217
|
-
return devnetConnection;
|
|
218
|
-
case "testnet":
|
|
219
|
-
return testnetConnection;
|
|
220
|
-
case "mainnet":
|
|
221
|
-
return mainnetConnection;
|
|
222
|
-
default:
|
|
223
|
-
return devnetConnection;
|
|
224
|
-
}
|
|
225
|
-
};
|
|
226
|
-
|
|
227
|
-
// src/libs/suiRpcProvider/index.ts
|
|
228
|
-
var SuiRpcProvider = class {
|
|
229
|
-
/**
|
|
230
|
-
*
|
|
231
|
-
* @param networkType, 'testnet' | 'mainnet' | 'devnet' | 'localnet', default is 'devnet'
|
|
232
|
-
* @param fullnodeUrl, the fullnode url, default is the preconfig fullnode url for the given network type
|
|
233
|
-
* @param faucetUrl, the faucet url, default is the preconfig faucet url for the given network type
|
|
234
|
-
*/
|
|
235
|
-
constructor({
|
|
236
|
-
fullnodeUrl,
|
|
237
|
-
faucetUrl,
|
|
238
|
-
networkType
|
|
239
|
-
} = {}) {
|
|
240
|
-
const defaultNetworkParams = getDefaultNetworkParams(
|
|
241
|
-
networkType || "devnet"
|
|
242
|
-
);
|
|
243
|
-
this.fullnodeUrl = fullnodeUrl || defaultNetworkParams.fullnode;
|
|
244
|
-
this.faucetUrl = faucetUrl || defaultNetworkParams.faucet;
|
|
245
|
-
const connection = new Connection({
|
|
246
|
-
fullnode: this.fullnodeUrl,
|
|
247
|
-
faucet: this.faucetUrl
|
|
248
|
-
});
|
|
249
|
-
this.provider = new JsonRpcProvider2(connection);
|
|
250
|
-
}
|
|
251
|
-
/**
|
|
252
|
-
* Request some SUI from faucet
|
|
253
|
-
* @Returns {Promise<boolean>}, true if the request is successful, false otherwise.
|
|
254
|
-
*/
|
|
255
|
-
async requestFaucet(addr) {
|
|
256
|
-
return requestFaucet(addr, this.provider);
|
|
257
|
-
}
|
|
258
|
-
async getBalance(addr, coinType) {
|
|
259
|
-
return this.provider.getBalance({ owner: addr, coinType });
|
|
260
|
-
}
|
|
261
|
-
async getDynamicFieldObject(parentId, name) {
|
|
262
|
-
return this.provider.getDynamicFieldObject({ parentId, name });
|
|
263
|
-
}
|
|
264
|
-
async getDynamicFields(parentId, cursor, limit) {
|
|
265
|
-
return this.provider.getDynamicFields({ parentId, cursor, limit });
|
|
266
|
-
}
|
|
267
|
-
async getOwnedObjects(owner, cursor, limit) {
|
|
268
|
-
return await this.provider.getOwnedObjects({ owner, cursor, limit });
|
|
269
|
-
}
|
|
270
|
-
async getObject(id) {
|
|
271
|
-
const options = { showContent: true, showDisplay: true, showType: true };
|
|
272
|
-
const object = await this.provider.getObject({ id, options });
|
|
273
|
-
const objectId = getObjectId(object);
|
|
274
|
-
const objectType = getObjectType(object);
|
|
275
|
-
const objectVersion = getObjectVersion(object);
|
|
276
|
-
const objectFields = getObjectFields(object);
|
|
277
|
-
const objectDisplay = getObjectDisplay(object);
|
|
278
|
-
return {
|
|
279
|
-
objectId,
|
|
280
|
-
objectType,
|
|
281
|
-
objectVersion,
|
|
282
|
-
objectFields,
|
|
283
|
-
objectDisplay
|
|
284
|
-
};
|
|
285
|
-
}
|
|
286
|
-
async getObjects(ids) {
|
|
287
|
-
const options = { showContent: true, showDisplay: true, showType: true };
|
|
288
|
-
const objects = await this.provider.multiGetObjects({ ids, options });
|
|
289
|
-
const parsedObjects = objects.map((object) => {
|
|
290
|
-
const objectId = getObjectId(object);
|
|
291
|
-
const objectType = getObjectType(object);
|
|
292
|
-
const objectVersion = getObjectVersion(object);
|
|
293
|
-
const objectFields = getObjectFields(object);
|
|
294
|
-
const objectDisplay = getObjectDisplay(object);
|
|
295
|
-
return {
|
|
296
|
-
objectId,
|
|
297
|
-
objectType,
|
|
298
|
-
objectVersion,
|
|
299
|
-
objectFields,
|
|
300
|
-
objectDisplay
|
|
301
|
-
};
|
|
302
|
-
});
|
|
303
|
-
return parsedObjects;
|
|
304
|
-
}
|
|
305
|
-
async getNormalizedMoveModulesByPackage(packageId) {
|
|
306
|
-
return this.provider.getNormalizedMoveModulesByPackage({ package: packageId });
|
|
307
|
-
}
|
|
308
|
-
/**
|
|
309
|
-
* @description Select coins that add up to the given amount.
|
|
310
|
-
* @param addr the address of the owner
|
|
311
|
-
* @param amount the amount that is needed for the coin
|
|
312
|
-
* @param coinType the coin type, default is '0x2::SUI::SUI'
|
|
313
|
-
*/
|
|
314
|
-
async selectCoins(addr, amount, coinType = "0x2::SUI::SUI") {
|
|
315
|
-
const coins = await this.provider.getCoins({ owner: addr, coinType });
|
|
316
|
-
const selectedCoins = [];
|
|
317
|
-
let totalAmount = 0;
|
|
318
|
-
coins.data.sort((a, b) => parseInt(b.balance) - parseInt(a.balance));
|
|
319
|
-
for (const coinData of coins.data) {
|
|
320
|
-
selectedCoins.push({
|
|
321
|
-
objectId: coinData.coinObjectId,
|
|
322
|
-
digest: coinData.digest,
|
|
323
|
-
version: coinData.version
|
|
324
|
-
});
|
|
325
|
-
totalAmount = totalAmount + parseInt(coinData.balance);
|
|
326
|
-
if (totalAmount >= amount) {
|
|
327
|
-
break;
|
|
328
|
-
}
|
|
329
|
-
}
|
|
330
|
-
if (!selectedCoins.length) {
|
|
331
|
-
throw new Error("No valid coins found for the transaction.");
|
|
332
|
-
}
|
|
333
|
-
return selectedCoins;
|
|
334
|
-
}
|
|
335
|
-
};
|
|
336
|
-
|
|
337
143
|
// src/libs/suiTxBuilder/index.ts
|
|
338
144
|
import {
|
|
339
145
|
TransactionBlock as TransactionBlock2,
|
|
@@ -583,6 +389,315 @@ var SuiTxBlock = class {
|
|
|
583
389
|
}
|
|
584
390
|
};
|
|
585
391
|
|
|
392
|
+
// src/libs/suiInteractor/suiInteractor.ts
|
|
393
|
+
import {
|
|
394
|
+
JsonRpcProvider as JsonRpcProvider2,
|
|
395
|
+
Connection,
|
|
396
|
+
getObjectDisplay,
|
|
397
|
+
getObjectFields,
|
|
398
|
+
getObjectId,
|
|
399
|
+
getObjectType,
|
|
400
|
+
getObjectVersion,
|
|
401
|
+
getSharedObjectInitialVersion
|
|
402
|
+
} from "@mysten/sui.js";
|
|
403
|
+
import { requestSuiFromFaucetV0, getFaucetHost } from "@mysten/sui.js/faucet";
|
|
404
|
+
|
|
405
|
+
// src/libs/suiModel/suiOwnedObject.ts
|
|
406
|
+
import {
|
|
407
|
+
getObjectChanges
|
|
408
|
+
} from "@mysten/sui.js";
|
|
409
|
+
var SuiOwnedObject = class {
|
|
410
|
+
constructor(param) {
|
|
411
|
+
this.objectId = param.objectId;
|
|
412
|
+
this.version = param.version;
|
|
413
|
+
this.digest = param.digest;
|
|
414
|
+
}
|
|
415
|
+
/**
|
|
416
|
+
* Check if the object is fully initialized.
|
|
417
|
+
* So that when it's used as an input, it won't be necessary to fetch from fullnode again.
|
|
418
|
+
* Which can save time when sending transactions.
|
|
419
|
+
*/
|
|
420
|
+
isFullObject() {
|
|
421
|
+
return !!this.version && !!this.digest;
|
|
422
|
+
}
|
|
423
|
+
asCallArg() {
|
|
424
|
+
if (!this.version || !this.digest) {
|
|
425
|
+
return this.objectId;
|
|
426
|
+
}
|
|
427
|
+
return {
|
|
428
|
+
Object: {
|
|
429
|
+
ImmOrOwned: {
|
|
430
|
+
objectId: this.objectId,
|
|
431
|
+
version: this.version,
|
|
432
|
+
digest: this.digest
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
};
|
|
436
|
+
}
|
|
437
|
+
/**
|
|
438
|
+
* Update object version & digest based on the transaction response.
|
|
439
|
+
* @param txResponse
|
|
440
|
+
*/
|
|
441
|
+
updateFromTxResponse(txResponse) {
|
|
442
|
+
const changes = getObjectChanges(txResponse);
|
|
443
|
+
if (!changes) {
|
|
444
|
+
throw new Error("Bad transaction response!");
|
|
445
|
+
}
|
|
446
|
+
for (const change of changes) {
|
|
447
|
+
if (change.type === "mutated" && change.objectId === this.objectId) {
|
|
448
|
+
this.digest = change.digest;
|
|
449
|
+
this.version = change.version;
|
|
450
|
+
return;
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
throw new Error("Could not find object in transaction response!");
|
|
454
|
+
}
|
|
455
|
+
};
|
|
456
|
+
|
|
457
|
+
// src/libs/suiModel/suiSharedObject.ts
|
|
458
|
+
var SuiSharedObject = class {
|
|
459
|
+
constructor(param) {
|
|
460
|
+
this.objectId = param.objectId;
|
|
461
|
+
this.initialSharedVersion = param.initialSharedVersion;
|
|
462
|
+
}
|
|
463
|
+
asCallArg(mutable = false) {
|
|
464
|
+
if (!this.initialSharedVersion) {
|
|
465
|
+
return this.objectId;
|
|
466
|
+
}
|
|
467
|
+
return {
|
|
468
|
+
Object: {
|
|
469
|
+
Shared: {
|
|
470
|
+
objectId: this.objectId,
|
|
471
|
+
initialSharedVersion: this.initialSharedVersion,
|
|
472
|
+
mutable
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
};
|
|
476
|
+
}
|
|
477
|
+
};
|
|
478
|
+
|
|
479
|
+
// src/libs/suiInteractor/util.ts
|
|
480
|
+
var delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
481
|
+
|
|
482
|
+
// src/libs/suiInteractor/suiInteractor.ts
|
|
483
|
+
var SuiInteractor = class {
|
|
484
|
+
constructor(fullNodeUrls, network) {
|
|
485
|
+
if (fullNodeUrls.length === 0)
|
|
486
|
+
throw new Error("fullNodeUrls must not be empty");
|
|
487
|
+
this.providers = fullNodeUrls.map(
|
|
488
|
+
(url) => new JsonRpcProvider2(new Connection({ fullnode: url }))
|
|
489
|
+
);
|
|
490
|
+
this.currentProvider = this.providers[0];
|
|
491
|
+
this.network = network;
|
|
492
|
+
}
|
|
493
|
+
switchToNextProvider() {
|
|
494
|
+
const currentProviderIdx = this.providers.indexOf(this.currentProvider);
|
|
495
|
+
this.currentProvider = this.providers[(currentProviderIdx + 1) % this.providers.length];
|
|
496
|
+
}
|
|
497
|
+
async sendTx(transactionBlock, signature) {
|
|
498
|
+
const txResOptions = {
|
|
499
|
+
showEvents: true,
|
|
500
|
+
showEffects: true,
|
|
501
|
+
showObjectChanges: true,
|
|
502
|
+
showBalanceChanges: true
|
|
503
|
+
};
|
|
504
|
+
for (const provider of this.providers) {
|
|
505
|
+
try {
|
|
506
|
+
const res = await provider.executeTransactionBlock({
|
|
507
|
+
transactionBlock,
|
|
508
|
+
signature,
|
|
509
|
+
options: txResOptions
|
|
510
|
+
});
|
|
511
|
+
return res;
|
|
512
|
+
} catch (err) {
|
|
513
|
+
console.warn(
|
|
514
|
+
`Failed to send transaction with fullnode ${provider.connection.fullnode}: ${err}`
|
|
515
|
+
);
|
|
516
|
+
await delay(2e3);
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
throw new Error("Failed to send transaction with all fullnodes");
|
|
520
|
+
}
|
|
521
|
+
async getObjects(ids) {
|
|
522
|
+
const options = {
|
|
523
|
+
showContent: true,
|
|
524
|
+
showDisplay: true,
|
|
525
|
+
showType: true,
|
|
526
|
+
showOwner: true
|
|
527
|
+
};
|
|
528
|
+
for (const provider of this.providers) {
|
|
529
|
+
try {
|
|
530
|
+
const objects = await provider.multiGetObjects({ ids, options });
|
|
531
|
+
const parsedObjects = objects.map((object) => {
|
|
532
|
+
const objectId = getObjectId(object);
|
|
533
|
+
const objectType = getObjectType(object);
|
|
534
|
+
const objectVersion = getObjectVersion(object);
|
|
535
|
+
const objectDigest = object.data ? object.data.digest : void 0;
|
|
536
|
+
const initialSharedVersion = getSharedObjectInitialVersion(object);
|
|
537
|
+
const objectFields = getObjectFields(object);
|
|
538
|
+
const objectDisplay = getObjectDisplay(object);
|
|
539
|
+
return {
|
|
540
|
+
objectId,
|
|
541
|
+
objectType,
|
|
542
|
+
objectVersion,
|
|
543
|
+
objectDigest,
|
|
544
|
+
objectFields,
|
|
545
|
+
objectDisplay,
|
|
546
|
+
initialSharedVersion
|
|
547
|
+
};
|
|
548
|
+
});
|
|
549
|
+
return parsedObjects;
|
|
550
|
+
} catch (err) {
|
|
551
|
+
await delay(2e3);
|
|
552
|
+
console.warn(
|
|
553
|
+
`Failed to get objects with fullnode ${provider.connection.fullnode}: ${err}`
|
|
554
|
+
);
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
throw new Error("Failed to get objects with all fullnodes");
|
|
558
|
+
}
|
|
559
|
+
async getObject(id) {
|
|
560
|
+
const objects = await this.getObjects([id]);
|
|
561
|
+
return objects[0];
|
|
562
|
+
}
|
|
563
|
+
async getDynamicFieldObject(parentId, name) {
|
|
564
|
+
for (const provider of this.providers) {
|
|
565
|
+
try {
|
|
566
|
+
return provider.getDynamicFieldObject({ parentId, name });
|
|
567
|
+
} catch (err) {
|
|
568
|
+
await delay(2e3);
|
|
569
|
+
console.warn(
|
|
570
|
+
`Failed to get objects with fullnode ${provider.connection.fullnode}: ${err}`
|
|
571
|
+
);
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
throw new Error("Failed to get objects with all fullnodes");
|
|
575
|
+
}
|
|
576
|
+
async getDynamicFields(parentId, cursor, limit) {
|
|
577
|
+
for (const provider of this.providers) {
|
|
578
|
+
try {
|
|
579
|
+
return provider.getDynamicFields({ parentId, cursor, limit });
|
|
580
|
+
} catch (err) {
|
|
581
|
+
await delay(2e3);
|
|
582
|
+
console.warn(
|
|
583
|
+
`Failed to get objects with fullnode ${provider.connection.fullnode}: ${err}`
|
|
584
|
+
);
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
throw new Error("Failed to get objects with all fullnodes");
|
|
588
|
+
}
|
|
589
|
+
async getOwnedObjects(owner, cursor, limit) {
|
|
590
|
+
for (const provider of this.providers) {
|
|
591
|
+
try {
|
|
592
|
+
return await provider.getOwnedObjects({ owner, cursor, limit });
|
|
593
|
+
} catch (err) {
|
|
594
|
+
await delay(2e3);
|
|
595
|
+
console.warn(
|
|
596
|
+
`Failed to get objects with fullnode ${provider.connection.fullnode}: ${err}`
|
|
597
|
+
);
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
throw new Error("Failed to get objects with all fullnodes");
|
|
601
|
+
}
|
|
602
|
+
async getNormalizedMoveModulesByPackage(packageId) {
|
|
603
|
+
for (const provider of this.providers) {
|
|
604
|
+
try {
|
|
605
|
+
return provider.getNormalizedMoveModulesByPackage({ package: packageId });
|
|
606
|
+
} catch (err) {
|
|
607
|
+
await delay(2e3);
|
|
608
|
+
console.warn(
|
|
609
|
+
`Failed to get objects with fullnode ${provider.connection.fullnode}: ${err}`
|
|
610
|
+
);
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
throw new Error("Failed to get objects with all fullnodes");
|
|
614
|
+
}
|
|
615
|
+
/**
|
|
616
|
+
* @description Update objects in a batch
|
|
617
|
+
* @param suiObjects
|
|
618
|
+
*/
|
|
619
|
+
async updateObjects(suiObjects) {
|
|
620
|
+
const objectIds = suiObjects.map((obj) => obj.objectId);
|
|
621
|
+
const objects = await this.getObjects(objectIds);
|
|
622
|
+
for (const object of objects) {
|
|
623
|
+
const suiObject = suiObjects.find(
|
|
624
|
+
(obj) => obj.objectId === object.objectId
|
|
625
|
+
);
|
|
626
|
+
if (suiObject instanceof SuiSharedObject) {
|
|
627
|
+
suiObject.initialSharedVersion = object.initialSharedVersion;
|
|
628
|
+
} else if (suiObject instanceof SuiOwnedObject) {
|
|
629
|
+
suiObject.version = object.objectVersion;
|
|
630
|
+
suiObject.digest = object.objectDigest;
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
/**
|
|
635
|
+
* @description Select coins that add up to the given amount.
|
|
636
|
+
* @param addr the address of the owner
|
|
637
|
+
* @param amount the amount that is needed for the coin
|
|
638
|
+
* @param coinType the coin type, default is '0x2::SUI::SUI'
|
|
639
|
+
*/
|
|
640
|
+
async selectCoins(addr, amount, coinType = "0x2::SUI::SUI") {
|
|
641
|
+
const selectedCoins = [];
|
|
642
|
+
let totalAmount = 0;
|
|
643
|
+
let hasNext = true, nextCursor = null;
|
|
644
|
+
while (hasNext && totalAmount < amount) {
|
|
645
|
+
const coins = await this.currentProvider.getCoins({
|
|
646
|
+
owner: addr,
|
|
647
|
+
coinType,
|
|
648
|
+
cursor: nextCursor
|
|
649
|
+
});
|
|
650
|
+
coins.data.sort((a, b) => parseInt(b.balance) - parseInt(a.balance));
|
|
651
|
+
for (const coinData of coins.data) {
|
|
652
|
+
selectedCoins.push({
|
|
653
|
+
objectId: coinData.coinObjectId,
|
|
654
|
+
digest: coinData.digest,
|
|
655
|
+
version: coinData.version
|
|
656
|
+
});
|
|
657
|
+
totalAmount = totalAmount + parseInt(coinData.balance);
|
|
658
|
+
if (totalAmount >= amount) {
|
|
659
|
+
break;
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
nextCursor = coins.nextCursor;
|
|
663
|
+
hasNext = coins.hasNextPage;
|
|
664
|
+
}
|
|
665
|
+
if (!selectedCoins.length) {
|
|
666
|
+
throw new Error("No valid coins found for the transaction.");
|
|
667
|
+
}
|
|
668
|
+
return selectedCoins;
|
|
669
|
+
}
|
|
670
|
+
async requestFaucet(address, network) {
|
|
671
|
+
await requestSuiFromFaucetV0({
|
|
672
|
+
host: getFaucetHost(network),
|
|
673
|
+
recipient: address
|
|
674
|
+
});
|
|
675
|
+
}
|
|
676
|
+
};
|
|
677
|
+
|
|
678
|
+
// src/libs/suiInteractor/defaultConfig.ts
|
|
679
|
+
import {
|
|
680
|
+
localnetConnection,
|
|
681
|
+
devnetConnection,
|
|
682
|
+
testnetConnection,
|
|
683
|
+
mainnetConnection
|
|
684
|
+
} from "@mysten/sui.js";
|
|
685
|
+
var defaultGasBudget = 10 ** 8;
|
|
686
|
+
var getDefaultConnection = (networkType = "devnet") => {
|
|
687
|
+
switch (networkType) {
|
|
688
|
+
case "localnet":
|
|
689
|
+
return localnetConnection;
|
|
690
|
+
case "devnet":
|
|
691
|
+
return devnetConnection;
|
|
692
|
+
case "testnet":
|
|
693
|
+
return testnetConnection;
|
|
694
|
+
case "mainnet":
|
|
695
|
+
return mainnetConnection;
|
|
696
|
+
default:
|
|
697
|
+
return devnetConnection;
|
|
698
|
+
}
|
|
699
|
+
};
|
|
700
|
+
|
|
586
701
|
// src/libs/suiContractFactory/index.ts
|
|
587
702
|
var SuiContractFactory = class {
|
|
588
703
|
// readonly #query: MapMessageQuery<ApiTypes> = {};
|
|
@@ -636,8 +751,6 @@ var SuiContractFactory = class {
|
|
|
636
751
|
}
|
|
637
752
|
);
|
|
638
753
|
}
|
|
639
|
-
async worldCall() {
|
|
640
|
-
}
|
|
641
754
|
// async call(arguments: ({
|
|
642
755
|
// kind: "Input";
|
|
643
756
|
// index: number;
|
|
@@ -668,7 +781,7 @@ function capitalizeFirstLetter(input) {
|
|
|
668
781
|
}
|
|
669
782
|
|
|
670
783
|
// src/obelisk.ts
|
|
671
|
-
|
|
784
|
+
import keccak256 from "keccak256";
|
|
672
785
|
function isUndefined(value) {
|
|
673
786
|
return value === void 0;
|
|
674
787
|
}
|
|
@@ -691,7 +804,7 @@ function createTx(meta, fn) {
|
|
|
691
804
|
var _query, _tx, _exec, _read;
|
|
692
805
|
var Obelisk = class {
|
|
693
806
|
/**
|
|
694
|
-
* Support the following ways to init the
|
|
807
|
+
* Support the following ways to init the ObeliskClient:
|
|
695
808
|
* 1. mnemonics
|
|
696
809
|
* 2. secretKey (base64 or hex)
|
|
697
810
|
* If none of them is provided, will generate a random mnemonics with 24 words.
|
|
@@ -700,15 +813,13 @@ var Obelisk = class {
|
|
|
700
813
|
* @param secretKey, base64 or hex string, when mnemonics is provided, secretKey will be ignored
|
|
701
814
|
* @param networkType, 'testnet' | 'mainnet' | 'devnet' | 'localnet', default is 'devnet'
|
|
702
815
|
* @param fullnodeUrl, the fullnode url, default is the preconfig fullnode url for the given network type
|
|
703
|
-
* @param faucetUrl, the faucet url, default is the preconfig faucet url for the given network type
|
|
704
816
|
* @param packageId
|
|
705
817
|
*/
|
|
706
818
|
constructor({
|
|
707
819
|
mnemonics,
|
|
708
820
|
secretKey,
|
|
709
821
|
networkType,
|
|
710
|
-
|
|
711
|
-
faucetUrl,
|
|
822
|
+
fullnodeUrls,
|
|
712
823
|
packageId,
|
|
713
824
|
metadata
|
|
714
825
|
} = {}) {
|
|
@@ -735,13 +846,8 @@ var Obelisk = class {
|
|
|
735
846
|
return await this.inspectTxn(tx);
|
|
736
847
|
});
|
|
737
848
|
this.accountManager = new SuiAccountManager({ mnemonics, secretKey });
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
faucetUrl,
|
|
741
|
-
networkType
|
|
742
|
-
});
|
|
743
|
-
this.epsId = "0xf2196f638c3174e18c0e31aa630a02fd516c2c5deec1ded72c0fea864c9f091a";
|
|
744
|
-
this.componentsId = "0x3bc407eb543149e42846ade59ac2a3c901584af4339dc1ecd0affd090529545f";
|
|
849
|
+
fullnodeUrls = fullnodeUrls || [getDefaultConnection(networkType).fullnode];
|
|
850
|
+
this.suiInteractor = new SuiInteractor(fullnodeUrls, networkType);
|
|
745
851
|
this.packageId = packageId;
|
|
746
852
|
this.metadata = metadata;
|
|
747
853
|
Object.values(metadata).forEach((value) => {
|
|
@@ -770,15 +876,6 @@ var Obelisk = class {
|
|
|
770
876
|
metadata
|
|
771
877
|
});
|
|
772
878
|
}
|
|
773
|
-
// async initialize() {
|
|
774
|
-
// const metadata = await this.loadData();
|
|
775
|
-
// this.metadata = metadata as SuiMoveNormalizedModules;
|
|
776
|
-
// this.contractFactory = new SuiContractFactory({
|
|
777
|
-
// packageId: this.packageId,
|
|
778
|
-
// metadata: this.metadata
|
|
779
|
-
// })
|
|
780
|
-
// return metadata
|
|
781
|
-
// }
|
|
782
879
|
get query() {
|
|
783
880
|
return __privateGet(this, _query);
|
|
784
881
|
}
|
|
@@ -793,7 +890,7 @@ var Obelisk = class {
|
|
|
793
890
|
*/
|
|
794
891
|
getSigner(derivePathParams) {
|
|
795
892
|
const keyPair = this.accountManager.getKeyPair(derivePathParams);
|
|
796
|
-
return new RawSigner(keyPair, this.
|
|
893
|
+
return new RawSigner(keyPair, this.suiInteractor.currentProvider);
|
|
797
894
|
}
|
|
798
895
|
/**
|
|
799
896
|
* @description Switch the current account with the given derivePathParams
|
|
@@ -813,7 +910,7 @@ var Obelisk = class {
|
|
|
813
910
|
return this.accountManager.currentAddress;
|
|
814
911
|
}
|
|
815
912
|
provider() {
|
|
816
|
-
return this.
|
|
913
|
+
return this.suiInteractor.currentProvider;
|
|
817
914
|
}
|
|
818
915
|
getPackageId() {
|
|
819
916
|
return this.contractFactory.packageId;
|
|
@@ -825,19 +922,18 @@ var Obelisk = class {
|
|
|
825
922
|
* Request some SUI from faucet
|
|
826
923
|
* @Returns {Promise<boolean>}, true if the request is successful, false otherwise.
|
|
827
924
|
*/
|
|
828
|
-
async requestFaucet(
|
|
829
|
-
|
|
830
|
-
return this.rpcProvider.requestFaucet(addr);
|
|
925
|
+
async requestFaucet(address, network) {
|
|
926
|
+
return this.suiInteractor.requestFaucet(address, network);
|
|
831
927
|
}
|
|
832
928
|
async getBalance(coinType, derivePathParams) {
|
|
833
929
|
const owner = this.accountManager.getAddress(derivePathParams);
|
|
834
|
-
return this.
|
|
930
|
+
return this.suiInteractor.currentProvider.getBalance({ owner, coinType });
|
|
835
931
|
}
|
|
836
932
|
async getObject(objectId) {
|
|
837
|
-
return this.
|
|
933
|
+
return this.suiInteractor.getObject(objectId);
|
|
838
934
|
}
|
|
839
935
|
async getObjects(objectIds) {
|
|
840
|
-
return this.
|
|
936
|
+
return this.suiInteractor.getObjects(objectIds);
|
|
841
937
|
}
|
|
842
938
|
async signTxn(tx, derivePathParams) {
|
|
843
939
|
tx = tx instanceof SuiTxBlock ? tx.txBlock : tx;
|
|
@@ -845,16 +941,11 @@ var Obelisk = class {
|
|
|
845
941
|
return signer.signTransactionBlock({ transactionBlock: tx });
|
|
846
942
|
}
|
|
847
943
|
async signAndSendTxn(tx, derivePathParams) {
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
showEffects: true,
|
|
854
|
-
showEvents: true,
|
|
855
|
-
showObjectChanges: true
|
|
856
|
-
}
|
|
857
|
-
});
|
|
944
|
+
const { transactionBlockBytes, signature } = await this.signTxn(
|
|
945
|
+
tx,
|
|
946
|
+
derivePathParams
|
|
947
|
+
);
|
|
948
|
+
return this.suiInteractor.sendTx(transactionBlockBytes, signature);
|
|
858
949
|
}
|
|
859
950
|
/**
|
|
860
951
|
* Transfer the given amount of SUI to the recipient
|
|
@@ -889,7 +980,7 @@ var Obelisk = class {
|
|
|
889
980
|
const tx = new SuiTxBlock();
|
|
890
981
|
const owner = this.accountManager.getAddress(derivePathParams);
|
|
891
982
|
const totalAmount = amounts.reduce((a, b) => a + b, 0);
|
|
892
|
-
const coins = await this.
|
|
983
|
+
const coins = await this.suiInteractor.selectCoins(
|
|
893
984
|
owner,
|
|
894
985
|
totalAmount,
|
|
895
986
|
coinType
|
|
@@ -934,7 +1025,7 @@ var Obelisk = class {
|
|
|
934
1025
|
*/
|
|
935
1026
|
async selectCoinsWithAmount(amount, coinType, owner) {
|
|
936
1027
|
owner = owner || this.accountManager.currentAddress;
|
|
937
|
-
const coins = await this.
|
|
1028
|
+
const coins = await this.suiInteractor.selectCoins(owner, amount, coinType);
|
|
938
1029
|
return coins.map((c) => c.objectId);
|
|
939
1030
|
}
|
|
940
1031
|
/**
|
|
@@ -957,30 +1048,17 @@ var Obelisk = class {
|
|
|
957
1048
|
*/
|
|
958
1049
|
async inspectTxn(tx, derivePathParams) {
|
|
959
1050
|
tx = tx instanceof SuiTxBlock ? tx.txBlock : tx;
|
|
960
|
-
return this.
|
|
1051
|
+
return this.suiInteractor.currentProvider.devInspectTransactionBlock({
|
|
961
1052
|
transactionBlock: tx,
|
|
962
1053
|
sender: this.getAddress(derivePathParams)
|
|
963
1054
|
});
|
|
964
1055
|
}
|
|
965
1056
|
async getWorld(worldObjectId) {
|
|
966
|
-
return this.
|
|
967
|
-
}
|
|
968
|
-
async getAllEntities(worldId, cursor, limit) {
|
|
969
|
-
const parentId = (await this.rpcProvider.getObject(worldId)).objectFields.entities.fields.id.id;
|
|
970
|
-
return await this.rpcProvider.getDynamicFields(parentId, cursor, limit);
|
|
971
|
-
}
|
|
972
|
-
async getEntity(worldId, entityId) {
|
|
973
|
-
const parentId = (await this.rpcProvider.getObject(worldId)).objectFields.entities.fields.id.id;
|
|
974
|
-
console.log(parentId);
|
|
975
|
-
const name = {
|
|
976
|
-
type: "0x2::object::ID",
|
|
977
|
-
value: entityId
|
|
978
|
-
};
|
|
979
|
-
return await this.rpcProvider.getDynamicFieldObject(parentId, name);
|
|
1057
|
+
return this.suiInteractor.getObject(worldObjectId);
|
|
980
1058
|
}
|
|
981
1059
|
async getComponents(worldId) {
|
|
982
|
-
const parentId = (await this.
|
|
983
|
-
return await this.
|
|
1060
|
+
const parentId = (await this.suiInteractor.getObject(worldId)).objectFields.components.fields.id.id;
|
|
1061
|
+
return await this.suiInteractor.getDynamicFields(parentId);
|
|
984
1062
|
}
|
|
985
1063
|
async getComponentByName(worldId, componentName) {
|
|
986
1064
|
const componentId = keccak256(`${capitalizeFirstLetter(componentName)} Component`);
|
|
@@ -988,18 +1066,16 @@ var Obelisk = class {
|
|
|
988
1066
|
}
|
|
989
1067
|
async getComponent(worldId, componentId) {
|
|
990
1068
|
const componentIdValue = Array.from(componentId);
|
|
991
|
-
const parentId = (await this.
|
|
992
|
-
console.log(parentId);
|
|
1069
|
+
const parentId = (await this.suiInteractor.getObject(worldId)).objectFields.components.fields.id.id;
|
|
993
1070
|
const name = {
|
|
994
|
-
// type: "0x2::object::ID",
|
|
995
1071
|
type: "vector<u8>",
|
|
996
1072
|
value: componentIdValue
|
|
997
1073
|
// value: [250,208,186,160,39,171,62,206,98,224,138,41,11,217,63,100,248,104,207,64,78,126,43,109,129,68,64,127,236,113,152,132]
|
|
998
1074
|
};
|
|
999
|
-
return await this.
|
|
1075
|
+
return await this.suiInteractor.getDynamicFieldObject(parentId, name);
|
|
1000
1076
|
}
|
|
1001
1077
|
async getOwnedEntities(owner, cursor, limit) {
|
|
1002
|
-
const ownedObjects = await this.
|
|
1078
|
+
const ownedObjects = await this.suiInteractor.getOwnedObjects(owner, cursor, limit);
|
|
1003
1079
|
let ownedEntities = [];
|
|
1004
1080
|
for (const object of ownedObjects.data) {
|
|
1005
1081
|
let objectDetail = await this.getObject(object.data.objectId);
|
|
@@ -1009,50 +1085,6 @@ var Obelisk = class {
|
|
|
1009
1085
|
}
|
|
1010
1086
|
return ownedEntities;
|
|
1011
1087
|
}
|
|
1012
|
-
async getTable(worldId, entityId) {
|
|
1013
|
-
const parentId = (await this.rpcProvider.getObject(worldId)).objectFields.storages.fields.id.id;
|
|
1014
|
-
console.log(parentId);
|
|
1015
|
-
const name = {
|
|
1016
|
-
type: "0x2::object::ID",
|
|
1017
|
-
value: entityId
|
|
1018
|
-
};
|
|
1019
|
-
return await this.rpcProvider.getDynamicFieldObject(parentId, name);
|
|
1020
|
-
}
|
|
1021
|
-
async getEntityComponents(worldId, entityId, cursor, limit) {
|
|
1022
|
-
const parentContent = (await this.getEntity(worldId, entityId)).data.content;
|
|
1023
|
-
const parentId = parentContent.fields.value.fields.components.fields.id.id;
|
|
1024
|
-
return await this.rpcProvider.getDynamicFields(parentId, cursor, limit);
|
|
1025
|
-
}
|
|
1026
|
-
async getEntityComponent(entityId, componentId) {
|
|
1027
|
-
const parentId = (await this.rpcProvider.getObject(entityId)).objectFields.id.id;
|
|
1028
|
-
const name = {
|
|
1029
|
-
type: "0x2::object::ID",
|
|
1030
|
-
value: componentId
|
|
1031
|
-
};
|
|
1032
|
-
return await this.rpcProvider.getDynamicFieldObject(parentId, name);
|
|
1033
|
-
}
|
|
1034
|
-
// async loadData() {
|
|
1035
|
-
// const jsonFileName = `metadata/${this.packageId}.json`;
|
|
1036
|
-
// try {
|
|
1037
|
-
// const data = await fs.promises.readFile(jsonFileName, 'utf-8');
|
|
1038
|
-
// const jsonData = JSON.parse(data);
|
|
1039
|
-
// return jsonData as SuiMoveNormalizedModules;
|
|
1040
|
-
// } catch (error) {
|
|
1041
|
-
// if (this.packageId !== undefined) {
|
|
1042
|
-
// const jsonData = await this.rpcProvider.getNormalizedMoveModulesByPackage(this.packageId);
|
|
1043
|
-
// fs.writeFile(jsonFileName, JSON.stringify(jsonData, null, 2), (err) => {
|
|
1044
|
-
// if (err) {
|
|
1045
|
-
// console.error('写入文件时出错:', err);
|
|
1046
|
-
// } else {
|
|
1047
|
-
// console.log('JSON 数据已保存到文件:', jsonFileName);
|
|
1048
|
-
// }
|
|
1049
|
-
// });
|
|
1050
|
-
// return jsonData as SuiMoveNormalizedModules;
|
|
1051
|
-
// } else {
|
|
1052
|
-
// console.error('please set your package id.');
|
|
1053
|
-
// }
|
|
1054
|
-
// }
|
|
1055
|
-
// }
|
|
1056
1088
|
};
|
|
1057
1089
|
_query = new WeakMap();
|
|
1058
1090
|
_tx = new WeakMap();
|
|
@@ -1061,11 +1093,10 @@ _read = new WeakMap();
|
|
|
1061
1093
|
|
|
1062
1094
|
// src/metadata/index.ts
|
|
1063
1095
|
async function getMetadata(networkType, packageId) {
|
|
1064
|
-
const
|
|
1065
|
-
|
|
1066
|
-
});
|
|
1096
|
+
const fullnodeUrls = [getDefaultConnection(networkType).fullnode];
|
|
1097
|
+
const suiInteractor = new SuiInteractor(fullnodeUrls);
|
|
1067
1098
|
if (packageId !== void 0) {
|
|
1068
|
-
const jsonData = await
|
|
1099
|
+
const jsonData = await suiInteractor.getNormalizedMoveModulesByPackage(packageId);
|
|
1069
1100
|
return jsonData;
|
|
1070
1101
|
} else {
|
|
1071
1102
|
console.error("please set your package id.");
|
|
@@ -1077,7 +1108,6 @@ export {
|
|
|
1077
1108
|
SUI_SYSTEM_STATE_OBJECT_ID2 as SUI_SYSTEM_STATE_OBJECT_ID,
|
|
1078
1109
|
SuiAccountManager,
|
|
1079
1110
|
SuiContractFactory,
|
|
1080
|
-
SuiRpcProvider,
|
|
1081
1111
|
SuiTxBlock,
|
|
1082
1112
|
TransactionBlock4 as TransactionBlock,
|
|
1083
1113
|
getMetadata
|