@gearbox-protocol/sdk 16.1.1 → 16.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/dist/cjs/onchain/accounts/CreditAccountsServiceV310.js +2 -2
- package/dist/cjs/onchain/accounts/intents/open-strategy.js +38 -4
- package/dist/cjs/onchain/chain/chains.js +2 -1
- package/dist/cjs/sdk/execute/ExecuteApi.js +1 -0
- package/dist/cjs/sdk/index.js +2 -0
- package/dist/cjs/sdk/prepare/PrepareApi.js +18 -1
- package/dist/cjs/sdk/prepare/errors.js +21 -0
- package/dist/cjs/sdk/prepare/index.js +2 -0
- package/dist/esm/onchain/accounts/CreditAccountsServiceV310.js +2 -2
- package/dist/esm/onchain/accounts/intents/open-strategy.js +38 -4
- package/dist/esm/onchain/chain/chains.js +2 -1
- package/dist/esm/sdk/execute/ExecuteApi.js +1 -0
- package/dist/esm/sdk/index.js +2 -2
- package/dist/esm/sdk/prepare/PrepareApi.js +19 -2
- package/dist/esm/sdk/prepare/errors.js +20 -1
- package/dist/esm/sdk/prepare/index.js +2 -2
- package/dist/types/onchain/accounts/intents/open-strategy.d.ts +34 -3
- package/dist/types/onchain/accounts/intents/tests/open-strategy.fixtures.d.ts +2 -2
- package/dist/types/onchain/accounts/types.d.ts +2 -1
- package/dist/types/sdk/index.d.ts +2 -2
- package/dist/types/sdk/prepare/PrepareApi.d.ts +2 -2
- package/dist/types/sdk/prepare/errors.d.ts +25 -1
- package/dist/types/sdk/prepare/index.d.ts +2 -2
- package/dist/types/sdk/prepare/types.d.ts +25 -2
- package/package.json +1 -1
|
@@ -205,7 +205,7 @@ var CreditAccountsServiceV310 = class extends require_onchain_base_SDKConstruct.
|
|
|
205
205
|
else if (typeof withdrawToken === "string") tokenToWithdraw = withdrawToken;
|
|
206
206
|
const { creditFacade } = cmSuite;
|
|
207
207
|
let calls = [
|
|
208
|
-
creditFacade.prepareIncreaseDebt(debt),
|
|
208
|
+
...debt > 0n ? [creditFacade.prepareIncreaseDebt(debt)] : [],
|
|
209
209
|
...creditFacade.prepareAddCollateral(collateral, permits),
|
|
210
210
|
...openPathCalls,
|
|
211
211
|
...tokenToWithdraw ? [creditFacade.prepareWithdrawCollateral(tokenToWithdraw, require_onchain_constants_math.MAX_UINT256, to)] : [],
|
|
@@ -217,7 +217,7 @@ var CreditAccountsServiceV310 = class extends require_onchain_base_SDKConstruct.
|
|
|
217
217
|
];
|
|
218
218
|
calls = await this.#prependMidasReceiveGreenlist(cm.address, calls);
|
|
219
219
|
calls = await this.prependPriceUpdates(cm.address, calls);
|
|
220
|
-
const tx = reopenCreditAccount ? cmSuite.multicallTx(reopenCreditAccount, calls) : cmSuite.openCreditAccountTx(to, calls, referralCode, rwaOptions);
|
|
220
|
+
const tx = reopenCreditAccount ? cmSuite.multicallTx(reopenCreditAccount, calls, rwaOptions) : cmSuite.openCreditAccountTx(to, calls, referralCode, rwaOptions);
|
|
221
221
|
tx.value = ethAmount.toString(10);
|
|
222
222
|
return tx;
|
|
223
223
|
}
|
|
@@ -7,7 +7,10 @@ const require_onchain_accounts_intents_utils_quotas_for_update = require("./util
|
|
|
7
7
|
const require_onchain_accounts_intents_utils_router_path = require("./utils/router-path.js");
|
|
8
8
|
require("./utils/index.js");
|
|
9
9
|
//#region src/onchain/accounts/intents/open-strategy.ts
|
|
10
|
-
/**
|
|
10
|
+
/**
|
|
11
|
+
* Stand-in account address, used when the opening creates its own account:
|
|
12
|
+
* nothing exists on chain until the tx lands.
|
|
13
|
+
*/
|
|
11
14
|
const NO_ACCOUNT = "0x0000000000000000000000000000000000000000";
|
|
12
15
|
/**
|
|
13
16
|
* Builds the state opening a leveraged position out of wallet collateral would
|
|
@@ -22,7 +25,8 @@ const NO_ACCOUNT = "0x0000000000000000000000000000000000000000";
|
|
|
22
25
|
* `sdk.accounts.openCA`.
|
|
23
26
|
*/
|
|
24
27
|
async function buildOpenStrategyState(props) {
|
|
25
|
-
|
|
28
|
+
if (props.empty) return emptyOpenState(props);
|
|
29
|
+
const { sdk, creditManager, collateral, targetToken, leverage, slippage = 0, quotaReserve, leftoverBalances = [], creditAccount: existing } = props;
|
|
26
30
|
require_onchain_accounts_intents_math.assertLeverageAtLeastOne(leverage);
|
|
27
31
|
const suite = sdk.marketRegister.findCreditManager(creditManager);
|
|
28
32
|
const market = sdk.marketRegister.findByCreditManager(creditManager);
|
|
@@ -32,7 +36,7 @@ async function buildOpenStrategyState(props) {
|
|
|
32
36
|
const margin = collateral.reduce((acc, a) => acc + convert(a.token, underlying, a.balance), 0n);
|
|
33
37
|
if (margin <= 0n) throw new require_onchain_validation_refusal.IntentPreviewError("insufficientSourceBalance", void 0, "openStrategy: collateral is worth nothing in underlying");
|
|
34
38
|
const debt = require_onchain_accounts_intents_math.debtForLeverage(margin, leverage);
|
|
35
|
-
const account = {
|
|
39
|
+
const account = existing ?? {
|
|
36
40
|
creditAccount: NO_ACCOUNT,
|
|
37
41
|
creditManager: creditManager.toLowerCase(),
|
|
38
42
|
creditFacade: suite.creditFacade.address.toLowerCase(),
|
|
@@ -101,7 +105,37 @@ async function buildOpenStrategyState(props) {
|
|
|
101
105
|
minAssets: minAssets.map(priced),
|
|
102
106
|
averageQuota,
|
|
103
107
|
minQuota,
|
|
104
|
-
calls: [...leg.calls]
|
|
108
|
+
calls: [...leg.calls],
|
|
109
|
+
creditAccount: existing?.creditAccount
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* The opening that holds an account and nothing else.
|
|
114
|
+
*
|
|
115
|
+
* Taken before the walk rather than threaded through it: the router has no
|
|
116
|
+
* guard for an empty basket and would still make its `eth_call`, and every
|
|
117
|
+
* assertion below reads amounts that are not there.
|
|
118
|
+
*/
|
|
119
|
+
async function emptyOpenState(props) {
|
|
120
|
+
const { sdk, creditManager } = props;
|
|
121
|
+
require_onchain_accounts_intents_guards.assertMarketOperable(sdk.marketRegister.findCreditManager(creditManager));
|
|
122
|
+
const snapshot = {
|
|
123
|
+
creditManager,
|
|
124
|
+
assets: [],
|
|
125
|
+
quotas: [],
|
|
126
|
+
totalDebt: 0n,
|
|
127
|
+
totalValue: 0n
|
|
128
|
+
};
|
|
129
|
+
const { assets: _assets, quotas: _quotas, ...projection } = sdk.positions.projection(snapshot, { availableLiquidityChange: 0n });
|
|
130
|
+
return {
|
|
131
|
+
...projection,
|
|
132
|
+
currentPrice: sdk.positions.currentPrice(snapshot),
|
|
133
|
+
priceImpact: void 0,
|
|
134
|
+
averageAssets: [],
|
|
135
|
+
minAssets: [],
|
|
136
|
+
averageQuota: [],
|
|
137
|
+
minQuota: [],
|
|
138
|
+
calls: []
|
|
105
139
|
};
|
|
106
140
|
}
|
|
107
141
|
/** Collateral plus the borrowed underlying, folded into one balance per token. */
|
|
@@ -118,7 +118,8 @@ const chains = {
|
|
|
118
118
|
"0x29350A3C2627FB78C7E915cd59AF754edf8998c5",
|
|
119
119
|
"0x5A93bc6983F80f5c55675eA4e77bb46DdC9665e8",
|
|
120
120
|
"0x5415092F06403F30486EF846dBAE0532c6Bd8759",
|
|
121
|
-
"0x9D866298f6B8E168a06e313D47059f2C097A92F1"
|
|
121
|
+
"0x9D866298f6B8E168a06e313D47059f2C097A92F1",
|
|
122
|
+
"0x0F4e4432977Bbf3962322996F1c9aeFdBC62256d"
|
|
122
123
|
]),
|
|
123
124
|
isPublic: true,
|
|
124
125
|
wellKnownToken: {
|
package/dist/cjs/sdk/index.js
CHANGED
|
@@ -47,7 +47,9 @@ exports.PreviewNamespace = require_sdk_preview_PreviewNamespace.PreviewNamespace
|
|
|
47
47
|
exports.SourceChainMismatchError = require_sdk_errors_SourceChainMismatchError.SourceChainMismatchError;
|
|
48
48
|
exports.SourceUnavailableError = require_sdk_errors_SourceUnavailableError.SourceUnavailableError;
|
|
49
49
|
exports.assertSameChains = require_sdk_errors_assertSameChains.assertSameChains;
|
|
50
|
+
exports.creditAccountNotEmpty = require_sdk_prepare_errors.creditAccountNotEmpty;
|
|
50
51
|
exports.creditAccountNotFound = require_sdk_prepare_errors.creditAccountNotFound;
|
|
52
|
+
exports.emptyOpenTakesNothing = require_sdk_prepare_errors.emptyOpenTakesNothing;
|
|
51
53
|
exports.everyChainFailed = require_sdk_errors_everyChainFailed.everyChainFailed;
|
|
52
54
|
exports.filterResponse = require_sdk_utils_filterResponse.filterResponse;
|
|
53
55
|
exports.mergeChainList = require_sdk_utils_mergeChains.mergeChainList;
|
|
@@ -9,6 +9,7 @@ const require_onchain_accounts_intents_index = require("../../onchain/accounts/i
|
|
|
9
9
|
require("../../onchain/index.js");
|
|
10
10
|
const require_sdk_prepare_errors = require("./errors.js");
|
|
11
11
|
const require_sdk_prepare_withdrawable_collaterals = require("./withdrawable-collaterals.js");
|
|
12
|
+
let viem = require("viem");
|
|
12
13
|
//#region src/sdk/prepare/PrepareApi.ts
|
|
13
14
|
/**
|
|
14
15
|
* {@inheritDoc IOpportunitiesPrepare}
|
|
@@ -182,8 +183,23 @@ var PrepareApi = class extends require_onchain_base_MultichainConstruct.Multicha
|
|
|
182
183
|
try {
|
|
183
184
|
const sdk = await this.#chain(strategy.chainId);
|
|
184
185
|
const at = stateBlock(sdk);
|
|
186
|
+
if (params.empty) {
|
|
187
|
+
if (params.collateral.length > 0 || params.creditAccount || params.leverage !== 0n) return require_model_result.sdkErr(require_sdk_prepare_errors.emptyOpenTakesNothing());
|
|
188
|
+
return opened(await service(sdk).openStrategyIntent({
|
|
189
|
+
sdk,
|
|
190
|
+
creditManager: strategy.creditManager,
|
|
191
|
+
empty: true
|
|
192
|
+
}), at);
|
|
193
|
+
}
|
|
185
194
|
const targetToken = params.targetToken ?? sdk.marketRegister.findCreditManager(strategy.creditManager).strategyTargetCollateral;
|
|
186
195
|
if (!targetToken) return require_model_result.sdkErr(require_sdk_prepare_errors.noStrategyTargetCollateral(strategy.creditManager));
|
|
196
|
+
let creditAccount;
|
|
197
|
+
if (params.creditAccount) {
|
|
198
|
+
const reused = await slice(sdk, params.creditAccount);
|
|
199
|
+
if (!reused || !(0, viem.isAddressEqual)(reused.creditManager, strategy.creditManager)) return require_model_result.sdkErr(require_sdk_prepare_errors.creditAccountNotFound(params.creditAccount));
|
|
200
|
+
if (reused.totalDebt > 0n || reused.tokens.some((t) => t.quota > 0n)) return require_model_result.sdkErr(require_sdk_prepare_errors.creditAccountNotEmpty(params.creditAccount));
|
|
201
|
+
creditAccount = reused;
|
|
202
|
+
}
|
|
187
203
|
return opened(await service(sdk).openStrategyIntent({
|
|
188
204
|
sdk,
|
|
189
205
|
creditManager: strategy.creditManager,
|
|
@@ -192,7 +208,8 @@ var PrepareApi = class extends require_onchain_base_MultichainConstruct.Multicha
|
|
|
192
208
|
leverage: params.leverage,
|
|
193
209
|
leftoverBalances: params.leftoverBalances,
|
|
194
210
|
slippage: params.slippage,
|
|
195
|
-
quotaReserve: params.quotaReserve
|
|
211
|
+
quotaReserve: params.quotaReserve,
|
|
212
|
+
creditAccount
|
|
196
213
|
}), at);
|
|
197
214
|
} catch (e) {
|
|
198
215
|
return require_model_result.sdkErr(require_sdk_prepare_errors.unexpectedFailure(e));
|
|
@@ -63,6 +63,16 @@ function noStrategyTargetCollateral(creditManager) {
|
|
|
63
63
|
};
|
|
64
64
|
}
|
|
65
65
|
/**
|
|
66
|
+
* {@inheritDoc CreditAccountNotEmptyError}
|
|
67
|
+
**/
|
|
68
|
+
function creditAccountNotEmpty(creditAccount) {
|
|
69
|
+
return {
|
|
70
|
+
code: "creditAccountNotEmpty",
|
|
71
|
+
message: `Credit account ${creditAccount} carries debt or quotas.`,
|
|
72
|
+
creditAccount
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
66
76
|
* {@inheritDoc CreditAccountNotFoundError}
|
|
67
77
|
**/
|
|
68
78
|
function creditAccountNotFound(creditAccount) {
|
|
@@ -73,6 +83,15 @@ function creditAccountNotFound(creditAccount) {
|
|
|
73
83
|
};
|
|
74
84
|
}
|
|
75
85
|
/**
|
|
86
|
+
* {@inheritDoc EmptyOpenTakesNothingError}
|
|
87
|
+
**/
|
|
88
|
+
function emptyOpenTakesNothing() {
|
|
89
|
+
return {
|
|
90
|
+
code: "emptyOpenTakesNothing",
|
|
91
|
+
message: "An empty opening takes no collateral, no account to reuse and no leverage."
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
76
95
|
* {@inheritDoc UnexpectedFailureError}
|
|
77
96
|
*
|
|
78
97
|
* Takes what was thrown, whatever that is: a `throw` is not obliged to raise an
|
|
@@ -87,7 +106,9 @@ function unexpectedFailure(thrown) {
|
|
|
87
106
|
};
|
|
88
107
|
}
|
|
89
108
|
//#endregion
|
|
109
|
+
exports.creditAccountNotEmpty = creditAccountNotEmpty;
|
|
90
110
|
exports.creditAccountNotFound = creditAccountNotFound;
|
|
111
|
+
exports.emptyOpenTakesNothing = emptyOpenTakesNothing;
|
|
91
112
|
exports.noStrategyTargetCollateral = noStrategyTargetCollateral;
|
|
92
113
|
exports.toRefusalError = toRefusalError;
|
|
93
114
|
exports.unexpectedFailure = unexpectedFailure;
|
|
@@ -5,7 +5,9 @@ const require_sdk_prepare_PrepareApi = require("./PrepareApi.js");
|
|
|
5
5
|
require("./types.js");
|
|
6
6
|
exports.IntentPreviewError = require_onchain_validation_refusal.IntentPreviewError;
|
|
7
7
|
exports.PrepareApi = require_sdk_prepare_PrepareApi.PrepareApi;
|
|
8
|
+
exports.creditAccountNotEmpty = require_sdk_prepare_errors.creditAccountNotEmpty;
|
|
8
9
|
exports.creditAccountNotFound = require_sdk_prepare_errors.creditAccountNotFound;
|
|
10
|
+
exports.emptyOpenTakesNothing = require_sdk_prepare_errors.emptyOpenTakesNothing;
|
|
9
11
|
exports.noStrategyTargetCollateral = require_sdk_prepare_errors.noStrategyTargetCollateral;
|
|
10
12
|
exports.raise = require_onchain_validation_refusal.raise;
|
|
11
13
|
exports.refuse = require_onchain_validation_refusal.refuse;
|
|
@@ -204,7 +204,7 @@ var CreditAccountsServiceV310 = class extends SDKConstruct {
|
|
|
204
204
|
else if (typeof withdrawToken === "string") tokenToWithdraw = withdrawToken;
|
|
205
205
|
const { creditFacade } = cmSuite;
|
|
206
206
|
let calls = [
|
|
207
|
-
creditFacade.prepareIncreaseDebt(debt),
|
|
207
|
+
...debt > 0n ? [creditFacade.prepareIncreaseDebt(debt)] : [],
|
|
208
208
|
...creditFacade.prepareAddCollateral(collateral, permits),
|
|
209
209
|
...openPathCalls,
|
|
210
210
|
...tokenToWithdraw ? [creditFacade.prepareWithdrawCollateral(tokenToWithdraw, MAX_UINT256, to)] : [],
|
|
@@ -216,7 +216,7 @@ var CreditAccountsServiceV310 = class extends SDKConstruct {
|
|
|
216
216
|
];
|
|
217
217
|
calls = await this.#prependMidasReceiveGreenlist(cm.address, calls);
|
|
218
218
|
calls = await this.prependPriceUpdates(cm.address, calls);
|
|
219
|
-
const tx = reopenCreditAccount ? cmSuite.multicallTx(reopenCreditAccount, calls) : cmSuite.openCreditAccountTx(to, calls, referralCode, rwaOptions);
|
|
219
|
+
const tx = reopenCreditAccount ? cmSuite.multicallTx(reopenCreditAccount, calls, rwaOptions) : cmSuite.openCreditAccountTx(to, calls, referralCode, rwaOptions);
|
|
220
220
|
tx.value = ethAmount.toString(10);
|
|
221
221
|
return tx;
|
|
222
222
|
}
|
|
@@ -6,7 +6,10 @@ import { getQuotasForUpdate } from "./utils/quotas-for-update.js";
|
|
|
6
6
|
import { createRouterPaths } from "./utils/router-path.js";
|
|
7
7
|
import "./utils/index.js";
|
|
8
8
|
//#region src/onchain/accounts/intents/open-strategy.ts
|
|
9
|
-
/**
|
|
9
|
+
/**
|
|
10
|
+
* Stand-in account address, used when the opening creates its own account:
|
|
11
|
+
* nothing exists on chain until the tx lands.
|
|
12
|
+
*/
|
|
10
13
|
const NO_ACCOUNT = "0x0000000000000000000000000000000000000000";
|
|
11
14
|
/**
|
|
12
15
|
* Builds the state opening a leveraged position out of wallet collateral would
|
|
@@ -21,7 +24,8 @@ const NO_ACCOUNT = "0x0000000000000000000000000000000000000000";
|
|
|
21
24
|
* `sdk.accounts.openCA`.
|
|
22
25
|
*/
|
|
23
26
|
async function buildOpenStrategyState(props) {
|
|
24
|
-
|
|
27
|
+
if (props.empty) return emptyOpenState(props);
|
|
28
|
+
const { sdk, creditManager, collateral, targetToken, leverage, slippage = 0, quotaReserve, leftoverBalances = [], creditAccount: existing } = props;
|
|
25
29
|
assertLeverageAtLeastOne(leverage);
|
|
26
30
|
const suite = sdk.marketRegister.findCreditManager(creditManager);
|
|
27
31
|
const market = sdk.marketRegister.findByCreditManager(creditManager);
|
|
@@ -31,7 +35,7 @@ async function buildOpenStrategyState(props) {
|
|
|
31
35
|
const margin = collateral.reduce((acc, a) => acc + convert(a.token, underlying, a.balance), 0n);
|
|
32
36
|
if (margin <= 0n) throw new IntentPreviewError("insufficientSourceBalance", void 0, "openStrategy: collateral is worth nothing in underlying");
|
|
33
37
|
const debt = debtForLeverage(margin, leverage);
|
|
34
|
-
const account = {
|
|
38
|
+
const account = existing ?? {
|
|
35
39
|
creditAccount: NO_ACCOUNT,
|
|
36
40
|
creditManager: creditManager.toLowerCase(),
|
|
37
41
|
creditFacade: suite.creditFacade.address.toLowerCase(),
|
|
@@ -100,7 +104,37 @@ async function buildOpenStrategyState(props) {
|
|
|
100
104
|
minAssets: minAssets.map(priced),
|
|
101
105
|
averageQuota,
|
|
102
106
|
minQuota,
|
|
103
|
-
calls: [...leg.calls]
|
|
107
|
+
calls: [...leg.calls],
|
|
108
|
+
creditAccount: existing?.creditAccount
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* The opening that holds an account and nothing else.
|
|
113
|
+
*
|
|
114
|
+
* Taken before the walk rather than threaded through it: the router has no
|
|
115
|
+
* guard for an empty basket and would still make its `eth_call`, and every
|
|
116
|
+
* assertion below reads amounts that are not there.
|
|
117
|
+
*/
|
|
118
|
+
async function emptyOpenState(props) {
|
|
119
|
+
const { sdk, creditManager } = props;
|
|
120
|
+
assertMarketOperable(sdk.marketRegister.findCreditManager(creditManager));
|
|
121
|
+
const snapshot = {
|
|
122
|
+
creditManager,
|
|
123
|
+
assets: [],
|
|
124
|
+
quotas: [],
|
|
125
|
+
totalDebt: 0n,
|
|
126
|
+
totalValue: 0n
|
|
127
|
+
};
|
|
128
|
+
const { assets: _assets, quotas: _quotas, ...projection } = sdk.positions.projection(snapshot, { availableLiquidityChange: 0n });
|
|
129
|
+
return {
|
|
130
|
+
...projection,
|
|
131
|
+
currentPrice: sdk.positions.currentPrice(snapshot),
|
|
132
|
+
priceImpact: void 0,
|
|
133
|
+
averageAssets: [],
|
|
134
|
+
minAssets: [],
|
|
135
|
+
averageQuota: [],
|
|
136
|
+
minQuota: [],
|
|
137
|
+
calls: []
|
|
104
138
|
};
|
|
105
139
|
}
|
|
106
140
|
/** Collateral plus the borrowed underlying, folded into one balance per token. */
|
|
@@ -117,7 +117,8 @@ const chains = {
|
|
|
117
117
|
"0x29350A3C2627FB78C7E915cd59AF754edf8998c5",
|
|
118
118
|
"0x5A93bc6983F80f5c55675eA4e77bb46DdC9665e8",
|
|
119
119
|
"0x5415092F06403F30486EF846dBAE0532c6Bd8759",
|
|
120
|
-
"0x9D866298f6B8E168a06e313D47059f2C097A92F1"
|
|
120
|
+
"0x9D866298f6B8E168a06e313D47059f2C097A92F1",
|
|
121
|
+
"0x0F4e4432977Bbf3962322996F1c9aeFdBC62256d"
|
|
121
122
|
]),
|
|
122
123
|
isPublic: true,
|
|
123
124
|
wellKnownToken: {
|
package/dist/esm/sdk/index.js
CHANGED
|
@@ -14,7 +14,7 @@ import { LiquidationsNamespace } from "./liquidations/LiquidationsNamespace.js";
|
|
|
14
14
|
import "./liquidations/index.js";
|
|
15
15
|
import { ExecuteApi } from "./execute/ExecuteApi.js";
|
|
16
16
|
import "./execute/index.js";
|
|
17
|
-
import { creditAccountNotFound, noStrategyTargetCollateral, toRefusalError, unexpectedFailure } from "./prepare/errors.js";
|
|
17
|
+
import { creditAccountNotEmpty, creditAccountNotFound, emptyOpenTakesNothing, noStrategyTargetCollateral, toRefusalError, unexpectedFailure } from "./prepare/errors.js";
|
|
18
18
|
import { PrepareApi } from "./prepare/PrepareApi.js";
|
|
19
19
|
import "./prepare/index.js";
|
|
20
20
|
import { filterResponse } from "./utils/filterResponse.js";
|
|
@@ -28,4 +28,4 @@ import { PreviewNamespace } from "./preview/PreviewNamespace.js";
|
|
|
28
28
|
import "./preview/index.js";
|
|
29
29
|
import { DEFAULT_MAX_STATE_AGE, GearboxSDK } from "./GearboxSDK.js";
|
|
30
30
|
import "./types.js";
|
|
31
|
-
export { AbstractNamespace, AllSourcesFailedError, AnalyticsNamespace, DEFAULT_MAX_OFFCHAIN_LAG, DEFAULT_MAX_STATE_AGE, ExecuteApi, GearboxSDK, IntentPreviewError, LiquidationsNamespace, MissingSourceError, NoSourceServedError, OpportunitiesNamespace, PositionsNamespace, PrepareApi, PreviewNamespace, SourceChainMismatchError, SourceUnavailableError, assertSameChains, creditAccountNotFound, everyChainFailed, filterResponse, mergeChainList, mergeChainOne, noStrategyTargetCollateral, raise, refuse, toRefusalError, unexpectedFailure };
|
|
31
|
+
export { AbstractNamespace, AllSourcesFailedError, AnalyticsNamespace, DEFAULT_MAX_OFFCHAIN_LAG, DEFAULT_MAX_STATE_AGE, ExecuteApi, GearboxSDK, IntentPreviewError, LiquidationsNamespace, MissingSourceError, NoSourceServedError, OpportunitiesNamespace, PositionsNamespace, PrepareApi, PreviewNamespace, SourceChainMismatchError, SourceUnavailableError, assertSameChains, creditAccountNotEmpty, creditAccountNotFound, emptyOpenTakesNothing, everyChainFailed, filterResponse, mergeChainList, mergeChainOne, noStrategyTargetCollateral, raise, refuse, toRefusalError, unexpectedFailure };
|
|
@@ -6,8 +6,9 @@ import { toToken } from "../../onchain/validation/token.js";
|
|
|
6
6
|
import { toCreditAccountSlice } from "../../onchain/accounts/intents/utils/credit-account-slice.js";
|
|
7
7
|
import { CreditAccountOperationsService } from "../../onchain/accounts/intents/index.js";
|
|
8
8
|
import "../../onchain/index.js";
|
|
9
|
-
import { creditAccountNotFound, noStrategyTargetCollateral, toRefusalError, unexpectedFailure } from "./errors.js";
|
|
9
|
+
import { creditAccountNotEmpty, creditAccountNotFound, emptyOpenTakesNothing, noStrategyTargetCollateral, toRefusalError, unexpectedFailure } from "./errors.js";
|
|
10
10
|
import { withdrawableCollaterals } from "./withdrawable-collaterals.js";
|
|
11
|
+
import { isAddressEqual } from "viem";
|
|
11
12
|
//#region src/sdk/prepare/PrepareApi.ts
|
|
12
13
|
/**
|
|
13
14
|
* {@inheritDoc IOpportunitiesPrepare}
|
|
@@ -181,8 +182,23 @@ var PrepareApi = class extends MultichainConstruct {
|
|
|
181
182
|
try {
|
|
182
183
|
const sdk = await this.#chain(strategy.chainId);
|
|
183
184
|
const at = stateBlock(sdk);
|
|
185
|
+
if (params.empty) {
|
|
186
|
+
if (params.collateral.length > 0 || params.creditAccount || params.leverage !== 0n) return sdkErr(emptyOpenTakesNothing());
|
|
187
|
+
return opened(await service(sdk).openStrategyIntent({
|
|
188
|
+
sdk,
|
|
189
|
+
creditManager: strategy.creditManager,
|
|
190
|
+
empty: true
|
|
191
|
+
}), at);
|
|
192
|
+
}
|
|
184
193
|
const targetToken = params.targetToken ?? sdk.marketRegister.findCreditManager(strategy.creditManager).strategyTargetCollateral;
|
|
185
194
|
if (!targetToken) return sdkErr(noStrategyTargetCollateral(strategy.creditManager));
|
|
195
|
+
let creditAccount;
|
|
196
|
+
if (params.creditAccount) {
|
|
197
|
+
const reused = await slice(sdk, params.creditAccount);
|
|
198
|
+
if (!reused || !isAddressEqual(reused.creditManager, strategy.creditManager)) return sdkErr(creditAccountNotFound(params.creditAccount));
|
|
199
|
+
if (reused.totalDebt > 0n || reused.tokens.some((t) => t.quota > 0n)) return sdkErr(creditAccountNotEmpty(params.creditAccount));
|
|
200
|
+
creditAccount = reused;
|
|
201
|
+
}
|
|
186
202
|
return opened(await service(sdk).openStrategyIntent({
|
|
187
203
|
sdk,
|
|
188
204
|
creditManager: strategy.creditManager,
|
|
@@ -191,7 +207,8 @@ var PrepareApi = class extends MultichainConstruct {
|
|
|
191
207
|
leverage: params.leverage,
|
|
192
208
|
leftoverBalances: params.leftoverBalances,
|
|
193
209
|
slippage: params.slippage,
|
|
194
|
-
quotaReserve: params.quotaReserve
|
|
210
|
+
quotaReserve: params.quotaReserve,
|
|
211
|
+
creditAccount
|
|
195
212
|
}), at);
|
|
196
213
|
} catch (e) {
|
|
197
214
|
return sdkErr(unexpectedFailure(e));
|
|
@@ -62,6 +62,16 @@ function noStrategyTargetCollateral(creditManager) {
|
|
|
62
62
|
};
|
|
63
63
|
}
|
|
64
64
|
/**
|
|
65
|
+
* {@inheritDoc CreditAccountNotEmptyError}
|
|
66
|
+
**/
|
|
67
|
+
function creditAccountNotEmpty(creditAccount) {
|
|
68
|
+
return {
|
|
69
|
+
code: "creditAccountNotEmpty",
|
|
70
|
+
message: `Credit account ${creditAccount} carries debt or quotas.`,
|
|
71
|
+
creditAccount
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
65
75
|
* {@inheritDoc CreditAccountNotFoundError}
|
|
66
76
|
**/
|
|
67
77
|
function creditAccountNotFound(creditAccount) {
|
|
@@ -72,6 +82,15 @@ function creditAccountNotFound(creditAccount) {
|
|
|
72
82
|
};
|
|
73
83
|
}
|
|
74
84
|
/**
|
|
85
|
+
* {@inheritDoc EmptyOpenTakesNothingError}
|
|
86
|
+
**/
|
|
87
|
+
function emptyOpenTakesNothing() {
|
|
88
|
+
return {
|
|
89
|
+
code: "emptyOpenTakesNothing",
|
|
90
|
+
message: "An empty opening takes no collateral, no account to reuse and no leverage."
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
75
94
|
* {@inheritDoc UnexpectedFailureError}
|
|
76
95
|
*
|
|
77
96
|
* Takes what was thrown, whatever that is: a `throw` is not obliged to raise an
|
|
@@ -86,4 +105,4 @@ function unexpectedFailure(thrown) {
|
|
|
86
105
|
};
|
|
87
106
|
}
|
|
88
107
|
//#endregion
|
|
89
|
-
export { creditAccountNotFound, noStrategyTargetCollateral, toRefusalError, unexpectedFailure };
|
|
108
|
+
export { creditAccountNotEmpty, creditAccountNotFound, emptyOpenTakesNothing, noStrategyTargetCollateral, toRefusalError, unexpectedFailure };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { IntentPreviewError, raise, refuse } from "../../onchain/validation/refusal.js";
|
|
2
|
-
import { creditAccountNotFound, noStrategyTargetCollateral, toRefusalError, unexpectedFailure } from "./errors.js";
|
|
2
|
+
import { creditAccountNotEmpty, creditAccountNotFound, emptyOpenTakesNothing, noStrategyTargetCollateral, toRefusalError, unexpectedFailure } from "./errors.js";
|
|
3
3
|
import { PrepareApi } from "./PrepareApi.js";
|
|
4
4
|
import "./types.js";
|
|
5
|
-
export { IntentPreviewError, PrepareApi, creditAccountNotFound, noStrategyTargetCollateral, raise, refuse, toRefusalError, unexpectedFailure };
|
|
5
|
+
export { IntentPreviewError, PrepareApi, creditAccountNotEmpty, creditAccountNotFound, emptyOpenTakesNothing, noStrategyTargetCollateral, raise, refuse, toRefusalError, unexpectedFailure };
|
|
@@ -4,11 +4,19 @@ import "../../../model/index.js";
|
|
|
4
4
|
import { Asset } from "../../base/types.js";
|
|
5
5
|
import { MultiCall } from "../../types/transactions.js";
|
|
6
6
|
import { OnchainSDK } from "../../OnchainSDK.js";
|
|
7
|
-
import { SimulationPrices } from "./types.js";
|
|
7
|
+
import { CreditAccountSlice, SimulationPrices } from "./types.js";
|
|
8
8
|
import "../../index.js";
|
|
9
9
|
import { Address } from "viem";
|
|
10
10
|
//#region src/onchain/accounts/intents/open-strategy.d.ts
|
|
11
|
-
|
|
11
|
+
/**
|
|
12
|
+
* Opening an account and putting a position on it in one transaction.
|
|
13
|
+
*
|
|
14
|
+
* The union says which of the two openings this is: {@link OpenStrategyEmpty}
|
|
15
|
+
* takes only the market, because an account holding nothing has nothing to
|
|
16
|
+
* route, no leverage to reach and no target to reach it in.
|
|
17
|
+
*/
|
|
18
|
+
type OpenStrategyProps = OpenStrategyFunded | OpenStrategyEmpty;
|
|
19
|
+
interface OpenStrategyFunded {
|
|
12
20
|
sdk: OnchainSDK;
|
|
13
21
|
/** Credit manager to open the account in. */
|
|
14
22
|
creditManager: Address;
|
|
@@ -24,6 +32,20 @@ interface OpenStrategyProps {
|
|
|
24
32
|
quotaReserve: number | undefined;
|
|
25
33
|
/** Balances to leave unswapped; everything else is routed into the target. */
|
|
26
34
|
leftoverBalances?: Asset[];
|
|
35
|
+
/**
|
|
36
|
+
* Existing credit account to put the position on, instead of opening one.
|
|
37
|
+
*
|
|
38
|
+
* Must carry no debt and no quotas. Growing a position that already exists
|
|
39
|
+
* is what the `DEPOSIT` intent is for.
|
|
40
|
+
**/
|
|
41
|
+
creditAccount?: CreditAccountSlice;
|
|
42
|
+
empty?: false;
|
|
43
|
+
}
|
|
44
|
+
/** Opening an account that holds nothing, for a position to land on later. */
|
|
45
|
+
interface OpenStrategyEmpty {
|
|
46
|
+
sdk: OnchainSDK;
|
|
47
|
+
creditManager: Address;
|
|
48
|
+
empty: true;
|
|
27
49
|
}
|
|
28
50
|
/**
|
|
29
51
|
* Projected result of opening a brand-new leveraged position.
|
|
@@ -49,6 +71,15 @@ interface OpenStrategyState extends Omit<AccountProjection, "assets" | "quotas">
|
|
|
49
71
|
minQuota: Asset[];
|
|
50
72
|
/** Router path; feeds `openCA.calls`. */
|
|
51
73
|
calls: MultiCall[];
|
|
74
|
+
/**
|
|
75
|
+
* The account this opening was simulated against and must be executed on,
|
|
76
|
+
* when it reuses one; `undefined` for an opening that creates its own.
|
|
77
|
+
*
|
|
78
|
+
* Carried here rather than asked of the caller again at `buildTx`, so the
|
|
79
|
+
* transaction cannot be built against an account the numbers were not
|
|
80
|
+
* computed for.
|
|
81
|
+
**/
|
|
82
|
+
creditAccount?: Address;
|
|
52
83
|
}
|
|
53
84
|
/**
|
|
54
85
|
* Builds the state opening a leveraged position out of wallet collateral would
|
|
@@ -64,4 +95,4 @@ interface OpenStrategyState extends Omit<AccountProjection, "assets" | "quotas">
|
|
|
64
95
|
*/
|
|
65
96
|
declare function buildOpenStrategyState(props: OpenStrategyProps): Promise<OpenStrategyState>;
|
|
66
97
|
//#endregion
|
|
67
|
-
export { OpenStrategyProps, OpenStrategyState, buildOpenStrategyState };
|
|
98
|
+
export { OpenStrategyEmpty, OpenStrategyFunded, OpenStrategyProps, OpenStrategyState, buildOpenStrategyState };
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Asset } from "../../../base/types.js";
|
|
2
2
|
import { OnchainSDK } from "../../../OnchainSDK.js";
|
|
3
|
-
import {
|
|
3
|
+
import { OpenStrategyFunded } from "../open-strategy.js";
|
|
4
4
|
import "../../../index.js";
|
|
5
5
|
import { MarketSdkExtras } from "../testing/market.js";
|
|
6
6
|
import { Address } from "viem";
|
|
@@ -51,6 +51,6 @@ declare const case_underlying_1x: OpenStrategyCase;
|
|
|
51
51
|
*/
|
|
52
52
|
declare const case_mixed_with_leftover: OpenStrategyCase;
|
|
53
53
|
declare function buildOpenStrategySdk(extras?: MarketSdkExtras): OnchainSDK;
|
|
54
|
-
declare function buildOpenStrategyProps(c: OpenStrategyCase, sdk: OnchainSDK):
|
|
54
|
+
declare function buildOpenStrategyProps(c: OpenStrategyCase, sdk: OnchainSDK): OpenStrategyFunded;
|
|
55
55
|
//#endregion
|
|
56
56
|
export { COLLATERAL_ANY, HALF_UND, KEEP_ANY, LEVERAGE_1X, LEVERAGE_2X, LEVERAGE_3X, LT, MARGIN_UND, OpenStrategyCase, buildOpenStrategyProps, buildOpenStrategySdk, case_mixed_with_leftover, case_underlying_1x, case_underlying_3x, quotaFor };
|
|
@@ -214,7 +214,8 @@ interface OpenCAProps extends PrepareUpdateQuotasProps {
|
|
|
214
214
|
*/
|
|
215
215
|
creditManager: Address;
|
|
216
216
|
/**
|
|
217
|
-
* Optional address of credit account to
|
|
217
|
+
* Optional address of an existing credit account to run the opening on,
|
|
218
|
+
* instead of opening a new one.
|
|
218
219
|
*/
|
|
219
220
|
reopenCreditAccount?: Address;
|
|
220
221
|
/**
|
|
@@ -3,7 +3,7 @@ import { LeverageBand } from "../onchain/accounts/intents/leverage-band.js";
|
|
|
3
3
|
import { OperationState, PathLossRate, WithdrawCeilings } from "../onchain/accounts/intents/types.js";
|
|
4
4
|
import { IAnalytics, IAnalyticsByMode } from "./analytics/types.js";
|
|
5
5
|
import { ILiquidations, ILiquidationsByMode } from "./liquidations/types.js";
|
|
6
|
-
import { AccountFlowError, CreditAccountNotFoundError, DebtOutOfRangeError, ForbiddenTokenError, InsufficientCollateralError, InsufficientPoolLiquidityError, InsufficientSourceBalanceError, LeverageOutOfRangeError, MalformedTransactionError, MarketExpiredError, MarketPausedError, MultipleDelayedWithdrawalsError, NoDelayedRouteError, NoRecordedIntentError, NoStrategyTargetCollateralError, OpenFlowError, PoolSunsetError, QuotaCountExceededError, QuotaLimitReachedError, RefusalErrors, UnexpectedFailureError, UnsupportedCollateralTokenError, UnsupportedTokenPairError, WithRouteRefusals, WithdrawalInProgressError, creditAccountNotFound, noStrategyTargetCollateral, toRefusalError, unexpectedFailure } from "./prepare/errors.js";
|
|
6
|
+
import { AccountFlowError, CreditAccountNotEmptyError, CreditAccountNotFoundError, DebtOutOfRangeError, EmptyOpenTakesNothingError, ForbiddenTokenError, InsufficientCollateralError, InsufficientPoolLiquidityError, InsufficientSourceBalanceError, LeverageOutOfRangeError, MalformedTransactionError, MarketExpiredError, MarketPausedError, MultipleDelayedWithdrawalsError, NoDelayedRouteError, NoRecordedIntentError, NoStrategyTargetCollateralError, OpenFlowError, PoolSunsetError, QuotaCountExceededError, QuotaLimitReachedError, RefusalErrors, UnexpectedFailureError, UnsupportedCollateralTokenError, UnsupportedTokenPairError, WithRouteRefusals, WithdrawalInProgressError, creditAccountNotEmpty, creditAccountNotFound, emptyOpenTakesNothing, noStrategyTargetCollateral, toRefusalError, unexpectedFailure } from "./prepare/errors.js";
|
|
7
7
|
import { AddCollateralParams, AdjustLeverageParams, DelayedStrategyResult, DepositStrategyParams, FinalizeParams, FinalizeResult, IOpportunitiesPrepare, LpParams, LpRedeemParams, LpResult, LpState, OpenStrategyParams, OpenStrategyResult, PoolInput, PositionInput, PrepareOptions, RepayStrategyParams, StrategyInput, StrategyResult, StrategyRoutesResult, WithdrawCollateralParams, WithdrawStrategyParams } from "./prepare/types.js";
|
|
8
8
|
import { ChainOf, PrepareApi } from "./prepare/PrepareApi.js";
|
|
9
9
|
import "./prepare/index.js";
|
|
@@ -38,4 +38,4 @@ import { SourceUnavailableError } from "./errors/SourceUnavailableError.js";
|
|
|
38
38
|
import { assertSameChains } from "./errors/assertSameChains.js";
|
|
39
39
|
import { everyChainFailed } from "./errors/everyChainFailed.js";
|
|
40
40
|
import "./errors/index.js";
|
|
41
|
-
export { AbstractNamespace, AccountFlowError, AccountPrepareRequest, AddCollateralParams, AdjustLeverageParams, AllSourcesFailedError, AnalyticsNamespace, BorrowLimitBinding, ChainOf, ChainRef, CreditAccountNotFoundError, DEFAULT_MAX_OFFCHAIN_LAG, DEFAULT_MAX_STATE_AGE, DebtOutOfRangeError, DelayedStrategyResult, DepositStrategyParams, EnsureFreshChains, type EntityMerger, ExecuteApi, type FilterResult, FinalizeParams, FinalizeResult, ForbiddenTokenError, GearboxSDK, GearboxSDKOptions, IAnalytics, IAnalyticsByMode, IGearboxSDK, ILiquidations, ILiquidationsByMode, INotices, INoticesByMode, IOpportunities, IOpportunitiesBase, IOpportunitiesByMode, IOpportunitiesExecute, IOpportunitiesOffchainBranch, IOpportunitiesOffchainOnly, IOpportunitiesOnchainBranch, IOpportunitiesOnchainOnly, IOpportunitiesPrepare, IOpportunityMergers, IPositionMergers, IPositions, IPositionsBase, IPositionsByMode, IPositionsOffchainBranch, IPositionsOffchainOnly, IPositionsOnchainBranch, IPositionsOnchainOnly, IPreview, IPreviewByMode, InsufficientCollateralError, InsufficientPoolLiquidityError, InsufficientSourceBalanceError, IntentPreviewError, type LeverageBand, LeverageOutOfRangeError, LiquidationsNamespace, type ListMerger, LpParams, LpRedeemParams, LpResult, LpState, MalformedTransactionError, MarketExpiredError, MarketPausedError, type MergeListResult, MergedQuery, MissingSourceError, Mode, MultipleDelayedWithdrawalsError, NamespaceOptions, NoDelayedRouteError, NoRecordedIntentError, NoSourceServedError, NoStrategyTargetCollateralError, OffchainByMode, OffchainSource, OnchainByMode, OnchainSource, OpenFlowError, OpenPrepareRequest, OpenStrategyParams, OpenStrategyResult, type OperationState, OpportunitiesNamespace, type PathLossRate, PlainMultichainSDKOptions, PoolInput, PoolPrepareRequest, PoolSunsetError, PositionInput, PositionsNamespace, PrepareApi, PrepareOptions, PrepareRequest, PreviewErrorDetails, PreviewErrorReason, PreviewIssue, PreviewNamespace, PreviewRefusal, QuotaCountExceededError, QuotaLimitReachedError, RefusalErrors, RepayStrategyParams, SourceChainMismatchError, SourceUnavailableError, StrategyInput, StrategyResult, StrategyRoutesResult, UnexpectedFailureError, UnsupportedCollateralTokenError, UnsupportedTokenPairError, WithRouteRefusals, type WithdrawCeilings, WithdrawCollateralParams, WithdrawStrategyParams, WithdrawalInProgressError, assertSameChains, creditAccountNotFound, everyChainFailed, filterResponse, mergeChainList, mergeChainOne, noStrategyTargetCollateral, raise, refuse, toRefusalError, unexpectedFailure };
|
|
41
|
+
export { AbstractNamespace, AccountFlowError, AccountPrepareRequest, AddCollateralParams, AdjustLeverageParams, AllSourcesFailedError, AnalyticsNamespace, BorrowLimitBinding, ChainOf, ChainRef, CreditAccountNotEmptyError, CreditAccountNotFoundError, DEFAULT_MAX_OFFCHAIN_LAG, DEFAULT_MAX_STATE_AGE, DebtOutOfRangeError, DelayedStrategyResult, DepositStrategyParams, EmptyOpenTakesNothingError, EnsureFreshChains, type EntityMerger, ExecuteApi, type FilterResult, FinalizeParams, FinalizeResult, ForbiddenTokenError, GearboxSDK, GearboxSDKOptions, IAnalytics, IAnalyticsByMode, IGearboxSDK, ILiquidations, ILiquidationsByMode, INotices, INoticesByMode, IOpportunities, IOpportunitiesBase, IOpportunitiesByMode, IOpportunitiesExecute, IOpportunitiesOffchainBranch, IOpportunitiesOffchainOnly, IOpportunitiesOnchainBranch, IOpportunitiesOnchainOnly, IOpportunitiesPrepare, IOpportunityMergers, IPositionMergers, IPositions, IPositionsBase, IPositionsByMode, IPositionsOffchainBranch, IPositionsOffchainOnly, IPositionsOnchainBranch, IPositionsOnchainOnly, IPreview, IPreviewByMode, InsufficientCollateralError, InsufficientPoolLiquidityError, InsufficientSourceBalanceError, IntentPreviewError, type LeverageBand, LeverageOutOfRangeError, LiquidationsNamespace, type ListMerger, LpParams, LpRedeemParams, LpResult, LpState, MalformedTransactionError, MarketExpiredError, MarketPausedError, type MergeListResult, MergedQuery, MissingSourceError, Mode, MultipleDelayedWithdrawalsError, NamespaceOptions, NoDelayedRouteError, NoRecordedIntentError, NoSourceServedError, NoStrategyTargetCollateralError, OffchainByMode, OffchainSource, OnchainByMode, OnchainSource, OpenFlowError, OpenPrepareRequest, OpenStrategyParams, OpenStrategyResult, type OperationState, OpportunitiesNamespace, type PathLossRate, PlainMultichainSDKOptions, PoolInput, PoolPrepareRequest, PoolSunsetError, PositionInput, PositionsNamespace, PrepareApi, PrepareOptions, PrepareRequest, PreviewErrorDetails, PreviewErrorReason, PreviewIssue, PreviewNamespace, PreviewRefusal, QuotaCountExceededError, QuotaLimitReachedError, RefusalErrors, RepayStrategyParams, SourceChainMismatchError, SourceUnavailableError, StrategyInput, StrategyResult, StrategyRoutesResult, UnexpectedFailureError, UnsupportedCollateralTokenError, UnsupportedTokenPairError, WithRouteRefusals, type WithdrawCeilings, WithdrawCollateralParams, WithdrawStrategyParams, WithdrawalInProgressError, assertSameChains, creditAccountNotEmpty, creditAccountNotFound, emptyOpenTakesNothing, everyChainFailed, filterResponse, mergeChainList, mergeChainOne, noStrategyTargetCollateral, raise, refuse, toRefusalError, unexpectedFailure };
|
|
@@ -9,7 +9,7 @@ import { MultichainConstruct } from "../../onchain/base/MultichainConstruct.js";
|
|
|
9
9
|
import { LeverageBand } from "../../onchain/accounts/intents/leverage-band.js";
|
|
10
10
|
import { WithdrawCeilings } from "../../onchain/accounts/intents/types.js";
|
|
11
11
|
import "../../onchain/index.js";
|
|
12
|
-
import { AccountFlowError, DebtOutOfRangeError, InsufficientPoolLiquidityError, LeverageOutOfRangeError, MultipleDelayedWithdrawalsError, NoDelayedRouteError, NoRecordedIntentError, NoStrategyTargetCollateralError, OpenFlowError, UnexpectedFailureError, UnsupportedCollateralTokenError, UnsupportedTokenPairError, WithRouteRefusals, WithdrawalInProgressError } from "./errors.js";
|
|
12
|
+
import { AccountFlowError, CreditAccountNotEmptyError, CreditAccountNotFoundError, DebtOutOfRangeError, EmptyOpenTakesNothingError, InsufficientPoolLiquidityError, LeverageOutOfRangeError, MultipleDelayedWithdrawalsError, NoDelayedRouteError, NoRecordedIntentError, NoStrategyTargetCollateralError, OpenFlowError, UnexpectedFailureError, UnsupportedCollateralTokenError, UnsupportedTokenPairError, WithRouteRefusals, WithdrawalInProgressError } from "./errors.js";
|
|
13
13
|
import { AddCollateralParams, AdjustLeverageParams, DepositStrategyParams, FinalizeParams, FinalizeResult, IOpportunitiesPrepare, LpParams, LpRedeemParams, LpResult, OpenStrategyParams, OpenStrategyResult, PoolInput, PositionInput, RepayStrategyParams, StrategyInput, StrategyResult, StrategyRoutesResult, WithdrawCollateralParams, WithdrawStrategyParams } from "./types.js";
|
|
14
14
|
import { EnsureFreshChains } from "../types.js";
|
|
15
15
|
import { Address } from "viem";
|
|
@@ -65,7 +65,7 @@ declare class PrepareApi extends MultichainConstruct implements IOpportunitiesPr
|
|
|
65
65
|
/**
|
|
66
66
|
* {@inheritDoc IOpportunitiesPrepare.openNewStrategy}
|
|
67
67
|
**/
|
|
68
|
-
openNewStrategy(strategy: StrategyInput, params: OpenStrategyParams): Promise<SDKReturn<OpenStrategyResult, OpenFlowError | DebtOutOfRangeError | LeverageOutOfRangeError | UnsupportedTokenPairError | InsufficientPoolLiquidityError | NoStrategyTargetCollateralError>>;
|
|
68
|
+
openNewStrategy(strategy: StrategyInput, params: OpenStrategyParams): Promise<SDKReturn<OpenStrategyResult, OpenFlowError | DebtOutOfRangeError | LeverageOutOfRangeError | UnsupportedTokenPairError | InsufficientPoolLiquidityError | NoStrategyTargetCollateralError | EmptyOpenTakesNothingError | CreditAccountNotFoundError | CreditAccountNotEmptyError>>;
|
|
69
69
|
/**
|
|
70
70
|
* {@inheritDoc IOpportunitiesPrepare.depositStrategy}
|
|
71
71
|
**/
|
|
@@ -232,6 +232,22 @@ interface CreditAccountNotFoundError extends IGearboxError {
|
|
|
232
232
|
code: "creditAccountNotFound";
|
|
233
233
|
creditAccount: Address;
|
|
234
234
|
}
|
|
235
|
+
/**
|
|
236
|
+
* The account named to open a position on carries debt or quotas.
|
|
237
|
+
*
|
|
238
|
+
* Growing a position that already exists is `depositStrategy`.
|
|
239
|
+
**/
|
|
240
|
+
interface CreditAccountNotEmptyError extends IGearboxError {
|
|
241
|
+
code: "creditAccountNotEmpty";
|
|
242
|
+
creditAccount: Address;
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* An empty opening was asked for with something to open it with: collateral to
|
|
246
|
+
* spend, an account to reuse, or a leverage to reach.
|
|
247
|
+
**/
|
|
248
|
+
interface EmptyOpenTakesNothingError extends IGearboxError {
|
|
249
|
+
code: "emptyOpenTakesNothing";
|
|
250
|
+
}
|
|
235
251
|
/**
|
|
236
252
|
* The SDK could not answer at all: a read that failed, a chain it is not
|
|
237
253
|
* connected to, a market or token address it knows nothing about, a contract
|
|
@@ -304,10 +320,18 @@ declare function toRefusalError<I extends PreviewIssue>(issue: I): RefusalErrors
|
|
|
304
320
|
* {@inheritDoc NoStrategyTargetCollateralError}
|
|
305
321
|
**/
|
|
306
322
|
declare function noStrategyTargetCollateral(creditManager: Address): NoStrategyTargetCollateralError;
|
|
323
|
+
/**
|
|
324
|
+
* {@inheritDoc CreditAccountNotEmptyError}
|
|
325
|
+
**/
|
|
326
|
+
declare function creditAccountNotEmpty(creditAccount: Address): CreditAccountNotEmptyError;
|
|
307
327
|
/**
|
|
308
328
|
* {@inheritDoc CreditAccountNotFoundError}
|
|
309
329
|
**/
|
|
310
330
|
declare function creditAccountNotFound(creditAccount: Address): CreditAccountNotFoundError;
|
|
331
|
+
/**
|
|
332
|
+
* {@inheritDoc EmptyOpenTakesNothingError}
|
|
333
|
+
**/
|
|
334
|
+
declare function emptyOpenTakesNothing(): EmptyOpenTakesNothingError;
|
|
311
335
|
/**
|
|
312
336
|
* {@inheritDoc UnexpectedFailureError}
|
|
313
337
|
*
|
|
@@ -316,4 +340,4 @@ declare function creditAccountNotFound(creditAccount: Address): CreditAccountNot
|
|
|
316
340
|
**/
|
|
317
341
|
declare function unexpectedFailure(thrown: unknown): UnexpectedFailureError;
|
|
318
342
|
//#endregion
|
|
319
|
-
export { AccountFlowError, CreditAccountNotFoundError, DebtOutOfRangeError, ForbiddenTokenError, InsufficientCollateralError, InsufficientPoolLiquidityError, InsufficientSourceBalanceError, LeverageOutOfRangeError, MalformedTransactionError, MarketExpiredError, MarketPausedError, MultipleDelayedWithdrawalsError, NoDelayedRouteError, NoRecordedIntentError, NoStrategyTargetCollateralError, OpenFlowError, PoolSunsetError, QuotaCountExceededError, QuotaLimitReachedError, RefusalErrors, UnexpectedFailureError, UnsupportedCollateralTokenError, UnsupportedTokenPairError, WithRouteRefusals, WithdrawalInProgressError, creditAccountNotFound, noStrategyTargetCollateral, toRefusalError, unexpectedFailure };
|
|
343
|
+
export { AccountFlowError, CreditAccountNotEmptyError, CreditAccountNotFoundError, DebtOutOfRangeError, EmptyOpenTakesNothingError, ForbiddenTokenError, InsufficientCollateralError, InsufficientPoolLiquidityError, InsufficientSourceBalanceError, LeverageOutOfRangeError, MalformedTransactionError, MarketExpiredError, MarketPausedError, MultipleDelayedWithdrawalsError, NoDelayedRouteError, NoRecordedIntentError, NoStrategyTargetCollateralError, OpenFlowError, PoolSunsetError, QuotaCountExceededError, QuotaLimitReachedError, RefusalErrors, UnexpectedFailureError, UnsupportedCollateralTokenError, UnsupportedTokenPairError, WithRouteRefusals, WithdrawalInProgressError, creditAccountNotEmpty, creditAccountNotFound, emptyOpenTakesNothing, noStrategyTargetCollateral, toRefusalError, unexpectedFailure };
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { BorrowLimitBinding, IntentPreviewError, PreviewErrorDetails, PreviewErrorReason, PreviewIssue, PreviewRefusal, raise, refuse } from "../../onchain/validation/refusal.js";
|
|
2
2
|
import { LeverageBand } from "../../onchain/accounts/intents/leverage-band.js";
|
|
3
3
|
import { OperationState, PathLossRate, WithdrawCeilings } from "../../onchain/accounts/intents/types.js";
|
|
4
|
-
import { AccountFlowError, CreditAccountNotFoundError, DebtOutOfRangeError, ForbiddenTokenError, InsufficientCollateralError, InsufficientPoolLiquidityError, InsufficientSourceBalanceError, LeverageOutOfRangeError, MalformedTransactionError, MarketExpiredError, MarketPausedError, MultipleDelayedWithdrawalsError, NoDelayedRouteError, NoRecordedIntentError, NoStrategyTargetCollateralError, OpenFlowError, PoolSunsetError, QuotaCountExceededError, QuotaLimitReachedError, RefusalErrors, UnexpectedFailureError, UnsupportedCollateralTokenError, UnsupportedTokenPairError, WithRouteRefusals, WithdrawalInProgressError, creditAccountNotFound, noStrategyTargetCollateral, toRefusalError, unexpectedFailure } from "./errors.js";
|
|
4
|
+
import { AccountFlowError, CreditAccountNotEmptyError, CreditAccountNotFoundError, DebtOutOfRangeError, EmptyOpenTakesNothingError, ForbiddenTokenError, InsufficientCollateralError, InsufficientPoolLiquidityError, InsufficientSourceBalanceError, LeverageOutOfRangeError, MalformedTransactionError, MarketExpiredError, MarketPausedError, MultipleDelayedWithdrawalsError, NoDelayedRouteError, NoRecordedIntentError, NoStrategyTargetCollateralError, OpenFlowError, PoolSunsetError, QuotaCountExceededError, QuotaLimitReachedError, RefusalErrors, UnexpectedFailureError, UnsupportedCollateralTokenError, UnsupportedTokenPairError, WithRouteRefusals, WithdrawalInProgressError, creditAccountNotEmpty, creditAccountNotFound, emptyOpenTakesNothing, noStrategyTargetCollateral, toRefusalError, unexpectedFailure } from "./errors.js";
|
|
5
5
|
import { AddCollateralParams, AdjustLeverageParams, DelayedStrategyResult, DepositStrategyParams, FinalizeParams, FinalizeResult, IOpportunitiesPrepare, LpParams, LpRedeemParams, LpResult, LpState, OpenStrategyParams, OpenStrategyResult, PoolInput, PositionInput, PrepareOptions, RepayStrategyParams, StrategyInput, StrategyResult, StrategyRoutesResult, WithdrawCollateralParams, WithdrawStrategyParams } from "./types.js";
|
|
6
6
|
import { ChainOf, PrepareApi } from "./PrepareApi.js";
|
|
7
|
-
export { AccountFlowError, AddCollateralParams, AdjustLeverageParams, BorrowLimitBinding, ChainOf, CreditAccountNotFoundError, DebtOutOfRangeError, DelayedStrategyResult, DepositStrategyParams, FinalizeParams, FinalizeResult, ForbiddenTokenError, IOpportunitiesPrepare, InsufficientCollateralError, InsufficientPoolLiquidityError, InsufficientSourceBalanceError, IntentPreviewError, type LeverageBand, LeverageOutOfRangeError, LpParams, LpRedeemParams, LpResult, LpState, MalformedTransactionError, MarketExpiredError, MarketPausedError, MultipleDelayedWithdrawalsError, NoDelayedRouteError, NoRecordedIntentError, NoStrategyTargetCollateralError, OpenFlowError, OpenStrategyParams, OpenStrategyResult, type OperationState, type PathLossRate, PoolInput, PoolSunsetError, PositionInput, PrepareApi, PrepareOptions, PreviewErrorDetails, PreviewErrorReason, PreviewIssue, PreviewRefusal, QuotaCountExceededError, QuotaLimitReachedError, RefusalErrors, RepayStrategyParams, StrategyInput, StrategyResult, StrategyRoutesResult, UnexpectedFailureError, UnsupportedCollateralTokenError, UnsupportedTokenPairError, WithRouteRefusals, type WithdrawCeilings, WithdrawCollateralParams, WithdrawStrategyParams, WithdrawalInProgressError, creditAccountNotFound, noStrategyTargetCollateral, raise, refuse, toRefusalError, unexpectedFailure };
|
|
7
|
+
export { AccountFlowError, AddCollateralParams, AdjustLeverageParams, BorrowLimitBinding, ChainOf, CreditAccountNotEmptyError, CreditAccountNotFoundError, DebtOutOfRangeError, DelayedStrategyResult, DepositStrategyParams, EmptyOpenTakesNothingError, FinalizeParams, FinalizeResult, ForbiddenTokenError, IOpportunitiesPrepare, InsufficientCollateralError, InsufficientPoolLiquidityError, InsufficientSourceBalanceError, IntentPreviewError, type LeverageBand, LeverageOutOfRangeError, LpParams, LpRedeemParams, LpResult, LpState, MalformedTransactionError, MarketExpiredError, MarketPausedError, MultipleDelayedWithdrawalsError, NoDelayedRouteError, NoRecordedIntentError, NoStrategyTargetCollateralError, OpenFlowError, OpenStrategyParams, OpenStrategyResult, type OperationState, type PathLossRate, PoolInput, PoolSunsetError, PositionInput, PrepareApi, PrepareOptions, PreviewErrorDetails, PreviewErrorReason, PreviewIssue, PreviewRefusal, QuotaCountExceededError, QuotaLimitReachedError, RefusalErrors, RepayStrategyParams, StrategyInput, StrategyResult, StrategyRoutesResult, UnexpectedFailureError, UnsupportedCollateralTokenError, UnsupportedTokenPairError, WithRouteRefusals, type WithdrawCeilings, WithdrawCollateralParams, WithdrawStrategyParams, WithdrawalInProgressError, creditAccountNotEmpty, creditAccountNotFound, emptyOpenTakesNothing, noStrategyTargetCollateral, raise, refuse, toRefusalError, unexpectedFailure };
|
|
@@ -14,7 +14,7 @@ import { AccountCalculatorOperation } from "../../onchain/accounts/intents/opera
|
|
|
14
14
|
import { ClaimRemainder, DelayedStart, OperationState, PathLossRate, ResumableIntent, RouteRefusals, WithdrawCeilings } from "../../onchain/accounts/intents/types.js";
|
|
15
15
|
import { OpenStrategyState } from "../../onchain/accounts/intents/open-strategy.js";
|
|
16
16
|
import "../../onchain/index.js";
|
|
17
|
-
import { AccountFlowError, DebtOutOfRangeError, InsufficientPoolLiquidityError, LeverageOutOfRangeError, MultipleDelayedWithdrawalsError, NoDelayedRouteError, NoRecordedIntentError, NoStrategyTargetCollateralError, OpenFlowError, UnexpectedFailureError, UnsupportedCollateralTokenError, UnsupportedTokenPairError, WithRouteRefusals, WithdrawalInProgressError } from "./errors.js";
|
|
17
|
+
import { AccountFlowError, CreditAccountNotEmptyError, CreditAccountNotFoundError, DebtOutOfRangeError, EmptyOpenTakesNothingError, InsufficientPoolLiquidityError, LeverageOutOfRangeError, MultipleDelayedWithdrawalsError, NoDelayedRouteError, NoRecordedIntentError, NoStrategyTargetCollateralError, OpenFlowError, UnexpectedFailureError, UnsupportedCollateralTokenError, UnsupportedTokenPairError, WithRouteRefusals, WithdrawalInProgressError } from "./errors.js";
|
|
18
18
|
import { Address } from "viem";
|
|
19
19
|
//#region src/sdk/prepare/types.d.ts
|
|
20
20
|
/**
|
|
@@ -332,6 +332,29 @@ interface OpenStrategyParams extends PrepareOptions {
|
|
|
332
332
|
targetToken?: Address;
|
|
333
333
|
/** Collateral to leave unswapped; everything else is routed into the target. */
|
|
334
334
|
leftoverBalances?: Asset[];
|
|
335
|
+
/**
|
|
336
|
+
* Existing credit account to open the position on, instead of creating one.
|
|
337
|
+
*
|
|
338
|
+
* Must belong to `strategy.creditManager` and carry no debt and no quotas —
|
|
339
|
+
* an account pre-opened by an {@link empty} opening.
|
|
340
|
+
* The projection is identical either way; only the transaction differs, and
|
|
341
|
+
* `execute.buildTx` reads which one to build off the result's own
|
|
342
|
+
* `state.creditAccount`.
|
|
343
|
+
**/
|
|
344
|
+
creditAccount?: Address;
|
|
345
|
+
/**
|
|
346
|
+
* Open the account holding nothing: no collateral, no debt, no quotas, and no
|
|
347
|
+
* route quoted. A wallet holds one so a position can be put on it later, by
|
|
348
|
+
* an opening that names it as {@link creditAccount}.
|
|
349
|
+
*
|
|
350
|
+
* {@link collateral} must be empty, {@link leverage} zero and
|
|
351
|
+
* {@link creditAccount} unset — the flag and the arguments have to agree.
|
|
352
|
+
* Neither leverage nor {@link targetToken} is read: with no collateral the
|
|
353
|
+
* debt is zero at any leverage, and there is nothing to route anywhere. Zero
|
|
354
|
+
* is the one leverage an ordinary opening refuses, so it cannot be mistaken
|
|
355
|
+
* for a request.
|
|
356
|
+
**/
|
|
357
|
+
empty?: boolean;
|
|
335
358
|
}
|
|
336
359
|
interface LpParams {
|
|
337
360
|
/**
|
|
@@ -438,7 +461,7 @@ interface IOpportunitiesPrepare {
|
|
|
438
461
|
* The one flow with no account yet, so the result carries no operation list —
|
|
439
462
|
* it feeds `sdk.accounts.openCA` instead.
|
|
440
463
|
**/
|
|
441
|
-
openNewStrategy(strategy: StrategyInput, params: OpenStrategyParams): Promise<SDKReturn<OpenStrategyResult, OpenFlowError | DebtOutOfRangeError | LeverageOutOfRangeError | UnsupportedTokenPairError | InsufficientPoolLiquidityError | NoStrategyTargetCollateralError>>;
|
|
464
|
+
openNewStrategy(strategy: StrategyInput, params: OpenStrategyParams): Promise<SDKReturn<OpenStrategyResult, OpenFlowError | DebtOutOfRangeError | LeverageOutOfRangeError | UnsupportedTokenPairError | InsufficientPoolLiquidityError | NoStrategyTargetCollateralError | EmptyOpenTakesNothingError | CreditAccountNotFoundError | CreditAccountNotEmptyError>>;
|
|
442
465
|
/**
|
|
443
466
|
* Growing a position: collateral in, debt drawn on top, both converted into
|
|
444
467
|
* the position token.
|