@gearbox-protocol/sdk 16.4.1 → 16.5.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.
@@ -99,7 +99,8 @@ async function buildBorrowState(props) {
99
99
  priceImpact: await require_onchain_accounts_intents_utils_price_impact.collectPriceImpact(leg?.probe ? [leg.probe] : [], {
100
100
  totalValue: margin,
101
101
  netValue: margin - debt,
102
- toUnderlying: (from, amount) => convert(from, underlying, amount)
102
+ toUnderlying: (from, amount) => convert(from, underlying, amount),
103
+ toUnderlyingAmount: market.toUnderlyingAmount
103
104
  }),
104
105
  collateral: priced(collateralToken, collateralAmount),
105
106
  borrowed: priced(borrowToken, payout),
@@ -87,7 +87,8 @@ async function buildOpenStrategyState(props) {
87
87
  const priceImpact = await require_onchain_accounts_intents_utils_price_impact.collectPriceImpact(leg.probe ? [leg.probe] : [], {
88
88
  totalValue: margin + debt,
89
89
  netValue: margin,
90
- toUnderlying: (from, amount) => convert(from, underlying, amount)
90
+ toUnderlying: (from, amount) => convert(from, underlying, amount),
91
+ toUnderlyingAmount: market.toUnderlyingAmount
91
92
  });
92
93
  return {
93
94
  ...projection,
@@ -51,6 +51,7 @@ async function realize(steps, props) {
51
51
  * can revert.
52
52
  */
53
53
  const ledger = new require_onchain_accounts_intents_utils_ledger.OperationLedger(start);
54
+ const opening = ledger.snapshot();
54
55
  /**
55
56
  * The same walk with every routed leg counted at the amount the pathfinder
56
57
  * expects to return. Nothing is built from it: it is where the position
@@ -377,8 +378,9 @@ async function realize(steps, props) {
377
378
  }));
378
379
  const priceImpact = await require_onchain_accounts_intents_utils_price_impact.collectPriceImpact(probes, {
379
380
  totalValue: projected.totalValue,
380
- netValue: projected.totalValue - debt,
381
- toUnderlying: (from, amount) => price(from, underlying, amount)
381
+ netValue: opening.totalValue - opening.debt,
382
+ toUnderlying: (from, amount) => price(from, underlying, amount),
383
+ toUnderlyingAmount: market.toUnderlyingAmount
382
384
  });
383
385
  const executionCost = traded.priced && traded.spentUnd > 0n ? require_onchain_constants_math.PERCENTAGE_FACTOR_1KK * (traded.returnedUnd - traded.spentUnd) / traded.spentUnd : void 0;
384
386
  const state = {
@@ -86,12 +86,15 @@ async function collectPriceImpact(probes, ctx) {
86
86
  lossUnd += loss;
87
87
  }
88
88
  if (expectedUnd <= 0n) return;
89
- return lossRate({
90
- lossUnd,
91
- expectedUnd,
92
- totalValue: ctx.totalValue,
93
- netValue: ctx.netValue
94
- });
89
+ return {
90
+ ...lossRate({
91
+ lossUnd,
92
+ expectedUnd,
93
+ totalValue: ctx.totalValue,
94
+ netValue: ctx.netValue
95
+ }),
96
+ absolutePriceImpact: ctx.toUnderlyingAmount(-lossUnd)
97
+ };
95
98
  }
96
99
  //#endregion
97
100
  exports.collectPriceImpact = collectPriceImpact;
@@ -98,7 +98,8 @@ async function buildBorrowState(props) {
98
98
  priceImpact: await collectPriceImpact(leg?.probe ? [leg.probe] : [], {
99
99
  totalValue: margin,
100
100
  netValue: margin - debt,
101
- toUnderlying: (from, amount) => convert(from, underlying, amount)
101
+ toUnderlying: (from, amount) => convert(from, underlying, amount),
102
+ toUnderlyingAmount: market.toUnderlyingAmount
102
103
  }),
103
104
  collateral: priced(collateralToken, collateralAmount),
104
105
  borrowed: priced(borrowToken, payout),
@@ -86,7 +86,8 @@ async function buildOpenStrategyState(props) {
86
86
  const priceImpact = await collectPriceImpact(leg.probe ? [leg.probe] : [], {
87
87
  totalValue: margin + debt,
88
88
  netValue: margin,
89
- toUnderlying: (from, amount) => convert(from, underlying, amount)
89
+ toUnderlying: (from, amount) => convert(from, underlying, amount),
90
+ toUnderlyingAmount: market.toUnderlyingAmount
90
91
  });
91
92
  return {
92
93
  ...projection,
@@ -50,6 +50,7 @@ async function realize(steps, props) {
50
50
  * can revert.
51
51
  */
52
52
  const ledger = new OperationLedger(start);
53
+ const opening = ledger.snapshot();
53
54
  /**
54
55
  * The same walk with every routed leg counted at the amount the pathfinder
55
56
  * expects to return. Nothing is built from it: it is where the position
@@ -376,8 +377,9 @@ async function realize(steps, props) {
376
377
  }));
377
378
  const priceImpact = await collectPriceImpact(probes, {
378
379
  totalValue: projected.totalValue,
379
- netValue: projected.totalValue - debt,
380
- toUnderlying: (from, amount) => price(from, underlying, amount)
380
+ netValue: opening.totalValue - opening.debt,
381
+ toUnderlying: (from, amount) => price(from, underlying, amount),
382
+ toUnderlyingAmount: market.toUnderlyingAmount
381
383
  });
382
384
  const executionCost = traded.priced && traded.spentUnd > 0n ? PERCENTAGE_FACTOR_1KK * (traded.returnedUnd - traded.spentUnd) / traded.spentUnd : void 0;
383
385
  const state = {
@@ -85,12 +85,15 @@ async function collectPriceImpact(probes, ctx) {
85
85
  lossUnd += loss;
86
86
  }
87
87
  if (expectedUnd <= 0n) return;
88
- return lossRate({
89
- lossUnd,
90
- expectedUnd,
91
- totalValue: ctx.totalValue,
92
- netValue: ctx.netValue
93
- });
88
+ return {
89
+ ...lossRate({
90
+ lossUnd,
91
+ expectedUnd,
92
+ totalValue: ctx.totalValue,
93
+ netValue: ctx.netValue
94
+ }),
95
+ absolutePriceImpact: ctx.toUnderlyingAmount(-lossUnd)
96
+ };
94
97
  }
95
98
  //#endregion
96
99
  export { collectPriceImpact, lossRate, startProbe };
@@ -31,6 +31,7 @@ interface PathLossRate {
31
31
  pathPriceImpact: bigint;
32
32
  netValuePriceImpact: bigint;
33
33
  totalValuePriceImpact: bigint;
34
+ absolutePriceImpact: TokenAmount;
34
35
  }
35
36
  /**
36
37
  * The two prices only a planned walk can quote, carried by every simulation
@@ -1,3 +1,5 @@
1
+ import { TokenAmount } from "../../../../model/primitives.js";
2
+ import "../../../../model/index.js";
1
3
  import { Asset } from "../../../base/types.js";
2
4
  import { IPriceOracleContract } from "../../../market/oracle/types.js";
3
5
  import { PathLossRate } from "../types.js";
@@ -33,7 +35,7 @@ declare function lossRate(args: {
33
35
  expectedUnd: bigint;
34
36
  totalValue: bigint;
35
37
  netValue: bigint;
36
- }): PathLossRate;
38
+ }): Omit<PathLossRate, "absolutePriceImpact">;
37
39
  /**
38
40
  * Folds every leg into one rate, in the underlying — the unit its bases are in.
39
41
  *
@@ -44,6 +46,7 @@ declare function collectPriceImpact(probes: LegProbe[], ctx: {
44
46
  totalValue: bigint;
45
47
  netValue: bigint;
46
48
  toUnderlying: (from: Address, amount: bigint) => bigint;
49
+ toUnderlyingAmount: (value: bigint) => TokenAmount;
47
50
  }): Promise<PathLossRate | undefined>;
48
51
  //#endregion
49
52
  export { LegProbe, collectPriceImpact, lossRate, startProbe };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gearbox-protocol/sdk",
3
- "version": "16.4.1",
3
+ "version": "16.5.0",
4
4
  "description": "Gearbox SDK",
5
5
  "license": "MIT",
6
6
  "repository": {