@bithomp/xrpl-api 3.3.2 → 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.
- package/lib/ledger/currency.js +81 -4
- package/lib/models/index.d.ts +1 -0
- package/lib/models/index.js +1 -0
- package/package.json +4 -4
package/lib/ledger/currency.js
CHANGED
|
@@ -39,7 +39,9 @@ const utils_1 = require("../parse/utils");
|
|
|
39
39
|
const transaction_1 = require("../parse/transaction");
|
|
40
40
|
const client_1 = require("../client");
|
|
41
41
|
const common_1 = require("../common");
|
|
42
|
-
const
|
|
42
|
+
const amm_info_1 = require("../models/amm_info");
|
|
43
|
+
const maxLength = 20;
|
|
44
|
+
const DECODED_HEX_CURRENCY_REGEX = /[^-\w\(\)\[\]\u0020-\u007E\u00A0-\uFFFF]*/g;
|
|
43
45
|
async function parseCurrencyInformation(currency) {
|
|
44
46
|
if (!currency || typeof currency !== "string") {
|
|
45
47
|
return null;
|
|
@@ -67,6 +69,12 @@ async function decodeCurrencyHex(currencyCode) {
|
|
|
67
69
|
if (prefix === "02" && isXlf15d(currencyCode)) {
|
|
68
70
|
return await decodeXlf15d(currencyCode);
|
|
69
71
|
}
|
|
72
|
+
else if (prefix === "03" && amm_info_1.AMM_LP_TOKEN_REGEX.test(currencyCode)) {
|
|
73
|
+
return decodeLPTokenHex(currencyCode);
|
|
74
|
+
}
|
|
75
|
+
else if (prefix === "01" && isDemurrageHex(currencyCode)) {
|
|
76
|
+
return decodeDemurrageHex(currencyCode);
|
|
77
|
+
}
|
|
70
78
|
else {
|
|
71
79
|
return decodeHex(currencyCode);
|
|
72
80
|
}
|
|
@@ -129,16 +137,85 @@ async function decodeXlf15d(currencyCode) {
|
|
|
129
137
|
};
|
|
130
138
|
}
|
|
131
139
|
function decodeHex(currencyHex) {
|
|
132
|
-
|
|
133
|
-
|
|
140
|
+
let hex = currencyHex;
|
|
141
|
+
const prefix = currencyHex.substring(0, 2);
|
|
142
|
+
if (prefix === "02") {
|
|
143
|
+
hex = currencyHex.substring(16, currencyHex.length);
|
|
144
|
+
}
|
|
145
|
+
const decoded = (0, utils_1.hexToString)(hex)
|
|
146
|
+
?.slice(0, maxLength)
|
|
147
|
+
?.replace(/^\u0000+/, "")
|
|
148
|
+
?.replace(/\u0000+$/, "")
|
|
149
|
+
?.trim();
|
|
150
|
+
const trimmed = decoded
|
|
151
|
+
.replace(DECODED_HEX_CURRENCY_REGEX, "")
|
|
152
|
+
.replace(/[�]*/g, "")
|
|
153
|
+
.replace(/\s+/g, " ")
|
|
154
|
+
.trim();
|
|
155
|
+
if (trimmed.length > 1 &&
|
|
156
|
+
trimmed.length >= decoded.length / 2 &&
|
|
157
|
+
trimmed.toUpperCase() !== (0, client_1.getNativeCurrency)()) {
|
|
134
158
|
return {
|
|
135
159
|
type: "hex",
|
|
136
160
|
currencyCode: currencyHex,
|
|
137
|
-
currency:
|
|
161
|
+
currency: trimmed,
|
|
138
162
|
};
|
|
139
163
|
}
|
|
140
164
|
return null;
|
|
141
165
|
}
|
|
166
|
+
function decodeLPTokenHex(currencyHex) {
|
|
167
|
+
return {
|
|
168
|
+
type: "lp_token",
|
|
169
|
+
currencyCode: currencyHex,
|
|
170
|
+
currency: "LP Token",
|
|
171
|
+
};
|
|
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
|
+
}
|
|
142
219
|
async function getLedger(ledgerIndex) {
|
|
143
220
|
let ledgerInfo = null;
|
|
144
221
|
try {
|
package/lib/models/index.d.ts
CHANGED
package/lib/models/index.js
CHANGED
|
@@ -21,6 +21,7 @@ exports.parseOfferFlags = exports.parseTrustlineFlags = exports.parseURITokenFla
|
|
|
21
21
|
__exportStar(require("./account_info"), exports);
|
|
22
22
|
__exportStar(require("./account_nfts"), exports);
|
|
23
23
|
__exportStar(require("./account_object"), exports);
|
|
24
|
+
__exportStar(require("./amm_info"), exports);
|
|
24
25
|
__exportStar(require("./ledger"), exports);
|
|
25
26
|
__exportStar(require("./manifest"), exports);
|
|
26
27
|
__exportStar(require("./transaction"), exports);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bithomp/xrpl-api",
|
|
3
|
-
"version": "3.3.
|
|
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",
|
|
@@ -52,15 +52,15 @@
|
|
|
52
52
|
"lib/**/*"
|
|
53
53
|
],
|
|
54
54
|
"dependencies": {
|
|
55
|
-
"axios": "^1.
|
|
55
|
+
"axios": "^1.10.0",
|
|
56
56
|
"base-x": "^5.0.1",
|
|
57
57
|
"bignumber.js": "^9.3.0",
|
|
58
58
|
"elliptic": "^6.6.1",
|
|
59
59
|
"lodash": "^4.17.21",
|
|
60
60
|
"ripple-address-codec": "5.0.0",
|
|
61
|
-
"ripple-binary-codec": "2.
|
|
61
|
+
"ripple-binary-codec": "2.4.1",
|
|
62
62
|
"ripple-keypairs": "2.0.0",
|
|
63
|
-
"xrpl": "4.
|
|
63
|
+
"xrpl": "4.3.0"
|
|
64
64
|
},
|
|
65
65
|
"devDependencies": {
|
|
66
66
|
"@eslint/eslintrc": "^3.3.1",
|