evernode-js-client 0.5.5 → 0.5.7

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 +79 -6
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -12156,6 +12156,19 @@ class BaseEvernodeClient {
12156
12156
  }
12157
12157
  }
12158
12158
  }
12159
+ else if (tx.Memos.length >= 1 &&
12160
+ tx.Memos[0].type === MemoTypes.HOST_TRANSFER && tx.Memos[0].format === MemoFormats.HEX && tx.Memos[0].data) {
12161
+
12162
+ const addrsBuf = Buffer.from(tx.Memos[0].data, 'hex');
12163
+
12164
+ return {
12165
+ name: EvernodeEvents.HostTransfer,
12166
+ data: {
12167
+ transaction: tx,
12168
+ transferee: codec.encodeAccountID(addrsBuf)
12169
+ }
12170
+ }
12171
+ }
12159
12172
 
12160
12173
  return null;
12161
12174
  }
@@ -12302,6 +12315,7 @@ const { Buffer } = __nccwpck_require__(4300);
12302
12315
  const codec = __nccwpck_require__(597);
12303
12316
  const { XflHelpers } = __nccwpck_require__(3243);
12304
12317
  const { EvernodeHelpers } = __nccwpck_require__(2523);
12318
+ const { StateHelpers } = __nccwpck_require__(3860);
12305
12319
 
12306
12320
  const OFFER_WAIT_TIMEOUT = 60;
12307
12321
 
@@ -12447,9 +12461,29 @@ class HostClient extends BaseEvernodeClient {
12447
12461
  }
12448
12462
  }
12449
12463
 
12464
+ // Check the availability of an initiated transfer.
12465
+ // Need to modify the amount accordingly.
12466
+ const stateTransfereeAddrKey = StateHelpers.generateTransfereeAddrStateKey(this.xrplAcc.address);
12467
+ const stateTransfereeAddrIndex = StateHelpers.getHookStateIndex(this.registryAddress, stateTransfereeAddrKey);
12468
+ let transfereeAddrLedgerEntry = {};
12469
+ let transfereeAddrStateData = {};
12470
+ let transferredNFTokenId = null;
12471
+
12472
+ try {
12473
+ const res = await this.xrplApi.getLedgerEntry(stateTransfereeAddrIndex);
12474
+ transfereeAddrLedgerEntry = { ...transfereeAddrLedgerEntry, ...res };
12475
+ transfereeAddrStateData = transfereeAddrLedgerEntry?.HookStateData;
12476
+ const transfereeAddrStateDecoded = StateHelpers.decodeTransfereeAddrState(Buffer.from(stateTransfereeAddrKey, 'hex'), Buffer.from(transfereeAddrStateData, 'hex'));
12477
+ transferredNFTokenId = transfereeAddrStateDecoded?.transferredNfTokenId;
12478
+
12479
+ }
12480
+ catch (e) {
12481
+ console.log("No initiated transfers were found.");
12482
+ }
12483
+
12450
12484
  const memoData = `${countryCode};${cpuMicroSec};${ramMb};${diskMb};${totalInstanceCount};${cpuModel};${cpuCount};${cpuSpeed};${description}`
12451
12485
  const tx = await this.xrplAcc.makePayment(this.registryAddress,
12452
- this.config.hostRegFee.toString(),
12486
+ (transferredNFTokenId) ? EvernodeConstants.NOW_IN_EVRS : this.config.hostRegFee.toString(),
12453
12487
  EvernodeConstants.EVR,
12454
12488
  this.config.evrIssuerAddress,
12455
12489
  [{ type: MemoTypes.HOST_REG, format: MemoFormats.TEXT, data: memoData }],
@@ -12461,7 +12495,7 @@ class HostClient extends BaseEvernodeClient {
12461
12495
  let attempts = 0;
12462
12496
  let offerLedgerIndex = 0;
12463
12497
  while (attempts < OFFER_WAIT_TIMEOUT) {
12464
- const nft = (await regAcc.getNfts()).find(n => n.URI === `${EvernodeConstants.NFT_PREFIX_HEX}${tx.id}`);
12498
+ const nft = (await regAcc.getNfts()).find(n => (n.URI === `${EvernodeConstants.NFT_PREFIX_HEX}${tx.id}`) || (n.NFTokenID === transferredNFTokenId));
12465
12499
  if (nft) {
12466
12500
  offer = (await regAcc.getNftOffers()).find(o => o.Destination === this.xrplAcc.address && o.NFTokenID === nft.NFTokenID && o.Flags === 1);
12467
12501
  offerLedgerIndex = this.xrplApi.ledgerIndex;
@@ -13461,7 +13495,8 @@ const EvernodeConstants = {
13461
13495
  EVR: 'EVR',
13462
13496
  NFT_PREFIX_HEX: '657672686F7374', // evrhost
13463
13497
  LEASE_NFT_PREFIX_HEX: '6576726C65617365', // evrlease
13464
- HOOK_NAMESPACE: '01EAF09326B4911554384121FF56FA8FECC215FDDE2EC35D9E59F2C53EC665A0'
13498
+ HOOK_NAMESPACE: '01EAF09326B4911554384121FF56FA8FECC215FDDE2EC35D9E59F2C53EC665A0',
13499
+ NOW_IN_EVRS: "0.00000001"
13465
13500
  }
13466
13501
 
13467
13502
  const MemoTypes = {
@@ -13998,9 +14033,14 @@ const HOST_CPU_MICROSEC_OFFSET = 64;
13998
14033
  const HOST_RAM_MB_OFFSET = 68;
13999
14034
  const HOST_DISK_MB_OFFSET = 72;
14000
14035
 
14036
+ const PREV_HOST_ADDRESS_OFFSET = 0;
14037
+ const TRANSFER_LEDGER_IDX_OFFSET = 20;
14038
+ const TRANSFERRED_NFT_ID_OFFSET = 28;
14039
+
14001
14040
  const STATE_KEY_TYPES = {
14002
14041
  TOKEN_ID: 2,
14003
- HOST_ADDR: 3
14042
+ HOST_ADDR: 3,
14043
+ TRANSFEREE_ADDR: 4
14004
14044
  }
14005
14045
 
14006
14046
  const MOMENT_TYPES = {
@@ -14008,8 +14048,14 @@ const MOMENT_TYPES = {
14008
14048
  TIMESTAMP: 1
14009
14049
  }
14010
14050
 
14051
+ const TRANSFER_STATES = {
14052
+ NO_TRANSFER: 0,
14053
+ HAS_A_TRANSFER: 1
14054
+ }
14055
+
14011
14056
  const EVERNODE_PREFIX = 'EVR';
14012
14057
  const HOST_ADDR_KEY_ZERO_COUNT = 8;
14058
+ const TRANSFEREE_ADDR_KEY_ZERO_COUNT = 8;
14013
14059
  const HOOK_STATE_LEDGER_TYPE_PREFIX = 118; // Decimal value of ASCII 'v'
14014
14060
  const PENDING_TRANSFER = 1;
14015
14061
 
@@ -14046,7 +14092,7 @@ class StateHelpers {
14046
14092
  activeInstances: stateDataBuf.readUInt32BE(HOST_ACT_INS_COUNT_OFFSET),
14047
14093
  lastHeartbeatIndex: Number(stateDataBuf.readBigUInt64BE(HOST_HEARTBEAT_LEDGER_IDX_OFFSET)),
14048
14094
  version: `${stateDataBuf.readUInt8(HOST_VERSION_OFFSET)}.${stateDataBuf.readUInt8(HOST_VERSION_OFFSET + 1)}.${stateDataBuf.readUInt8(HOST_VERSION_OFFSET + 2)}`,
14049
- hasPendingTransfer: (stateDataBuf.readUInt8(HOST_TRANSFER_FLAG_OFFSET) === PENDING_TRANSFER) ? true : false
14095
+ hasPendingTransfer: (stateDataBuf.length > HOST_TRANSFER_FLAG_OFFSET && (stateDataBuf.readUInt8(HOST_TRANSFER_FLAG_OFFSET) === PENDING_TRANSFER)) ? TRANSFER_STATES.HAS_A_TRANSFER : TRANSFER_STATES.NO_TRANSFER
14050
14096
  }
14051
14097
  if (stateDataBuf.length > HOST_REG_TIMESTAMP_OFFSET)
14052
14098
  data.registrationTimestamp = Number(stateDataBuf.readBigUInt64BE(HOST_REG_TIMESTAMP_OFFSET));
@@ -14065,6 +14111,18 @@ class StateHelpers {
14065
14111
  }
14066
14112
  }
14067
14113
 
14114
+
14115
+ static decodeTransfereeAddrState(stateKeyBuf, stateDataBuf) {
14116
+ const prevHostClassicAddress = codec.encodeAccountID(stateDataBuf.slice(PREV_HOST_ADDRESS_OFFSET, TRANSFER_LEDGER_IDX_OFFSET));
14117
+ return {
14118
+ futureOwnerAddress: codec.encodeAccountID(stateKeyBuf.slice(12)),
14119
+ prevHostAddressKey: this.generateHostAddrStateKey(prevHostClassicAddress),
14120
+ prevHostAddress: prevHostClassicAddress,
14121
+ transferLedgerIdx: Number(stateDataBuf.readBigUInt64BE(TRANSFER_LEDGER_IDX_OFFSET)),
14122
+ transferredNfTokenId: stateDataBuf.slice(TRANSFERRED_NFT_ID_OFFSET, 60).toString('hex').toUpperCase()
14123
+ }
14124
+ }
14125
+
14068
14126
  static decodeStateData(stateKey, stateData) {
14069
14127
  const hexKey = stateKey.toString('hex').toUpperCase();
14070
14128
  if (Buffer.from(HookStateKeys.PREFIX_HOST_ADDR, 'hex').compare(stateKey, 0, 4) === 0) {
@@ -14089,7 +14147,8 @@ class StateHelpers {
14089
14147
  else if (Buffer.from(HookStateKeys.PREFIX_TRANSFEREE_ADDR, 'hex').compare(stateKey, 0, 4) === 0) {
14090
14148
  return {
14091
14149
  type: this.StateTypes.TRANSFEREE_ADDR,
14092
- key: hexKey
14150
+ key: hexKey,
14151
+ ...this.decodeTransfereeAddrState(stateKey, stateData)
14093
14152
  }
14094
14153
  }
14095
14154
  else if (Buffer.from(HookStateKeys.HOST_COUNT, 'hex').compare(stateKey) === 0) {
@@ -14265,6 +14324,20 @@ class StateHelpers {
14265
14324
  return stateKeyBuf.toString('hex').toUpperCase();
14266
14325
  }
14267
14326
 
14327
+ static generateTransfereeAddrStateKey(address) {
14328
+ // 1 byte - Key Type.
14329
+ // 8 bytes - Zeros.
14330
+ let buf = Buffer.allocUnsafe(9);
14331
+ buf.writeUInt8(STATE_KEY_TYPES.TRANSFEREE_ADDR);
14332
+ for (let i = 0; i < TRANSFEREE_ADDR_KEY_ZERO_COUNT; i++) {
14333
+ buf.writeUInt8(0, i + 1);
14334
+ }
14335
+
14336
+ const addrBuf = Buffer.from(codec.decodeAccountID(address), "hex");
14337
+ const stateKeyBuf = Buffer.concat([Buffer.from(EVERNODE_PREFIX, "utf-8"), buf, addrBuf]);
14338
+ return stateKeyBuf.toString('hex').toUpperCase();
14339
+ }
14340
+
14268
14341
  static getHookStateIndex(hookAccount, stateKey, hookNamespace = EvernodeConstants.HOOK_NAMESPACE) {
14269
14342
  const typeBuf = Buffer.allocUnsafe(2);
14270
14343
  typeBuf.writeInt16BE(HOOK_STATE_LEDGER_TYPE_PREFIX);
package/package.json CHANGED
@@ -6,7 +6,7 @@
6
6
  ],
7
7
  "homepage": "https://github.com/HotPocketDev/evernode-js-client",
8
8
  "license": "MIT",
9
- "version": "0.5.5",
9
+ "version": "0.5.7",
10
10
  "dependencies": {
11
11
  "elliptic": "6.5.4",
12
12
  "ripple-address-codec": "4.2.0",