evernode-js-client 0.6.21-definition-1.13 → 0.6.22
Sign up to get free protection for your applications and to get access to all the features.
- package/index.js +131 -90
- package/package.json +1 -1
package/index.js
CHANGED
@@ -51840,6 +51840,7 @@ const { FirestoreHandler } = __nccwpck_require__(9718);
|
|
51840
51840
|
const { StateHelpers } = __nccwpck_require__(3860);
|
51841
51841
|
const { EvernodeHelpers } = __nccwpck_require__(2523);
|
51842
51842
|
const { HookHelpers } = __nccwpck_require__(4675);
|
51843
|
+
const xrpl = __nccwpck_require__(4666);
|
51843
51844
|
|
51844
51845
|
const CANDIDATE_PROPOSE_HASHES_PARAM_OFFSET = 0;
|
51845
51846
|
const CANDIDATE_PROPOSE_KEYLETS_PARAM_OFFSET = 96;
|
@@ -52219,7 +52220,7 @@ class BaseEvernodeClient {
|
|
52219
52220
|
extendRefId: tx.hash,
|
52220
52221
|
tenant: tx.Account,
|
52221
52222
|
currency: tx.Amount.currency,
|
52222
|
-
payment: parseFloat(tx.Amount.value),
|
52223
|
+
payment: (tx.Flags & xrpl.PaymentFlags.tfPartialPayment) ? parseFloat(tx.DeliveredAmount.value) : parseFloat(tx.Amount.value),
|
52223
52224
|
uriTokenId: uriTokenId
|
52224
52225
|
}
|
52225
52226
|
}
|
@@ -54219,6 +54220,10 @@ class TenantClient extends BaseEvernodeClient {
|
|
54219
54220
|
for (let t of txList) {
|
54220
54221
|
t.tx.Memos = TransactionHelper.deserializeMemos(t.tx?.Memos);
|
54221
54222
|
t.tx.HookParameters = TransactionHelper.deserializeHookParams(t.tx?.HookParameters);
|
54223
|
+
|
54224
|
+
if (t.meta?.delivered_amount)
|
54225
|
+
t.tx.DeliveredAmount = t.meta.delivered_amount;
|
54226
|
+
|
54222
54227
|
const res = await this.extractEvernodeEvent(t.tx);
|
54223
54228
|
if ((res?.name === EvernodeEvents.AcquireSuccess || res?.name === EvernodeEvents.AcquireError) && res?.data?.acquireRefId === tx.id) {
|
54224
54229
|
clearTimeout(failTimeout);
|
@@ -54330,6 +54335,10 @@ class TenantClient extends BaseEvernodeClient {
|
|
54330
54335
|
for (let t of txList) {
|
54331
54336
|
t.tx.Memos = TransactionHelper.deserializeMemos(t.tx.Memos);
|
54332
54337
|
t.tx.HookParameters = TransactionHelper.deserializeHookParams(t.tx?.HookParameters);
|
54338
|
+
|
54339
|
+
if (t.meta?.delivered_amount)
|
54340
|
+
t.tx.DeliveredAmount = t.meta.delivered_amount;
|
54341
|
+
|
54333
54342
|
const res = await this.extractEvernodeEvent(t.tx);
|
54334
54343
|
if ((res?.name === TenantEvents.ExtendSuccess || res?.name === TenantEvents.ExtendError) && res?.data?.extendRefId === tx.id) {
|
54335
54344
|
clearTimeout(failTimeout);
|
@@ -54413,6 +54422,7 @@ const DefinitionsUrl = 'https://raw.githubusercontent.com/EvernodeXRPL/evernode-
|
|
54413
54422
|
|
54414
54423
|
const DefaultValues = {
|
54415
54424
|
xrplApi: null,
|
54425
|
+
useCentralizedRegistry: false,
|
54416
54426
|
}
|
54417
54427
|
|
54418
54428
|
const HookTypes = {
|
@@ -55168,6 +55178,10 @@ class FirestoreHandler {
|
|
55168
55178
|
#collectionPrefix = null;
|
55169
55179
|
|
55170
55180
|
constructor(options = {}) {
|
55181
|
+
if (!Defaults.values.useCentralizedRegistry) {
|
55182
|
+
console.warn("Please change the useCentralizedRegistry flag to true in Defaults if you want to use this function!")
|
55183
|
+
throw new Error("Centralized function is in use!!")
|
55184
|
+
}
|
55171
55185
|
this.#projectId = options.stateIndexId || Defaults.values.stateIndexId;
|
55172
55186
|
this.#collectionPrefix = options.collectionPrefix || Defaults.values.governorAddress;
|
55173
55187
|
}
|
@@ -57287,6 +57301,9 @@ class XrplAccount {
|
|
57287
57301
|
NetworkID: Defaults.values.networkID
|
57288
57302
|
}
|
57289
57303
|
|
57304
|
+
if (options?.Flags)
|
57305
|
+
txOptions.Flags = options.Flags;
|
57306
|
+
|
57290
57307
|
Object.assign(tx, txOptions);
|
57291
57308
|
const txnBlob = this.xrplApi.xrplHelper.encode(tx);
|
57292
57309
|
const fees = options.fee || await this.xrplApi.getTransactionFee(txnBlob);
|
@@ -57365,12 +57382,11 @@ class XrplApi {
|
|
57365
57382
|
#client;
|
57366
57383
|
#events = new EventEmitter();
|
57367
57384
|
#addressSubscriptions = [];
|
57368
|
-
#initialConnectCalled = false;
|
57369
57385
|
#isPermanentlyDisconnected = false;
|
57370
|
-
#
|
57386
|
+
#isConnectionAcquired = false;
|
57387
|
+
#isClientAcquired = false;
|
57371
57388
|
#isPrimaryServerConnected = false;
|
57372
57389
|
#isFallbackServerConnected = false;
|
57373
|
-
#isAttemptingConnection = false;
|
57374
57390
|
#xrplClientOptions;
|
57375
57391
|
#autoReconnect;
|
57376
57392
|
|
@@ -57386,47 +57402,77 @@ class XrplApi {
|
|
57386
57402
|
throw 'Either primaryServer or fallbackServers required.';
|
57387
57403
|
|
57388
57404
|
this.#xrplClientOptions = options.xrplClientOptions;
|
57389
|
-
const initServer = this.#primaryServer || this.#fallbackServers[0];
|
57390
|
-
const client = new xrpl.Client(initServer, this.#xrplClientOptions);
|
57391
|
-
this.#initXrplClient(client);
|
57392
57405
|
this.#autoReconnect = options.autoReconnect ?? true;
|
57393
57406
|
}
|
57394
57407
|
|
57395
|
-
async #
|
57396
|
-
while (this.#
|
57408
|
+
async #acquireClient() {
|
57409
|
+
while (this.#isClientAcquired || this.#isConnectionAcquired) {
|
57397
57410
|
await new Promise((resolve) => setTimeout(resolve, 100));
|
57398
57411
|
}
|
57399
|
-
|
57400
|
-
this.#isClientLocked = true;
|
57401
|
-
this.#client.removeAllListeners(); // Remove existing event listeners to avoid them getting called from the old client object.
|
57402
|
-
await this.#client.disconnect();
|
57403
|
-
this.#isClientLocked = false;
|
57404
|
-
}
|
57405
|
-
this.#client = client;
|
57412
|
+
this.#isClientAcquired = true;
|
57406
57413
|
}
|
57407
57414
|
|
57408
|
-
|
57409
|
-
|
57410
|
-
|
57415
|
+
#releaseClient() {
|
57416
|
+
this.#isClientAcquired = false;
|
57417
|
+
}
|
57418
|
+
|
57419
|
+
async #acquireConnection() {
|
57420
|
+
while (this.#isClientAcquired) {
|
57421
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
57411
57422
|
}
|
57423
|
+
this.#isConnectionAcquired = true;
|
57424
|
+
}
|
57425
|
+
|
57426
|
+
#releaseConnection() {
|
57427
|
+
this.#isConnectionAcquired = false;
|
57428
|
+
}
|
57412
57429
|
|
57430
|
+
async #setXrplClient(client) {
|
57431
|
+
await this.#acquireClient();
|
57413
57432
|
try {
|
57414
|
-
|
57433
|
+
if (this.#client) // Clear all listeners if there's an already created client.
|
57434
|
+
await this.#client.removeAllListeners();
|
57435
|
+
|
57436
|
+
this.#client = client;
|
57437
|
+
this.#releaseClient();
|
57415
57438
|
}
|
57416
57439
|
catch (e) {
|
57440
|
+
this.#releaseClient();
|
57417
57441
|
console.log("Error occurred in Client initiation:", e)
|
57418
57442
|
}
|
57443
|
+
}
|
57444
|
+
|
57445
|
+
async #handleClientConnect(client) {
|
57446
|
+
await this.#initEventListeners(client);
|
57419
57447
|
|
57420
|
-
|
57448
|
+
if (!client.isConnected())
|
57449
|
+
await client.connect();
|
57450
|
+
}
|
57451
|
+
|
57452
|
+
async #initEventListeners(client) {
|
57453
|
+
// First remove all the listeners.
|
57454
|
+
try {
|
57455
|
+
await client.removeAllListeners();
|
57456
|
+
}
|
57457
|
+
catch { }
|
57458
|
+
|
57459
|
+
client.on('error', (errorCode, errorMessage) => {
|
57421
57460
|
console.log(errorCode + ': ' + errorMessage);
|
57422
57461
|
});
|
57423
57462
|
|
57424
|
-
|
57463
|
+
client.on('connected', async () => {
|
57464
|
+
await this.#setXrplClient(client)
|
57465
|
+
});
|
57466
|
+
|
57467
|
+
client.on('disconnected', async (code) => {
|
57468
|
+
this.#isPrimaryServerConnected = false;
|
57469
|
+
this.#isFallbackServerConnected = false;
|
57470
|
+
|
57425
57471
|
if (this.#autoReconnect && !this.#isPermanentlyDisconnected) {
|
57426
|
-
console.log(`Connection failure for ${
|
57472
|
+
console.log(`Connection failure for ${client.url} (code:${code})`);
|
57427
57473
|
console.log("Re-initializing xrpl client.");
|
57428
57474
|
try {
|
57429
|
-
|
57475
|
+
await this.#connectXrplClient(true);
|
57430
57476
|
}
|
57431
57477
|
catch (e) {
|
57432
57478
|
console.log("Error occurred while re-initializing", e)
|
@@ -57434,12 +57480,12 @@ class XrplApi {
|
|
57434
57480
|
}
|
57435
57481
|
});
|
57436
57482
|
|
57437
|
-
|
57483
|
+
client.on('ledgerClosed', (ledger) => {
|
57438
57484
|
this.ledgerIndex = ledger.ledger_index;
|
57439
57485
|
this.#events.emit(XrplApiEvents.LEDGER, ledger);
|
57440
57486
|
});
|
57441
57487
|
|
57442
|
-
|
57488
|
+
client.on("transaction", async (data) => {
|
57443
57489
|
if (data.validated) {
|
57444
57490
|
// NFTokenAcceptOffer transactions does not contain a Destination. So we check whether the accepted offer is created by which subscribed account
|
57445
57491
|
if (data.transaction.TransactionType === 'URITokenBuy') {
|
@@ -57467,7 +57513,12 @@ class XrplApi {
|
|
57467
57513
|
LedgerHash: data.ledger_hash,
|
57468
57514
|
LedgerIndex: data.ledger_index,
|
57469
57515
|
...data.transaction
|
57470
|
-
};
|
57516
|
+
};
|
57517
|
+
|
57518
|
+
if (data.meta?.delivered_amount)
|
57519
|
+
tx.DeliveredAmount = data.meta.delivered_amount;
|
57520
|
+
|
57521
|
+
// Create an object copy. Otherwise xrpl client will mutate the transaction object,
|
57471
57522
|
const eventName = tx.TransactionType.toLowerCase();
|
57472
57523
|
// Emit the event only for successful transactions, Otherwise emit error.
|
57473
57524
|
if (data.engine_result === "tesSUCCESS") {
|
@@ -57480,7 +57531,6 @@ class XrplApi {
|
|
57480
57531
|
}
|
57481
57532
|
}
|
57482
57533
|
}
|
57483
|
-
|
57484
57534
|
});
|
57485
57535
|
}
|
57486
57536
|
|
@@ -57496,30 +57546,22 @@ class XrplApi {
|
|
57496
57546
|
if (this.#isPrimaryServerConnected || this.#isPermanentlyDisconnected) {
|
57497
57547
|
break serverIterator;
|
57498
57548
|
}
|
57499
|
-
|
57549
|
+
++attempt;
|
57500
57550
|
try {
|
57501
|
-
while (this.#isAttemptingConnection) {
|
57502
|
-
await new Promise((resolve) => setTimeout(resolve, 100));
|
57503
|
-
}
|
57504
|
-
this.#isAttemptingConnection = true;
|
57505
57551
|
const client = new xrpl.Client(server, this.#xrplClientOptions);
|
57506
57552
|
if (!this.#isPrimaryServerConnected) {
|
57507
57553
|
await this.#handleClientConnect(client);
|
57508
|
-
await this.#initXrplClient(client);
|
57509
57554
|
this.#isFallbackServerConnected = true;
|
57510
|
-
console.log(`Successfully connected to the fallback server ${server}`)
|
57511
57555
|
}
|
57512
|
-
this.#isAttemptingConnection = false;
|
57513
57556
|
break serverIterator;
|
57514
57557
|
}
|
57515
57558
|
catch (e) {
|
57516
57559
|
console.log(`Error occurred while connecting to fallback server ${server}`, e)
|
57517
|
-
this.#isAttemptingConnection = false;
|
57518
57560
|
if (!this.#isPermanentlyDisconnected) {
|
57519
57561
|
if (!maxRounds || round < maxRounds)
|
57520
57562
|
console.log(`Fallback server ${server} connection attempt ${attempt} failed. Retrying in ${2 * round}s...`);
|
57521
57563
|
else
|
57522
|
-
|
57564
|
+
throw `Fallback server ${server} connection max attempts failed.`;
|
57523
57565
|
await new Promise(resolve => setTimeout(resolve, 2 * round * 1000));
|
57524
57566
|
}
|
57525
57567
|
}
|
@@ -57529,30 +57571,25 @@ class XrplApi {
|
|
57529
57571
|
}
|
57530
57572
|
}
|
57531
57573
|
|
57532
|
-
async #attemptPrimaryServerReconnect() {
|
57574
|
+
async #attemptPrimaryServerReconnect(maxAttempts = null) {
|
57533
57575
|
let attempt = 0;
|
57534
57576
|
while (!this.#isPermanentlyDisconnected && !this.#isPrimaryServerConnected) { // Keep attempting until consumer calls disconnect() manually.
|
57535
|
-
|
57577
|
+
++attempt;
|
57536
57578
|
try {
|
57537
57579
|
const client = new xrpl.Client(this.#primaryServer, this.#xrplClientOptions);
|
57538
|
-
while (this.#isAttemptingConnection) {
|
57539
|
-
await new Promise((resolve) => setTimeout(resolve, 100));
|
57540
|
-
}
|
57541
|
-
this.#isAttemptingConnection = true;
|
57542
57580
|
await this.#handleClientConnect(client);
|
57543
|
-
await this.#initXrplClient(client);
|
57544
57581
|
this.#isFallbackServerConnected = false;
|
57545
57582
|
this.#isPrimaryServerConnected = true;
|
57546
|
-
console.log("Successfully connected to the primary server");
|
57547
|
-
this.#isAttemptingConnection = false;
|
57548
57583
|
break;
|
57549
57584
|
}
|
57550
57585
|
catch (e) {
|
57551
57586
|
console.log("Error occurred while re-connecting to the primary server", e)
|
57552
|
-
this.#isAttemptingConnection = false;
|
57553
57587
|
if (!this.#isPermanentlyDisconnected) {
|
57554
57588
|
const delaySec = 2 * attempt; // Retry with backoff delay.
|
57555
|
-
|
57589
|
+
if (!maxAttempts || attempt < maxAttempts)
|
57590
|
+
console.log(`Primary server ${this.#primaryServer} attempt ${attempt} failed. Retrying in ${delaySec}s...`);
|
57591
|
+
else
|
57592
|
+
throw `Primary server ${this.#primaryServer} connection max attempts failed.`;
|
57556
57593
|
await new Promise(resolve => setTimeout(resolve, delaySec * 1000));
|
57557
57594
|
}
|
57558
57595
|
}
|
@@ -57566,26 +57603,22 @@ class XrplApi {
|
|
57566
57603
|
} else {
|
57567
57604
|
this.#attemptFallbackServerReconnect();
|
57568
57605
|
}
|
57569
|
-
await this.#waitForReconnection();
|
57570
57606
|
}
|
57571
57607
|
else {
|
57572
|
-
|
57573
|
-
|
57574
|
-
|
57575
|
-
this.#
|
57576
|
-
} catch {
|
57577
|
-
await this.#attemptFallbackServerReconnect(1, 1);
|
57578
|
-
if (this.#isFallbackServerConnected)
|
57579
|
-
await this.#attemptPrimaryServerReconnect();
|
57608
|
+
if (this.#primaryServer) {
|
57609
|
+
Promise.all([this.#attemptFallbackServerReconnect(1, 1), this.#attemptPrimaryServerReconnect(1)]);
|
57610
|
+
} else {
|
57611
|
+
this.#attemptFallbackServerReconnect(1, 1);
|
57580
57612
|
}
|
57581
57613
|
}
|
57582
57614
|
|
57615
|
+
await this.#waitForConnection();
|
57616
|
+
|
57583
57617
|
// After connection established, check again whether maintainConnections has become false.
|
57584
57618
|
// This is in case the consumer has called disconnect() while connection is being established.
|
57585
57619
|
if (!this.#isPermanentlyDisconnected) {
|
57586
|
-
this
|
57587
|
-
|
57588
|
-
this.#isClientLocked = false;
|
57620
|
+
this.ledgerIndex = await this.#getLedgerIndex();
|
57621
|
+
|
57589
57622
|
this.#subscribeToStream('ledger');
|
57590
57623
|
|
57591
57624
|
// Re-subscribe to existing account address subscriptions (in case this is a reconnect)
|
@@ -57621,20 +57654,16 @@ class XrplApi {
|
|
57621
57654
|
}
|
57622
57655
|
|
57623
57656
|
async #handleClientRequest(request = {}) {
|
57624
|
-
|
57625
|
-
|
57657
|
+
await this.#acquireConnection();
|
57658
|
+
try {
|
57659
|
+
const response = await this.#client.request(request);
|
57660
|
+
this.#releaseConnection();
|
57661
|
+
return response;
|
57662
|
+
}
|
57663
|
+
catch (e) {
|
57664
|
+
this.#releaseConnection();
|
57665
|
+
throw e;
|
57626
57666
|
}
|
57627
|
-
this.#isClientLocked = true;
|
57628
|
-
const response = await this.#client.request(request);
|
57629
|
-
|
57630
|
-
this.#isClientLocked = false;
|
57631
|
-
return response;
|
57632
|
-
}
|
57633
|
-
|
57634
|
-
async #handleClientConnect(client = this.#client) {
|
57635
|
-
this.#isClientLocked = true;
|
57636
|
-
await client.connect();
|
57637
|
-
this.#isClientLocked = false;
|
57638
57667
|
}
|
57639
57668
|
|
57640
57669
|
on(event, handler) {
|
@@ -57649,7 +57678,7 @@ class XrplApi {
|
|
57649
57678
|
this.#events.off(event, handler);
|
57650
57679
|
}
|
57651
57680
|
|
57652
|
-
async #
|
57681
|
+
async #waitForConnection() {
|
57653
57682
|
while (!(this.#isPrimaryServerConnected || this.#isFallbackServerConnected)) {
|
57654
57683
|
await new Promise(resolve => setTimeout(resolve, 100));
|
57655
57684
|
}
|
@@ -57657,10 +57686,6 @@ class XrplApi {
|
|
57657
57686
|
}
|
57658
57687
|
|
57659
57688
|
async connect() {
|
57660
|
-
if (this.#initialConnectCalled) {
|
57661
|
-
return
|
57662
|
-
}
|
57663
|
-
this.#initialConnectCalled = true;
|
57664
57689
|
this.#isPermanentlyDisconnected = false;
|
57665
57690
|
await this.#connectXrplClient();
|
57666
57691
|
const definitions = await this.#handleClientRequest({ command: 'server_definitions' })
|
@@ -57668,15 +57693,33 @@ class XrplApi {
|
|
57668
57693
|
}
|
57669
57694
|
|
57670
57695
|
async disconnect() {
|
57671
|
-
this.#
|
57672
|
-
this.#isPermanentlyDisconnected = true;
|
57696
|
+
await this.#acquireClient();
|
57673
57697
|
|
57674
|
-
|
57675
|
-
this.#
|
57676
|
-
|
57677
|
-
this.#
|
57678
|
-
|
57679
|
-
|
57698
|
+
try {
|
57699
|
+
this.#isPermanentlyDisconnected = true;
|
57700
|
+
|
57701
|
+
if (this.#client.isConnected()) {
|
57702
|
+
await this.#client.disconnect().catch(console.error);
|
57703
|
+
}
|
57704
|
+
this.#releaseClient();
|
57705
|
+
}
|
57706
|
+
catch (e) {
|
57707
|
+
this.#releaseClient();
|
57708
|
+
throw e;
|
57709
|
+
}
|
57710
|
+
}
|
57711
|
+
|
57712
|
+
async #getLedgerIndex() {
|
57713
|
+
await this.#acquireConnection();
|
57714
|
+
|
57715
|
+
try {
|
57716
|
+
const index = await this.#client.getLedgerIndex();
|
57717
|
+
this.#releaseConnection();
|
57718
|
+
return index;
|
57719
|
+
}
|
57720
|
+
catch (e) {
|
57721
|
+
this.#releaseConnection();
|
57722
|
+
throw e;
|
57680
57723
|
}
|
57681
57724
|
}
|
57682
57725
|
|
@@ -57806,9 +57849,7 @@ class XrplApi {
|
|
57806
57849
|
|
57807
57850
|
await new Promise(r => setTimeout(r, LEDGER_CLOSE_TIME));
|
57808
57851
|
|
57809
|
-
|
57810
|
-
const latestLedger = await this.#client.getLedgerIndex();
|
57811
|
-
this.#isClientLocked = false;
|
57852
|
+
const latestLedger = await this.#getLedgerIndex();
|
57812
57853
|
|
57813
57854
|
if (lastLedger < latestLedger) {
|
57814
57855
|
throw `The latest ledger sequence ${latestLedger} is greater than the transaction's LastLedgerSequence (${lastLedger}).\n` +
|