@gmx-io/sdk 2.1.2 → 2.2.0
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/build/cjs/src/configs/chains.js +9 -1
- package/build/cjs/src/utils/__tests__/numbers.spec.js +79 -0
- package/build/cjs/src/utils/errors/transactionsErrors.js +1 -0
- package/build/cjs/src/utils/numbers/index.js +1 -0
- package/build/cjs/src/utils/numbers/parts.js +48 -0
- package/build/cjs/src/utils/numbers/utils.js +67 -26
- package/build/cjs/src/utils/orderTransactions/utils.js +55 -1
- package/build/esm/src/configs/chains.js +7 -0
- package/build/esm/src/utils/__tests__/numbers.spec.js +80 -1
- package/build/esm/src/utils/errors/transactionsErrors.js +1 -0
- package/build/esm/src/utils/numbers/index.js +1 -0
- package/build/esm/src/utils/numbers/parts.js +41 -0
- package/build/esm/src/utils/numbers/utils.js +57 -24
- package/build/esm/src/utils/orderTransactions/utils.js +53 -1
- package/build/tsconfig.tsbuildinfo +1 -1
- package/build/types/src/configs/chains.d.ts +1 -0
- package/build/types/src/utils/errors/transactionsErrors.d.ts +1 -0
- package/build/types/src/utils/numbers/index.d.ts +1 -0
- package/build/types/src/utils/numbers/parts.d.ts +12 -0
- package/build/types/src/utils/numbers/utils.d.ts +25 -9
- package/build/types/src/utils/orderTransactions/utils.d.ts +7 -0
- package/package.json +1 -1
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getExecutionFeeConfig = exports.getTokenExplorerUrl = exports.getExplorerUrl = exports.getChainDefaultCollateralSymbol = exports.getChainWrappedTokenSymbol = exports.getChainNativeTokenSymbol = exports.isChainDisabled = exports.getGasPriceBuffer = exports.getResidualGasUsdConfig = exports.getMinExecutionFeeUsd = exports.getMaxPriorityFeePerGas = exports.getGasPricePremium = exports.getMaxFeePerGas = exports.isTestnetChain = exports.isContractsChain = exports.getExcessiveExecutionFee = exports.getHighExecutionFee = exports.getViemChain = exports.getChainIdBySlug = exports.getChainSlug = exports.getChainName = exports.VIEM_CHAIN_BY_CHAIN_ID = exports.megaeth = exports.GMX_ACCOUNT_PSEUDO_CHAIN_ID = exports.SOURCE_CHAIN_IDS = exports.SETTLEMENT_CHAIN_IDS_DEV = exports.SETTLEMENT_CHAIN_IDS = exports.CONTRACTS_CHAIN_IDS_DEV = exports.CONTRACTS_CHAIN_IDS = exports.SOURCE_SEPOLIA = exports.SOURCE_OPTIMISM_SEPOLIA = exports.SOURCE_BSC_MAINNET = exports.SOURCE_BASE_MAINNET = exports.SOURCE_ETHEREUM_MAINNET = exports.ARBITRUM_SEPOLIA = exports.MEGAETH = exports.AVALANCHE_FUJI = exports.AVALANCHE = exports.ARBITRUM = void 0;
|
|
3
|
+
exports.getExecutionFeeConfig = exports.getTokenExplorerUrl = exports.getExplorerUrl = exports.getChainDefaultCollateralSymbol = exports.getChainWrappedTokenSymbol = exports.getChainNativeTokenSymbol = exports.isChainDisabled = exports.getGasPriceBuffer = exports.getResidualGasUsdConfig = exports.getMinExecutionFeeUsd = exports.getMaxPriorityFeePerGas = exports.getExecutionFeePriorityFeeAllowance = exports.getGasPricePremium = exports.getMaxFeePerGas = exports.isTestnetChain = exports.isContractsChain = exports.getExcessiveExecutionFee = exports.getHighExecutionFee = exports.getViemChain = exports.getChainIdBySlug = exports.getChainSlug = exports.getChainName = exports.VIEM_CHAIN_BY_CHAIN_ID = exports.megaeth = exports.GMX_ACCOUNT_PSEUDO_CHAIN_ID = exports.SOURCE_CHAIN_IDS = exports.SETTLEMENT_CHAIN_IDS_DEV = exports.SETTLEMENT_CHAIN_IDS = exports.CONTRACTS_CHAIN_IDS_DEV = exports.CONTRACTS_CHAIN_IDS = exports.SOURCE_SEPOLIA = exports.SOURCE_OPTIMISM_SEPOLIA = exports.SOURCE_BSC_MAINNET = exports.SOURCE_BASE_MAINNET = exports.SOURCE_ETHEREUM_MAINNET = exports.ARBITRUM_SEPOLIA = exports.MEGAETH = exports.AVALANCHE_FUJI = exports.AVALANCHE = exports.ARBITRUM = void 0;
|
|
4
4
|
const viem_1 = require("viem");
|
|
5
5
|
const chains_1 = require("viem/chains");
|
|
6
6
|
const chainIds_1 = require("./chainIds");
|
|
@@ -286,6 +286,14 @@ function getGasPricePremium(chainId) {
|
|
|
286
286
|
return CONTRACTS_CHAIN_CONFIGS[chainId]?.gasPricePremium;
|
|
287
287
|
}
|
|
288
288
|
exports.getGasPricePremium = getGasPricePremium;
|
|
289
|
+
// Priority fee wallets may add, charged on Arbitrum One since 2026-09-23; express is relayed with a zero tip
|
|
290
|
+
const EXECUTION_FEE_PRIORITY_FEE_ALLOWANCE = {
|
|
291
|
+
[chainIds_1.ARBITRUM]: 30000000n, // 0.03 gwei
|
|
292
|
+
};
|
|
293
|
+
function getExecutionFeePriorityFeeAllowance(chainId) {
|
|
294
|
+
return EXECUTION_FEE_PRIORITY_FEE_ALLOWANCE[chainId] ?? 0n;
|
|
295
|
+
}
|
|
296
|
+
exports.getExecutionFeePriorityFeeAllowance = getExecutionFeePriorityFeeAllowance;
|
|
289
297
|
function getMaxPriorityFeePerGas(chainId) {
|
|
290
298
|
return CONTRACTS_CHAIN_CONFIGS[chainId]?.maxPriorityFeePerGas;
|
|
291
299
|
}
|
|
@@ -546,3 +546,82 @@ const ONE_USD = 1000000000000000000000000000000n;
|
|
|
546
546
|
(0, vitest_1.expect)((0, numbers_1.formatPriceImpactBps)(-81n, -10000n)).toBeUndefined();
|
|
547
547
|
});
|
|
548
548
|
});
|
|
549
|
+
(0, vitest_1.describe)("number parts", () => {
|
|
550
|
+
const usd = (amount) => (0, numbers_1.expandDecimals)(amount, factors_1.USD_DECIMALS);
|
|
551
|
+
(0, vitest_1.it)("string formatters are the joined parts", () => {
|
|
552
|
+
const cases = [
|
|
553
|
+
[(0, numbers_1.formatUsd)(usd(1234)), (0, numbers_1.formatUsdParts)(usd(1234))],
|
|
554
|
+
[
|
|
555
|
+
(0, numbers_1.formatUsd)(-usd(1), { displayPlus: true, displayDecimals: 4 }),
|
|
556
|
+
(0, numbers_1.formatUsdParts)(-usd(1), { displayPlus: true, displayDecimals: 4 }),
|
|
557
|
+
],
|
|
558
|
+
[(0, numbers_1.formatUsd)(usd(2000000000)), (0, numbers_1.formatUsdParts)(usd(2000000000))],
|
|
559
|
+
[(0, numbers_1.formatUsd)(1n), (0, numbers_1.formatUsdParts)(1n)],
|
|
560
|
+
[(0, numbers_1.formatUsd)(undefined, { fallbackToZero: true }), (0, numbers_1.formatUsdParts)(undefined, { fallbackToZero: true })],
|
|
561
|
+
[(0, numbers_1.formatDeltaUsd)(usd(5), (0, numbers_1.expandDecimals)(3, 2)), (0, numbers_1.formatDeltaUsdParts)(usd(5), (0, numbers_1.expandDecimals)(3, 2))],
|
|
562
|
+
[
|
|
563
|
+
(0, numbers_1.formatDeltaUsd)(0n, undefined, { showPlusForZero: true }),
|
|
564
|
+
(0, numbers_1.formatDeltaUsdParts)(0n, undefined, { showPlusForZero: true }),
|
|
565
|
+
],
|
|
566
|
+
[(0, numbers_1.formatBigUsd)(usd(123456789)), (0, numbers_1.formatBigUsdParts)(usd(123456789))],
|
|
567
|
+
[(0, numbers_1.formatUsdPrice)(usd(3)), (0, numbers_1.formatUsdPriceParts)(usd(3))],
|
|
568
|
+
[(0, numbers_1.formatUsdPrice)(-1n), (0, numbers_1.formatUsdPriceParts)(-1n)],
|
|
569
|
+
[
|
|
570
|
+
(0, numbers_1.formatAmountHuman)(usd(1150000000), factors_1.USD_DECIMALS, true),
|
|
571
|
+
(0, numbers_1.formatAmountHumanParts)(usd(1150000000), factors_1.USD_DECIMALS, true),
|
|
572
|
+
],
|
|
573
|
+
[
|
|
574
|
+
(0, numbers_1.formatAmountHuman)(-usd(12500), factors_1.USD_DECIMALS, false, 2),
|
|
575
|
+
(0, numbers_1.formatAmountHumanParts)(-usd(12500), factors_1.USD_DECIMALS, false, 2),
|
|
576
|
+
],
|
|
577
|
+
[(0, numbers_1.formatAmountHuman)(undefined, factors_1.USD_DECIMALS), (0, numbers_1.formatAmountHumanParts)(undefined, factors_1.USD_DECIMALS)],
|
|
578
|
+
[(0, numbers_1.numberWithCommas)("1234.5", { showDollar: true }), (0, numbers_1.numberWithCommasParts)("1234.5", { showDollar: true })],
|
|
579
|
+
[
|
|
580
|
+
(0, numbers_1.formatTokenAmountWithUsd)((0, numbers_1.expandDecimals)(1, 18), usd(2500), "ETH", 18),
|
|
581
|
+
(0, numbers_1.formatTokenAmountWithUsdParts)((0, numbers_1.expandDecimals)(1, 18), usd(2500), "ETH", 18),
|
|
582
|
+
],
|
|
583
|
+
];
|
|
584
|
+
for (const [text, parts] of cases) {
|
|
585
|
+
(0, vitest_1.expect)(parts && (0, numbers_1.joinNumberParts)(parts)).toBe(text);
|
|
586
|
+
}
|
|
587
|
+
});
|
|
588
|
+
(0, vitest_1.it)("marks the currency affix and keeps magnitude suffixes as text", () => {
|
|
589
|
+
(0, vitest_1.expect)((0, numbers_1.formatUsdParts)(-usd(1234))).toEqual([
|
|
590
|
+
{ kind: "text", text: "-" },
|
|
591
|
+
{ kind: "currency", text: "$ " },
|
|
592
|
+
{ kind: "text", text: "1,234.00" },
|
|
593
|
+
]);
|
|
594
|
+
(0, vitest_1.expect)((0, numbers_1.formatUsdParts)(usd(2000000000))).toEqual([
|
|
595
|
+
{ kind: "text", text: "> " },
|
|
596
|
+
{ kind: "currency", text: "$ " },
|
|
597
|
+
{ kind: "text", text: "1,000,000,000.00" },
|
|
598
|
+
]);
|
|
599
|
+
(0, vitest_1.expect)((0, numbers_1.formatDeltaUsdParts)(usd(5), (0, numbers_1.expandDecimals)(3, 2))).toEqual([
|
|
600
|
+
{ kind: "text", text: "+" },
|
|
601
|
+
{ kind: "currency", text: "$ " },
|
|
602
|
+
{ kind: "text", text: "5.00 (+3.00%)" },
|
|
603
|
+
]);
|
|
604
|
+
(0, vitest_1.expect)((0, numbers_1.formatAmountHumanParts)(usd(1150000000), factors_1.USD_DECIMALS, true)).toEqual([
|
|
605
|
+
{ kind: "currency", text: "$ " },
|
|
606
|
+
{ kind: "text", text: "1.1b" },
|
|
607
|
+
]);
|
|
608
|
+
(0, vitest_1.expect)((0, numbers_1.formatAmountHumanParts)((0, numbers_1.expandDecimals)(12500, 18), 18)).toEqual([{ kind: "text", text: "12.5k" }]);
|
|
609
|
+
(0, vitest_1.expect)((0, numbers_1.formatAmountHumanParts)(usd(999), factors_1.USD_DECIMALS, true)).toEqual([
|
|
610
|
+
{ kind: "currency", text: "$ " },
|
|
611
|
+
{ kind: "text", text: "999.0" },
|
|
612
|
+
]);
|
|
613
|
+
(0, vitest_1.expect)((0, numbers_1.formatUsdPriceParts)(-1n)).toEqual([{ kind: "text", text: "NA" }]);
|
|
614
|
+
(0, vitest_1.expect)((0, numbers_1.formatUsdParts)(undefined)).toBeUndefined();
|
|
615
|
+
});
|
|
616
|
+
(0, vitest_1.it)("numberParts merges adjacent text and skips empty inputs", () => {
|
|
617
|
+
(0, vitest_1.expect)((0, numbers_1.numberParts)("> ", undefined, "", "-", numbers_1.USD_PART, "1.00", [{ kind: "multiplier", text: "x" }])).toEqual([
|
|
618
|
+
{ kind: "text", text: "> -" },
|
|
619
|
+
{ kind: "currency", text: "$ " },
|
|
620
|
+
{ kind: "text", text: "1.00" },
|
|
621
|
+
{ kind: "multiplier", text: "x" },
|
|
622
|
+
]);
|
|
623
|
+
(0, vitest_1.expect)((0, numbers_1.isNumberParts)((0, numbers_1.formatUsdParts)(usd(1)))).toBe(true);
|
|
624
|
+
(0, vitest_1.expect)((0, numbers_1.isNumberParts)(["$ 1.00"])).toBe(false);
|
|
625
|
+
(0, vitest_1.expect)((0, numbers_1.isNumberParts)("$ 1.00")).toBe(false);
|
|
626
|
+
});
|
|
627
|
+
});
|
|
@@ -65,6 +65,7 @@ var CustomErrorName;
|
|
|
65
65
|
CustomErrorName["DisabledMarket"] = "DisabledMarket";
|
|
66
66
|
CustomErrorName["EndOfOracleSimulation"] = "EndOfOracleSimulation";
|
|
67
67
|
CustomErrorName["ExternalCallFailed"] = "ExternalCallFailed";
|
|
68
|
+
CustomErrorName["InsufficientBridgeOutputAmount"] = "InsufficientBridgeOutputAmount";
|
|
68
69
|
CustomErrorName["InsufficientCollateralAmount"] = "InsufficientCollateralAmount";
|
|
69
70
|
CustomErrorName["InsufficientCollateralUsd"] = "InsufficientCollateralUsd";
|
|
70
71
|
CustomErrorName["InsufficientExecutionGas"] = "InsufficientExecutionGas";
|
|
@@ -15,4 +15,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./utils"), exports);
|
|
18
|
+
__exportStar(require("./parts"), exports);
|
|
18
19
|
__exportStar(require("./prbMath"), exports);
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isNumberParts = exports.joinNumberParts = exports.numberParts = exports.multiplierPart = exports.USD_PART = exports.USD_SYMBOL = void 0;
|
|
4
|
+
exports.USD_SYMBOL = "$ ";
|
|
5
|
+
exports.USD_PART = { kind: "currency", text: exports.USD_SYMBOL };
|
|
6
|
+
function multiplierPart(text) {
|
|
7
|
+
return { kind: "multiplier", text };
|
|
8
|
+
}
|
|
9
|
+
exports.multiplierPart = multiplierPart;
|
|
10
|
+
function numberParts(...inputs) {
|
|
11
|
+
const parts = [];
|
|
12
|
+
const push = (part) => {
|
|
13
|
+
if (!part.text) {
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
const last = parts[parts.length - 1];
|
|
17
|
+
if (part.kind === "text" && last?.kind === "text") {
|
|
18
|
+
parts[parts.length - 1] = { kind: "text", text: last.text + part.text };
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
parts.push(part);
|
|
22
|
+
};
|
|
23
|
+
for (const input of inputs) {
|
|
24
|
+
if (input === undefined) {
|
|
25
|
+
continue;
|
|
26
|
+
}
|
|
27
|
+
if (typeof input === "string") {
|
|
28
|
+
push({ kind: "text", text: input });
|
|
29
|
+
}
|
|
30
|
+
else if (Array.isArray(input)) {
|
|
31
|
+
input.forEach(push);
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
push(input);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return parts;
|
|
38
|
+
}
|
|
39
|
+
exports.numberParts = numberParts;
|
|
40
|
+
function joinNumberParts(parts) {
|
|
41
|
+
return parts.map((part) => part.text).join("");
|
|
42
|
+
}
|
|
43
|
+
exports.joinNumberParts = joinNumberParts;
|
|
44
|
+
function isNumberParts(value) {
|
|
45
|
+
return (Array.isArray(value) &&
|
|
46
|
+
value.every((item) => typeof item === "object" && item !== null && "kind" in item && "text" in item && typeof item.text === "string"));
|
|
47
|
+
}
|
|
48
|
+
exports.isNumberParts = isNumberParts;
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
exports.absDiffBps = exports.clamp = exports.calculateDisplayDecimals = exports.deserializeBigIntsInObject = exports.serializeBigIntsInObject = exports.removeTrailingZeros = exports.maxbigint = exports.minBigNumber = exports.roundBigIntToDecimals = exports.roundToOrder = exports.roundToTwoDecimals = exports.roundUpDivision = exports.parseValue = exports.toBigNumberWithDecimals = exports.roundWithDecimals = exports.getPlusOrMinusSymbol = exports.padDecimals = exports.limitDecimals = exports.getLimitedDisplay = exports.formatAmountFree = void 0;
|
|
3
|
+
exports.formatAmountHumanParts = exports.formatNumberHuman = exports.formatNumberHumanParts = exports.formatPercentageDisplay = exports.formatUsdPrice = exports.formatUsdPriceParts = exports.formatRatePercentage = exports.formatTokenAmountWithUsd = exports.formatTokenAmountWithUsdParts = exports.formatTokenAmount = exports.formatPercentage = exports.formatDeltaUsd = exports.formatDeltaUsdParts = exports.formatBigUsd = exports.formatBigUsdParts = exports.formatUsd = exports.formatUsdParts = exports.adjustForDecimals = exports.bigintToNumber = exports.trimZeroDecimals = exports.numberToBigint = exports.applyFactor = exports.roundsToZero = exports.roundUpMagnitudeDivision = exports.formatPriceImpactBps = exports.formatBasisPoints = exports.getBasisPoints = exports.basisPointsToFloat = exports.toBigInt = exports.expandDecimals = exports.safeDivide = exports.isNonZero = exports.TRIGGER_PREFIX_BELOW = exports.TRIGGER_PREFIX_ABOVE = exports.PERCENT_PRECISION_DECIMALS = exports.isUint256 = exports.parseUint256DecimalString = exports.MaxInt256 = exports.MaxUint256 = exports.BN_NEGATIVE_ONE = exports.BN_ONE = exports.BN_ZERO = exports.FLOAT_PRECISION_SQRT = exports.FLOAT_PRECISION_SQRT_DECIMALS = exports.PRECISION = exports.PRECISION_DECIMALS = exports.BASIS_POINTS_DECIMALS = exports.BASIS_POINTS_DIVISOR_BIGINT = exports.BASIS_POINTS_DIVISOR = exports.USD_DECIMALS = void 0;
|
|
4
|
+
exports.absDiffBps = exports.clamp = exports.calculateDisplayDecimals = exports.deserializeBigIntsInObject = exports.serializeBigIntsInObject = exports.removeTrailingZeros = exports.maxbigint = exports.minBigNumber = exports.roundBigIntToDecimals = exports.roundToOrder = exports.roundToTwoDecimals = exports.roundUpDivision = exports.parseValue = exports.toBigNumberWithDecimals = exports.roundWithDecimals = exports.getPlusOrMinusSymbol = exports.padDecimals = exports.limitDecimals = exports.getLimitedDisplay = exports.formatAmountFree = exports.formatArrayAmount = exports.formatKeyAmount = exports.formatAmount = exports.numberWithCommas = exports.numberWithCommasParts = exports.formatFactor = exports.formatBalanceAmount = exports.formatAmountHuman = void 0;
|
|
5
5
|
const viem_1 = require("viem");
|
|
6
6
|
const bigmath_1 = require("../bigmath");
|
|
7
|
+
const parts_1 = require("./parts");
|
|
7
8
|
exports.USD_DECIMALS = 30;
|
|
8
9
|
exports.BASIS_POINTS_DIVISOR = 10000;
|
|
9
10
|
exports.BASIS_POINTS_DIVISOR_BIGINT = 10000n;
|
|
@@ -175,7 +176,7 @@ function adjustForDecimals(amount, divDecimals, mulDecimals) {
|
|
|
175
176
|
return (amount * expandDecimals(1, mulDecimals)) / expandDecimals(1, divDecimals);
|
|
176
177
|
}
|
|
177
178
|
exports.adjustForDecimals = adjustForDecimals;
|
|
178
|
-
function
|
|
179
|
+
function formatUsdParts(usd, opts = {}) {
|
|
179
180
|
const { fallbackToZero = false, displayDecimals = 2 } = opts;
|
|
180
181
|
if (typeof usd !== "bigint") {
|
|
181
182
|
if (fallbackToZero) {
|
|
@@ -201,17 +202,29 @@ function formatUsd(usd, opts = {}) {
|
|
|
201
202
|
const sign = usd < 0n ? "-" : maybePlus;
|
|
202
203
|
const symbol = exceedingInfo.symbol ? `${exceedingInfo.symbol}\u00a0` : "";
|
|
203
204
|
const displayUsd = (0, exports.formatAmount)(exceedingInfo.value, exports.USD_DECIMALS, displayDecimals, true);
|
|
204
|
-
return
|
|
205
|
+
return (0, parts_1.numberParts)(symbol, sign, parts_1.USD_PART, displayUsd);
|
|
206
|
+
}
|
|
207
|
+
exports.formatUsdParts = formatUsdParts;
|
|
208
|
+
function formatUsd(usd, opts = {}) {
|
|
209
|
+
const parts = formatUsdParts(usd, opts);
|
|
210
|
+
return parts && (0, parts_1.joinNumberParts)(parts);
|
|
205
211
|
}
|
|
206
212
|
exports.formatUsd = formatUsd;
|
|
213
|
+
function formatBigUsdParts(amount, opts = {}) {
|
|
214
|
+
return formatUsdParts(amount, {
|
|
215
|
+
maxThreshold: "9999999999999999999999999",
|
|
216
|
+
displayDecimals: opts.displayDecimals ?? 0,
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
exports.formatBigUsdParts = formatBigUsdParts;
|
|
207
220
|
function formatBigUsd(amount, opts = {}) {
|
|
208
|
-
return
|
|
221
|
+
return (0, parts_1.joinNumberParts)(formatBigUsdParts(amount, opts));
|
|
209
222
|
}
|
|
210
223
|
exports.formatBigUsd = formatBigUsd;
|
|
211
|
-
function
|
|
224
|
+
function formatDeltaUsdParts(deltaUsd, percentage, opts = {}) {
|
|
212
225
|
if (typeof deltaUsd !== "bigint") {
|
|
213
226
|
if (opts.fallbackToZero) {
|
|
214
|
-
return
|
|
227
|
+
return (0, parts_1.numberParts)(formatUsdParts(0n), ` (${(0, exports.formatAmount)(0n, 2, 2)}%)`);
|
|
215
228
|
}
|
|
216
229
|
return undefined;
|
|
217
230
|
}
|
|
@@ -220,7 +233,12 @@ function formatDeltaUsd(deltaUsd, percentage, opts = {}) {
|
|
|
220
233
|
const percentageStr = percentage !== undefined && !opts.hidePercentage ? ` (${sign}${formatPercentage(bigmath_1.bigMath.abs(percentage))})` : "";
|
|
221
234
|
const deltaUsdStr = (0, exports.formatAmount)(exceedingInfo.value, exports.USD_DECIMALS, 2, true);
|
|
222
235
|
const symbol = exceedingInfo.symbol ? `${exceedingInfo.symbol} ` : "";
|
|
223
|
-
return
|
|
236
|
+
return (0, parts_1.numberParts)(symbol, sign, parts_1.USD_PART, deltaUsdStr, percentageStr);
|
|
237
|
+
}
|
|
238
|
+
exports.formatDeltaUsdParts = formatDeltaUsdParts;
|
|
239
|
+
function formatDeltaUsd(deltaUsd, percentage, opts = {}) {
|
|
240
|
+
const parts = formatDeltaUsdParts(deltaUsd, percentage, opts);
|
|
241
|
+
return parts && (0, parts_1.joinNumberParts)(parts);
|
|
224
242
|
}
|
|
225
243
|
exports.formatDeltaUsd = formatDeltaUsd;
|
|
226
244
|
function formatPercentage(percentage, opts = {}) {
|
|
@@ -272,7 +290,7 @@ function formatTokenAmount(amount, tokenDecimals, symbol, opts = {}) {
|
|
|
272
290
|
return `${amountStr}${symbolStr}`;
|
|
273
291
|
}
|
|
274
292
|
exports.formatTokenAmount = formatTokenAmount;
|
|
275
|
-
function
|
|
293
|
+
function formatTokenAmountWithUsdParts(tokenAmount, usdAmount, tokenSymbol, tokenDecimals, opts = {}) {
|
|
276
294
|
if (typeof tokenAmount !== "bigint" || typeof usdAmount !== "bigint" || !tokenSymbol || !tokenDecimals) {
|
|
277
295
|
if (!opts.fallbackToZero) {
|
|
278
296
|
return undefined;
|
|
@@ -283,11 +301,16 @@ function formatTokenAmountWithUsd(tokenAmount, usdAmount, tokenSymbol, tokenDeci
|
|
|
283
301
|
useCommas: true,
|
|
284
302
|
displayPlus: opts.displayPlus,
|
|
285
303
|
});
|
|
286
|
-
const
|
|
304
|
+
const usdParts = formatUsdParts(usdAmount, {
|
|
287
305
|
fallbackToZero: opts.fallbackToZero,
|
|
288
306
|
displayPlus: opts.displayPlus,
|
|
289
307
|
});
|
|
290
|
-
return `${tokenStr} (
|
|
308
|
+
return (0, parts_1.numberParts)(`${tokenStr} (`, usdParts, ")");
|
|
309
|
+
}
|
|
310
|
+
exports.formatTokenAmountWithUsdParts = formatTokenAmountWithUsdParts;
|
|
311
|
+
function formatTokenAmountWithUsd(tokenAmount, usdAmount, tokenSymbol, tokenDecimals, opts = {}) {
|
|
312
|
+
const parts = formatTokenAmountWithUsdParts(tokenAmount, usdAmount, tokenSymbol, tokenDecimals, opts);
|
|
313
|
+
return parts && (0, parts_1.joinNumberParts)(parts);
|
|
291
314
|
}
|
|
292
315
|
exports.formatTokenAmountWithUsd = formatTokenAmountWithUsd;
|
|
293
316
|
/**
|
|
@@ -304,19 +327,24 @@ function formatRatePercentage(rate, opts) {
|
|
|
304
327
|
return `${plurOrMinus}\u200a${(0, exports.formatAmount)(amount, 30, opts?.displayDecimals ?? 4)}%`;
|
|
305
328
|
}
|
|
306
329
|
exports.formatRatePercentage = formatRatePercentage;
|
|
307
|
-
function
|
|
330
|
+
function formatUsdPriceParts(price, opts = {}) {
|
|
308
331
|
if (price === undefined) {
|
|
309
332
|
return;
|
|
310
333
|
}
|
|
311
334
|
if (price < 0n) {
|
|
312
|
-
return "NA";
|
|
335
|
+
return (0, parts_1.numberParts)("NA");
|
|
313
336
|
}
|
|
314
337
|
const decimals = calculateDisplayDecimals(price, undefined, opts.visualMultiplier);
|
|
315
|
-
return
|
|
338
|
+
return formatUsdParts(price, {
|
|
316
339
|
...opts,
|
|
317
340
|
displayDecimals: decimals,
|
|
318
341
|
});
|
|
319
342
|
}
|
|
343
|
+
exports.formatUsdPriceParts = formatUsdPriceParts;
|
|
344
|
+
function formatUsdPrice(price, opts = {}) {
|
|
345
|
+
const parts = formatUsdPriceParts(price, opts);
|
|
346
|
+
return parts && (0, parts_1.joinNumberParts)(parts);
|
|
347
|
+
}
|
|
320
348
|
exports.formatUsdPrice = formatUsdPrice;
|
|
321
349
|
function formatPercentageDisplay(percentage, hideThreshold) {
|
|
322
350
|
if (hideThreshold && percentage < hideThreshold) {
|
|
@@ -325,33 +353,42 @@ function formatPercentageDisplay(percentage, hideThreshold) {
|
|
|
325
353
|
return `${percentage}%`;
|
|
326
354
|
}
|
|
327
355
|
exports.formatPercentageDisplay = formatPercentageDisplay;
|
|
328
|
-
function
|
|
356
|
+
function formatNumberHumanParts(n, showDollar = false, displayDecimals = 1) {
|
|
329
357
|
// For large numbers, we can neglect the decimals to avoid decimals in cases like 9999999.99999
|
|
330
358
|
if (n >= 1000000) {
|
|
331
359
|
n = Math.round(n);
|
|
332
360
|
}
|
|
333
361
|
const isNegative = n < 0;
|
|
334
362
|
const absN = Math.abs(n);
|
|
335
|
-
const sign =
|
|
363
|
+
const sign = isNegative ? "-" : "";
|
|
364
|
+
const currency = showDollar ? parts_1.USD_PART : undefined;
|
|
336
365
|
// Promote the suffix when the displayed value rounds to 1000 in the smaller unit.
|
|
337
366
|
if (Number((absN / 1000000).toFixed(displayDecimals)) >= 1000) {
|
|
338
|
-
return
|
|
367
|
+
return (0, parts_1.numberParts)(sign, currency, `${(absN / 1000000000).toFixed(displayDecimals)}b`);
|
|
339
368
|
}
|
|
340
369
|
if (Number((absN / 1000).toFixed(displayDecimals)) >= 1000) {
|
|
341
|
-
return
|
|
370
|
+
return (0, parts_1.numberParts)(sign, currency, `${(absN / 1000000).toFixed(displayDecimals)}m`);
|
|
342
371
|
}
|
|
343
372
|
if (Number(absN.toFixed(displayDecimals)) >= 1000) {
|
|
344
|
-
return
|
|
373
|
+
return (0, parts_1.numberParts)(sign, currency, `${(absN / 1000).toFixed(displayDecimals)}k`);
|
|
345
374
|
}
|
|
346
|
-
return
|
|
375
|
+
return (0, parts_1.numberParts)(sign, currency, absN.toFixed(displayDecimals));
|
|
376
|
+
}
|
|
377
|
+
exports.formatNumberHumanParts = formatNumberHumanParts;
|
|
378
|
+
function formatNumberHuman(n, showDollar = false, displayDecimals = 1) {
|
|
379
|
+
return (0, parts_1.joinNumberParts)(formatNumberHumanParts(n, showDollar, displayDecimals));
|
|
347
380
|
}
|
|
348
381
|
exports.formatNumberHuman = formatNumberHuman;
|
|
349
|
-
function
|
|
382
|
+
function formatAmountHumanParts(amount, tokenDecimals, showDollar = false, displayDecimals = 1) {
|
|
350
383
|
if (amount === undefined) {
|
|
351
|
-
return "...";
|
|
384
|
+
return (0, parts_1.numberParts)("...");
|
|
352
385
|
}
|
|
353
386
|
let n = Number((0, exports.formatAmount)(amount, tokenDecimals));
|
|
354
|
-
return
|
|
387
|
+
return formatNumberHumanParts(n, showDollar, displayDecimals);
|
|
388
|
+
}
|
|
389
|
+
exports.formatAmountHumanParts = formatAmountHumanParts;
|
|
390
|
+
function formatAmountHuman(amount, tokenDecimals, showDollar = false, displayDecimals = 1) {
|
|
391
|
+
return (0, parts_1.joinNumberParts)(formatAmountHumanParts(amount, tokenDecimals, showDollar, displayDecimals));
|
|
355
392
|
}
|
|
356
393
|
exports.formatAmountHuman = formatAmountHuman;
|
|
357
394
|
function formatBalanceAmount(amount, tokenDecimals, tokenSymbol, { showZero = false, toExponential = true, isStable = false, signed = false, } = {}) {
|
|
@@ -410,13 +447,17 @@ function formatFactor(factor) {
|
|
|
410
447
|
return (0, exports.formatAmount)(factor, exports.PRECISION_DECIMALS, factorDecimals);
|
|
411
448
|
}
|
|
412
449
|
exports.formatFactor = formatFactor;
|
|
413
|
-
function
|
|
450
|
+
function numberWithCommasParts(x, { showDollar = false } = {}) {
|
|
414
451
|
if (x === undefined || x === null) {
|
|
415
|
-
return "...";
|
|
452
|
+
return (0, parts_1.numberParts)("...");
|
|
416
453
|
}
|
|
417
454
|
const parts = x.toString().split(".");
|
|
418
455
|
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
|
|
419
|
-
return
|
|
456
|
+
return (0, parts_1.numberParts)(showDollar ? parts_1.USD_PART : undefined, parts.join("."));
|
|
457
|
+
}
|
|
458
|
+
exports.numberWithCommasParts = numberWithCommasParts;
|
|
459
|
+
function numberWithCommas(x, opts = {}) {
|
|
460
|
+
return (0, parts_1.joinNumberParts)(numberWithCommasParts(x, opts));
|
|
420
461
|
}
|
|
421
462
|
exports.numberWithCommas = numberWithCommas;
|
|
422
463
|
const formatAmount = (amount, tokenDecimals, displayDecimals, useCommas, defaultValue, visualMultiplier) => {
|
|
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.getIsInvalidBatchReceiver = exports.getBatchIsNativePayment = exports.getIsEmptyBatch = exports.getBatchSwapsCount = exports.getBatchRequiredActions = exports.encodeExchangeRouterMulticall = exports.buildCancelOrderMulticall = exports.buildUpdateOrderMulticall = exports.buildCreateOrderMulticall = exports.getBatchOrderMulticallPayload = exports.getExternalCallsPayload = exports.getEmptyExternalCallsPayload = exports.combineExternalCalls = exports.getBatchExternalCalls = exports.buildTokenTransfersParamsForIncreaseOrSwap = exports.buildTokenTransfersParamsForDecrease = exports.getBatchExternalSwapGasLimit = exports.getBatchTotalPayCollateralAmount = exports.getBatchTotalExecutionFee = exports.buildUpdateOrderPayload = exports.getIsTwapOrderPayload = exports.buildTwapOrdersPayloads = exports.buildDecreaseOrderPayload = exports.buildIncreaseOrderPayload = exports.buildSwapOrderPayload = void 0;
|
|
6
|
+
exports.getIsInvalidBatchReceiver = exports.getBatchIsNativePayment = exports.getIsEmptyBatch = exports.getBatchSwapsCount = exports.getBatchRequiredActions = exports.encodeExchangeRouterMulticall = exports.buildCancelOrderMulticall = exports.buildUpdateOrderMulticall = exports.buildCreateOrderMulticall = exports.getBatchOrderMulticallPayload = exports.getExternalCallsPayload = exports.getEmptyExternalCallsPayload = exports.combineExternalCalls = exports.getBatchExternalCalls = exports.buildTokenTransfersParamsForIncreaseOrSwap = exports.buildTokenTransfersParamsForDecrease = exports.getBatchExternalSwapGasLimit = exports.getBatchTotalPayCollateralAmount = exports.getExpressBatchOrderParams = exports.getBatchExecutionFeeEstimates = exports.getBatchTotalExecutionFee = exports.buildUpdateOrderPayload = exports.getIsTwapOrderPayload = exports.buildTwapOrdersPayloads = exports.buildDecreaseOrderPayload = exports.buildIncreaseOrderPayload = exports.buildSwapOrderPayload = void 0;
|
|
7
7
|
const uniq_1 = __importDefault(require("lodash/uniq"));
|
|
8
8
|
const viem_1 = require("viem");
|
|
9
9
|
const ExchangeRouter_1 = __importDefault(require("../../abis/ExchangeRouter"));
|
|
@@ -12,6 +12,7 @@ const Token_1 = __importDefault(require("../../abis/Token"));
|
|
|
12
12
|
const chains_1 = require("../../configs/chains");
|
|
13
13
|
const contracts_1 = require("../../configs/contracts");
|
|
14
14
|
const tokens_1 = require("../../configs/tokens");
|
|
15
|
+
const bigmath_1 = require("../bigmath");
|
|
15
16
|
const numbers_1 = require("../numbers");
|
|
16
17
|
const objects_1 = require("../objects");
|
|
17
18
|
const orders_1 = require("../orders");
|
|
@@ -328,6 +329,59 @@ function getBatchTotalExecutionFee({ batchParams, tokensData, chainId, allowEmpt
|
|
|
328
329
|
};
|
|
329
330
|
}
|
|
330
331
|
exports.getBatchTotalExecutionFee = getBatchTotalExecutionFee;
|
|
332
|
+
function getBatchExecutionFeeEstimates(batchParams) {
|
|
333
|
+
return batchParams.createOrderParams.map((co) => ({
|
|
334
|
+
executionFee: co.orderPayload.numbers.executionFee,
|
|
335
|
+
gasLimit: co.params.executionGasLimit,
|
|
336
|
+
}));
|
|
337
|
+
}
|
|
338
|
+
exports.getBatchExecutionFeeEstimates = getBatchExecutionFeeEstimates;
|
|
339
|
+
// Expects wallet-priced params: apply once, right before estimating or signing for the relay
|
|
340
|
+
function getExpressBatchOrderParams(chainId, batchParams) {
|
|
341
|
+
const allowance = (0, chains_1.getExecutionFeePriorityFeeAllowance)(chainId);
|
|
342
|
+
if (allowance === 0n) {
|
|
343
|
+
return batchParams;
|
|
344
|
+
}
|
|
345
|
+
const orderVaultAddress = (0, contracts_1.getContract)(chainId, "OrderVault");
|
|
346
|
+
return {
|
|
347
|
+
...batchParams,
|
|
348
|
+
updateOrderParams: batchParams.updateOrderParams.map((uo) => {
|
|
349
|
+
const delta = bigmath_1.bigMath.min(allowance * (uo.params.executionGasLimit ?? 0n), uo.updatePayload.executionFeeTopUp);
|
|
350
|
+
if (delta === 0n) {
|
|
351
|
+
return uo;
|
|
352
|
+
}
|
|
353
|
+
return {
|
|
354
|
+
...uo,
|
|
355
|
+
params: { ...uo.params, executionFeeTopUp: uo.params.executionFeeTopUp - delta },
|
|
356
|
+
updatePayload: { ...uo.updatePayload, executionFeeTopUp: uo.updatePayload.executionFeeTopUp - delta },
|
|
357
|
+
};
|
|
358
|
+
}),
|
|
359
|
+
createOrderParams: batchParams.createOrderParams.map((co) => {
|
|
360
|
+
const delta = bigmath_1.bigMath.min(allowance * co.params.executionGasLimit, co.orderPayload.numbers.executionFee);
|
|
361
|
+
if (delta === 0n) {
|
|
362
|
+
return co;
|
|
363
|
+
}
|
|
364
|
+
return {
|
|
365
|
+
...co,
|
|
366
|
+
params: { ...co.params, executionFeeAmount: co.params.executionFeeAmount - delta },
|
|
367
|
+
orderPayload: {
|
|
368
|
+
...co.orderPayload,
|
|
369
|
+
numbers: { ...co.orderPayload.numbers, executionFee: co.orderPayload.numbers.executionFee - delta },
|
|
370
|
+
},
|
|
371
|
+
tokenTransfersParams: co.tokenTransfersParams
|
|
372
|
+
? {
|
|
373
|
+
...co.tokenTransfersParams,
|
|
374
|
+
value: co.tokenTransfersParams.value - delta,
|
|
375
|
+
tokenTransfers: co.tokenTransfersParams.tokenTransfers.map((transfer) => transfer.tokenAddress === tokens_1.NATIVE_TOKEN_ADDRESS && transfer.destination === orderVaultAddress
|
|
376
|
+
? { ...transfer, amount: transfer.amount - delta }
|
|
377
|
+
: transfer),
|
|
378
|
+
}
|
|
379
|
+
: co.tokenTransfersParams,
|
|
380
|
+
};
|
|
381
|
+
}),
|
|
382
|
+
};
|
|
383
|
+
}
|
|
384
|
+
exports.getExpressBatchOrderParams = getExpressBatchOrderParams;
|
|
331
385
|
function getBatchTotalPayCollateralAmount(batchParams) {
|
|
332
386
|
const payAmounts = {};
|
|
333
387
|
for (const co of batchParams.createOrderParams) {
|
|
@@ -264,6 +264,13 @@ export function getMaxFeePerGas(chainId) {
|
|
|
264
264
|
export function getGasPricePremium(chainId) {
|
|
265
265
|
return CONTRACTS_CHAIN_CONFIGS[chainId]?.gasPricePremium;
|
|
266
266
|
}
|
|
267
|
+
// Priority fee wallets may add, charged on Arbitrum One since 2026-09-23; express is relayed with a zero tip
|
|
268
|
+
const EXECUTION_FEE_PRIORITY_FEE_ALLOWANCE = {
|
|
269
|
+
[ARBITRUM]: 30000000n, // 0.03 gwei
|
|
270
|
+
};
|
|
271
|
+
export function getExecutionFeePriorityFeeAllowance(chainId) {
|
|
272
|
+
return EXECUTION_FEE_PRIORITY_FEE_ALLOWANCE[chainId] ?? 0n;
|
|
273
|
+
}
|
|
267
274
|
export function getMaxPriorityFeePerGas(chainId) {
|
|
268
275
|
return CONTRACTS_CHAIN_CONFIGS[chainId]?.maxPriorityFeePerGas;
|
|
269
276
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { describe, expect, it } from "vitest";
|
|
2
2
|
import { BASIS_POINTS_DIVISOR_BIGINT, USD_DECIMALS } from "../../configs/factors";
|
|
3
|
-
import { applyFactor, basisPointsToFloat, bigintToNumber, BN_NEGATIVE_ONE, BN_ONE, BN_ZERO, expandDecimals, formatAmount, formatAmountHuman, formatBalanceAmount, formatBasisPoints, formatPriceImpactBps, formatFactor, formatNumberHuman, formatPercentage, formatTokenAmount, formatUsdPrice, getBasisPoints, isUint256, MaxUint256, numberToBigint, parseUint256DecimalString, PERCENT_PRECISION_DECIMALS, PRECISION, PRECISION_DECIMALS, parseValue, removeTrailingZeros, trimZeroDecimals, roundWithDecimals, roundUpMagnitudeDivision, roundsToZero, toBigNumberWithDecimals, } from "../numbers";
|
|
3
|
+
import { applyFactor, basisPointsToFloat, bigintToNumber, BN_NEGATIVE_ONE, BN_ONE, BN_ZERO, expandDecimals, formatAmount, formatAmountHuman, formatBalanceAmount, formatBasisPoints, formatPriceImpactBps, formatFactor, formatNumberHuman, formatPercentage, formatTokenAmount, formatUsdPrice, getBasisPoints, isUint256, MaxUint256, numberToBigint, parseUint256DecimalString, PERCENT_PRECISION_DECIMALS, PRECISION, PRECISION_DECIMALS, parseValue, removeTrailingZeros, trimZeroDecimals, roundWithDecimals, roundUpMagnitudeDivision, roundsToZero, toBigNumberWithDecimals, formatUsd, formatUsdParts, formatDeltaUsd, formatDeltaUsdParts, formatBigUsd, formatBigUsdParts, formatUsdPriceParts, formatAmountHumanParts, formatTokenAmountWithUsd, formatTokenAmountWithUsdParts, numberWithCommas, numberWithCommasParts, numberParts, joinNumberParts, isNumberParts, USD_PART, } from "../numbers";
|
|
4
4
|
const ONE_USD = 1000000000000000000000000000000n;
|
|
5
5
|
describe("numbers utils", () => {
|
|
6
6
|
it("constants", () => {
|
|
@@ -544,3 +544,82 @@ describe("formatPriceImpactBps", () => {
|
|
|
544
544
|
expect(formatPriceImpactBps(-81n, -10000n)).toBeUndefined();
|
|
545
545
|
});
|
|
546
546
|
});
|
|
547
|
+
describe("number parts", () => {
|
|
548
|
+
const usd = (amount) => expandDecimals(amount, USD_DECIMALS);
|
|
549
|
+
it("string formatters are the joined parts", () => {
|
|
550
|
+
const cases = [
|
|
551
|
+
[formatUsd(usd(1234)), formatUsdParts(usd(1234))],
|
|
552
|
+
[
|
|
553
|
+
formatUsd(-usd(1), { displayPlus: true, displayDecimals: 4 }),
|
|
554
|
+
formatUsdParts(-usd(1), { displayPlus: true, displayDecimals: 4 }),
|
|
555
|
+
],
|
|
556
|
+
[formatUsd(usd(2000000000)), formatUsdParts(usd(2000000000))],
|
|
557
|
+
[formatUsd(1n), formatUsdParts(1n)],
|
|
558
|
+
[formatUsd(undefined, { fallbackToZero: true }), formatUsdParts(undefined, { fallbackToZero: true })],
|
|
559
|
+
[formatDeltaUsd(usd(5), expandDecimals(3, 2)), formatDeltaUsdParts(usd(5), expandDecimals(3, 2))],
|
|
560
|
+
[
|
|
561
|
+
formatDeltaUsd(0n, undefined, { showPlusForZero: true }),
|
|
562
|
+
formatDeltaUsdParts(0n, undefined, { showPlusForZero: true }),
|
|
563
|
+
],
|
|
564
|
+
[formatBigUsd(usd(123456789)), formatBigUsdParts(usd(123456789))],
|
|
565
|
+
[formatUsdPrice(usd(3)), formatUsdPriceParts(usd(3))],
|
|
566
|
+
[formatUsdPrice(-1n), formatUsdPriceParts(-1n)],
|
|
567
|
+
[
|
|
568
|
+
formatAmountHuman(usd(1150000000), USD_DECIMALS, true),
|
|
569
|
+
formatAmountHumanParts(usd(1150000000), USD_DECIMALS, true),
|
|
570
|
+
],
|
|
571
|
+
[
|
|
572
|
+
formatAmountHuman(-usd(12500), USD_DECIMALS, false, 2),
|
|
573
|
+
formatAmountHumanParts(-usd(12500), USD_DECIMALS, false, 2),
|
|
574
|
+
],
|
|
575
|
+
[formatAmountHuman(undefined, USD_DECIMALS), formatAmountHumanParts(undefined, USD_DECIMALS)],
|
|
576
|
+
[numberWithCommas("1234.5", { showDollar: true }), numberWithCommasParts("1234.5", { showDollar: true })],
|
|
577
|
+
[
|
|
578
|
+
formatTokenAmountWithUsd(expandDecimals(1, 18), usd(2500), "ETH", 18),
|
|
579
|
+
formatTokenAmountWithUsdParts(expandDecimals(1, 18), usd(2500), "ETH", 18),
|
|
580
|
+
],
|
|
581
|
+
];
|
|
582
|
+
for (const [text, parts] of cases) {
|
|
583
|
+
expect(parts && joinNumberParts(parts)).toBe(text);
|
|
584
|
+
}
|
|
585
|
+
});
|
|
586
|
+
it("marks the currency affix and keeps magnitude suffixes as text", () => {
|
|
587
|
+
expect(formatUsdParts(-usd(1234))).toEqual([
|
|
588
|
+
{ kind: "text", text: "-" },
|
|
589
|
+
{ kind: "currency", text: "$ " },
|
|
590
|
+
{ kind: "text", text: "1,234.00" },
|
|
591
|
+
]);
|
|
592
|
+
expect(formatUsdParts(usd(2000000000))).toEqual([
|
|
593
|
+
{ kind: "text", text: "> " },
|
|
594
|
+
{ kind: "currency", text: "$ " },
|
|
595
|
+
{ kind: "text", text: "1,000,000,000.00" },
|
|
596
|
+
]);
|
|
597
|
+
expect(formatDeltaUsdParts(usd(5), expandDecimals(3, 2))).toEqual([
|
|
598
|
+
{ kind: "text", text: "+" },
|
|
599
|
+
{ kind: "currency", text: "$ " },
|
|
600
|
+
{ kind: "text", text: "5.00 (+3.00%)" },
|
|
601
|
+
]);
|
|
602
|
+
expect(formatAmountHumanParts(usd(1150000000), USD_DECIMALS, true)).toEqual([
|
|
603
|
+
{ kind: "currency", text: "$ " },
|
|
604
|
+
{ kind: "text", text: "1.1b" },
|
|
605
|
+
]);
|
|
606
|
+
expect(formatAmountHumanParts(expandDecimals(12500, 18), 18)).toEqual([{ kind: "text", text: "12.5k" }]);
|
|
607
|
+
expect(formatAmountHumanParts(usd(999), USD_DECIMALS, true)).toEqual([
|
|
608
|
+
{ kind: "currency", text: "$ " },
|
|
609
|
+
{ kind: "text", text: "999.0" },
|
|
610
|
+
]);
|
|
611
|
+
expect(formatUsdPriceParts(-1n)).toEqual([{ kind: "text", text: "NA" }]);
|
|
612
|
+
expect(formatUsdParts(undefined)).toBeUndefined();
|
|
613
|
+
});
|
|
614
|
+
it("numberParts merges adjacent text and skips empty inputs", () => {
|
|
615
|
+
expect(numberParts("> ", undefined, "", "-", USD_PART, "1.00", [{ kind: "multiplier", text: "x" }])).toEqual([
|
|
616
|
+
{ kind: "text", text: "> -" },
|
|
617
|
+
{ kind: "currency", text: "$ " },
|
|
618
|
+
{ kind: "text", text: "1.00" },
|
|
619
|
+
{ kind: "multiplier", text: "x" },
|
|
620
|
+
]);
|
|
621
|
+
expect(isNumberParts(formatUsdParts(usd(1)))).toBe(true);
|
|
622
|
+
expect(isNumberParts(["$ 1.00"])).toBe(false);
|
|
623
|
+
expect(isNumberParts("$ 1.00")).toBe(false);
|
|
624
|
+
});
|
|
625
|
+
});
|
|
@@ -62,6 +62,7 @@ export var CustomErrorName;
|
|
|
62
62
|
CustomErrorName["DisabledMarket"] = "DisabledMarket";
|
|
63
63
|
CustomErrorName["EndOfOracleSimulation"] = "EndOfOracleSimulation";
|
|
64
64
|
CustomErrorName["ExternalCallFailed"] = "ExternalCallFailed";
|
|
65
|
+
CustomErrorName["InsufficientBridgeOutputAmount"] = "InsufficientBridgeOutputAmount";
|
|
65
66
|
CustomErrorName["InsufficientCollateralAmount"] = "InsufficientCollateralAmount";
|
|
66
67
|
CustomErrorName["InsufficientCollateralUsd"] = "InsufficientCollateralUsd";
|
|
67
68
|
CustomErrorName["InsufficientExecutionGas"] = "InsufficientExecutionGas";
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
export const USD_SYMBOL = "$ ";
|
|
2
|
+
export const USD_PART = { kind: "currency", text: USD_SYMBOL };
|
|
3
|
+
export function multiplierPart(text) {
|
|
4
|
+
return { kind: "multiplier", text };
|
|
5
|
+
}
|
|
6
|
+
export function numberParts(...inputs) {
|
|
7
|
+
const parts = [];
|
|
8
|
+
const push = (part) => {
|
|
9
|
+
if (!part.text) {
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
const last = parts[parts.length - 1];
|
|
13
|
+
if (part.kind === "text" && last?.kind === "text") {
|
|
14
|
+
parts[parts.length - 1] = { kind: "text", text: last.text + part.text };
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
parts.push(part);
|
|
18
|
+
};
|
|
19
|
+
for (const input of inputs) {
|
|
20
|
+
if (input === undefined) {
|
|
21
|
+
continue;
|
|
22
|
+
}
|
|
23
|
+
if (typeof input === "string") {
|
|
24
|
+
push({ kind: "text", text: input });
|
|
25
|
+
}
|
|
26
|
+
else if (Array.isArray(input)) {
|
|
27
|
+
input.forEach(push);
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
push(input);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return parts;
|
|
34
|
+
}
|
|
35
|
+
export function joinNumberParts(parts) {
|
|
36
|
+
return parts.map((part) => part.text).join("");
|
|
37
|
+
}
|
|
38
|
+
export function isNumberParts(value) {
|
|
39
|
+
return (Array.isArray(value) &&
|
|
40
|
+
value.every((item) => typeof item === "object" && item !== null && "kind" in item && "text" in item && typeof item.text === "string"));
|
|
41
|
+
}
|