@gearbox-protocol/sdk 18.4.0 → 18.4.1-aave.2
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/model/index.js +1 -4
- package/dist/cjs/offchain/GearboxAPI.js +0 -7
- package/dist/cjs/offchain/index.js +0 -3
- package/dist/cjs/onchain/accounts/bots/AccountBotsService.js +29 -0
- package/dist/cjs/onchain/accounts/bots/PeripheryCompressorV310Contract.js +2 -1
- package/dist/cjs/onchain/accounts/intents/index.js +1 -0
- package/dist/cjs/onchain/accounts/intents/operations.js +21 -0
- package/dist/cjs/onchain/accounts/intents/plan.js +33 -0
- package/dist/cjs/onchain/accounts/intents/realize.js +44 -0
- package/dist/cjs/onchain/accounts/intents/testing/market.js +1 -0
- package/dist/cjs/onchain/accounts/intents/testing/sdk-mock.js +2 -1
- package/dist/cjs/onchain/accounts/intents/utils/ledger.js +8 -0
- package/dist/cjs/onchain/chain/chains.js +4 -0
- package/dist/cjs/onchain/index.js +16 -0
- package/dist/cjs/onchain/market/ZapperRegister.js +1 -1
- package/dist/cjs/onchain/market/adapters/abi/aave/iAaveHorizonBridge.js +94 -0
- package/dist/cjs/onchain/market/adapters/abi/aave/iAaveOracle.js +20 -0
- package/dist/cjs/onchain/market/adapters/abi/aave/iAaveV3Pool.js +278 -0
- package/dist/cjs/onchain/market/adapters/abi/aave/iPoolAddressesProvider.js +11 -0
- package/dist/cjs/onchain/market/adapters/abi/aave/index.js +9 -0
- package/dist/cjs/onchain/market/adapters/abi/actionAbi.js +1 -0
- package/dist/cjs/onchain/market/adapters/abi/adapters/iAaveHorizonBridgeAdapter.js +224 -0
- package/dist/cjs/onchain/market/adapters/abi/adapters/iAaveV3PoolAdapter.js +177 -0
- package/dist/cjs/onchain/market/adapters/abi/adapters/index.js +4 -0
- package/dist/cjs/onchain/market/adapters/abi/conctructorAbi.js +2 -0
- package/dist/cjs/onchain/market/adapters/abi/index.js +13 -0
- package/dist/cjs/onchain/market/adapters/contracts/AaveHorizonBridgeAdapterContract.js +171 -0
- package/dist/cjs/onchain/market/adapters/contracts/AaveV3PoolAdapterContract.js +199 -0
- package/dist/cjs/onchain/market/adapters/contracts/AbstractAdapter.js +8 -0
- package/dist/cjs/onchain/market/adapters/contracts/index.js +4 -0
- package/dist/cjs/onchain/market/adapters/createAdapter.js +4 -0
- package/dist/cjs/onchain/market/adapters/index.js +16 -0
- package/dist/cjs/onchain/market/adapters/types.js +2 -0
- package/dist/cjs/onchain/market/index.js +16 -0
- package/dist/cjs/onchain/positions/projectAavePosition.js +175 -0
- package/dist/cjs/onchain/preview/preview/loadAdapterReplayState.js +17 -0
- package/dist/cjs/onchain/preview/preview/previewOperation.js +2 -0
- package/dist/cjs/sdk/GearboxSDK.js +0 -9
- package/dist/cjs/sdk/prepare/PrepareApi.js +56 -0
- package/dist/cjs/sdk/preview/PreviewNamespace.js +42 -0
- package/dist/esm/model/index.js +2 -3
- package/dist/esm/offchain/GearboxAPI.js +0 -7
- package/dist/esm/offchain/index.js +1 -3
- package/dist/esm/onchain/accounts/bots/AccountBotsService.js +29 -0
- package/dist/esm/onchain/accounts/bots/PeripheryCompressorV310Contract.js +1 -1
- package/dist/esm/onchain/accounts/intents/index.js +2 -1
- package/dist/esm/onchain/accounts/intents/operations.js +20 -1
- package/dist/esm/onchain/accounts/intents/plan.js +33 -1
- package/dist/esm/onchain/accounts/intents/realize.js +46 -2
- package/dist/esm/onchain/accounts/intents/testing/market.js +1 -0
- package/dist/esm/onchain/accounts/intents/testing/sdk-mock.js +2 -1
- package/dist/esm/onchain/accounts/intents/utils/ledger.js +8 -0
- package/dist/esm/onchain/chain/chains.js +4 -0
- package/dist/esm/onchain/index.js +9 -1
- package/dist/esm/onchain/market/ZapperRegister.js +1 -1
- package/dist/esm/onchain/market/adapters/abi/aave/iAaveHorizonBridge.js +93 -0
- package/dist/esm/onchain/market/adapters/abi/aave/iAaveOracle.js +19 -0
- package/dist/esm/onchain/market/adapters/abi/aave/iAaveV3Pool.js +277 -0
- package/dist/esm/onchain/market/adapters/abi/aave/iPoolAddressesProvider.js +10 -0
- package/dist/esm/onchain/market/adapters/abi/aave/index.js +5 -0
- package/dist/esm/onchain/market/adapters/abi/actionAbi.js +1 -0
- package/dist/esm/onchain/market/adapters/abi/adapters/iAaveHorizonBridgeAdapter.js +223 -0
- package/dist/esm/onchain/market/adapters/abi/adapters/iAaveV3PoolAdapter.js +176 -0
- package/dist/esm/onchain/market/adapters/abi/adapters/index.js +3 -1
- package/dist/esm/onchain/market/adapters/abi/conctructorAbi.js +2 -0
- package/dist/esm/onchain/market/adapters/abi/index.js +8 -1
- package/dist/esm/onchain/market/adapters/contracts/AaveHorizonBridgeAdapterContract.js +170 -0
- package/dist/esm/onchain/market/adapters/contracts/AaveV3PoolAdapterContract.js +198 -0
- package/dist/esm/onchain/market/adapters/contracts/AbstractAdapter.js +8 -0
- package/dist/esm/onchain/market/adapters/contracts/index.js +3 -1
- package/dist/esm/onchain/market/adapters/createAdapter.js +4 -0
- package/dist/esm/onchain/market/adapters/index.js +9 -1
- package/dist/esm/onchain/market/adapters/types.js +2 -0
- package/dist/esm/onchain/market/index.js +9 -1
- package/dist/esm/onchain/positions/projectAavePosition.js +174 -0
- package/dist/esm/onchain/preview/preview/loadAdapterReplayState.js +16 -0
- package/dist/esm/onchain/preview/preview/previewOperation.js +2 -0
- package/dist/esm/sdk/GearboxSDK.js +0 -9
- package/dist/esm/sdk/prepare/PrepareApi.js +57 -1
- package/dist/esm/sdk/preview/PreviewNamespace.js +42 -0
- package/dist/types/model/aave.d.ts +34 -0
- package/dist/types/model/analytics.d.ts +2 -2
- package/dist/types/model/index.d.ts +6 -7
- package/dist/types/model/opportunities.d.ts +1 -1
- package/dist/types/model/positions.d.ts +1 -1
- package/dist/types/model/previews.d.ts +15 -0
- package/dist/types/offchain/AbstractOffchainNamespace.d.ts +1 -1
- package/dist/types/offchain/GearboxAPI.d.ts +0 -6
- package/dist/types/offchain/analytics/OffchainAnalytics.d.ts +1 -1
- package/dist/types/offchain/analytics/types.d.ts +1 -1
- package/dist/types/offchain/index.d.ts +1 -3
- package/dist/types/offchain/opportunities/OffchainOpportunities.d.ts +1 -1
- package/dist/types/offchain/opportunities/types.d.ts +1 -1
- package/dist/types/offchain/positions/OffchainPositions.d.ts +1 -1
- package/dist/types/offchain/positions/types.d.ts +1 -1
- package/dist/types/onchain/accounts/bots/AccountBotsService.d.ts +11 -1
- package/dist/types/onchain/accounts/bots/index.d.ts +2 -2
- package/dist/types/onchain/accounts/bots/types.d.ts +7 -1
- package/dist/types/onchain/accounts/index.d.ts +2 -2
- package/dist/types/onchain/accounts/intents/operations.d.ts +43 -1
- package/dist/types/onchain/accounts/intents/plan.d.ts +26 -1
- package/dist/types/onchain/accounts/intents/testing/market.d.ts +4 -0
- package/dist/types/onchain/accounts/intents/testing/sdk-mock.d.ts +4 -0
- package/dist/types/onchain/accounts/intents/types.d.ts +20 -1
- package/dist/types/onchain/index.d.ts +10 -2
- package/dist/types/onchain/market/adapters/abi/aave/iAaveHorizonBridge.d.ts +83 -0
- package/dist/types/onchain/market/adapters/abi/aave/iAaveOracle.d.ts +22 -0
- package/dist/types/onchain/market/adapters/abi/aave/iAaveV3Pool.d.ts +229 -0
- package/dist/types/onchain/market/adapters/abi/aave/iPoolAddressesProvider.d.ts +11 -0
- package/dist/types/onchain/market/adapters/abi/aave/index.d.ts +5 -0
- package/dist/types/onchain/market/adapters/abi/adapters/iAaveHorizonBridgeAdapter.d.ts +207 -0
- package/dist/types/onchain/market/adapters/abi/adapters/iAaveV3PoolAdapter.d.ts +172 -0
- package/dist/types/onchain/market/adapters/abi/adapters/index.d.ts +3 -1
- package/dist/types/onchain/market/adapters/abi/index.d.ts +8 -1
- package/dist/types/onchain/market/adapters/contracts/AaveHorizonBridgeAdapterContract.d.ts +341 -0
- package/dist/types/onchain/market/adapters/contracts/AaveV3PoolAdapterContract.d.ts +450 -0
- package/dist/types/onchain/market/adapters/contracts/AbstractAdapter.d.ts +8 -0
- package/dist/types/onchain/market/adapters/contracts/index.d.ts +3 -1
- package/dist/types/onchain/market/adapters/index.d.ts +9 -1
- package/dist/types/onchain/market/adapters/types.d.ts +3 -1
- package/dist/types/onchain/market/index.d.ts +9 -1
- package/dist/types/onchain/positions/projectAavePosition.d.ts +45 -0
- package/dist/types/onchain/preview/preview/loadAdapterReplayState.d.ts +13 -0
- package/dist/types/sdk/GearboxSDK.d.ts +1 -8
- package/dist/types/sdk/analytics/AnalyticsNamespace.d.ts +1 -1
- package/dist/types/sdk/analytics/types.d.ts +1 -1
- package/dist/types/sdk/index.d.ts +3 -3
- package/dist/types/sdk/opportunities/OpportunitiesNamespace.d.ts +1 -1
- package/dist/types/sdk/opportunities/types.d.ts +1 -1
- package/dist/types/sdk/positions/PositionsNamespace.d.ts +1 -1
- package/dist/types/sdk/positions/types.d.ts +1 -1
- package/dist/types/sdk/prepare/PrepareApi.d.ts +11 -3
- package/dist/types/sdk/prepare/index.d.ts +2 -2
- package/dist/types/sdk/prepare/types.d.ts +83 -1
- package/dist/types/sdk/preview/PreviewNamespace.d.ts +5 -1
- package/dist/types/sdk/preview/types.d.ts +12 -1
- package/dist/types/sdk/types.d.ts +0 -20
- package/package.json +1 -1
- package/dist/cjs/model/notices.schema.js +0 -25
- package/dist/cjs/offchain/notices/OffchainNotices.js +0 -27
- package/dist/cjs/offchain/notices/index.js +0 -3
- package/dist/esm/model/notices.schema.js +0 -23
- package/dist/esm/offchain/notices/OffchainNotices.js +0 -26
- package/dist/esm/offchain/notices/index.js +0 -1
- package/dist/types/model/notices.d.ts +0 -28
- package/dist/types/model/notices.schema.d.ts +0 -22
- package/dist/types/offchain/notices/OffchainNotices.d.ts +0 -18
- package/dist/types/offchain/notices/index.d.ts +0 -2
- /package/dist/cjs/model/{notices.js → aave.js} +0 -0
- /package/dist/esm/model/{notices.js → aave.js} +0 -0
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_onchain_constants_math = require("../constants/math.js");
|
|
3
|
+
const require_onchain_market_adapters_abi_aave_iAaveOracle = require("../market/adapters/abi/aave/iAaveOracle.js");
|
|
4
|
+
const require_onchain_market_adapters_abi_aave_iAaveV3Pool = require("../market/adapters/abi/aave/iAaveV3Pool.js");
|
|
5
|
+
const require_onchain_market_adapters_abi_aave_iPoolAddressesProvider = require("../market/adapters/abi/aave/iPoolAddressesProvider.js");
|
|
6
|
+
require("../market/adapters/abi/aave/index.js");
|
|
7
|
+
const require_onchain_market_adapters_contracts_AaveHorizonBridgeAdapterContract = require("../market/adapters/contracts/AaveHorizonBridgeAdapterContract.js");
|
|
8
|
+
const require_onchain_market_adapters_contracts_AaveV3PoolAdapterContract = require("../market/adapters/contracts/AaveV3PoolAdapterContract.js");
|
|
9
|
+
const require_onchain_market_math = require("../market/math.js");
|
|
10
|
+
let viem = require("viem");
|
|
11
|
+
//#region src/onchain/positions/projectAavePosition.ts
|
|
12
|
+
/**
|
|
13
|
+
* Where an opening lands once it has moved to Aave Horizon, from the snapshot
|
|
14
|
+
* of the credit account it is built in.
|
|
15
|
+
*
|
|
16
|
+
* The finalizer bot supplies the account's RWA to the pool and borrows the
|
|
17
|
+
* Gearbox debt against it on the owner's behalf, so the Aave side is read off
|
|
18
|
+
* the pool for `owner`: the reserve's LTV and liquidation threshold (its eMode
|
|
19
|
+
* category's, where the owner is in one that lists it), the Aave oracle's
|
|
20
|
+
* prices, the variable borrow rate of the underlying, and whatever the owner
|
|
21
|
+
* already holds and owes there. `healthFactor` and `liquidationPrice` are of
|
|
22
|
+
* that whole position; the values describe this opening alone.
|
|
23
|
+
*
|
|
24
|
+
* The RWA is either on the account already, and the position is on Aave at
|
|
25
|
+
* once (the bot's move is the next block), or still being minted: then the
|
|
26
|
+
* account holds the withdrawal phantom token of a delayed operation the
|
|
27
|
+
* withdrawal compressor lists for the credit manager, the mint takes that
|
|
28
|
+
* operation's `withdrawalLength` and is assumed to come out one for one, and
|
|
29
|
+
* the Gearbox debt accrues interest at the credit manager's rate meanwhile.
|
|
30
|
+
*
|
|
31
|
+
* `undefined` when the credit manager cannot move the account to Aave: no
|
|
32
|
+
* pool adapter lists an asset of the account or the RWA it is being minted
|
|
33
|
+
* into, or no bridge adapter takes its aToken against the underlying.
|
|
34
|
+
**/
|
|
35
|
+
async function projectAavePosition(sdk, snapshot, owner) {
|
|
36
|
+
const suite = sdk.marketRegister.findCreditManager(snapshot.creditManager);
|
|
37
|
+
const market = sdk.marketRegister.findByCreditManager(snapshot.creditManager);
|
|
38
|
+
const { underlying } = market;
|
|
39
|
+
const adapters = suite.creditManager.adapters.values();
|
|
40
|
+
const poolAdapters = adapters.filter((p) => p instanceof require_onchain_market_adapters_contracts_AaveV3PoolAdapterContract.AaveV3PoolAdapterContract);
|
|
41
|
+
const compressor = sdk.withdrawalCompressor;
|
|
42
|
+
const delayed = compressor?.state ? compressor.getWithdrawableAssets(snapshot.creditManager) : [];
|
|
43
|
+
let collateral;
|
|
44
|
+
let mintDuration = 0;
|
|
45
|
+
let poolAdapter;
|
|
46
|
+
for (const a of snapshot.assets) {
|
|
47
|
+
if (a.balance <= 10n || (0, viem.isAddressEqual)(a.token, underlying)) continue;
|
|
48
|
+
const mint = delayed.find((d) => (0, viem.isAddressEqual)(d.withdrawalPhantomToken, a.token));
|
|
49
|
+
const token = mint ? mint.underlying : a.token;
|
|
50
|
+
poolAdapter = poolAdapters.find((p) => p.isAssetAllowed(token));
|
|
51
|
+
if (poolAdapter) {
|
|
52
|
+
collateral = {
|
|
53
|
+
token,
|
|
54
|
+
balance: a.balance
|
|
55
|
+
};
|
|
56
|
+
mintDuration = mint ? Number(mint.withdrawalLength) : 0;
|
|
57
|
+
break;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
if (!collateral || !poolAdapter) return;
|
|
61
|
+
const pool = poolAdapter.targetContract;
|
|
62
|
+
const [rwaReserve, debtReserve, account, eMode, provider] = await sdk.client.multicall({
|
|
63
|
+
contracts: [
|
|
64
|
+
{
|
|
65
|
+
address: pool,
|
|
66
|
+
abi: require_onchain_market_adapters_abi_aave_iAaveV3Pool.iAaveV3PoolAbi,
|
|
67
|
+
functionName: "getReserveData",
|
|
68
|
+
args: [collateral.token]
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
address: pool,
|
|
72
|
+
abi: require_onchain_market_adapters_abi_aave_iAaveV3Pool.iAaveV3PoolAbi,
|
|
73
|
+
functionName: "getReserveData",
|
|
74
|
+
args: [underlying]
|
|
75
|
+
},
|
|
76
|
+
{
|
|
77
|
+
address: pool,
|
|
78
|
+
abi: require_onchain_market_adapters_abi_aave_iAaveV3Pool.iAaveV3PoolAbi,
|
|
79
|
+
functionName: "getUserAccountData",
|
|
80
|
+
args: [owner]
|
|
81
|
+
},
|
|
82
|
+
{
|
|
83
|
+
address: pool,
|
|
84
|
+
abi: require_onchain_market_adapters_abi_aave_iAaveV3Pool.iAaveV3PoolAbi,
|
|
85
|
+
functionName: "getUserEMode",
|
|
86
|
+
args: [owner]
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
address: pool,
|
|
90
|
+
abi: require_onchain_market_adapters_abi_aave_iAaveV3Pool.iAaveV3PoolAbi,
|
|
91
|
+
functionName: "ADDRESSES_PROVIDER"
|
|
92
|
+
}
|
|
93
|
+
],
|
|
94
|
+
allowFailure: false,
|
|
95
|
+
batchSize: 0
|
|
96
|
+
});
|
|
97
|
+
if (!adapters.some((b) => b instanceof require_onchain_market_adapters_contracts_AaveHorizonBridgeAdapterContract.AaveHorizonBridgeAdapterContract && b.isATokenAllowed(rwaReserve.aTokenAddress) && b.isDebtTokenAllowed(underlying))) return;
|
|
98
|
+
const oracle = await sdk.client.readContract({
|
|
99
|
+
address: provider,
|
|
100
|
+
abi: require_onchain_market_adapters_abi_aave_iPoolAddressesProvider.iPoolAddressesProviderAbi,
|
|
101
|
+
functionName: "getPriceOracle"
|
|
102
|
+
});
|
|
103
|
+
const [[rwaPrice, underlyingPrice], categoryParams] = await Promise.all([sdk.client.readContract({
|
|
104
|
+
address: oracle,
|
|
105
|
+
abi: require_onchain_market_adapters_abi_aave_iAaveOracle.iAaveOracleAbi,
|
|
106
|
+
functionName: "getAssetsPrices",
|
|
107
|
+
args: [[collateral.token, underlying]]
|
|
108
|
+
}), eMode > 0n ? sdk.client.multicall({
|
|
109
|
+
contracts: [{
|
|
110
|
+
address: pool,
|
|
111
|
+
abi: require_onchain_market_adapters_abi_aave_iAaveV3Pool.iAaveV3PoolAbi,
|
|
112
|
+
functionName: "getEModeCategoryCollateralConfig",
|
|
113
|
+
args: [Number(eMode)]
|
|
114
|
+
}, {
|
|
115
|
+
address: pool,
|
|
116
|
+
abi: require_onchain_market_adapters_abi_aave_iAaveV3Pool.iAaveV3PoolAbi,
|
|
117
|
+
functionName: "getEModeCategoryCollateralBitmap",
|
|
118
|
+
args: [Number(eMode)]
|
|
119
|
+
}],
|
|
120
|
+
allowFailure: false,
|
|
121
|
+
batchSize: 0
|
|
122
|
+
}) : void 0]);
|
|
123
|
+
const rwaConfig = rwaReserve.configuration.data;
|
|
124
|
+
let ltv = rwaConfig & 65535n;
|
|
125
|
+
let liquidationThreshold = rwaConfig >> 16n & 65535n;
|
|
126
|
+
const [category, bitmap] = categoryParams ?? [];
|
|
127
|
+
if (category && bitmap && bitmap >> BigInt(rwaReserve.id) & 1n) {
|
|
128
|
+
ltv = BigInt(category.ltv);
|
|
129
|
+
liquidationThreshold = BigInt(category.liquidationThreshold);
|
|
130
|
+
}
|
|
131
|
+
const rwaScale = 10n ** (rwaConfig >> 48n & 255n);
|
|
132
|
+
const underlyingScale = 10n ** (debtReserve.configuration.data >> 48n & 255n);
|
|
133
|
+
const collateralBase = account[0];
|
|
134
|
+
const debtBase = account[1];
|
|
135
|
+
const currentLiquidationThreshold = account[3];
|
|
136
|
+
const currentLtv = account[4];
|
|
137
|
+
const gearboxPool = { availableLiquidityChange: -snapshot.totalDebt };
|
|
138
|
+
const gearboxRate = sdk.positions.metrics(snapshot, gearboxPool).borrowRate.totalOnDebt;
|
|
139
|
+
const interest = snapshot.totalDebt * BigInt(gearboxRate) * BigInt(mintDuration) / (require_onchain_constants_math.PERCENTAGE_FACTOR * BigInt(require_onchain_constants_math.SECONDS_PER_YEAR));
|
|
140
|
+
const totalDebt = snapshot.totalDebt + interest;
|
|
141
|
+
const suppliedBase = collateral.balance * rwaPrice / rwaScale;
|
|
142
|
+
const owedBase = debtBase + ceilDiv(totalDebt * underlyingPrice, underlyingScale);
|
|
143
|
+
const weightedLiquidation = collateralBase * currentLiquidationThreshold + suppliedBase * liquidationThreshold;
|
|
144
|
+
const borrowableBase = (collateralBase * currentLtv + suppliedBase * ltv) / require_onchain_constants_math.PERCENTAGE_FACTOR;
|
|
145
|
+
const totalValue = collateral.balance * rwaPrice * underlyingScale / (rwaScale * underlyingPrice);
|
|
146
|
+
const rate = Number(debtReserve.currentVariableBorrowRate * require_onchain_constants_math.PERCENTAGE_FACTOR / require_onchain_constants_math.RAY);
|
|
147
|
+
const uncovered = owedBase * require_onchain_constants_math.PERCENTAGE_FACTOR - collateralBase * currentLiquidationThreshold;
|
|
148
|
+
const liquidationPrice = liquidationThreshold === 0n ? null : uncovered <= 0n ? 0n : uncovered * rwaScale * require_onchain_constants_math.PRICE_DECIMALS / (collateral.balance * liquidationThreshold * underlyingPrice);
|
|
149
|
+
return {
|
|
150
|
+
aave: {
|
|
151
|
+
totalValue: market.toUnderlyingAmount(totalValue),
|
|
152
|
+
totalDebt: market.toUnderlyingAmount(totalDebt),
|
|
153
|
+
netValue: market.toUnderlyingAmount(totalValue - totalDebt),
|
|
154
|
+
assets: [market.priceOracle.toTokenAmount(collateral.token, collateral.balance)],
|
|
155
|
+
healthFactor: Number(owedBase > 0n ? weightedLiquidation / owedBase : require_onchain_constants_math.MAX_UINT16),
|
|
156
|
+
liquidationPrice,
|
|
157
|
+
borrowRate: {
|
|
158
|
+
total: totalValue > 0n ? Number(BigInt(rate) * totalDebt / totalValue) : 0,
|
|
159
|
+
totalOnDebt: rate,
|
|
160
|
+
base: rate,
|
|
161
|
+
quotas: []
|
|
162
|
+
},
|
|
163
|
+
leverage: require_onchain_market_math.calcPositionLeverage(totalValue, totalDebt),
|
|
164
|
+
mintPeriodInterest: market.toUnderlyingAmount(interest),
|
|
165
|
+
mintDuration,
|
|
166
|
+
estLiveAt: Number(sdk.timestamp) + mintDuration
|
|
167
|
+
},
|
|
168
|
+
maxDebt: borrowableBase > debtBase ? (borrowableBase - debtBase) * underlyingScale / underlyingPrice : 0n
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
function ceilDiv(a, b) {
|
|
172
|
+
return (a + b - 1n) / b;
|
|
173
|
+
}
|
|
174
|
+
//#endregion
|
|
175
|
+
exports.projectAavePosition = projectAavePosition;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_onchain_market_adapters_contracts_AbstractAdapter = require("../../market/adapters/contracts/AbstractAdapter.js");
|
|
3
|
+
//#region src/onchain/preview/preview/loadAdapterReplayState.ts
|
|
4
|
+
/**
|
|
5
|
+
* Has every adapter called in `multicall` read what it needs to replay its
|
|
6
|
+
* call, see {@link AbstractAdapterContract.loadReplayState}. Runs before
|
|
7
|
+
* {@link replayInnerOperations}, which cannot wait on the chain.
|
|
8
|
+
*/
|
|
9
|
+
async function loadAdapterReplayState(sdk, multicall) {
|
|
10
|
+
await Promise.all(multicall.map((op) => {
|
|
11
|
+
if (op.operation !== "Execute") return;
|
|
12
|
+
const adapter = sdk.getContract(op.adapter);
|
|
13
|
+
return adapter instanceof require_onchain_market_adapters_contracts_AbstractAdapter.AbstractAdapterContract ? adapter.loadReplayState(op.calldata) : void 0;
|
|
14
|
+
}));
|
|
15
|
+
}
|
|
16
|
+
//#endregion
|
|
17
|
+
exports.loadAdapterReplayState = loadAdapterReplayState;
|
|
@@ -12,6 +12,7 @@ const require_onchain_preview_preview_detectDelayedOperation = require("./detect
|
|
|
12
12
|
const require_onchain_preview_preview_estimateClaimableAt = require("./estimateClaimableAt.js");
|
|
13
13
|
const require_onchain_preview_preview_previewAdjustStrategyPosition = require("./previewAdjustStrategyPosition.js");
|
|
14
14
|
const require_onchain_preview_preview_previewExitOrRepayStrategyPosition = require("./previewExitOrRepayStrategyPosition.js");
|
|
15
|
+
const require_onchain_preview_preview_loadAdapterReplayState = require("./loadAdapterReplayState.js");
|
|
15
16
|
const require_onchain_preview_preview_previewOpenStrategyPosition = require("./previewOpenStrategyPosition.js");
|
|
16
17
|
const require_onchain_preview_preview_previewPoolPositionOperation = require("./previewPoolPositionOperation.js");
|
|
17
18
|
const require_onchain_preview_preview_replayMulticall = require("./replayMulticall.js");
|
|
@@ -34,6 +35,7 @@ async function previewOperation(sdk, input, options, creditAccount) {
|
|
|
34
35
|
if (!parsed.ok) return parsed;
|
|
35
36
|
const operation = parsed.data;
|
|
36
37
|
if (require_onchain_preview_parse_types.isPoolOperation(operation)) return require_onchain_preview_preview_previewPoolPositionOperation.previewPoolPositionOperation(sdk, operation, options);
|
|
38
|
+
if ("multicall" in operation) await require_onchain_preview_preview_loadAdapterReplayState.loadAdapterReplayState(sdk, operation.multicall);
|
|
37
39
|
if (operation.operation === "OpenCreditAccount" || operation.operation === "RWAOpenCreditAccount") {
|
|
38
40
|
const replayed = require_onchain_preview_preview_replayMulticall.replayMulticall(sdk, operation);
|
|
39
41
|
if (!replayed.ok) return replayed;
|
|
@@ -81,13 +81,6 @@ var GearboxSDK = class {
|
|
|
81
81
|
* Namespace for claimable rewards. Absent in `offchain` mode.
|
|
82
82
|
**/
|
|
83
83
|
rewards;
|
|
84
|
-
/**
|
|
85
|
-
* The banners the backend attaches to a pool opportunity or a strategy
|
|
86
|
-
* position, see {@link Notice}. Top-level because the subject is either
|
|
87
|
-
* kind of entity, so neither namespace owns it. Backend-only, hence gated
|
|
88
|
-
* by mode like every other backend read: absent in `onchain` mode.
|
|
89
|
-
**/
|
|
90
|
-
notices;
|
|
91
84
|
#attachOptions;
|
|
92
85
|
#onchain;
|
|
93
86
|
#offchain;
|
|
@@ -147,8 +140,6 @@ var GearboxSDK = class {
|
|
|
147
140
|
this.liquidations = this.#onchain ? new require_sdk_liquidations_LiquidationsNamespace.LiquidationsNamespace(this.#onchain, namespaceOptions) : void 0;
|
|
148
141
|
this.preview = this.#onchain ? new require_sdk_preview_PreviewNamespace.PreviewNamespace(this.#onchain, namespaceOptions) : void 0;
|
|
149
142
|
this.rewards = this.#onchain ? new require_sdk_rewards_RewardsNamespace.RewardsNamespace(this.#onchain, rewards, namespaceOptions) : void 0;
|
|
150
|
-
const backend = this.#offchain;
|
|
151
|
-
this.notices = backend ? (subject) => backend.notices.list(subject) : void 0;
|
|
152
143
|
}
|
|
153
144
|
/**
|
|
154
145
|
* Attaches the on-chain SDK when this instance owns one; a no-op in `offchain`
|
|
@@ -3,6 +3,7 @@ const require_onchain_constants_math = require("../../onchain/constants/math.js"
|
|
|
3
3
|
const require_onchain_utils_hex = require("../../onchain/utils/hex.js");
|
|
4
4
|
const require_onchain_base_MultichainConstruct = require("../../onchain/base/MultichainConstruct.js");
|
|
5
5
|
require("../../onchain/market/math.js");
|
|
6
|
+
const require_model_errors_operation_errors = require("../../model/errors/operation-errors.js");
|
|
6
7
|
const require_model_errors_prepare_errors = require("../../model/errors/prepare-errors.js");
|
|
7
8
|
const require_model_result = require("../../model/result.js");
|
|
8
9
|
require("../../model/index.js");
|
|
@@ -14,6 +15,7 @@ const require_onchain_accounts_intents_utils_credit_account_slice = require("../
|
|
|
14
15
|
const require_onchain_accounts_intents_resolveQuotaUpdate = require("../../onchain/accounts/intents/resolveQuotaUpdate.js");
|
|
15
16
|
const require_onchain_accounts_intents_index = require("../../onchain/accounts/intents/index.js");
|
|
16
17
|
require("../../onchain/index.js");
|
|
18
|
+
const require_onchain_positions_projectAavePosition = require("../../onchain/positions/projectAavePosition.js");
|
|
17
19
|
const require_sdk_prepare_withdrawable_collaterals = require("./withdrawable-collaterals.js");
|
|
18
20
|
let viem = require("viem");
|
|
19
21
|
//#region src/sdk/prepare/PrepareApi.ts
|
|
@@ -258,6 +260,51 @@ var PrepareApi = class extends require_onchain_base_MultichainConstruct.Multicha
|
|
|
258
260
|
}
|
|
259
261
|
}
|
|
260
262
|
/**
|
|
263
|
+
* {@inheritDoc IOpportunitiesPrepare.openAavePosition}
|
|
264
|
+
**/
|
|
265
|
+
async openAavePosition(strategy, params) {
|
|
266
|
+
const opened = await this.openNewStrategy(strategy, params);
|
|
267
|
+
if (!opened.ok) return opened;
|
|
268
|
+
const { state } = opened.data;
|
|
269
|
+
try {
|
|
270
|
+
const sdk = this.sdk.chain(strategy.chainId);
|
|
271
|
+
const projected = await require_onchain_positions_projectAavePosition.projectAavePosition(sdk, {
|
|
272
|
+
creditManager: state.creditManager,
|
|
273
|
+
assets: state.averageAssets.map((a) => ({
|
|
274
|
+
token: a.token.address,
|
|
275
|
+
balance: a.value
|
|
276
|
+
})),
|
|
277
|
+
quotas: state.averageQuota,
|
|
278
|
+
totalDebt: state.totalDebt.value,
|
|
279
|
+
totalValue: state.totalValue.value
|
|
280
|
+
}, params.wallet);
|
|
281
|
+
const suite = sdk.marketRegister.findCreditManager(strategy.creditManager);
|
|
282
|
+
if (!projected) {
|
|
283
|
+
const target = params.targetToken ?? suite.strategy?.targetCollateral;
|
|
284
|
+
return refused(target ? require_model_errors_prepare_errors.unsupportedCollateralToken(require_onchain_validation_helpers_token.toToken(sdk, target)) : require_model_errors_prepare_errors.noStrategyTargetCollateral(strategy.creditManager), state);
|
|
285
|
+
}
|
|
286
|
+
const { aave, maxDebt } = projected;
|
|
287
|
+
if (aave.totalDebt.value > maxDebt) {
|
|
288
|
+
const market = sdk.marketRegister.findByCreditManager(strategy.creditManager);
|
|
289
|
+
const { creditFacade } = suite;
|
|
290
|
+
return refused(require_model_errors_operation_errors.debtOutOfRange({
|
|
291
|
+
requested: aave.totalDebt,
|
|
292
|
+
minDebt: market.toUnderlyingAmount(creditFacade.minDebt),
|
|
293
|
+
maxDebt: market.toUnderlyingAmount(maxDebt)
|
|
294
|
+
}), state);
|
|
295
|
+
}
|
|
296
|
+
return require_model_result.sdkOk({
|
|
297
|
+
...opened.data,
|
|
298
|
+
state: {
|
|
299
|
+
...state,
|
|
300
|
+
aave
|
|
301
|
+
}
|
|
302
|
+
});
|
|
303
|
+
} catch (e) {
|
|
304
|
+
return refused(require_model_errors_prepare_errors.unexpectedFailure(e), state);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
261
308
|
* {@inheritDoc IOpportunitiesPrepare.borrow}
|
|
262
309
|
**/
|
|
263
310
|
async borrow(strategy, params) {
|
|
@@ -345,6 +392,15 @@ var PrepareApi = class extends require_onchain_base_MultichainConstruct.Multicha
|
|
|
345
392
|
});
|
|
346
393
|
}
|
|
347
394
|
/**
|
|
395
|
+
* {@inheritDoc IOpportunitiesPrepare.moveToAave}
|
|
396
|
+
**/
|
|
397
|
+
async moveToAave(position, params) {
|
|
398
|
+
return this.#startIntent(position, {}, {
|
|
399
|
+
type: "MOVE_TO_AAVE",
|
|
400
|
+
token: params.token
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
/**
|
|
348
404
|
* {@inheritDoc IOpportunitiesPrepare.adjustLeverage}
|
|
349
405
|
**/
|
|
350
406
|
async adjustLeverage(position, params) {
|
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_model_result = require("../../model/result.js");
|
|
3
|
+
require("../../model/index.js");
|
|
2
4
|
const require_onchain_validation_checkOperation = require("../../onchain/validation/checkOperation.js");
|
|
3
5
|
const require_onchain_validation_checkSimulation = require("../../onchain/validation/checkSimulation.js");
|
|
6
|
+
const require_onchain_preview_parse_parseOperationCalldata = require("../../onchain/preview/parse/parseOperationCalldata.js");
|
|
4
7
|
const require_onchain_preview_preview_previewOperation = require("../../onchain/preview/preview/previewOperation.js");
|
|
5
8
|
require("../../onchain/index.js");
|
|
9
|
+
const require_onchain_positions_projectAavePosition = require("../../onchain/positions/projectAavePosition.js");
|
|
6
10
|
//#region src/sdk/preview/PreviewNamespace.ts
|
|
7
11
|
/**
|
|
8
12
|
* {@inheritDoc IPreview}
|
|
@@ -23,6 +27,44 @@ var PreviewNamespace = class {
|
|
|
23
27
|
return require_onchain_preview_preview_previewOperation.previewOperation(sdk, input, options);
|
|
24
28
|
}
|
|
25
29
|
/**
|
|
30
|
+
* {@inheritDoc IPreview.openAavePosition}
|
|
31
|
+
**/
|
|
32
|
+
async openAavePosition(input, options) {
|
|
33
|
+
const previewed = await this.previewOperation(input, options);
|
|
34
|
+
if (!previewed.ok) return previewed;
|
|
35
|
+
const preview = previewed.data;
|
|
36
|
+
if (preview.operation !== "OpenCreditAccount") return require_model_result.sdkErr({
|
|
37
|
+
code: "unsupportedOperation",
|
|
38
|
+
message: `operation "${preview.operation}" is not an Aave position opening`,
|
|
39
|
+
operation: preview.operation
|
|
40
|
+
});
|
|
41
|
+
const sdk = this.#onchain.chain(input.chainId);
|
|
42
|
+
const parsed = require_onchain_preview_parse_parseOperationCalldata.parseOperationCalldata(sdk, input);
|
|
43
|
+
const owner = parsed.ok && parsed.data.operation === "OpenCreditAccount" ? parsed.data.onBehalfOf : input.sender;
|
|
44
|
+
const projected = await require_onchain_positions_projectAavePosition.projectAavePosition(sdk, {
|
|
45
|
+
creditManager: preview.creditManager,
|
|
46
|
+
assets: preview.estAssets.map((a) => ({
|
|
47
|
+
token: a.token.address,
|
|
48
|
+
balance: a.value
|
|
49
|
+
})),
|
|
50
|
+
quotas: preview.quotas.map((q) => ({
|
|
51
|
+
token: q.token.address,
|
|
52
|
+
balance: q.value
|
|
53
|
+
})),
|
|
54
|
+
totalDebt: preview.totalDebt.value,
|
|
55
|
+
totalValue: preview.estTotalValue.value
|
|
56
|
+
}, owner);
|
|
57
|
+
if (!projected) return require_model_result.sdkErr({
|
|
58
|
+
code: "unsupportedOperation",
|
|
59
|
+
message: `credit manager ${preview.creditManager} cannot move this opening to Aave`,
|
|
60
|
+
operation: preview.operation
|
|
61
|
+
});
|
|
62
|
+
return require_model_result.sdkOk({
|
|
63
|
+
...preview,
|
|
64
|
+
aave: projected.aave
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
26
68
|
* {@inheritDoc IPreview.checkOperation}
|
|
27
69
|
**/
|
|
28
70
|
async checkOperation(props, options) {
|
package/dist/esm/model/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import "./aave.js";
|
|
1
2
|
import "./analytics.js";
|
|
2
3
|
import { CHART_METRIC_UNITS, CHART_RANGES, CHART_UNAVAILABLE_CODES, POOL_OPPORTUNITY_CHART_METRICS, POOL_POSITION_CHART_METRICS, PROTOCOL_CHART_METRICS, STRATEGY_OPPORTUNITY_CHART_METRICS, STRATEGY_POSITION_CHART_METRICS } from "./charts.js";
|
|
3
4
|
import { backendPreferred, compareTagOf, offchainOnly, onchainOnly, tolerance } from "./compare.schema.js";
|
|
@@ -21,8 +22,6 @@ import { creditAccountNotEmpty, creditAccountNotFound, multipleDelayedWithdrawal
|
|
|
21
22
|
import { malformedTransaction } from "./errors/preview-errors.js";
|
|
22
23
|
import "./errors/index.js";
|
|
23
24
|
import { matchesLiquidatableAccountFilter } from "./liquidations.js";
|
|
24
|
-
import "./notices.js";
|
|
25
|
-
import { noticeKindSchema, noticeSchema } from "./notices.schema.js";
|
|
26
25
|
import { matchesOpportunityFilter, opportunityId, poolOpportunityId, strategyOpportunityId } from "./opportunities.js";
|
|
27
26
|
import { STRATEGY_POSITION_COLLATERAL_ERROR, liquidationPositionId, matchesPositionFilter, poolPositionId, positionId, strategyPositionId } from "./positions.js";
|
|
28
27
|
import { asEstimated } from "./previews.js";
|
|
@@ -33,4 +32,4 @@ import { safeValue, sdkErr, sdkOk } from "./result.js";
|
|
|
33
32
|
import "./rewards.js";
|
|
34
33
|
import "./withdrawals.js";
|
|
35
34
|
import { positionClaimableWithdrawalSchema, positionPendingWithdrawalSchema, positionWithdrawalsSchema, withdrawalOutputAmountSchema } from "./withdrawals.schema.js";
|
|
36
|
-
export { ANALYTICS_POSITIONS_DEFAULT_LIMIT, ANALYTICS_POSITIONS_MAX_LIMIT, CHART_METRIC_UNITS, CHART_RANGES, CHART_UNAVAILABLE_CODES, FILTER_ALL, KYC_REGISTRATION_LINKS, POOL_OPPORTUNITY_CHART_METRICS, POOL_POSITION_CHART_METRICS, PROTOCOL_CHART_METRICS, RWA_FACTORY_SECURITIZE, RWA_FACTORY_TYPES, RWA_PROTOCOLS, SECURITIZE_REGISTER_VAULT_TYPES, STRATEGY_OPPORTUNITY_CHART_METRICS, STRATEGY_POSITION_CHART_METRICS, STRATEGY_POSITION_COLLATERAL_ERROR, accountNotMidasGreenlisted, amountSchema, analyticsChartQueryOptionsSchema, analyticsChartQueryParamsSchema, analyticsChartQuerySchema, analyticsContractOptionSchema, analyticsContractRefSchema, analyticsCuratorOptionSchema, analyticsPositionFilterOptionsQuerySchema, analyticsPositionFilterOptionsSchema, analyticsPositionListOptionsSchema, analyticsPositionListQueryParamsSchema, analyticsPositionListQuerySchema, analyticsPositionPageSchema, analyticsPositionSchema, analyticsPositionSortFieldSchema, analyticsSortDirectionSchema, apyBreakdownSchema, asEstimated, assetTypeSchema, backendPreferred, booleanParamSchema, borrowRateBreakdownSchema, bpsSchema, chainFailedSchema, chainIdSchema, chainMetadataSchema, chainSucceededSchema, chartBundleSchemaFor, chartDenominationSchema, chartMetricSchema, chartQueryCodec, chartQueryParamsSchema, chartQuerySchema, chartRangeSchema, chartSeriesSchema, chartValueSchema, chartWindowSchema, compareTagOf, creditAccountFrozen, creditAccountNotEmpty, creditAccountNotFound, creditManagerPaused, curatorNameSchema, curatorSchema, dataSourceSchema, debtOutOfRange, delayedReceivedAssetSchema, encodeFlag, filterAllSchema, filterable, forbiddenToken, instantReceivedAssetSchema, insufficientAllowance, insufficientBalance, insufficientCollateral, insufficientPoolLiquidity, isFilterSet, kycRequirementSchema, leverageOutOfRange, leverageSchema, liquidatableAccountFilterSchema, liquidatableAccountSchema, liquidationApprovalSchema, liquidationDetailsSchema, liquidationPositionId, liquidationPositionSchema, liquidatorNotEligible, malformedTransaction, marketExpired, matchesLiquidatableAccountFilter, matchesOpportunityFilter, matchesPositionFilter, multipleDelayedWithdrawals, noDelayedRoute, noRecordedIntent, noStrategyTargetCollateral, notEmergencyLiquidator,
|
|
35
|
+
export { ANALYTICS_POSITIONS_DEFAULT_LIMIT, ANALYTICS_POSITIONS_MAX_LIMIT, CHART_METRIC_UNITS, CHART_RANGES, CHART_UNAVAILABLE_CODES, FILTER_ALL, KYC_REGISTRATION_LINKS, POOL_OPPORTUNITY_CHART_METRICS, POOL_POSITION_CHART_METRICS, PROTOCOL_CHART_METRICS, RWA_FACTORY_SECURITIZE, RWA_FACTORY_TYPES, RWA_PROTOCOLS, SECURITIZE_REGISTER_VAULT_TYPES, STRATEGY_OPPORTUNITY_CHART_METRICS, STRATEGY_POSITION_CHART_METRICS, STRATEGY_POSITION_COLLATERAL_ERROR, accountNotMidasGreenlisted, amountSchema, analyticsChartQueryOptionsSchema, analyticsChartQueryParamsSchema, analyticsChartQuerySchema, analyticsContractOptionSchema, analyticsContractRefSchema, analyticsCuratorOptionSchema, analyticsPositionFilterOptionsQuerySchema, analyticsPositionFilterOptionsSchema, analyticsPositionListOptionsSchema, analyticsPositionListQueryParamsSchema, analyticsPositionListQuerySchema, analyticsPositionPageSchema, analyticsPositionSchema, analyticsPositionSortFieldSchema, analyticsSortDirectionSchema, apyBreakdownSchema, asEstimated, assetTypeSchema, backendPreferred, booleanParamSchema, borrowRateBreakdownSchema, bpsSchema, chainFailedSchema, chainIdSchema, chainMetadataSchema, chainSucceededSchema, chartBundleSchemaFor, chartDenominationSchema, chartMetricSchema, chartQueryCodec, chartQueryParamsSchema, chartQuerySchema, chartRangeSchema, chartSeriesSchema, chartValueSchema, chartWindowSchema, compareTagOf, creditAccountFrozen, creditAccountNotEmpty, creditAccountNotFound, creditManagerPaused, curatorNameSchema, curatorSchema, dataSourceSchema, debtOutOfRange, delayedReceivedAssetSchema, encodeFlag, filterAllSchema, filterable, forbiddenToken, instantReceivedAssetSchema, insufficientAllowance, insufficientBalance, insufficientCollateral, insufficientPoolLiquidity, isFilterSet, kycRequirementSchema, leverageOutOfRange, leverageSchema, liquidatableAccountFilterSchema, liquidatableAccountSchema, liquidationApprovalSchema, liquidationDetailsSchema, liquidationPositionId, liquidationPositionSchema, liquidatorNotEligible, malformedTransaction, marketExpired, matchesLiquidatableAccountFilter, matchesOpportunityFilter, matchesPositionFilter, multipleDelayedWithdrawals, noDelayedRoute, noRecordedIntent, noStrategyTargetCollateral, notEmergencyLiquidator, offchainOnly, onchainOnly, opportunityBaseSchema, opportunityDetailSchema, opportunityFilterQueryParamsSchema, opportunityFilterQuerySchema, opportunityFilterSchema, opportunityId, opportunityKeySchema, opportunityKindSchema, opportunitySchema, opportunityTotalsSchema, partialLiquidationImpossible, pnlBreakdownSchema, pointRewardsSchema, pointsProgramPnLSchema, pointsProgramSchema, pointsRewardsPnLSchema, poolOpportunityChartMetricSchema, poolOpportunityDetailSchema, poolOpportunityId, poolOpportunityKeySchema, poolOpportunitySchema, poolPaused, poolPositionChartMetricSchema, poolPositionId, poolPositionKeySchema, poolPositionSchema, poolSunset, positionClaimableWithdrawalSchema, positionCollateralSchema, positionFilterQueryParamsSchema, positionFilterQuerySchema, positionFilterSchema, positionId, positionKeySchema, positionKindSchema, positionPendingWithdrawalSchema, positionSchema, positionTransactionKindSchema, positionTransactionSchema, positionWithdrawalsSchema, positionsTotalsSchema, priceFeedDataSchema, priceFeedSummarySchema, protocolChartMetricSchema, quotaAboveMax, quotaAssetSchema, quotaCountExceeded, quotaInactive, quotaLimitReached, quotaUnchanged, rateCurvePointSchema, rateCurveSchema, receivedAssetSchema, reservePriceLimited, responseMetadataSchema, responseSchema, rewardsPnLSchema, rewardsSchema, rwaOpenRequirementsNotMet, rwaProtocolSchema, safeValue, sdkErr, sdkOk, strategyOpportunityChartMetricSchema, strategyOpportunityDetailSchema, strategyOpportunityId, strategyOpportunityKeySchema, strategyOpportunitySchema, strategyPositionChartMetricSchema, strategyPositionId, strategyPositionKeySchema, strategyPositionSchema, timestampSchema, tokenAmountSchema, tokenQuotaRateSchema, tokenRewardsPnLSchema, tokenRewardsSchema, tokenSchema, tolerance, txCallSchema, underlyingTokenSchema, unexpectedFailure, unpriceableTokenError, unsupportedCollateralToken, unsupportedTokenPair, withdrawalInProgress, withdrawalOutputAmountSchema };
|
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
import { OffchainAnalytics } from "./analytics/OffchainAnalytics.js";
|
|
2
2
|
import "./analytics/index.js";
|
|
3
|
-
import { OffchainNotices } from "./notices/OffchainNotices.js";
|
|
4
|
-
import "./notices/index.js";
|
|
5
3
|
import { OffchainOpportunities } from "./opportunities/OffchainOpportunities.js";
|
|
6
4
|
import "./opportunities/index.js";
|
|
7
5
|
import { OffchainPositions } from "./positions/OffchainPositions.js";
|
|
@@ -39,16 +37,11 @@ var GearboxAPI = class {
|
|
|
39
37
|
* Namespace for the positions a wallet holds.
|
|
40
38
|
**/
|
|
41
39
|
positions;
|
|
42
|
-
/**
|
|
43
|
-
* Namespace for the notices the backend attaches to entities.
|
|
44
|
-
**/
|
|
45
|
-
notices;
|
|
46
40
|
constructor(options) {
|
|
47
41
|
this.chainIds = [...options.chainIds];
|
|
48
42
|
this.analytics = new OffchainAnalytics(options);
|
|
49
43
|
this.opportunities = new OffchainOpportunities(options);
|
|
50
44
|
this.positions = new OffchainPositions(options);
|
|
51
|
-
this.notices = new OffchainNotices(options);
|
|
52
45
|
}
|
|
53
46
|
};
|
|
54
47
|
//#endregion
|
|
@@ -13,12 +13,10 @@ import { AbstractOffchainNamespace } from "./AbstractOffchainNamespace.js";
|
|
|
13
13
|
import { OffchainAnalyticsPositions } from "./analytics/OffchainAnalyticsPositions.js";
|
|
14
14
|
import { OffchainAnalytics } from "./analytics/OffchainAnalytics.js";
|
|
15
15
|
import "./analytics/index.js";
|
|
16
|
-
import { OffchainNotices } from "./notices/OffchainNotices.js";
|
|
17
|
-
import "./notices/index.js";
|
|
18
16
|
import { OffchainOpportunities } from "./opportunities/OffchainOpportunities.js";
|
|
19
17
|
import "./opportunities/index.js";
|
|
20
18
|
import { OffchainPositions } from "./positions/OffchainPositions.js";
|
|
21
19
|
import "./positions/index.js";
|
|
22
20
|
import { GearboxAPI } from "./GearboxAPI.js";
|
|
23
21
|
import "./types.js";
|
|
24
|
-
export { AbstractOffchainNamespace, GearboxAPI, OffchainAnalytics, OffchainAnalyticsPositions, OffchainInvalidJsonError, OffchainNotConfiguredError, OffchainNotImplementedError,
|
|
22
|
+
export { AbstractOffchainNamespace, GearboxAPI, OffchainAnalytics, OffchainAnalyticsPositions, OffchainInvalidJsonError, OffchainNotConfiguredError, OffchainNotImplementedError, OffchainOpportunities, OffchainPositions, OffchainRequestFailedError, OffchainStatusError, OffchainTransportError, OffchainValidationError, backendMessage, errorCause, readResponseBody };
|
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import { iBotListV310Abi } from "../../../abi/310/generated.js";
|
|
2
|
+
import { peripheryCompressorAbi } from "../../../abi/compressors/peripheryCompressor.js";
|
|
3
|
+
import { AddressMap } from "../../utils/AddressMap.js";
|
|
2
4
|
import { AP_PERIPHERY_COMPRESSOR } from "../../constants/address-provider.js";
|
|
3
5
|
import { VERSION_RANGE_310 } from "../../constants/versions.js";
|
|
4
6
|
import "../../constants/index.js";
|
|
7
|
+
import "../../utils/index.js";
|
|
5
8
|
import { SDKConstruct } from "../../base/SDKConstruct.js";
|
|
6
9
|
import "../../base/index.js";
|
|
7
10
|
import { iBotAbi } from "./abi.js";
|
|
@@ -14,6 +17,32 @@ import { PeripheryCompressorV310Contract } from "./PeripheryCompressorV310Contra
|
|
|
14
17
|
* and builds the transactions that connect or disconnect one.
|
|
15
18
|
**/
|
|
16
19
|
var AccountBotsService = class extends SDKConstruct {
|
|
20
|
+
#marketBots = new AddressMap(void 0, "marketBots");
|
|
21
|
+
/**
|
|
22
|
+
* The bots a market configurator lists among its periphery contracts.
|
|
23
|
+
*
|
|
24
|
+
* Read once per configurator and kept for the service's lifetime: the list
|
|
25
|
+
* changes only when the curator adds or removes a bot. A failed read is not
|
|
26
|
+
* kept, so the next call tries again.
|
|
27
|
+
*
|
|
28
|
+
* @param marketConfigurator - Configurator to list the bots of.
|
|
29
|
+
**/
|
|
30
|
+
getMarketBots(marketConfigurator) {
|
|
31
|
+
let bots = this.#marketBots.get(marketConfigurator);
|
|
32
|
+
if (!bots) {
|
|
33
|
+
bots = this.client.readContract({
|
|
34
|
+
address: this.#compressor.address,
|
|
35
|
+
abi: peripheryCompressorAbi,
|
|
36
|
+
functionName: "getBots",
|
|
37
|
+
args: [marketConfigurator]
|
|
38
|
+
}).catch((e) => {
|
|
39
|
+
this.#marketBots.delete(marketConfigurator);
|
|
40
|
+
throw e;
|
|
41
|
+
});
|
|
42
|
+
this.#marketBots.upsert(marketConfigurator, bots);
|
|
43
|
+
}
|
|
44
|
+
return bots;
|
|
45
|
+
}
|
|
17
46
|
/**
|
|
18
47
|
* Reads the bots connected to each of the given credit accounts.
|
|
19
48
|
*
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
+
import { peripheryCompressorAbi } from "../../../abi/compressors/peripheryCompressor.js";
|
|
1
2
|
import { BaseContract } from "../../base/BaseContract.js";
|
|
2
3
|
import "../../base/index.js";
|
|
3
|
-
import { peripheryCompressorAbi } from "../../../abi/compressors/peripheryCompressor.js";
|
|
4
4
|
//#region src/onchain/accounts/bots/PeripheryCompressorV310Contract.ts
|
|
5
5
|
const abi = peripheryCompressorAbi;
|
|
6
6
|
var PeripheryCompressorV310Contract = class extends BaseContract {
|
|
@@ -17,7 +17,7 @@ import { maxBorrow } from "./maxBorrow.js";
|
|
|
17
17
|
import { maxBorrowAndWithdraw } from "./maxBorrowAndWithdraw.js";
|
|
18
18
|
import { maxWithdrawCollateral } from "./maxWithdrawCollateral.js";
|
|
19
19
|
import { buildOpenStrategyState } from "./open-strategy.js";
|
|
20
|
-
import { planAddCollateral, planAdjustLeverage, planAdjustLeverageDelayed, planBorrowAndWithdraw, planDeposit, planRepay, planUpdateQuota, planWithdraw, planWithdrawAsset, planWithdrawDelayed } from "./plan.js";
|
|
20
|
+
import { planAddCollateral, planAdjustLeverage, planAdjustLeverageDelayed, planBorrowAndWithdraw, planDeposit, planMoveToAave, planRepay, planUpdateQuota, planWithdraw, planWithdrawAsset, planWithdrawDelayed } from "./plan.js";
|
|
21
21
|
import { withdrawLimits } from "./withdraw-limits.js";
|
|
22
22
|
import { realize } from "./realize.js";
|
|
23
23
|
import { planTail, projectTail } from "./tail.js";
|
|
@@ -65,6 +65,7 @@ var CreditAccountOperationsService = class extends SDKConstruct {
|
|
|
65
65
|
case "ADJUST_LEVERAGE": return planAdjustLeverage(intent, view);
|
|
66
66
|
case "DEPOSIT": return planDeposit(intent, view);
|
|
67
67
|
case "REPAY": return planRepay(intent, view);
|
|
68
|
+
case "MOVE_TO_AAVE": return planMoveToAave(intent, view);
|
|
68
69
|
case "WITHDRAW": return planWithdraw(intent, view);
|
|
69
70
|
case "UPDATE_QUOTA": return planUpdateQuota(intent, view);
|
|
70
71
|
default: throw new Error(`${intent.type} - not implemented`);
|
|
@@ -134,6 +134,25 @@ function buildClaimDelayedWithdrawalOperation(input) {
|
|
|
134
134
|
})
|
|
135
135
|
};
|
|
136
136
|
}
|
|
137
|
+
function buildAaveSupplyOperation(input) {
|
|
138
|
+
return {
|
|
139
|
+
type: "aaveSupply",
|
|
140
|
+
token: input.token,
|
|
141
|
+
aToken: input.aToken,
|
|
142
|
+
amount: input.amount,
|
|
143
|
+
calls: [input.adapter.supplyDiffCall(input.token, 0n)]
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
function buildAaveOpenPositionOperation(input) {
|
|
147
|
+
return {
|
|
148
|
+
type: "aaveOpenPosition",
|
|
149
|
+
aToken: input.aToken,
|
|
150
|
+
aTokenAmount: input.aTokenAmount,
|
|
151
|
+
debtToken: input.debtToken,
|
|
152
|
+
borrowAmount: input.borrowAmount,
|
|
153
|
+
calls: [input.adapter.openPositionDiffCall(input.aToken, input.debtToken, 0n, input.borrowAmount)]
|
|
154
|
+
};
|
|
155
|
+
}
|
|
137
156
|
/**
|
|
138
157
|
* What a request or a claim puts on the account right away, as opposed to what
|
|
139
158
|
* only the phantom token represents.
|
|
@@ -149,4 +168,4 @@ function instantOutput(outputs, token) {
|
|
|
149
168
|
}
|
|
150
169
|
}
|
|
151
170
|
//#endregion
|
|
152
|
-
export { buildAddCollateralOperation, buildClaimDelayedWithdrawalOperation, buildCloseSwapOperation, buildDecreaseDebtOperation, buildIncreaseDebtOperation, buildQuotaUpdateOperation, buildStartDelayedWithdrawalOperation, buildSwapOperation, buildUnwrapRwaCollateralOperation, buildWithdrawCollateralOperation, buildWrapRwaCollateralOperation, instantOutput };
|
|
171
|
+
export { buildAaveOpenPositionOperation, buildAaveSupplyOperation, buildAddCollateralOperation, buildClaimDelayedWithdrawalOperation, buildCloseSwapOperation, buildDecreaseDebtOperation, buildIncreaseDebtOperation, buildQuotaUpdateOperation, buildStartDelayedWithdrawalOperation, buildSwapOperation, buildUnwrapRwaCollateralOperation, buildWithdrawCollateralOperation, buildWrapRwaCollateralOperation, instantOutput };
|
|
@@ -104,6 +104,38 @@ function planRepay(intent, view) {
|
|
|
104
104
|
];
|
|
105
105
|
}
|
|
106
106
|
/**
|
|
107
|
+
* The position leaves for Aave Horizon: the RWA is supplied to the Aave pool,
|
|
108
|
+
* its aTokens go to the owner with a loan against them, and that loan settles
|
|
109
|
+
* the account's own.
|
|
110
|
+
*
|
|
111
|
+
* Aave lends the debt plus the {@link SETTLE_MARGIN} headroom, since the facade
|
|
112
|
+
* is told to repay everything outstanding by the time the transaction lands;
|
|
113
|
+
* what it does not take stays on the account.
|
|
114
|
+
*/
|
|
115
|
+
function planMoveToAave(intent, view) {
|
|
116
|
+
const amount = view.balanceOf(intent.token);
|
|
117
|
+
if (eq(intent.token, view.underlying) || isZeroBalance(amount)) throw new IntentPreviewError(unsupportedCollateralToken(toToken(view.sdk, intent.token)), `moveToAave: ${intent.token} is the underlying or not held by the account`);
|
|
118
|
+
if (view.debt <= 0n) throw new IntentPreviewError(debtOutOfRange({
|
|
119
|
+
requested: toTokenAmount(view.sdk, view.underlying, view.debt),
|
|
120
|
+
minDebt: toTokenAmount(view.sdk, view.underlying, view.debtLimits.minDebt),
|
|
121
|
+
maxDebt: toTokenAmount(view.sdk, view.underlying, view.debtLimits.maxDebt)
|
|
122
|
+
}), "moveToAave: the account owes nothing");
|
|
123
|
+
return [
|
|
124
|
+
{
|
|
125
|
+
kind: "aaveSupply",
|
|
126
|
+
token: intent.token,
|
|
127
|
+
amount
|
|
128
|
+
},
|
|
129
|
+
{
|
|
130
|
+
kind: "aaveOpenPosition",
|
|
131
|
+
token: intent.token,
|
|
132
|
+
borrow: withMargin(view.debt)
|
|
133
|
+
},
|
|
134
|
+
clearQuotas(),
|
|
135
|
+
repay(view.debt)
|
|
136
|
+
];
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
107
139
|
* One token's quota changes; no balance moves and debt is untouched.
|
|
108
140
|
*
|
|
109
141
|
* Only a token the account holds can be quoted here, and only on an account
|
|
@@ -445,4 +477,4 @@ function assertPositive(amount, flow) {
|
|
|
445
477
|
if (amount <= 0n) throw new IntentPreviewError(insufficientBalance(), `${flow}: amount must be positive`);
|
|
446
478
|
}
|
|
447
479
|
//#endregion
|
|
448
|
-
export { RAISED, planAddCollateral, planAdjustLeverage, planAdjustLeverageDelayed, planBorrowAndWithdraw, planDeposit, planFinishClaimOnly, planFinishCloseAccount, planFinishDecreaseLeverage, planFinishWithdraw, planRepay, planUpdateQuota, planWithdraw, planWithdrawAsset, planWithdrawDelayed };
|
|
480
|
+
export { RAISED, planAddCollateral, planAdjustLeverage, planAdjustLeverageDelayed, planBorrowAndWithdraw, planDeposit, planFinishClaimOnly, planFinishCloseAccount, planFinishDecreaseLeverage, planFinishWithdraw, planMoveToAave, planRepay, planUpdateQuota, planWithdraw, planWithdrawAsset, planWithdrawDelayed };
|