evernode-js-client 0.4.50 → 0.4.53

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. package/index.js +149 -102
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -11715,7 +11715,6 @@ const { EncryptionHelper } = __nccwpck_require__(4832);
11715
11715
  const { EventEmitter } = __nccwpck_require__(6170);
11716
11716
  const { UtilHelpers } = __nccwpck_require__(6687);
11717
11717
  const { FirestoreHandler } = __nccwpck_require__(9718);
11718
- const { XflHelpers } = __nccwpck_require__(3243);
11719
11718
  const { StateHelpers } = __nccwpck_require__(3860);
11720
11719
 
11721
11720
  class BaseEvernodeClient {
@@ -11828,16 +11827,27 @@ class BaseEvernodeClient {
11828
11827
 
11829
11828
  async #getEvernodeConfig() {
11830
11829
  let states = await this.getHookStates();
11831
- return {
11832
- evrIssuerAddress: codec.encodeAccountID(Buffer.from(UtilHelpers.getStateData(states, HookStateKeys.EVR_ISSUER_ADDR), 'hex')),
11833
- foundationAddress: codec.encodeAccountID(Buffer.from(UtilHelpers.getStateData(states, HookStateKeys.FOUNDATION_ADDR), 'hex')),
11834
- hostRegFee: Number(Buffer.from(UtilHelpers.getStateData(states, HookStateKeys.HOST_REG_FEE), 'hex').readBigUInt64BE()),
11835
- momentSize: Buffer.from(UtilHelpers.getStateData(states, HookStateKeys.MOMENT_SIZE), 'hex').readUInt16BE(),
11836
- hostHeartbeatFreq: Buffer.from(UtilHelpers.getStateData(states, HookStateKeys.HOST_HEARTBEAT_FREQ), 'hex').readUInt16BE(),
11837
- momentBaseIdx: Number(Buffer.from(UtilHelpers.getStateData(states, HookStateKeys.MOMENT_BASE_IDX), 'hex').readBigInt64BE()),
11838
- purchaserTargetPrice: XflHelpers.toString(Buffer.from(UtilHelpers.getStateData(states, HookStateKeys.PURCHASER_TARGET_PRICE), 'hex').readBigInt64BE()),
11839
- leaseAcquireWindow: Buffer.from(UtilHelpers.getStateData(states, HookStateKeys.LEASE_ACQUIRE_WINDOW), 'hex').readUInt16BE()
11840
- };
11830
+ const configStateKeys = {
11831
+ evrIssuerAddress: HookStateKeys.EVR_ISSUER_ADDR,
11832
+ foundationAddress: HookStateKeys.FOUNDATION_ADDR,
11833
+ hostRegFee: HookStateKeys.HOST_REG_FEE,
11834
+ momentSize: HookStateKeys.MOMENT_SIZE,
11835
+ hostHeartbeatFreq: HookStateKeys.HOST_HEARTBEAT_FREQ,
11836
+ momentBaseIdx: HookStateKeys.MOMENT_BASE_IDX,
11837
+ purchaserTargetPrice: HookStateKeys.PURCHASER_TARGET_PRICE,
11838
+ leaseAcquireWindow: HookStateKeys.LEASE_ACQUIRE_WINDOW,
11839
+ rewardInfo: HookStateKeys.REWARD_INFO,
11840
+ rewardConfiguaration: HookStateKeys.REWARD_CONFIGURATION,
11841
+ hostCount: HookStateKeys.HOST_COUNT
11842
+ }
11843
+ let config = {};
11844
+ for (const [key, value] of Object.entries(configStateKeys)) {
11845
+ const stateKey = Buffer.from(value, 'hex');
11846
+ const stateData = Buffer.from(UtilHelpers.getStateData(states, value), 'hex');
11847
+ const decoded = StateHelpers.decodeStateData(stateKey, stateData);
11848
+ config[key] = decoded.value;
11849
+ }
11850
+ return config;
11841
11851
  }
11842
11852
 
11843
11853
  async refreshConfig() {
@@ -11850,13 +11860,13 @@ class BaseEvernodeClient {
11850
11860
  else if (!tx)
11851
11861
  console.log('handleEvernodeEvent: Invalid transaction.');
11852
11862
  else {
11853
- const ev = await this.#extractEvernodeEvent(tx);
11863
+ const ev = await this.extractEvernodeEvent(tx);
11854
11864
  if (ev && this.#watchEvents.find(e => e === ev.name))
11855
11865
  this.events.emit(ev.name, ev.data);
11856
11866
  }
11857
11867
  }
11858
11868
 
11859
- async #extractEvernodeEvent(tx) {
11869
+ async extractEvernodeEvent(tx) {
11860
11870
  if (tx.TransactionType === 'NFTokenAcceptOffer' && tx.NFTokenSellOffer && tx.Memos.length >= 1 &&
11861
11871
  tx.Memos[0].type === MemoTypes.ACQUIRE_LEASE && tx.Memos[0].format === MemoFormats.BASE64 && tx.Memos[0].data) {
11862
11872
 
@@ -11875,8 +11885,8 @@ class BaseEvernodeClient {
11875
11885
  data: {
11876
11886
  transaction: tx,
11877
11887
  host: tx.Destination,
11878
- nfTokenId: tx.NFTokenSellOffer.NFTokenID,
11879
- leaseAmount: tx.NFTokenSellOffer.Amount.value,
11888
+ nfTokenId: tx.NFTokenSellOffer?.NFTokenID,
11889
+ leaseAmount: tx.NFTokenSellOffer?.Amount?.value,
11880
11890
  acquireRefId: tx.hash,
11881
11891
  tenant: tx.Account,
11882
11892
  payload: payload
@@ -12600,16 +12610,13 @@ module.exports = {
12600
12610
 
12601
12611
  const { BaseEvernodeClient } = __nccwpck_require__(6263);
12602
12612
  const { EvernodeEvents, MemoFormats, MemoTypes, ErrorCodes, ErrorReasons, EvernodeConstants } = __nccwpck_require__(9849);
12603
- const { EventEmitter } = __nccwpck_require__(6170);
12604
12613
  const { EncryptionHelper } = __nccwpck_require__(4832);
12605
12614
  const { XrplAccount } = __nccwpck_require__(9329);
12606
12615
  const { UtilHelpers } = __nccwpck_require__(6687);
12607
12616
  const { Buffer } = __nccwpck_require__(4300);
12608
12617
  const codec = __nccwpck_require__(597);
12609
12618
  const { EvernodeHelpers } = __nccwpck_require__(2523);
12610
-
12611
- const ACQUIRE_WATCH_PREFIX = 'acquire_';
12612
- const EXTEND_WATCH_PREFIX = 'extend_';
12619
+ const { TransactionHelper } = __nccwpck_require__(7071);
12613
12620
 
12614
12621
  const DEFAULT_WAIT_TIMEOUT = 60000;
12615
12622
 
@@ -12622,24 +12629,8 @@ const TenantEvents = {
12622
12629
 
12623
12630
  class TenantClient extends BaseEvernodeClient {
12624
12631
 
12625
- #respWatcher = new EventEmitter();
12626
-
12627
12632
  constructor(xrpAddress, xrpSecret, options = {}) {
12628
- super(xrpAddress, xrpSecret, Object.values(TenantEvents), true, options);
12629
-
12630
- this.on(TenantEvents.AcquireSuccess, (ev) => {
12631
- this.#respWatcher.emit(ACQUIRE_WATCH_PREFIX + ev.acquireRefId, { success: true, data: ev.payload, transaction: ev.transaction });
12632
- });
12633
- this.on(TenantEvents.AcquireError, (ev) => {
12634
- this.#respWatcher.emit(ACQUIRE_WATCH_PREFIX + ev.acquireRefId, { success: false, data: ev.reason, transaction: ev.transaction });
12635
- });
12636
-
12637
- this.on(TenantEvents.ExtendSuccess, (ev) => {
12638
- this.#respWatcher.emit(EXTEND_WATCH_PREFIX + ev.extendRefId, { success: true, transaction: ev.transaction, expiryMoment: ev.expiryMoment });
12639
- });
12640
- this.on(TenantEvents.ExtendError, (ev) => {
12641
- this.#respWatcher.emit(EXTEND_WATCH_PREFIX + ev.extendRefId, { success: false, data: ev.reason, transaction: ev.transaction });
12642
- });
12633
+ super(xrpAddress, xrpSecret, Object.values(TenantEvents), false, options);
12643
12634
  }
12644
12635
 
12645
12636
  async prepareAccount() {
@@ -12702,28 +12693,42 @@ class TenantClient extends BaseEvernodeClient {
12702
12693
  return this.xrplAcc.buyNft(selectedOfferIndex, [{ type: MemoTypes.ACQUIRE_LEASE, format: MemoFormats.BASE64, data: ecrypted }], options.transactionOptions);
12703
12694
  }
12704
12695
 
12705
- watchAcquireResponse(tx, options = {}) {
12706
- return new Promise(async (resolve, reject) => {
12696
+ async watchAcquireResponse(tx, options = {}) {
12707
12697
  console.log(`Waiting for acquire response... (txHash: ${tx.id})`);
12708
12698
 
12709
- const watchEvent = ACQUIRE_WATCH_PREFIX + tx.id;
12710
-
12711
12699
  const failTimeout = setTimeout(() => {
12712
- this.#respWatcher.off(watchEvent);
12713
- reject({ error: ErrorCodes.ACQUIRE_ERR, reason: ErrorReasons.TIMEOUT });
12700
+ throw({ error: ErrorCodes.ACQUIRE_ERR, reason: ErrorReasons.TIMEOUT });
12714
12701
  }, options.timeout || DEFAULT_WAIT_TIMEOUT);
12715
-
12716
- this.#respWatcher.once(watchEvent, async (ev) => {
12717
- clearTimeout(failTimeout);
12718
- if (ev.success) {
12719
- const instanceInfo = ev.data;
12720
- resolve({ instance: instanceInfo.content, transaction: ev.transaction });
12721
- }
12722
- else {
12723
- reject({ error: ErrorCodes.ACQUIRE_ERR, reason: ev.data, transaction: ev.transaction });
12702
+
12703
+ let relevantTx = null;
12704
+ while (!relevantTx) {
12705
+ const txList = await this.xrplAcc.getAccountTrx(tx.details.ledger_index);
12706
+ for (let t of txList) {
12707
+ t.tx.Memos = TransactionHelper.deserializeMemos(t.tx?.Memos);
12708
+ const res = await this.extractEvernodeEvent(t.tx);
12709
+ if ((res?.name === EvernodeEvents.AcquireSuccess || res?.name === EvernodeEvents.AcquireError) && res?.data?.acquireRefId === tx.id) {
12710
+ clearTimeout(failTimeout);
12711
+ relevantTx = res;
12712
+ break;
12713
+ }
12724
12714
  }
12725
- })
12726
- });
12715
+ await new Promise(resolve => setTimeout(resolve, 2000));
12716
+ }
12717
+
12718
+ if (relevantTx?.name === TenantEvents.AcquireSuccess) {
12719
+ return({
12720
+ transaction: relevantTx?.data.transaction,
12721
+ instance: relevantTx?.data.payload.content,
12722
+ acquireRefId: relevantTx?.data.acquireRefId
12723
+ });
12724
+ } else if (relevantTx?.name === TenantEvents.AcquireError) {
12725
+ throw({
12726
+ error: ErrorCodes.ACQUIRE_ERR,
12727
+ transaction: relevantTx?.data.transaction,
12728
+ reason: relevantTx?.data.reason,
12729
+ acquireRefId: relevantTx?.data.acquireRefId
12730
+ })
12731
+ }
12727
12732
  }
12728
12733
 
12729
12734
  acquireLease(hostAddress, requirement, options = {}) {
@@ -12732,13 +12737,11 @@ class TenantClient extends BaseEvernodeClient {
12732
12737
  reject({ error: ErrorCodes.ACQUIRE_ERR, reason: error.reason || ErrorReasons.TRANSACTION_FAILURE, content: error.error || error });
12733
12738
  });
12734
12739
  if (tx) {
12735
- const response = await this.watchAcquireResponse(tx, options).catch(error => {
12736
- error.acquireRefId = tx.id;
12737
- reject(error);
12738
- });
12739
- if (response) {
12740
- response.acquireRefId = tx.id;
12740
+ try {
12741
+ const response = await this.watchAcquireResponse(tx, options);
12741
12742
  resolve(response);
12743
+ } catch (error) {
12744
+ reject(error);
12742
12745
  }
12743
12746
  }
12744
12747
  });
@@ -12750,27 +12753,41 @@ class TenantClient extends BaseEvernodeClient {
12750
12753
  [{ type: MemoTypes.EXTEND_LEASE, format: MemoFormats.HEX, data: tokenID }], options.transactionOptions);
12751
12754
  }
12752
12755
 
12753
- watchExtendResponse(tx, options = {}) {
12754
- return new Promise(async (resolve, reject) => {
12755
- console.log(`Waiting for extend lease response... (txHash: ${tx.id})`);
12756
-
12757
- const watchEvent = EXTEND_WATCH_PREFIX + tx.id;
12756
+ async watchExtendResponse(tx, minLedgerIndex, options = {}) {
12757
+ console.log(`Waiting for extend lease response... (txHash: ${tx.id})`);
12758
12758
 
12759
- const failTimeout = setTimeout(() => {
12760
- this.#respWatcher.off(watchEvent);
12761
- reject({ error: ErrorCodes.EXTEND_ERR, reason: ErrorReasons.TIMEOUT });
12762
- }, options.timeout || DEFAULT_WAIT_TIMEOUT);
12759
+ const failTimeout = setTimeout(() => {
12760
+ throw({ error: ErrorCodes.EXTEND_ERR, reason: ErrorReasons.TIMEOUT });
12761
+ }, options.timeout || DEFAULT_WAIT_TIMEOUT);
12763
12762
 
12764
- this.#respWatcher.once(watchEvent, async (ev) => {
12765
- clearTimeout(failTimeout);
12766
- if (ev.success) {
12767
- resolve({ transaction: ev.transaction, expiryMoment: ev.expiryMoment });
12768
- }
12769
- else {
12770
- reject({ error: ErrorCodes.EXTEND_ERR, reason: ev.data, transaction: ev.transaction });
12763
+ let relevantTx = null;
12764
+ while (!relevantTx) {
12765
+ const txList = await this.xrplAcc.getAccountTrx(minLedgerIndex);
12766
+ for (let t of txList) {
12767
+ t.tx.Memos = TransactionHelper.deserializeMemos(t.tx.Memos);
12768
+ const res = await this.extractEvernodeEvent(t.tx);
12769
+ if ((res?.name === TenantEvents.ExtendSuccess || res?.name === TenantEvents.ExtendError) && res?.data?.extendRefId === tx.id) {
12770
+ clearTimeout(failTimeout);
12771
+ relevantTx = res;
12772
+ break;
12771
12773
  }
12774
+ }
12775
+ await new Promise(resolve => setTimeout(resolve, 1000));
12776
+ }
12777
+
12778
+ if (relevantTx?.name === TenantEvents.ExtendSuccess) {
12779
+ return({
12780
+ transaction: relevantTx?.data.transaction,
12781
+ expiryMoment: relevantTx?.data.expiryMoment,
12782
+ extendeRefId: relevantTx?.data.extendRefId
12783
+ });
12784
+ } else if (relevantTx?.name === TenantEvents.ExtendError) {
12785
+ throw({
12786
+ error: ErrorCodes.EXTEND_ERR,
12787
+ transaction: relevantTx?.data.transaction,
12788
+ reason: relevantTx?.data.reason
12772
12789
  })
12773
- });
12790
+ }
12774
12791
  }
12775
12792
 
12776
12793
  extendLease(hostAddress, moments, instanceName, options = {}) {
@@ -12783,19 +12800,20 @@ class TenantClient extends BaseEvernodeClient {
12783
12800
  return;
12784
12801
  }
12785
12802
 
12803
+ let minLedgerIndex = this.xrplApi.ledgerIndex;
12804
+
12786
12805
  // Get the agreement lease amount from the nft and calculate EVR amount to be sent.
12787
12806
  const uriInfo = UtilHelpers.decodeLeaseNftUri(nft.URI);
12788
12807
  const tx = await this.extendLeaseSubmit(hostAddress, moments * uriInfo.leaseAmount, tokenID, options).catch(error => {
12789
12808
  reject({ error: ErrorCodes.EXTEND_ERR, reason: error.reason || ErrorReasons.TRANSACTION_FAILURE, content: error.error || error });
12790
12809
  });
12810
+
12791
12811
  if (tx) {
12792
- const response = await this.watchExtendResponse(tx, options).catch(error => {
12793
- error.extendeRefId = tx.id;
12794
- reject(error);
12795
- });
12796
- if (response) {
12797
- response.extendeRefId = tx.id;
12812
+ try {
12813
+ const response = await this.watchExtendResponse(tx, minLedgerIndex, options)
12798
12814
  resolve(response);
12815
+ } catch (error) {
12816
+ reject(error);
12799
12817
  }
12800
12818
  }
12801
12819
  });
@@ -13657,6 +13675,8 @@ const { XflHelpers } = __nccwpck_require__(3243);
13657
13675
  const { FirestoreHandler } = __nccwpck_require__(9718);
13658
13676
  const { StateHelpers } = __nccwpck_require__(3860);
13659
13677
  const { UtilHelpers } = __nccwpck_require__(6687);
13678
+ const { TransactionHelper } = __nccwpck_require__(7071);
13679
+ const { EncryptionHelper } = __nccwpck_require__(4832);
13660
13680
 
13661
13681
  module.exports = {
13662
13682
  RegistryClient,
@@ -13675,6 +13695,8 @@ module.exports = {
13675
13695
  StateHelpers,
13676
13696
  FirestoreHandler,
13677
13697
  UtilHelpers,
13698
+ TransactionHelper,
13699
+ EncryptionHelper,
13678
13700
  HookStateKeys,
13679
13701
  MemoTypes
13680
13702
  }
@@ -14352,6 +14374,9 @@ class XrplAccount {
14352
14374
  return xrpl.parseAccountRootFlags((await this.getInfo()).Flags);
14353
14375
  }
14354
14376
 
14377
+ async getAccountTrx(minLedgerIndex = -1, maxLedgerIndex = -1, isForward = true) {
14378
+ return await this.xrplApi.getAccountTrx(this.address, { ledger_index_min: minLedgerIndex, ledger_index_max: maxLedgerIndex, forward: isForward});
14379
+ }
14355
14380
 
14356
14381
  setAccountFields(fields, options = {}) {
14357
14382
  /**
@@ -14714,6 +14739,16 @@ const { DefaultValues } = __nccwpck_require__(8262);
14714
14739
  const { TransactionHelper } = __nccwpck_require__(7071);
14715
14740
  const { XrplApiEvents } = __nccwpck_require__(3307);
14716
14741
 
14742
+ const MAX_PAGE_LIMIT = 400;
14743
+ const API_REQ_TYPE = {
14744
+ NAMESPACE_ENTRIES: 'namespace_entries',
14745
+ ACCOUNT_OBJECTS: 'account_objects',
14746
+ LINES: 'lines',
14747
+ ACCOUNT_NFTS: 'account_nfts',
14748
+ OFFERS: 'offers',
14749
+ TRANSACTIONS: 'transactions'
14750
+ }
14751
+
14717
14752
  class XrplApi {
14718
14753
 
14719
14754
  #rippledServer;
@@ -14836,6 +14871,29 @@ class XrplApi {
14836
14871
  }
14837
14872
  }
14838
14873
 
14874
+ async #requestWithPaging(requestObj, requestType) {
14875
+ let res = [];
14876
+ let checked = false;
14877
+ let resp;
14878
+ let count = requestObj?.limit;
14879
+
14880
+ while ((!count || count > 0) && (!checked || resp?.result?.marker)) {
14881
+ checked = true;
14882
+ requestObj.limit = count ? Math.min(count, MAX_PAGE_LIMIT) : MAX_PAGE_LIMIT;
14883
+ if (resp?.result?.marker)
14884
+ requestObj.marker = resp?.result?.marker;
14885
+ else
14886
+ delete requestObj.marker;
14887
+ resp = (await this.#client.request(requestObj));
14888
+ if (resp?.result && resp?.result[requestType])
14889
+ res.push(...resp.result[requestType]);
14890
+ if (count)
14891
+ count -= requestObj.limit;
14892
+ }
14893
+
14894
+ return res;
14895
+ }
14896
+
14839
14897
  on(event, handler) {
14840
14898
  this.#events.on(event, handler);
14841
14899
  }
@@ -14884,17 +14942,11 @@ class XrplApi {
14884
14942
  }
14885
14943
 
14886
14944
  async getAccountObjects(address, options) {
14887
- const resp = (await this.#client.request({ command: 'account_objects', account: address, ...options }));
14888
- if (resp?.result?.account_objects)
14889
- return resp.result.account_objects;
14890
- return [];
14945
+ return this.#requestWithPaging({ command: 'account_objects', account: address, ...options }, API_REQ_TYPE.ACCOUNT_OBJECTS);
14891
14946
  }
14892
14947
 
14893
14948
  async getNamespaceEntries(address, namespaceId, options) {
14894
- const resp = (await this.#client.request({ command: 'account_namespace', account: address, namespace_id: namespaceId, ...options }));
14895
- if (resp?.result?.namespace_entries)
14896
- return resp.result.namespace_entries;
14897
- return [];
14949
+ return this.#requestWithPaging({ command: 'account_namespace', account: address, namespace_id: namespaceId, ...options }, API_REQ_TYPE.NAMESPACE_ENTRIES);
14898
14950
  }
14899
14951
 
14900
14952
  async getNftOffers(address, options) {
@@ -14904,24 +14956,19 @@ class XrplApi {
14904
14956
  }
14905
14957
 
14906
14958
  async getTrustlines(address, options) {
14907
- const resp = (await this.#client.request({ command: 'account_lines', account: address, ledger_index: "validated", ...options }));
14908
- if (resp?.result?.lines)
14909
- return resp.result.lines;
14910
- return [];
14959
+ return this.#requestWithPaging({ command: 'account_lines', account: address, ledger_index: "validated", ...options }, API_REQ_TYPE.LINES);
14960
+ }
14961
+
14962
+ async getAccountTrx(address, options) {
14963
+ return this.#requestWithPaging({ command: 'account_tx', account: address, ...options }, API_REQ_TYPE.TRANSACTIONS);
14911
14964
  }
14912
14965
 
14913
14966
  async getNfts(address, options) {
14914
- const resp = (await this.#client.request({ command: 'account_nfts', account: address, ledger_index: "validated", ...options }));
14915
- if (resp?.result?.account_nfts)
14916
- return resp.result.account_nfts;
14917
- return [];
14967
+ return this.#requestWithPaging({ command: 'account_nfts', account: address, ledger_index: "validated", ...options }, API_REQ_TYPE.ACCOUNT_NFTS);
14918
14968
  }
14919
14969
 
14920
14970
  async getOffers(address, options) {
14921
- const resp = (await this.#client.request({ command: 'account_offers', account: address, ledger_index: "validated", ...options }));
14922
- if (resp?.result?.offers)
14923
- return resp.result.offers;
14924
- return [];
14971
+ return this.#requestWithPaging({ command: 'account_offers', account: address, ledger_index: "validated", ...options }, API_REQ_TYPE.OFFERS);
14925
14972
  }
14926
14973
 
14927
14974
  async getLedgerEntry(index, options) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "evernode-js-client",
3
- "version": "0.4.50",
3
+ "version": "0.4.53",
4
4
  "dependencies": {
5
5
  "elliptic": "6.5.4",
6
6
  "ripple-address-codec": "4.2.0",