@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,5 +1,6 @@
|
|
|
1
1
|
import { formatUnits, parseUnits } from "viem";
|
|
2
2
|
import { bigMath } from "../bigmath";
|
|
3
|
+
import { joinNumberParts, numberParts, USD_PART } from "./parts";
|
|
3
4
|
export const USD_DECIMALS = 30;
|
|
4
5
|
export const BASIS_POINTS_DIVISOR = 10000;
|
|
5
6
|
export const BASIS_POINTS_DIVISOR_BIGINT = 10000n;
|
|
@@ -154,7 +155,7 @@ export function bigintToNumber(value, decimals) {
|
|
|
154
155
|
export function adjustForDecimals(amount, divDecimals, mulDecimals) {
|
|
155
156
|
return (amount * expandDecimals(1, mulDecimals)) / expandDecimals(1, divDecimals);
|
|
156
157
|
}
|
|
157
|
-
export function
|
|
158
|
+
export function formatUsdParts(usd, opts = {}) {
|
|
158
159
|
const { fallbackToZero = false, displayDecimals = 2 } = opts;
|
|
159
160
|
if (typeof usd !== "bigint") {
|
|
160
161
|
if (fallbackToZero) {
|
|
@@ -180,15 +181,25 @@ export function formatUsd(usd, opts = {}) {
|
|
|
180
181
|
const sign = usd < 0n ? "-" : maybePlus;
|
|
181
182
|
const symbol = exceedingInfo.symbol ? `${exceedingInfo.symbol}\u00a0` : "";
|
|
182
183
|
const displayUsd = formatAmount(exceedingInfo.value, USD_DECIMALS, displayDecimals, true);
|
|
183
|
-
return
|
|
184
|
+
return numberParts(symbol, sign, USD_PART, displayUsd);
|
|
185
|
+
}
|
|
186
|
+
export function formatUsd(usd, opts = {}) {
|
|
187
|
+
const parts = formatUsdParts(usd, opts);
|
|
188
|
+
return parts && joinNumberParts(parts);
|
|
189
|
+
}
|
|
190
|
+
export function formatBigUsdParts(amount, opts = {}) {
|
|
191
|
+
return formatUsdParts(amount, {
|
|
192
|
+
maxThreshold: "9999999999999999999999999",
|
|
193
|
+
displayDecimals: opts.displayDecimals ?? 0,
|
|
194
|
+
});
|
|
184
195
|
}
|
|
185
196
|
export function formatBigUsd(amount, opts = {}) {
|
|
186
|
-
return
|
|
197
|
+
return joinNumberParts(formatBigUsdParts(amount, opts));
|
|
187
198
|
}
|
|
188
|
-
export function
|
|
199
|
+
export function formatDeltaUsdParts(deltaUsd, percentage, opts = {}) {
|
|
189
200
|
if (typeof deltaUsd !== "bigint") {
|
|
190
201
|
if (opts.fallbackToZero) {
|
|
191
|
-
return
|
|
202
|
+
return numberParts(formatUsdParts(0n), ` (${formatAmount(0n, 2, 2)}%)`);
|
|
192
203
|
}
|
|
193
204
|
return undefined;
|
|
194
205
|
}
|
|
@@ -197,7 +208,11 @@ export function formatDeltaUsd(deltaUsd, percentage, opts = {}) {
|
|
|
197
208
|
const percentageStr = percentage !== undefined && !opts.hidePercentage ? ` (${sign}${formatPercentage(bigMath.abs(percentage))})` : "";
|
|
198
209
|
const deltaUsdStr = formatAmount(exceedingInfo.value, USD_DECIMALS, 2, true);
|
|
199
210
|
const symbol = exceedingInfo.symbol ? `${exceedingInfo.symbol} ` : "";
|
|
200
|
-
return
|
|
211
|
+
return numberParts(symbol, sign, USD_PART, deltaUsdStr, percentageStr);
|
|
212
|
+
}
|
|
213
|
+
export function formatDeltaUsd(deltaUsd, percentage, opts = {}) {
|
|
214
|
+
const parts = formatDeltaUsdParts(deltaUsd, percentage, opts);
|
|
215
|
+
return parts && joinNumberParts(parts);
|
|
201
216
|
}
|
|
202
217
|
export function formatPercentage(percentage, opts = {}) {
|
|
203
218
|
const { fallbackToZero = false, signed = false, displayDecimals = 2, bps = true, showPlus = true, useCommas = false, } = opts;
|
|
@@ -246,7 +261,7 @@ export function formatTokenAmount(amount, tokenDecimals, symbol, opts = {}) {
|
|
|
246
261
|
}
|
|
247
262
|
return `${amountStr}${symbolStr}`;
|
|
248
263
|
}
|
|
249
|
-
export function
|
|
264
|
+
export function formatTokenAmountWithUsdParts(tokenAmount, usdAmount, tokenSymbol, tokenDecimals, opts = {}) {
|
|
250
265
|
if (typeof tokenAmount !== "bigint" || typeof usdAmount !== "bigint" || !tokenSymbol || !tokenDecimals) {
|
|
251
266
|
if (!opts.fallbackToZero) {
|
|
252
267
|
return undefined;
|
|
@@ -257,11 +272,15 @@ export function formatTokenAmountWithUsd(tokenAmount, usdAmount, tokenSymbol, to
|
|
|
257
272
|
useCommas: true,
|
|
258
273
|
displayPlus: opts.displayPlus,
|
|
259
274
|
});
|
|
260
|
-
const
|
|
275
|
+
const usdParts = formatUsdParts(usdAmount, {
|
|
261
276
|
fallbackToZero: opts.fallbackToZero,
|
|
262
277
|
displayPlus: opts.displayPlus,
|
|
263
278
|
});
|
|
264
|
-
return `${tokenStr} (
|
|
279
|
+
return numberParts(`${tokenStr} (`, usdParts, ")");
|
|
280
|
+
}
|
|
281
|
+
export function formatTokenAmountWithUsd(tokenAmount, usdAmount, tokenSymbol, tokenDecimals, opts = {}) {
|
|
282
|
+
const parts = formatTokenAmountWithUsdParts(tokenAmount, usdAmount, tokenSymbol, tokenDecimals, opts);
|
|
283
|
+
return parts && joinNumberParts(parts);
|
|
265
284
|
}
|
|
266
285
|
/**
|
|
267
286
|
*
|
|
@@ -276,51 +295,62 @@ export function formatRatePercentage(rate, opts) {
|
|
|
276
295
|
const amount = bigMath.abs(rate * 100n);
|
|
277
296
|
return `${plurOrMinus}\u200a${formatAmount(amount, 30, opts?.displayDecimals ?? 4)}%`;
|
|
278
297
|
}
|
|
279
|
-
export function
|
|
298
|
+
export function formatUsdPriceParts(price, opts = {}) {
|
|
280
299
|
if (price === undefined) {
|
|
281
300
|
return;
|
|
282
301
|
}
|
|
283
302
|
if (price < 0n) {
|
|
284
|
-
return "NA";
|
|
303
|
+
return numberParts("NA");
|
|
285
304
|
}
|
|
286
305
|
const decimals = calculateDisplayDecimals(price, undefined, opts.visualMultiplier);
|
|
287
|
-
return
|
|
306
|
+
return formatUsdParts(price, {
|
|
288
307
|
...opts,
|
|
289
308
|
displayDecimals: decimals,
|
|
290
309
|
});
|
|
291
310
|
}
|
|
311
|
+
export function formatUsdPrice(price, opts = {}) {
|
|
312
|
+
const parts = formatUsdPriceParts(price, opts);
|
|
313
|
+
return parts && joinNumberParts(parts);
|
|
314
|
+
}
|
|
292
315
|
export function formatPercentageDisplay(percentage, hideThreshold) {
|
|
293
316
|
if (hideThreshold && percentage < hideThreshold) {
|
|
294
317
|
return "";
|
|
295
318
|
}
|
|
296
319
|
return `${percentage}%`;
|
|
297
320
|
}
|
|
298
|
-
export function
|
|
321
|
+
export function formatNumberHumanParts(n, showDollar = false, displayDecimals = 1) {
|
|
299
322
|
// For large numbers, we can neglect the decimals to avoid decimals in cases like 9999999.99999
|
|
300
323
|
if (n >= 1000000) {
|
|
301
324
|
n = Math.round(n);
|
|
302
325
|
}
|
|
303
326
|
const isNegative = n < 0;
|
|
304
327
|
const absN = Math.abs(n);
|
|
305
|
-
const sign =
|
|
328
|
+
const sign = isNegative ? "-" : "";
|
|
329
|
+
const currency = showDollar ? USD_PART : undefined;
|
|
306
330
|
// Promote the suffix when the displayed value rounds to 1000 in the smaller unit.
|
|
307
331
|
if (Number((absN / 1000000).toFixed(displayDecimals)) >= 1000) {
|
|
308
|
-
return
|
|
332
|
+
return numberParts(sign, currency, `${(absN / 1000000000).toFixed(displayDecimals)}b`);
|
|
309
333
|
}
|
|
310
334
|
if (Number((absN / 1000).toFixed(displayDecimals)) >= 1000) {
|
|
311
|
-
return
|
|
335
|
+
return numberParts(sign, currency, `${(absN / 1000000).toFixed(displayDecimals)}m`);
|
|
312
336
|
}
|
|
313
337
|
if (Number(absN.toFixed(displayDecimals)) >= 1000) {
|
|
314
|
-
return
|
|
338
|
+
return numberParts(sign, currency, `${(absN / 1000).toFixed(displayDecimals)}k`);
|
|
315
339
|
}
|
|
316
|
-
return
|
|
340
|
+
return numberParts(sign, currency, absN.toFixed(displayDecimals));
|
|
317
341
|
}
|
|
318
|
-
export function
|
|
342
|
+
export function formatNumberHuman(n, showDollar = false, displayDecimals = 1) {
|
|
343
|
+
return joinNumberParts(formatNumberHumanParts(n, showDollar, displayDecimals));
|
|
344
|
+
}
|
|
345
|
+
export function formatAmountHumanParts(amount, tokenDecimals, showDollar = false, displayDecimals = 1) {
|
|
319
346
|
if (amount === undefined) {
|
|
320
|
-
return "...";
|
|
347
|
+
return numberParts("...");
|
|
321
348
|
}
|
|
322
349
|
let n = Number(formatAmount(amount, tokenDecimals));
|
|
323
|
-
return
|
|
350
|
+
return formatNumberHumanParts(n, showDollar, displayDecimals);
|
|
351
|
+
}
|
|
352
|
+
export function formatAmountHuman(amount, tokenDecimals, showDollar = false, displayDecimals = 1) {
|
|
353
|
+
return joinNumberParts(formatAmountHumanParts(amount, tokenDecimals, showDollar, displayDecimals));
|
|
324
354
|
}
|
|
325
355
|
export function formatBalanceAmount(amount, tokenDecimals, tokenSymbol, { showZero = false, toExponential = true, isStable = false, signed = false, } = {}) {
|
|
326
356
|
if (amount === undefined)
|
|
@@ -376,13 +406,16 @@ export function formatFactor(factor) {
|
|
|
376
406
|
const factorDecimals = Math.max(PRECISION_DECIMALS - trailingZeroes, 0);
|
|
377
407
|
return formatAmount(factor, PRECISION_DECIMALS, factorDecimals);
|
|
378
408
|
}
|
|
379
|
-
export function
|
|
409
|
+
export function numberWithCommasParts(x, { showDollar = false } = {}) {
|
|
380
410
|
if (x === undefined || x === null) {
|
|
381
|
-
return "...";
|
|
411
|
+
return numberParts("...");
|
|
382
412
|
}
|
|
383
413
|
const parts = x.toString().split(".");
|
|
384
414
|
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
|
|
385
|
-
return
|
|
415
|
+
return numberParts(showDollar ? USD_PART : undefined, parts.join("."));
|
|
416
|
+
}
|
|
417
|
+
export function numberWithCommas(x, opts = {}) {
|
|
418
|
+
return joinNumberParts(numberWithCommasParts(x, opts));
|
|
386
419
|
}
|
|
387
420
|
export const formatAmount = (amount, tokenDecimals, displayDecimals, useCommas, defaultValue, visualMultiplier) => {
|
|
388
421
|
if (defaultValue === undefined || defaultValue === null) {
|
|
@@ -3,9 +3,10 @@ import { encodeFunctionData, zeroAddress, zeroHash } from "viem";
|
|
|
3
3
|
import ExchangeRouterAbi from "../../abis/ExchangeRouter";
|
|
4
4
|
import { abis } from "../../abis/index";
|
|
5
5
|
import ERC20ABI from "../../abis/Token";
|
|
6
|
-
import { getExcessiveExecutionFee, getHighExecutionFee } from "../../configs/chains";
|
|
6
|
+
import { getExcessiveExecutionFee, getExecutionFeePriorityFeeAllowance, getHighExecutionFee, } from "../../configs/chains";
|
|
7
7
|
import { getContract } from "../../configs/contracts";
|
|
8
8
|
import { convertTokenAddress, getToken, getWrappedToken, NATIVE_TOKEN_ADDRESS } from "../../configs/tokens";
|
|
9
|
+
import { bigMath } from "../bigmath";
|
|
9
10
|
import { expandDecimals, MaxUint256, USD_DECIMALS } from "../numbers";
|
|
10
11
|
import { getByKey } from "../objects";
|
|
11
12
|
import { isIncreaseOrderType, isSwapOrderType } from "../orders";
|
|
@@ -315,6 +316,57 @@ export function getBatchTotalExecutionFee({ batchParams, tokensData, chainId, al
|
|
|
315
316
|
isFeeVeryHigh,
|
|
316
317
|
};
|
|
317
318
|
}
|
|
319
|
+
export function getBatchExecutionFeeEstimates(batchParams) {
|
|
320
|
+
return batchParams.createOrderParams.map((co) => ({
|
|
321
|
+
executionFee: co.orderPayload.numbers.executionFee,
|
|
322
|
+
gasLimit: co.params.executionGasLimit,
|
|
323
|
+
}));
|
|
324
|
+
}
|
|
325
|
+
// Expects wallet-priced params: apply once, right before estimating or signing for the relay
|
|
326
|
+
export function getExpressBatchOrderParams(chainId, batchParams) {
|
|
327
|
+
const allowance = getExecutionFeePriorityFeeAllowance(chainId);
|
|
328
|
+
if (allowance === 0n) {
|
|
329
|
+
return batchParams;
|
|
330
|
+
}
|
|
331
|
+
const orderVaultAddress = getContract(chainId, "OrderVault");
|
|
332
|
+
return {
|
|
333
|
+
...batchParams,
|
|
334
|
+
updateOrderParams: batchParams.updateOrderParams.map((uo) => {
|
|
335
|
+
const delta = bigMath.min(allowance * (uo.params.executionGasLimit ?? 0n), uo.updatePayload.executionFeeTopUp);
|
|
336
|
+
if (delta === 0n) {
|
|
337
|
+
return uo;
|
|
338
|
+
}
|
|
339
|
+
return {
|
|
340
|
+
...uo,
|
|
341
|
+
params: { ...uo.params, executionFeeTopUp: uo.params.executionFeeTopUp - delta },
|
|
342
|
+
updatePayload: { ...uo.updatePayload, executionFeeTopUp: uo.updatePayload.executionFeeTopUp - delta },
|
|
343
|
+
};
|
|
344
|
+
}),
|
|
345
|
+
createOrderParams: batchParams.createOrderParams.map((co) => {
|
|
346
|
+
const delta = bigMath.min(allowance * co.params.executionGasLimit, co.orderPayload.numbers.executionFee);
|
|
347
|
+
if (delta === 0n) {
|
|
348
|
+
return co;
|
|
349
|
+
}
|
|
350
|
+
return {
|
|
351
|
+
...co,
|
|
352
|
+
params: { ...co.params, executionFeeAmount: co.params.executionFeeAmount - delta },
|
|
353
|
+
orderPayload: {
|
|
354
|
+
...co.orderPayload,
|
|
355
|
+
numbers: { ...co.orderPayload.numbers, executionFee: co.orderPayload.numbers.executionFee - delta },
|
|
356
|
+
},
|
|
357
|
+
tokenTransfersParams: co.tokenTransfersParams
|
|
358
|
+
? {
|
|
359
|
+
...co.tokenTransfersParams,
|
|
360
|
+
value: co.tokenTransfersParams.value - delta,
|
|
361
|
+
tokenTransfers: co.tokenTransfersParams.tokenTransfers.map((transfer) => transfer.tokenAddress === NATIVE_TOKEN_ADDRESS && transfer.destination === orderVaultAddress
|
|
362
|
+
? { ...transfer, amount: transfer.amount - delta }
|
|
363
|
+
: transfer),
|
|
364
|
+
}
|
|
365
|
+
: co.tokenTransfersParams,
|
|
366
|
+
};
|
|
367
|
+
}),
|
|
368
|
+
};
|
|
369
|
+
}
|
|
318
370
|
export function getBatchTotalPayCollateralAmount(batchParams) {
|
|
319
371
|
const payAmounts = {};
|
|
320
372
|
for (const co of batchParams.createOrderParams) {
|