evernode-js-client 0.4.15 → 0.4.18

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 +62 -52
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -11,8 +11,6 @@ const { EvernodeEvents, MemoTypes, MemoFormats, EvernodeConstants, HookStateKeys
11
11
  const { DefaultValues } = __nccwpck_require__(262);
12
12
  const { EncryptionHelper } = __nccwpck_require__(832);
13
13
  const { EventEmitter } = __nccwpck_require__(170);
14
- const codec = __nccwpck_require__(597);
15
- const { Buffer } = __nccwpck_require__(300);
16
14
  const { UtilHelpers } = __nccwpck_require__(687);
17
15
  const { FirestoreHandler } = __nccwpck_require__(718);
18
16
 
@@ -133,10 +131,15 @@ class BaseEvernodeClient {
133
131
  hostRegFee: UtilHelpers.getStateData(states, HookStateKeys.HOST_REG_FEE),
134
132
  momentSize: UtilHelpers.getStateData(states, HookStateKeys.MOMENT_SIZE),
135
133
  hostHeartbeatFreq: UtilHelpers.getStateData(states, HookStateKeys.HOST_HEARTBEAT_FREQ),
136
- momentBaseIdx: UtilHelpers.getStateData(states, HookStateKeys.MOMENT_BASE_IDX)
134
+ momentBaseIdx: UtilHelpers.getStateData(states, HookStateKeys.MOMENT_BASE_IDX),
135
+ purchaserTargetPrice: UtilHelpers.getStateData(states, HookStateKeys.PURCHASER_TARGET_PRICE)
137
136
  };
138
137
  }
139
138
 
139
+ async refreshConfig() {
140
+ this.config = await this.#getEvernodeConfig();
141
+ }
142
+
140
143
  async #handleEvernodeEvent(tx, error) {
141
144
  if (error)
142
145
  console.error(error);
@@ -179,37 +182,16 @@ class BaseEvernodeClient {
179
182
  console.log('Failed to decrypt redeem data.');
180
183
  }
181
184
 
182
- if (tx.Memos.length >= 2 &&
183
- tx.Memos[1].type === MemoTypes.REDEEM_ORIGIN && tx.Memos[1].format === MemoFormats.HEX && tx.Memos[1].data) {
184
-
185
- // If the origin memo exists, get the token and user information from it.
186
- const buf = Buffer.from(tx.Memos[1].data, 'hex');
187
-
188
- return {
189
- name: EvernodeEvents.Redeem,
190
- data: {
191
- transaction: tx,
192
- redeemRefId: buf.slice(31, 63).toString('hex'),
193
- user: codec.encodeAccountID(buf.slice(0, 20)),
194
- host: tx.Destination,
195
- token: buf.slice(28, 31).toString(),
196
- moments: parseInt(buf.slice(20, 28).readBigInt64BE(0)),
197
- payload: payload
198
- }
199
- }
200
- }
201
- else {
202
- return {
203
- name: EvernodeEvents.Redeem,
204
- data: {
205
- transaction: tx,
206
- redeemRefId: tx.Hash,
207
- user: tx.Account,
208
- host: tx.Amount.issuer,
209
- token: tx.Amount.currency,
210
- moments: parseInt(tx.Amount.value),
211
- payload: payload
212
- }
185
+ return {
186
+ name: EvernodeEvents.Redeem,
187
+ data: {
188
+ transaction: tx,
189
+ redeemRefId: tx.hash,
190
+ user: tx.Account,
191
+ host: tx.Amount.issuer,
192
+ token: tx.Amount.currency,
193
+ moments: parseInt(tx.Amount.value),
194
+ payload: payload
213
195
  }
214
196
  }
215
197
  }
@@ -418,6 +400,22 @@ class HostClient extends BaseEvernodeClient {
418
400
  if (await this.isRegistered())
419
401
  throw "Host already registered.";
420
402
 
403
+ // Check whether is there any missed NFT sell offer that needs to be accepted
404
+ // from the client-side in order to complete the registration.
405
+ const regNft = await this.getRegistrationNft();
406
+ if (!regNft) {
407
+ const regInfo = await this.getHosts({ address: this.xrplAcc.address });
408
+ if (regInfo.length !== 0) {
409
+ const registryAcc = new XrplAccount(this.registryAddress, null, { xrplApi: this.xrplApi });
410
+ const sellOffer = (await registryAcc.getNftOffers()).find(o => o.TokenID == regInfo[0].nfTokenId);
411
+ if (sellOffer) {
412
+ await this.xrplAcc.buyNft(sellOffer.index);
413
+ console.log("Registration was successfully completed after acquiring the NFT.");
414
+ return await this.isRegistered();
415
+ }
416
+ }
417
+ }
418
+
421
419
  const memoData = `${hostingToken};${countryCode};${cpuMicroSec};${ramMb};${diskMb};${totalInstanceCount};${description}`
422
420
  const tx = await this.xrplAcc.makePayment(this.registryAddress,
423
421
  this.config.hostRegFee.toString(),
@@ -453,8 +451,10 @@ class HostClient extends BaseEvernodeClient {
453
451
  const nft = (await registryAcc.getNfts()).find(n => n.URI === `${EvernodeConstants.NFT_PREFIX_HEX}${tx.id}`);
454
452
  if (nft) {
455
453
  const sellOffer = (await registryAcc.getNftOffers()).find(o => o.TokenID === nft.TokenID && o.Flags === 1);
456
- await this.xrplAcc.buyNft(sellOffer.index);
457
- tx.isSellOfferAccepted = true;
454
+ if (sellOffer) {
455
+ await this.xrplAcc.buyNft(sellOffer.index);
456
+ tx.isSellOfferAccepted = true;
457
+ }
458
458
  }
459
459
  }
460
460
  }
@@ -473,7 +473,7 @@ class HostClient extends BaseEvernodeClient {
473
473
  options.transactionOptions);
474
474
 
475
475
  console.log('Waiting for the buy offer')
476
- const regAcc = new XrplAccount(this.registryAddress);
476
+ const regAcc = new XrplAccount(this.registryAddress, null, { xrplApi: this.xrplApi });
477
477
  let offer = null;
478
478
  let attempts = 0;
479
479
  while (attempts < OFFER_WAIT_TIMEOUT) {
@@ -492,6 +492,15 @@ class HostClient extends BaseEvernodeClient {
492
492
  return await this.isRegistered();
493
493
  }
494
494
 
495
+ async heartbeat(options = {}) {
496
+ return this.xrplAcc.makePayment(this.registryAddress,
497
+ XrplConstants.MIN_XRP_AMOUNT,
498
+ XrplConstants.XRP,
499
+ null,
500
+ [{ type: MemoTypes.HEARTBEAT, format: "", data: "" }],
501
+ options.transactionOptions);
502
+ }
503
+
495
504
  async redeemSuccess(txHash, userAddress, instanceInfo, options = {}) {
496
505
 
497
506
  // Encrypt the instance info with the user's encryption key (Specified in MessageKey field of the user account).
@@ -505,7 +514,7 @@ class HostClient extends BaseEvernodeClient {
505
514
  { type: MemoTypes.REDEEM_SUCCESS, format: MemoFormats.BASE64, data: encrypted },
506
515
  { type: MemoTypes.REDEEM_REF, format: MemoFormats.HEX, data: txHash }];
507
516
 
508
- return this.xrplAcc.makePayment(this.registryAddress,
517
+ return this.xrplAcc.makePayment(userAddress,
509
518
  XrplConstants.MIN_XRP_AMOUNT,
510
519
  XrplConstants.XRP,
511
520
  null,
@@ -513,29 +522,19 @@ class HostClient extends BaseEvernodeClient {
513
522
  options.transactionOptions);
514
523
  }
515
524
 
516
- async redeemError(txHash, reason, options = {}) {
525
+ async redeemError(txHash, userAddress, reason, options = {}) {
517
526
 
518
527
  const memos = [
519
528
  { type: MemoTypes.REDEEM_ERROR, format: MemoFormats.JSON, data: { type: ErrorCodes.REDEEM_ERR, reason: reason } },
520
529
  { type: MemoTypes.REDEEM_REF, format: MemoFormats.HEX, data: txHash }];
521
530
 
522
- return this.xrplAcc.makePayment(this.registryAddress,
531
+ return this.xrplAcc.makePayment(userAddress,
523
532
  XrplConstants.MIN_XRP_AMOUNT,
524
533
  XrplConstants.XRP,
525
534
  null,
526
535
  memos,
527
536
  options.transactionOptions);
528
537
  }
529
-
530
- async heartbeat(options = {}) {
531
- return this.xrplAcc.makePayment(this.registryAddress,
532
- XrplConstants.MIN_XRP_AMOUNT,
533
- XrplConstants.XRP,
534
- null,
535
- this.xrplAcc.address,
536
- [{ type: MemoTypes.HEARTBEAT, format: "", data: "" }],
537
- options.transactionOptions);
538
- }
539
538
  }
540
539
 
541
540
  module.exports = {
@@ -1089,7 +1088,6 @@ const EvernodeConstants = {
1089
1088
 
1090
1089
  const MemoTypes = {
1091
1090
  REDEEM: 'evnRedeem',
1092
- REDEEM_ORIGIN: 'evnRedeemOrigin',
1093
1091
  REDEEM_SUCCESS: 'evnRedeemSuccess',
1094
1092
  REDEEM_ERROR: 'evnRedeemError',
1095
1093
  REDEEM_REF: 'evnRedeemRef',
@@ -1123,6 +1121,7 @@ const HookStateKeys = {
1123
1121
  MINT_LIMIT: "4556520100000000000000000000000000000000000000000000000000000004",
1124
1122
  FIXED_REG_FEE: "4556520100000000000000000000000000000000000000000000000000000005",
1125
1123
  HOST_HEARTBEAT_FREQ: "4556520100000000000000000000000000000000000000000000000000000006",
1124
+ PURCHASER_TARGET_PRICE: "4556520100000000000000000000000000000000000000000000000000000007",
1126
1125
  // Singleton
1127
1126
  HOST_COUNT: "4556523200000000000000000000000000000000000000000000000000000000",
1128
1127
  MOMENT_BASE_IDX: "4556523300000000000000000000000000000000000000000000000000000000",
@@ -1482,6 +1481,7 @@ module.exports = {
1482
1481
  const codec = __nccwpck_require__(597);
1483
1482
  const { Buffer } = __nccwpck_require__(300);
1484
1483
  const { HookStateKeys } = __nccwpck_require__(849);
1484
+ const { XflHelpers } = __nccwpck_require__(243);
1485
1485
 
1486
1486
  const NFTOKEN_PREFIX = '00080000';
1487
1487
 
@@ -1520,7 +1520,7 @@ class StateHelpers {
1520
1520
  registrationFee: Number(stateDataBuf.readBigUInt64BE(HOST_REG_FEE_OFFSET)),
1521
1521
  noOfTotalInstances: stateDataBuf.readUInt32BE(HOST_TOT_INS_COUNT_OFFSET),
1522
1522
  noOfActiveInstances: stateDataBuf.readUInt32BE(HOST_ACT_INS_COUNT_OFFSET),
1523
- lastHeartbeatLedger: stateDataBuf.readUInt32BE(HOST_HEARTBEAT_LEDGER_IDX_OFFSET)
1523
+ lastHeartbeatLedger: Number(stateDataBuf.readBigUInt64BE(HOST_HEARTBEAT_LEDGER_IDX_OFFSET))
1524
1524
  }
1525
1525
  }
1526
1526
 
@@ -1583,6 +1583,15 @@ class StateHelpers {
1583
1583
  value: Number(stateData.readBigUInt64BE())
1584
1584
  }
1585
1585
  }
1586
+ else if (Buffer.from(HookStateKeys.PURCHASER_TARGET_PRICE, 'hex').compare(stateKey) === 0) {
1587
+ const xfl = stateData.readBigInt64BE(0);
1588
+ const val = XflHelpers.toString(xfl);
1589
+ return {
1590
+ type: this.StateTypes.CONFIGURATION,
1591
+ key: hexKey,
1592
+ value: val
1593
+ }
1594
+ }
1586
1595
  else
1587
1596
  throw { type: 'Validation Error', message: 'Invalid state key.' };
1588
1597
  }
@@ -1613,6 +1622,7 @@ class StateHelpers {
1613
1622
  else if (Buffer.from(HookStateKeys.EVR_ISSUER_ADDR, 'hex').compare(stateKey) === 0 ||
1614
1623
  Buffer.from(HookStateKeys.FOUNDATION_ADDR, 'hex').compare(stateKey) === 0 ||
1615
1624
  Buffer.from(HookStateKeys.MOMENT_SIZE, 'hex').compare(stateKey) === 0 ||
1625
+ Buffer.from(HookStateKeys.PURCHASER_TARGET_PRICE, 'hex').compare(stateKey) === 0 ||
1616
1626
  Buffer.from(HookStateKeys.HOST_HEARTBEAT_FREQ, 'hex').compare(stateKey) ||
1617
1627
  Buffer.from(HookStateKeys.MINT_LIMIT, 'hex').compare(stateKey) === 0 ||
1618
1628
  Buffer.from(HookStateKeys.FIXED_REG_FEE, 'hex').compare(stateKey) === 0) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "evernode-js-client",
3
- "version": "0.4.15",
3
+ "version": "0.4.18",
4
4
  "dependencies": {
5
5
  "elliptic": "6.5.4",
6
6
  "ripple-address-codec": "4.2.0",