@gearbox-protocol/sdk 17.12.0 → 17.12.1

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.
@@ -1,10 +1,11 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ const require_onchain_utils_bigint_math = require("../../../utils/bigint-math.js");
2
3
  const require_onchain_constants_math = require("../../../constants/math.js");
3
4
  //#region src/onchain/accounts/intents/utils/price-impact.ts
4
- /** `b1 = b0 / V0`: a dollar of the basket, the reference implementation's anchor. */
5
- const PROBE_UNIT_USD_WAD = require_onchain_constants_math.WAD;
5
+ /** A $100 reference basket limits output-token quantisation in small quotes. */
6
+ const PROBE_UNIT_USD_WAD = 100n * require_onchain_constants_math.WAD;
6
7
  /**
7
- * `V0 = Σ b0ᵢ·pᵢ`, then `b1 = b0 / V0`, proportions kept.
8
+ * `V0 = Σ b0ᵢ·pᵢ`, then scale the basket to $100, proportions kept.
8
9
  *
9
10
  * Refuses only what the reference refuses — a basket worth nothing, or one that
10
11
  * rounds away entirely. Stricter guards here would report nothing where the old
@@ -73,6 +74,8 @@ async function collectPriceImpact(probes, ctx) {
73
74
  const quotes = await Promise.all(probes.map((leg) => leg.probe));
74
75
  let expectedUnd = 0n;
75
76
  let lossUnd = 0n;
77
+ let lowerLossUnd = 0n;
78
+ let upperLossUnd = 0n;
76
79
  for (const [index, leg] of probes.entries()) {
77
80
  const unit = quotes[index];
78
81
  if (unit === void 0 || unit <= 0n) return;
@@ -82,10 +85,17 @@ async function collectPriceImpact(probes, ctx) {
82
85
  if (expectedInUnd <= 0n) return;
83
86
  const loss = toUnderlyingSigned(ctx.toUnderlying, leg.tokenOut, expected - leg.realAmount);
84
87
  if (loss === void 0) return;
88
+ const lower = expected - leg.realAmount - 1n;
89
+ const upper = require_onchain_utils_bigint_math.BigIntMath.ceilDiv((unit + 1n) * leg.basketWad, leg.probeWad) - leg.realAmount;
90
+ const lowerInUnd = toUnderlyingSigned(ctx.toUnderlying, leg.tokenOut, lower);
91
+ const upperInUnd = toUnderlyingSigned(ctx.toUnderlying, leg.tokenOut, upper);
92
+ if (lowerInUnd === void 0 || upperInUnd === void 0) return;
93
+ lowerLossUnd += lowerInUnd - (lower > 0n ? 1n : 0n);
94
+ upperLossUnd += upperInUnd + (upper < 0n ? 1n : 0n);
85
95
  expectedUnd += expectedInUnd;
86
96
  lossUnd += loss;
87
97
  }
88
- if (expectedUnd <= 0n) return;
98
+ if (lowerLossUnd <= 0n && upperLossUnd >= 0n) lossUnd = 0n;
89
99
  return {
90
100
  ...lossRate({
91
101
  lossUnd,
@@ -1,9 +1,10 @@
1
+ import { BigIntMath } from "../../../utils/bigint-math.js";
1
2
  import { PERCENTAGE_FACTOR_1KK, PRICE_DECIMALS, WAD } from "../../../constants/math.js";
2
3
  //#region src/onchain/accounts/intents/utils/price-impact.ts
3
- /** `b1 = b0 / V0`: a dollar of the basket, the reference implementation's anchor. */
4
- const PROBE_UNIT_USD_WAD = WAD;
4
+ /** A $100 reference basket limits output-token quantisation in small quotes. */
5
+ const PROBE_UNIT_USD_WAD = 100n * WAD;
5
6
  /**
6
- * `V0 = Σ b0ᵢ·pᵢ`, then `b1 = b0 / V0`, proportions kept.
7
+ * `V0 = Σ b0ᵢ·pᵢ`, then scale the basket to $100, proportions kept.
7
8
  *
8
9
  * Refuses only what the reference refuses — a basket worth nothing, or one that
9
10
  * rounds away entirely. Stricter guards here would report nothing where the old
@@ -72,6 +73,8 @@ async function collectPriceImpact(probes, ctx) {
72
73
  const quotes = await Promise.all(probes.map((leg) => leg.probe));
73
74
  let expectedUnd = 0n;
74
75
  let lossUnd = 0n;
76
+ let lowerLossUnd = 0n;
77
+ let upperLossUnd = 0n;
75
78
  for (const [index, leg] of probes.entries()) {
76
79
  const unit = quotes[index];
77
80
  if (unit === void 0 || unit <= 0n) return;
@@ -81,10 +84,17 @@ async function collectPriceImpact(probes, ctx) {
81
84
  if (expectedInUnd <= 0n) return;
82
85
  const loss = toUnderlyingSigned(ctx.toUnderlying, leg.tokenOut, expected - leg.realAmount);
83
86
  if (loss === void 0) return;
87
+ const lower = expected - leg.realAmount - 1n;
88
+ const upper = BigIntMath.ceilDiv((unit + 1n) * leg.basketWad, leg.probeWad) - leg.realAmount;
89
+ const lowerInUnd = toUnderlyingSigned(ctx.toUnderlying, leg.tokenOut, lower);
90
+ const upperInUnd = toUnderlyingSigned(ctx.toUnderlying, leg.tokenOut, upper);
91
+ if (lowerInUnd === void 0 || upperInUnd === void 0) return;
92
+ lowerLossUnd += lowerInUnd - (lower > 0n ? 1n : 0n);
93
+ upperLossUnd += upperInUnd + (upper < 0n ? 1n : 0n);
84
94
  expectedUnd += expectedInUnd;
85
95
  lossUnd += loss;
86
96
  }
87
- if (expectedUnd <= 0n) return;
97
+ if (lowerLossUnd <= 0n && upperLossUnd >= 0n) lossUnd = 0n;
88
98
  return {
89
99
  ...lossRate({
90
100
  lossUnd,
@@ -55,7 +55,8 @@ interface ExecutionCost {
55
55
  interface SimulationPrices {
56
56
  /**
57
57
  * What the routed legs lost to market depth. `undefined` where nothing was
58
- * routed or nothing could be measured — never a manufactured zero.
58
+ * routed or nothing could be measured. Zero also represents a sign that
59
+ * cannot be distinguished within the routed outputs' rounding precision.
59
60
  */
60
61
  priceImpact: PathLossRate | undefined;
61
62
  /**
@@ -45,6 +45,7 @@ declare function lossRate(args: {
45
45
  declare function collectPriceImpact(probes: LegProbe[], ctx: {
46
46
  totalValue: bigint;
47
47
  netValue: bigint;
48
+ /** Oracle conversion rounded up, as in safeConvert. */
48
49
  toUnderlying: (from: Address, amount: bigint) => bigint;
49
50
  toUnderlyingAmount: (value: bigint) => TokenAmount;
50
51
  }): Promise<PathLossRate | undefined>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gearbox-protocol/sdk",
3
- "version": "17.12.0",
3
+ "version": "17.12.1",
4
4
  "description": "Gearbox SDK",
5
5
  "license": "MIT",
6
6
  "repository": {