@gearbox-protocol/sdk 16.4.1 → 16.5.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.
- package/dist/cjs/onchain/accounts/CreditAccountsServiceV310.js +1 -0
- package/dist/cjs/onchain/accounts/intents/borrow.js +2 -1
- package/dist/cjs/onchain/accounts/intents/open-strategy.js +2 -1
- package/dist/cjs/onchain/accounts/intents/realize.js +4 -2
- package/dist/cjs/onchain/accounts/intents/utils/price-impact.js +9 -6
- package/dist/cjs/onchain/accounts/utils/midasUtils.js +7 -1
- package/dist/cjs/onchain/core/createAddressProvider.js +2 -5
- package/dist/esm/onchain/accounts/CreditAccountsServiceV310.js +1 -0
- package/dist/esm/onchain/accounts/intents/borrow.js +2 -1
- package/dist/esm/onchain/accounts/intents/open-strategy.js +2 -1
- package/dist/esm/onchain/accounts/intents/realize.js +4 -2
- package/dist/esm/onchain/accounts/intents/utils/price-impact.js +9 -6
- package/dist/esm/onchain/accounts/utils/midasUtils.js +8 -2
- package/dist/esm/onchain/core/createAddressProvider.js +2 -5
- package/dist/types/onchain/accounts/intents/types.d.ts +1 -0
- package/dist/types/onchain/accounts/intents/utils/price-impact.d.ts +4 -1
- package/dist/types/onchain/accounts/utils/midasUtils.d.ts +2 -1
- package/package.json +1 -1
|
@@ -213,6 +213,7 @@ var CreditAccountsServiceV310 = class extends require_onchain_base_SDKConstruct.
|
|
|
213
213
|
}),
|
|
214
214
|
...callsAfter ?? []
|
|
215
215
|
];
|
|
216
|
+
calls = await this.#prependMidasReceiveGreenlist(cm.address, calls);
|
|
216
217
|
calls = await this.prependPriceUpdates(cm.address, calls);
|
|
217
218
|
const tx = reopenCreditAccount ? cmSuite.multicallTx(reopenCreditAccount, calls, rwaOptions) : cmSuite.openCreditAccountTx(to, calls, referralCode, rwaOptions);
|
|
218
219
|
tx.value = ethAmount.toString(10);
|
|
@@ -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:
|
|
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
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
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;
|
|
@@ -4,6 +4,7 @@ let viem = require("viem");
|
|
|
4
4
|
//#region src/onchain/accounts/utils/midasUtils.ts
|
|
5
5
|
const ADAPTER_MIDAS_GATEWAY = "ADAPTER::MIDAS_GATEWAY";
|
|
6
6
|
const ADAPTER_MIDAS_ISSUANCE_VAULT = "ADAPTER::MIDAS_ISSUANCE_VAULT";
|
|
7
|
+
const MGLOBAL_MTOKEN = (0, viem.getAddress)("0x7433806912Eae67919e66aea853d46Fa0aef98A8");
|
|
7
8
|
/**
|
|
8
9
|
* Permissionless gateways have no greenlist and reject `receiveGreenlist`,
|
|
9
10
|
* see MidasMode in integrations-v3
|
|
@@ -27,7 +28,8 @@ const receiveGreenlistCallData = (0, viem.encodeFunctionData)({
|
|
|
27
28
|
*
|
|
28
29
|
* Returns `calls` unchanged when nothing has to be greenlisted: no issuance
|
|
29
30
|
* vault is called, its mToken has no gateway adapter on this credit manager,
|
|
30
|
-
* the gateway is permissionless,
|
|
31
|
+
* the gateway is permissionless, the call is already there, or the mToken is
|
|
32
|
+
* mGLOBAL (temporary: its gateway cannot grant the greenlist).
|
|
31
33
|
*/
|
|
32
34
|
async function prependMidasReceiveGreenlist(props) {
|
|
33
35
|
const { cm, client, calls, logger } = props;
|
|
@@ -37,6 +39,10 @@ async function prependMidasReceiveGreenlist(props) {
|
|
|
37
39
|
if (gatewayByMToken.size === 0 || calledVaults.length === 0) return calls;
|
|
38
40
|
const gateways = new require_onchain_utils_AddressMap.AddressMap();
|
|
39
41
|
for (const vault of calledVaults) {
|
|
42
|
+
if ((0, viem.isAddressEqual)(vault.mToken, MGLOBAL_MTOKEN)) {
|
|
43
|
+
logger?.debug(`midas: skipping receiveGreenlist for mGLOBAL ${vault.mToken}`);
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
40
46
|
const gateway = gatewayByMToken.get(vault.mToken);
|
|
41
47
|
if (!gateway) logger?.debug(`midas: no gateway adapter for mToken ${vault.mToken} minted by ${vault.address} on ${cm.name}, cannot greenlist the credit account`);
|
|
42
48
|
else if (calls.some((call) => (0, viem.isAddressEqual)(call.target, gateway.address) && call.callData === receiveGreenlistCallData)) logger?.debug(`midas: gateway adapter ${gateway.address} already receives the greenlist`);
|
|
@@ -1,15 +1,12 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
2
|
const require_abi_iVersion = require("../../abi/iVersion.js");
|
|
3
|
-
|
|
3
|
+
require("../constants/address-provider.js");
|
|
4
4
|
const require_onchain_constants_versions = require("../constants/versions.js");
|
|
5
5
|
require("../constants/index.js");
|
|
6
6
|
const require_onchain_utils_hex = require("../utils/hex.js");
|
|
7
7
|
const require_onchain_core_AddressProviderV310Contract = require("./AddressProviderV310Contract.js");
|
|
8
8
|
//#region src/onchain/core/createAddressProvider.ts
|
|
9
|
-
const OVERRIDE_ADDRESSES = {
|
|
10
|
-
[require_onchain_constants_address_provider.AP_PRICE_FEED_COMPRESSOR]: { 311: "0x1fA2637B9fab0CD14290A7EE908DDc9688a15120" },
|
|
11
|
-
[require_onchain_constants_address_provider.AP_MARKET_COMPRESSOR]: { 311: "0x0C27F242f6e9F2A9AD3261bE6e439De3B948bcA2" }
|
|
12
|
-
} };
|
|
9
|
+
const OVERRIDE_ADDRESSES = {};
|
|
13
10
|
async function createAddressProvider(sdk, address) {
|
|
14
11
|
let v;
|
|
15
12
|
if (require_onchain_utils_hex.hexEq(address, "0xF7f0a609BfAb9a0A98786951ef10e5FE26cC1E38")) v = 310n;
|
|
@@ -212,6 +212,7 @@ var CreditAccountsServiceV310 = class extends SDKConstruct {
|
|
|
212
212
|
}),
|
|
213
213
|
...callsAfter ?? []
|
|
214
214
|
];
|
|
215
|
+
calls = await this.#prependMidasReceiveGreenlist(cm.address, calls);
|
|
215
216
|
calls = await this.prependPriceUpdates(cm.address, calls);
|
|
216
217
|
const tx = reopenCreditAccount ? cmSuite.multicallTx(reopenCreditAccount, calls, rwaOptions) : cmSuite.openCreditAccountTx(to, calls, referralCode, rwaOptions);
|
|
217
218
|
tx.value = ethAmount.toString(10);
|
|
@@ -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:
|
|
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
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
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 };
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { AddressMap } from "../../utils/AddressMap.js";
|
|
2
|
-
import { encodeFunctionData, isAddressEqual, parseAbi } from "viem";
|
|
2
|
+
import { encodeFunctionData, getAddress, isAddressEqual, parseAbi } from "viem";
|
|
3
3
|
//#region src/onchain/accounts/utils/midasUtils.ts
|
|
4
4
|
const ADAPTER_MIDAS_GATEWAY = "ADAPTER::MIDAS_GATEWAY";
|
|
5
5
|
const ADAPTER_MIDAS_ISSUANCE_VAULT = "ADAPTER::MIDAS_ISSUANCE_VAULT";
|
|
6
|
+
const MGLOBAL_MTOKEN = getAddress("0x7433806912Eae67919e66aea853d46Fa0aef98A8");
|
|
6
7
|
/**
|
|
7
8
|
* Permissionless gateways have no greenlist and reject `receiveGreenlist`,
|
|
8
9
|
* see MidasMode in integrations-v3
|
|
@@ -26,7 +27,8 @@ const receiveGreenlistCallData = encodeFunctionData({
|
|
|
26
27
|
*
|
|
27
28
|
* Returns `calls` unchanged when nothing has to be greenlisted: no issuance
|
|
28
29
|
* vault is called, its mToken has no gateway adapter on this credit manager,
|
|
29
|
-
* the gateway is permissionless,
|
|
30
|
+
* the gateway is permissionless, the call is already there, or the mToken is
|
|
31
|
+
* mGLOBAL (temporary: its gateway cannot grant the greenlist).
|
|
30
32
|
*/
|
|
31
33
|
async function prependMidasReceiveGreenlist(props) {
|
|
32
34
|
const { cm, client, calls, logger } = props;
|
|
@@ -36,6 +38,10 @@ async function prependMidasReceiveGreenlist(props) {
|
|
|
36
38
|
if (gatewayByMToken.size === 0 || calledVaults.length === 0) return calls;
|
|
37
39
|
const gateways = new AddressMap();
|
|
38
40
|
for (const vault of calledVaults) {
|
|
41
|
+
if (isAddressEqual(vault.mToken, MGLOBAL_MTOKEN)) {
|
|
42
|
+
logger?.debug(`midas: skipping receiveGreenlist for mGLOBAL ${vault.mToken}`);
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
39
45
|
const gateway = gatewayByMToken.get(vault.mToken);
|
|
40
46
|
if (!gateway) logger?.debug(`midas: no gateway adapter for mToken ${vault.mToken} minted by ${vault.address} on ${cm.name}, cannot greenlist the credit account`);
|
|
41
47
|
else if (calls.some((call) => isAddressEqual(call.target, gateway.address) && call.callData === receiveGreenlistCallData)) logger?.debug(`midas: gateway adapter ${gateway.address} already receives the greenlist`);
|
|
@@ -1,14 +1,11 @@
|
|
|
1
1
|
import { iVersionAbi } from "../../abi/iVersion.js";
|
|
2
|
-
import
|
|
2
|
+
import "../constants/address-provider.js";
|
|
3
3
|
import { isV310 } from "../constants/versions.js";
|
|
4
4
|
import "../constants/index.js";
|
|
5
5
|
import { hexEq } from "../utils/hex.js";
|
|
6
6
|
import { AddressProviderV310Contract } from "./AddressProviderV310Contract.js";
|
|
7
7
|
//#region src/onchain/core/createAddressProvider.ts
|
|
8
|
-
const OVERRIDE_ADDRESSES = {
|
|
9
|
-
[AP_PRICE_FEED_COMPRESSOR]: { 311: "0x1fA2637B9fab0CD14290A7EE908DDc9688a15120" },
|
|
10
|
-
[AP_MARKET_COMPRESSOR]: { 311: "0x0C27F242f6e9F2A9AD3261bE6e439De3B948bcA2" }
|
|
11
|
-
} };
|
|
8
|
+
const OVERRIDE_ADDRESSES = {};
|
|
12
9
|
async function createAddressProvider(sdk, address) {
|
|
13
10
|
let v;
|
|
14
11
|
if (hexEq(address, "0xF7f0a609BfAb9a0A98786951ef10e5FE26cC1E38")) v = 310n;
|
|
@@ -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 };
|
|
@@ -31,7 +31,8 @@ interface PrependMidasReceiveGreenlistProps {
|
|
|
31
31
|
*
|
|
32
32
|
* Returns `calls` unchanged when nothing has to be greenlisted: no issuance
|
|
33
33
|
* vault is called, its mToken has no gateway adapter on this credit manager,
|
|
34
|
-
* the gateway is permissionless,
|
|
34
|
+
* the gateway is permissionless, the call is already there, or the mToken is
|
|
35
|
+
* mGLOBAL (temporary: its gateway cannot grant the greenlist).
|
|
35
36
|
*/
|
|
36
37
|
declare function prependMidasReceiveGreenlist(props: PrependMidasReceiveGreenlistProps): Promise<MultiCall[]>;
|
|
37
38
|
//#endregion
|