@bithomp/xrpl-api 3.3.8 → 3.3.9

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.
@@ -72,6 +72,9 @@ async function decodeCurrencyHex(currencyCode) {
72
72
  else if (prefix === "03" && amm_info_1.AMM_LP_TOKEN_REGEX.test(currencyCode)) {
73
73
  return decodeLPTokenHex(currencyCode);
74
74
  }
75
+ else if (prefix === "01" && isDemurrageHex(currencyCode)) {
76
+ return decodeDemurrageHex(currencyCode);
77
+ }
75
78
  else {
76
79
  return decodeHex(currencyCode);
77
80
  }
@@ -167,6 +170,52 @@ function decodeLPTokenHex(currencyHex) {
167
170
  currency: "LP Token",
168
171
  };
169
172
  }
173
+ function decodeDemurrageHex(currencyHex) {
174
+ const hex = currencyHex.substring(2, 8);
175
+ const currencyText = (0, utils_1.hexToString)(hex)?.trim()?.replace(/\0/g, "");
176
+ let valuePrefix = "";
177
+ const year = 31536000;
178
+ const decimals = 2;
179
+ const interestPeriod = hex2double(currencyHex.substring(16, 32));
180
+ const interest = Math.exp(year / interestPeriod);
181
+ const percentage = (interest * 100) - 100;
182
+ const decimalMultiplier = decimals ? Math.pow(10, decimals) : 100;
183
+ const rate = Math.round(percentage * decimalMultiplier) / decimalMultiplier;
184
+ valuePrefix = `(${rate > 0 ? "+" : ""}${rate}%pa)`;
185
+ const currency = `${currencyText} ${valuePrefix}`;
186
+ return {
187
+ type: "demurrage",
188
+ currencyCode: currencyHex,
189
+ currency,
190
+ };
191
+ }
192
+ function isDemurrageHex(currencyHex) {
193
+ const hex = currencyHex.substring(2, 8);
194
+ const currencyText = (0, utils_1.hexToString)(hex)?.trim()?.replace(/\0/g, "");
195
+ if (!currencyText || currencyText.length < 3) {
196
+ return false;
197
+ }
198
+ if (!currencyText.match(/^[a-zA-Z0-9\s]+$/)) {
199
+ return false;
200
+ }
201
+ if (currencyText.toUpperCase() === (0, client_1.getNativeCurrency)()) {
202
+ return false;
203
+ }
204
+ const interestPeriod = hex2double(currencyHex.substring(16, 32));
205
+ if (isNaN(interestPeriod)) {
206
+ return false;
207
+ }
208
+ const interestStart = Buffer.from(currencyHex.substring(8, 16), "hex").readUInt32BE(0);
209
+ if (isNaN(interestStart) || interestStart < 0) {
210
+ return false;
211
+ }
212
+ return true;
213
+ }
214
+ function hex2double(hex) {
215
+ const buffer = Buffer.from(hex, "hex");
216
+ const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
217
+ return view.getFloat64(0, false);
218
+ }
170
219
  async function getLedger(ledgerIndex) {
171
220
  let ledgerInfo = null;
172
221
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bithomp/xrpl-api",
3
- "version": "3.3.8",
3
+ "version": "3.3.9",
4
4
  "description": "A Bithomp JavaScript/TypeScript library for interacting with the XRP Ledger",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -43,7 +43,7 @@
43
43
  "format": "prettier --write \"src/**/*.ts\" \"src/**/*.js\"",
44
44
  "lint": "eslint",
45
45
  "prepare": "npm run build",
46
- "prepublishOnly": "npm test && npm run lint",
46
+ "-prepublishOnly": "npm test && npm run lint",
47
47
  "preversion": "npm run lint",
48
48
  "version": "npm run format && git add -A src",
49
49
  "postversion": "git push && git push --tags"