@gearbox-protocol/sdk 3.0.0-vfour.94 → 3.0.0-vfour.96
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/sdk/index.cjs +157 -58
- package/dist/cjs/sdk/index.d.ts +19 -11
- package/dist/esm/sdk/index.d.mts +19 -11
- package/dist/esm/sdk/index.mjs +157 -58
- package/package.json +1 -1
package/dist/cjs/sdk/index.cjs
CHANGED
|
@@ -21225,8 +21225,9 @@ var RedstoneUpdater = class extends SDKConstruct {
|
|
|
21225
21225
|
setHistoricalTimestamp(timestampMs) {
|
|
21226
21226
|
this.#historicalTimestampMs = 6e4 * Math.floor(timestampMs / 6e4);
|
|
21227
21227
|
}
|
|
21228
|
-
async getUpdateTxs(feeds) {
|
|
21228
|
+
async getUpdateTxs(feeds, logContext = {}) {
|
|
21229
21229
|
this.#logger?.debug(
|
|
21230
|
+
logContext,
|
|
21230
21231
|
`generating update transactions for ${feeds.length} redstone price feeds`
|
|
21231
21232
|
);
|
|
21232
21233
|
const groupedFeeds = {};
|
|
@@ -21265,6 +21266,7 @@ var RedstoneUpdater = class extends SDKConstruct {
|
|
|
21265
21266
|
}
|
|
21266
21267
|
}
|
|
21267
21268
|
this.#logger?.debug(
|
|
21269
|
+
logContext,
|
|
21268
21270
|
`generated ${results.length} update transactions for redstone price feeds`
|
|
21269
21271
|
);
|
|
21270
21272
|
return results;
|
|
@@ -21473,9 +21475,10 @@ var PriceFeedRegister = class extends SDKConstruct {
|
|
|
21473
21475
|
/**
|
|
21474
21476
|
* Returns RawTxs to update price feeds
|
|
21475
21477
|
* @param priceFeeds top-level price feeds, actual updatable price feeds will be derived. If not provided will use all price feeds that are attached
|
|
21478
|
+
* @param logContext extra information for logging
|
|
21476
21479
|
* @returns
|
|
21477
21480
|
*/
|
|
21478
|
-
async generatePriceFeedsUpdateTxs(priceFeeds) {
|
|
21481
|
+
async generatePriceFeedsUpdateTxs(priceFeeds, logContext = {}) {
|
|
21479
21482
|
const updateables = priceFeeds ? priceFeeds.flatMap((pf) => pf.updatableDependencies()) : this.#feeds.values();
|
|
21480
21483
|
const txs = [];
|
|
21481
21484
|
const redstonePFs = [];
|
|
@@ -21486,7 +21489,10 @@ var PriceFeedRegister = class extends SDKConstruct {
|
|
|
21486
21489
|
}
|
|
21487
21490
|
let maxTimestamp = 0;
|
|
21488
21491
|
if (redstonePFs.length > 0) {
|
|
21489
|
-
const redstoneUpdates = await this.#redstoneUpdater.getUpdateTxs(
|
|
21492
|
+
const redstoneUpdates = await this.#redstoneUpdater.getUpdateTxs(
|
|
21493
|
+
redstonePFs,
|
|
21494
|
+
logContext
|
|
21495
|
+
);
|
|
21490
21496
|
for (const { tx, timestamp } of redstoneUpdates) {
|
|
21491
21497
|
if (timestamp > maxTimestamp) {
|
|
21492
21498
|
maxTimestamp = timestamp;
|
|
@@ -21496,6 +21502,7 @@ var PriceFeedRegister = class extends SDKConstruct {
|
|
|
21496
21502
|
}
|
|
21497
21503
|
const result = { txs, timestamp: maxTimestamp };
|
|
21498
21504
|
this.logger?.debug(
|
|
21505
|
+
logContext,
|
|
21499
21506
|
`generated ${txs.length} price feed update transactions, timestamp: ${maxTimestamp}`
|
|
21500
21507
|
);
|
|
21501
21508
|
if (txs.length) {
|
|
@@ -22743,7 +22750,9 @@ var CreditAccountsService = class extends SDKConstruct {
|
|
|
22743
22750
|
if (raw.success) {
|
|
22744
22751
|
return raw;
|
|
22745
22752
|
}
|
|
22746
|
-
const { txs: priceUpdateTxs, timestamp: _ } = await this.sdk.priceFeeds.generatePriceFeedsUpdateTxs(
|
|
22753
|
+
const { txs: priceUpdateTxs, timestamp: _ } = await this.sdk.priceFeeds.generatePriceFeedsUpdateTxs(void 0, {
|
|
22754
|
+
account
|
|
22755
|
+
});
|
|
22747
22756
|
const resp = await simulateMulticall(this.provider.publicClient, {
|
|
22748
22757
|
contracts: [
|
|
22749
22758
|
...priceUpdateTxs.map(rawTxToMulticallPriceUpdate),
|
|
@@ -22824,7 +22833,11 @@ var CreditAccountsService = class extends SDKConstruct {
|
|
|
22824
22833
|
creditManager: cm.creditManager,
|
|
22825
22834
|
slippage
|
|
22826
22835
|
});
|
|
22827
|
-
const priceUpdates = await this.getPriceUpdatesForFacade(
|
|
22836
|
+
const priceUpdates = await this.getPriceUpdatesForFacade(
|
|
22837
|
+
account.creditManager,
|
|
22838
|
+
account,
|
|
22839
|
+
void 0
|
|
22840
|
+
);
|
|
22828
22841
|
const tx = cm.creditFacade.liquidateCreditAccount(
|
|
22829
22842
|
account.creditAccount,
|
|
22830
22843
|
to,
|
|
@@ -22862,7 +22875,7 @@ var CreditAccountsService = class extends SDKConstruct {
|
|
|
22862
22875
|
...this.#prepareDecreaseDebt(ca),
|
|
22863
22876
|
...this.#prepareDisableTokens(ca),
|
|
22864
22877
|
...assetsToWithdraw.map(
|
|
22865
|
-
(t) => this.#prepareWithdrawToken(ca, t, MAX_UINT256, to)
|
|
22878
|
+
(t) => this.#prepareWithdrawToken(ca.creditFacade, t, MAX_UINT256, to)
|
|
22866
22879
|
)
|
|
22867
22880
|
];
|
|
22868
22881
|
const tx = operation === "close" ? cm.creditFacade.closeCreditAccount(ca.creditAccount, calls) : cm.creditFacade.multicall(ca.creditAccount, calls);
|
|
@@ -22890,13 +22903,13 @@ var CreditAccountsService = class extends SDKConstruct {
|
|
|
22890
22903
|
const cm = this.sdk.marketRegister.findCreditManager(ca.creditManager);
|
|
22891
22904
|
const addCollateral = collateralAssets.filter((a) => a.balance > 0);
|
|
22892
22905
|
const calls = [
|
|
22893
|
-
...this.#prepareAddCollateral(
|
|
22906
|
+
...this.#prepareAddCollateral(ca.creditFacade, addCollateral, permits),
|
|
22894
22907
|
...this.#prepareDisableQuotas(ca),
|
|
22895
22908
|
...this.#prepareDecreaseDebt(ca),
|
|
22896
22909
|
...this.#prepareDisableTokens(ca),
|
|
22897
22910
|
// TODO: probably needs a better way to handle reward tokens
|
|
22898
22911
|
...assetsToWithdraw.map(
|
|
22899
|
-
(t) => this.#prepareWithdrawToken(ca, t, MAX_UINT256, to)
|
|
22912
|
+
(t) => this.#prepareWithdrawToken(ca.creditFacade, t, MAX_UINT256, to)
|
|
22900
22913
|
)
|
|
22901
22914
|
];
|
|
22902
22915
|
const tx = operation === "close" ? cm.creditFacade.closeCreditAccount(ca.creditAccount, calls) : cm.creditFacade.multicall(ca.creditAccount, calls);
|
|
@@ -22919,13 +22932,17 @@ var CreditAccountsService = class extends SDKConstruct {
|
|
|
22919
22932
|
to
|
|
22920
22933
|
}) {
|
|
22921
22934
|
const cm = this.sdk.marketRegister.findCreditManager(ca.creditManager);
|
|
22922
|
-
const priceUpdates = await this.getPriceUpdatesForFacade(
|
|
22935
|
+
const priceUpdates = await this.getPriceUpdatesForFacade(
|
|
22936
|
+
ca.creditManager,
|
|
22937
|
+
ca,
|
|
22938
|
+
void 0
|
|
22939
|
+
);
|
|
22923
22940
|
const addCollateral = collateralAssets.filter((a) => a.balance > 0);
|
|
22924
22941
|
const calls = [
|
|
22925
22942
|
...priceUpdates,
|
|
22926
|
-
...this.#prepareAddCollateral(
|
|
22943
|
+
...this.#prepareAddCollateral(ca.creditFacade, addCollateral, permits),
|
|
22927
22944
|
...assetsToWithdraw.map(
|
|
22928
|
-
(t) => this.#prepareWithdrawToken(ca, t, MAX_UINT256, to)
|
|
22945
|
+
(t) => this.#prepareWithdrawToken(ca.creditFacade, t, MAX_UINT256, to)
|
|
22929
22946
|
)
|
|
22930
22947
|
];
|
|
22931
22948
|
const tx = cm.creditFacade.liquidateCreditAccount(
|
|
@@ -22940,14 +22957,18 @@ var CreditAccountsService = class extends SDKConstruct {
|
|
|
22940
22957
|
const cm = this.sdk.marketRegister.findCreditManager(
|
|
22941
22958
|
creditAccount.creditManager
|
|
22942
22959
|
);
|
|
22943
|
-
const priceUpdates = await this.getPriceUpdatesForFacade(
|
|
22960
|
+
const priceUpdates = await this.getPriceUpdatesForFacade(
|
|
22961
|
+
creditAccount.creditManager,
|
|
22962
|
+
creditAccount,
|
|
22963
|
+
void 0
|
|
22964
|
+
);
|
|
22944
22965
|
const calls = [
|
|
22945
22966
|
...priceUpdates,
|
|
22946
|
-
...this.#prepareUpdateQuotas(props)
|
|
22967
|
+
...this.#prepareUpdateQuotas(props.creditAccount.creditFacade, props)
|
|
22947
22968
|
];
|
|
22948
22969
|
const tx = cm.creditFacade.multicall(creditAccount.creditAccount, [
|
|
22949
22970
|
...priceUpdates,
|
|
22950
|
-
...this.#prepareUpdateQuotas(props)
|
|
22971
|
+
...this.#prepareUpdateQuotas(props.creditAccount.creditFacade, props)
|
|
22951
22972
|
]);
|
|
22952
22973
|
return {
|
|
22953
22974
|
tx,
|
|
@@ -22959,15 +22980,19 @@ var CreditAccountsService = class extends SDKConstruct {
|
|
|
22959
22980
|
const cm = this.sdk.marketRegister.findCreditManager(
|
|
22960
22981
|
creditAccount.creditManager
|
|
22961
22982
|
);
|
|
22962
|
-
const priceUpdatesCalls = await this.getPriceUpdatesForFacade(
|
|
22983
|
+
const priceUpdatesCalls = await this.getPriceUpdatesForFacade(
|
|
22984
|
+
creditAccount.creditManager,
|
|
22985
|
+
creditAccount,
|
|
22986
|
+
props.averageQuota
|
|
22987
|
+
);
|
|
22963
22988
|
const calls = [
|
|
22964
22989
|
...priceUpdatesCalls,
|
|
22965
22990
|
...this.#prepareAddCollateral(
|
|
22991
|
+
creditAccount.creditFacade,
|
|
22966
22992
|
[asset],
|
|
22967
|
-
creditAccount,
|
|
22968
22993
|
permit ? { [asset.token]: permit } : {}
|
|
22969
22994
|
),
|
|
22970
|
-
...this.#prepareUpdateQuotas(props)
|
|
22995
|
+
...this.#prepareUpdateQuotas(creditAccount.creditFacade, props)
|
|
22971
22996
|
];
|
|
22972
22997
|
const tx = cm.creditFacade.multicall(creditAccount.creditAccount, calls);
|
|
22973
22998
|
tx.value = ethAmount.toString(10);
|
|
@@ -22985,20 +23010,19 @@ var CreditAccountsService = class extends SDKConstruct {
|
|
|
22985
23010
|
const cm = this.sdk.marketRegister.findCreditManager(
|
|
22986
23011
|
creditAccount.creditManager
|
|
22987
23012
|
);
|
|
22988
|
-
const priceUpdatesCalls = await this.getPriceUpdatesForFacade(
|
|
23013
|
+
const priceUpdatesCalls = await this.getPriceUpdatesForFacade(
|
|
23014
|
+
creditAccount.creditManager,
|
|
23015
|
+
creditAccount,
|
|
23016
|
+
void 0
|
|
23017
|
+
);
|
|
22989
23018
|
const underlyingEnabled = (creditAccount.enabledTokensMask & 1n) === 1n;
|
|
22990
23019
|
const shouldEnable = !isDecrease && !underlyingEnabled;
|
|
22991
23020
|
const calls = [
|
|
22992
23021
|
...priceUpdatesCalls,
|
|
22993
|
-
...shouldEnable ? this.#prepareEnableTokens(creditAccount, [
|
|
22994
|
-
|
|
22995
|
-
|
|
22996
|
-
|
|
22997
|
-
abi: iCreditFacadeV3MulticallAbi,
|
|
22998
|
-
functionName: isDecrease ? "decreaseDebt" : "increaseDebt",
|
|
22999
|
-
args: [change]
|
|
23000
|
-
})
|
|
23001
|
-
}
|
|
23022
|
+
...shouldEnable ? this.#prepareEnableTokens(creditAccount.creditFacade, [
|
|
23023
|
+
creditAccount.underlying
|
|
23024
|
+
]) : [],
|
|
23025
|
+
this.#prepareChangeDebt(creditAccount.creditFacade, change, isDecrease)
|
|
23002
23026
|
];
|
|
23003
23027
|
const tx = cm.creditFacade.multicall(creditAccount.creditAccount, calls);
|
|
23004
23028
|
return { tx, calls };
|
|
@@ -23008,13 +23032,22 @@ var CreditAccountsService = class extends SDKConstruct {
|
|
|
23008
23032
|
const cm = this.sdk.marketRegister.findCreditManager(
|
|
23009
23033
|
creditAccount.creditManager
|
|
23010
23034
|
);
|
|
23011
|
-
const priceUpdatesCalls = await this.getPriceUpdatesForFacade(
|
|
23035
|
+
const priceUpdatesCalls = await this.getPriceUpdatesForFacade(
|
|
23036
|
+
creditAccount.creditManager,
|
|
23037
|
+
creditAccount,
|
|
23038
|
+
void 0
|
|
23039
|
+
);
|
|
23012
23040
|
const calls = [
|
|
23013
23041
|
...priceUpdatesCalls,
|
|
23014
23042
|
...assetsToWithdraw.map(
|
|
23015
|
-
(a) => this.#prepareWithdrawToken(
|
|
23043
|
+
(a) => this.#prepareWithdrawToken(
|
|
23044
|
+
creditAccount.creditFacade,
|
|
23045
|
+
a.token,
|
|
23046
|
+
a.balance,
|
|
23047
|
+
to
|
|
23048
|
+
)
|
|
23016
23049
|
),
|
|
23017
|
-
...this.#prepareUpdateQuotas(props)
|
|
23050
|
+
...this.#prepareUpdateQuotas(creditAccount.creditFacade, props)
|
|
23018
23051
|
];
|
|
23019
23052
|
const tx = cm.creditFacade.multicall(creditAccount.creditAccount, calls);
|
|
23020
23053
|
return { tx, calls };
|
|
@@ -23025,11 +23058,15 @@ var CreditAccountsService = class extends SDKConstruct {
|
|
|
23025
23058
|
const cm = this.sdk.marketRegister.findCreditManager(
|
|
23026
23059
|
creditAccount.creditManager
|
|
23027
23060
|
);
|
|
23028
|
-
const priceUpdatesCalls = await this.getPriceUpdatesForFacade(
|
|
23061
|
+
const priceUpdatesCalls = await this.getPriceUpdatesForFacade(
|
|
23062
|
+
creditAccount.creditManager,
|
|
23063
|
+
creditAccount,
|
|
23064
|
+
props.averageQuota
|
|
23065
|
+
);
|
|
23029
23066
|
const calls = [
|
|
23030
23067
|
...priceUpdatesCalls,
|
|
23031
23068
|
...swapCalls,
|
|
23032
|
-
...this.#prepareUpdateQuotas(props)
|
|
23069
|
+
...this.#prepareUpdateQuotas(creditAccount.creditFacade, props)
|
|
23033
23070
|
];
|
|
23034
23071
|
const tx = cm.creditFacade.multicall(creditAccount.creditAccount, calls);
|
|
23035
23072
|
return { tx, calls };
|
|
@@ -23040,14 +23077,18 @@ var CreditAccountsService = class extends SDKConstruct {
|
|
|
23040
23077
|
const cm = this.sdk.marketRegister.findCreditManager(
|
|
23041
23078
|
creditAccount.creditManager
|
|
23042
23079
|
);
|
|
23043
|
-
const priceUpdatesCalls = await this.getPriceUpdatesForFacade(
|
|
23080
|
+
const priceUpdatesCalls = await this.getPriceUpdatesForFacade(
|
|
23081
|
+
creditAccount.creditManager,
|
|
23082
|
+
creditAccount,
|
|
23083
|
+
props.averageQuota
|
|
23084
|
+
);
|
|
23044
23085
|
const calls = [
|
|
23045
23086
|
...priceUpdatesCalls,
|
|
23046
23087
|
...claimCalls,
|
|
23047
23088
|
...tokensToDisable.map(
|
|
23048
|
-
(a) => this.#prepareDisableToken(a.token
|
|
23089
|
+
(a) => this.#prepareDisableToken(creditAccount.creditFacade, a.token)
|
|
23049
23090
|
),
|
|
23050
|
-
...this.#prepareUpdateQuotas(props)
|
|
23091
|
+
...this.#prepareUpdateQuotas(creditAccount.creditFacade, props)
|
|
23051
23092
|
];
|
|
23052
23093
|
const tx = cm.creditFacade.multicall(creditAccount.creditAccount, calls);
|
|
23053
23094
|
return { tx, calls };
|
|
@@ -23118,16 +23159,64 @@ var CreditAccountsService = class extends SDKConstruct {
|
|
|
23118
23159
|
}
|
|
23119
23160
|
return this.sdk.priceFeeds.generatePriceFeedsUpdateTxs(priceFeeds);
|
|
23120
23161
|
}
|
|
23162
|
+
async getUpdateForAccount(creditManager, creditAccount, desiredQuotas) {
|
|
23163
|
+
const tokensByPool = /* @__PURE__ */ new Map();
|
|
23164
|
+
const oracleByPool = /* @__PURE__ */ new Map();
|
|
23165
|
+
const quotaRecord = desiredQuotas ? assetsMap(desiredQuotas) : desiredQuotas;
|
|
23166
|
+
const caBalancesRecord = creditAccount ? assetsMap(creditAccount.tokens) : creditAccount;
|
|
23167
|
+
const market = this.sdk.marketRegister.findByCreditManager(creditManager);
|
|
23168
|
+
const cm = this.sdk.marketRegister.findCreditManager(creditManager).creditManager;
|
|
23169
|
+
const pool = market.poolFactory.pool.address;
|
|
23170
|
+
oracleByPool.set(pool, market.priceOracle);
|
|
23171
|
+
const insertToken = (p, t) => {
|
|
23172
|
+
const tokens = tokensByPool.get(p) ?? /* @__PURE__ */ new Set();
|
|
23173
|
+
tokens.add(t);
|
|
23174
|
+
tokensByPool.set(pool, tokens);
|
|
23175
|
+
};
|
|
23176
|
+
for (const t of cm.collateralTokens) {
|
|
23177
|
+
if (creditAccount && caBalancesRecord && quotaRecord) {
|
|
23178
|
+
const balanceAsset = caBalancesRecord.get(t);
|
|
23179
|
+
const balance = balanceAsset?.balance || 0n;
|
|
23180
|
+
const mask = balanceAsset?.mask || 0n;
|
|
23181
|
+
const isEnabled = (mask & creditAccount.enabledTokensMask) !== 0n;
|
|
23182
|
+
const quotaAsset = quotaRecord.get(t);
|
|
23183
|
+
const quotaBalance = quotaAsset?.balance || 0n;
|
|
23184
|
+
if (balance > 10n && isEnabled || quotaBalance > 0)
|
|
23185
|
+
insertToken(pool, t);
|
|
23186
|
+
} else if (creditAccount && caBalancesRecord) {
|
|
23187
|
+
const balanceAsset = caBalancesRecord.get(t);
|
|
23188
|
+
const balance = balanceAsset?.balance || 0n;
|
|
23189
|
+
const mask = balanceAsset?.mask || 0n;
|
|
23190
|
+
const isEnabled = (mask & creditAccount.enabledTokensMask) !== 0n;
|
|
23191
|
+
if (balance > 10n && isEnabled) insertToken(pool, t);
|
|
23192
|
+
} else if (quotaRecord) {
|
|
23193
|
+
const quotaAsset = quotaRecord.get(t);
|
|
23194
|
+
const quotaBalance = quotaAsset?.balance || 0n;
|
|
23195
|
+
if (quotaBalance > 0) insertToken(pool, t);
|
|
23196
|
+
}
|
|
23197
|
+
}
|
|
23198
|
+
const priceFeeds = [];
|
|
23199
|
+
for (const [pool2, priceFeedFactory] of oracleByPool.entries()) {
|
|
23200
|
+
const tokens = Array.from(tokensByPool.get(pool2) ?? []);
|
|
23201
|
+
priceFeeds.push(...priceFeedFactory.priceFeedsForTokens(tokens));
|
|
23202
|
+
}
|
|
23203
|
+
return this.sdk.priceFeeds.generatePriceFeedsUpdateTxs(
|
|
23204
|
+
priceFeeds,
|
|
23205
|
+
creditAccount ? { account: creditAccount.creditAccount } : void 0
|
|
23206
|
+
);
|
|
23207
|
+
}
|
|
23121
23208
|
/**
|
|
23122
23209
|
* Returns account price updates in a non-encoded format
|
|
23123
23210
|
* @param acc
|
|
23124
23211
|
* @returns
|
|
23125
23212
|
*/
|
|
23126
|
-
async getOnDemandPriceUpdates(
|
|
23127
|
-
const market = this.sdk.marketRegister.findByCreditManager(
|
|
23128
|
-
|
|
23213
|
+
async getOnDemandPriceUpdates(creditManager, creditAccount, desiredQuotas) {
|
|
23214
|
+
const market = this.sdk.marketRegister.findByCreditManager(creditManager);
|
|
23215
|
+
const update = await this.getUpdateForAccount(
|
|
23216
|
+
creditManager,
|
|
23217
|
+
creditAccount,
|
|
23218
|
+
desiredQuotas
|
|
23129
23219
|
);
|
|
23130
|
-
const update = await this.getUpdateForAccounts([acc]);
|
|
23131
23220
|
return market.priceOracle.onDemandPriceUpdates(update);
|
|
23132
23221
|
}
|
|
23133
23222
|
/**
|
|
@@ -23135,9 +23224,13 @@ var CreditAccountsService = class extends SDKConstruct {
|
|
|
23135
23224
|
* @param acc
|
|
23136
23225
|
* @returns
|
|
23137
23226
|
*/
|
|
23138
|
-
async getPriceUpdatesForFacade(
|
|
23139
|
-
const cm = this.sdk.marketRegister.findCreditManager(
|
|
23140
|
-
const updates = await this.getOnDemandPriceUpdates(
|
|
23227
|
+
async getPriceUpdatesForFacade(creditManager, creditAccount, desiredQuotas) {
|
|
23228
|
+
const cm = this.sdk.marketRegister.findCreditManager(creditManager);
|
|
23229
|
+
const updates = await this.getOnDemandPriceUpdates(
|
|
23230
|
+
creditManager,
|
|
23231
|
+
creditAccount,
|
|
23232
|
+
desiredQuotas
|
|
23233
|
+
);
|
|
23141
23234
|
return cm.creditFacade.encodeOnDemandPriceUpdates(updates);
|
|
23142
23235
|
}
|
|
23143
23236
|
#prepareDisableQuotas(ca) {
|
|
@@ -23156,17 +23249,13 @@ var CreditAccountsService = class extends SDKConstruct {
|
|
|
23156
23249
|
}
|
|
23157
23250
|
return calls;
|
|
23158
23251
|
}
|
|
23159
|
-
#prepareUpdateQuotas({
|
|
23160
|
-
creditAccount,
|
|
23161
|
-
averageQuota,
|
|
23162
|
-
minQuota
|
|
23163
|
-
}) {
|
|
23252
|
+
#prepareUpdateQuotas(creditFacade, { averageQuota, minQuota }) {
|
|
23164
23253
|
const minRecord = assetsMap(minQuota);
|
|
23165
23254
|
const calls = averageQuota.map((q) => {
|
|
23166
23255
|
const minAsset = minRecord.get(q.token);
|
|
23167
23256
|
const min = minAsset && minAsset?.balance > 0 ? minAsset.balance : 0n;
|
|
23168
23257
|
return {
|
|
23169
|
-
target:
|
|
23258
|
+
target: creditFacade,
|
|
23170
23259
|
callData: viem.encodeFunctionData({
|
|
23171
23260
|
abi: iCreditFacadeV3MulticallAbi,
|
|
23172
23261
|
functionName: "updateQuota",
|
|
@@ -23195,14 +23284,14 @@ var CreditAccountsService = class extends SDKConstruct {
|
|
|
23195
23284
|
const calls = [];
|
|
23196
23285
|
for (const t of ca.tokens) {
|
|
23197
23286
|
if (t.token !== ca.underlying && (t.mask & ca.enabledTokensMask) !== 0n && t.quota === 0n) {
|
|
23198
|
-
calls.push(this.#prepareDisableToken(t.token
|
|
23287
|
+
calls.push(this.#prepareDisableToken(ca.creditFacade, t.token));
|
|
23199
23288
|
}
|
|
23200
23289
|
}
|
|
23201
23290
|
return calls;
|
|
23202
23291
|
}
|
|
23203
|
-
#prepareDisableToken(
|
|
23292
|
+
#prepareDisableToken(creditFacade, token) {
|
|
23204
23293
|
return {
|
|
23205
|
-
target:
|
|
23294
|
+
target: creditFacade,
|
|
23206
23295
|
callData: viem.encodeFunctionData({
|
|
23207
23296
|
abi: iCreditFacadeV3MulticallAbi,
|
|
23208
23297
|
functionName: "disableToken",
|
|
@@ -23210,9 +23299,9 @@ var CreditAccountsService = class extends SDKConstruct {
|
|
|
23210
23299
|
})
|
|
23211
23300
|
};
|
|
23212
23301
|
}
|
|
23213
|
-
#prepareEnableTokens(
|
|
23302
|
+
#prepareEnableTokens(creditFacade, tokens) {
|
|
23214
23303
|
return tokens.map((t) => ({
|
|
23215
|
-
target:
|
|
23304
|
+
target: creditFacade,
|
|
23216
23305
|
callData: viem.encodeFunctionData({
|
|
23217
23306
|
abi: iCreditFacadeV3MulticallAbi,
|
|
23218
23307
|
functionName: "enableToken",
|
|
@@ -23220,9 +23309,9 @@ var CreditAccountsService = class extends SDKConstruct {
|
|
|
23220
23309
|
})
|
|
23221
23310
|
}));
|
|
23222
23311
|
}
|
|
23223
|
-
#prepareWithdrawToken(
|
|
23312
|
+
#prepareWithdrawToken(creditFacade, token, amount, to) {
|
|
23224
23313
|
return {
|
|
23225
|
-
target:
|
|
23314
|
+
target: creditFacade,
|
|
23226
23315
|
callData: viem.encodeFunctionData({
|
|
23227
23316
|
abi: iCreditFacadeV3MulticallAbi,
|
|
23228
23317
|
functionName: "withdrawCollateral",
|
|
@@ -23230,12 +23319,22 @@ var CreditAccountsService = class extends SDKConstruct {
|
|
|
23230
23319
|
})
|
|
23231
23320
|
};
|
|
23232
23321
|
}
|
|
23233
|
-
#
|
|
23322
|
+
#prepareChangeDebt(creditFacade, change, isDecrease) {
|
|
23323
|
+
return {
|
|
23324
|
+
target: creditFacade,
|
|
23325
|
+
callData: viem.encodeFunctionData({
|
|
23326
|
+
abi: iCreditFacadeV3MulticallAbi,
|
|
23327
|
+
functionName: isDecrease ? "decreaseDebt" : "increaseDebt",
|
|
23328
|
+
args: [change]
|
|
23329
|
+
})
|
|
23330
|
+
};
|
|
23331
|
+
}
|
|
23332
|
+
#prepareAddCollateral(creditFacade, assets, permits) {
|
|
23234
23333
|
const calls = assets.map(({ token, balance }) => {
|
|
23235
23334
|
const p = permits[token];
|
|
23236
23335
|
if (p) {
|
|
23237
23336
|
return {
|
|
23238
|
-
target:
|
|
23337
|
+
target: creditFacade,
|
|
23239
23338
|
callData: viem.encodeFunctionData({
|
|
23240
23339
|
abi: iCreditFacadeV3MulticallAbi,
|
|
23241
23340
|
functionName: "addCollateralWithPermit",
|
|
@@ -23244,7 +23343,7 @@ var CreditAccountsService = class extends SDKConstruct {
|
|
|
23244
23343
|
};
|
|
23245
23344
|
}
|
|
23246
23345
|
return {
|
|
23247
|
-
target:
|
|
23346
|
+
target: creditFacade,
|
|
23248
23347
|
callData: viem.encodeFunctionData({
|
|
23249
23348
|
abi: iCreditFacadeV3MulticallAbi,
|
|
23250
23349
|
functionName: "addCollateral",
|
package/dist/cjs/sdk/index.d.ts
CHANGED
|
@@ -22374,9 +22374,10 @@ declare class PriceFeedRegister extends SDKConstruct implements IHooks<PriceFeed
|
|
|
22374
22374
|
/**
|
|
22375
22375
|
* Returns RawTxs to update price feeds
|
|
22376
22376
|
* @param priceFeeds top-level price feeds, actual updatable price feeds will be derived. If not provided will use all price feeds that are attached
|
|
22377
|
+
* @param logContext extra information for logging
|
|
22377
22378
|
* @returns
|
|
22378
22379
|
*/
|
|
22379
|
-
generatePriceFeedsUpdateTxs(priceFeeds?: IPriceFeedContract[]): Promise<UpdatePriceFeedsResult>;
|
|
22380
|
+
generatePriceFeedsUpdateTxs(priceFeeds?: IPriceFeedContract[], logContext?: Record<string, any>): Promise<UpdatePriceFeedsResult>;
|
|
22380
22381
|
mustGet(address: Address): IPriceFeedContract;
|
|
22381
22382
|
getOrCreate(data: PriceFeedTreeNode): IPriceFeedContract;
|
|
22382
22383
|
/**
|
|
@@ -25049,7 +25050,7 @@ declare class RouterV3Contract extends BaseContract<abi> implements IHooks<Route
|
|
|
25049
25050
|
getAvailableConnectors(collateralTokens: Array<Address>): Array<Address>;
|
|
25050
25051
|
}
|
|
25051
25052
|
declare function balancesMap(assets: Array<Asset>): AddressMap<bigint>;
|
|
25052
|
-
declare function assetsMap(assets: Array<
|
|
25053
|
+
declare function assetsMap<T extends Asset>(assets: Array<T> | readonly T[]): AddressMap<T>;
|
|
25053
25054
|
|
|
25054
25055
|
interface SDKOptions {
|
|
25055
25056
|
/**
|
|
@@ -25276,26 +25277,32 @@ interface RepayAndLiquidateCreditAccountProps {
|
|
|
25276
25277
|
to: Address;
|
|
25277
25278
|
permits: Record<string, PermitResult>;
|
|
25278
25279
|
}
|
|
25279
|
-
interface
|
|
25280
|
-
creditAccount: CreditAccountDataSlice;
|
|
25280
|
+
interface PrepareUpdateQuotasProps {
|
|
25281
25281
|
minQuota: Array<Asset>;
|
|
25282
25282
|
averageQuota: Array<Asset>;
|
|
25283
25283
|
}
|
|
25284
|
-
interface
|
|
25284
|
+
interface UpdateQuotasProps extends PrepareUpdateQuotasProps {
|
|
25285
|
+
creditAccount: CreditAccountDataSlice;
|
|
25286
|
+
}
|
|
25287
|
+
interface AddCollateralProps extends PrepareUpdateQuotasProps {
|
|
25285
25288
|
asset: Asset;
|
|
25286
25289
|
ethAmount: bigint;
|
|
25287
|
-
permit
|
|
25290
|
+
permit: PermitResult | undefined;
|
|
25291
|
+
creditAccount: CreditAccountDataSlice;
|
|
25288
25292
|
}
|
|
25289
|
-
interface WithdrawCollateralProps extends
|
|
25293
|
+
interface WithdrawCollateralProps extends PrepareUpdateQuotasProps {
|
|
25290
25294
|
assetsToWithdraw: Array<Asset>;
|
|
25291
25295
|
to: Address;
|
|
25296
|
+
creditAccount: CreditAccountDataSlice;
|
|
25292
25297
|
}
|
|
25293
|
-
interface ExecuteSwapProps extends
|
|
25298
|
+
interface ExecuteSwapProps extends PrepareUpdateQuotasProps {
|
|
25294
25299
|
calls: Array<MultiCall>;
|
|
25300
|
+
creditAccount: CreditAccountDataSlice;
|
|
25295
25301
|
}
|
|
25296
|
-
interface ClaimFarmRewardsProps extends
|
|
25302
|
+
interface ClaimFarmRewardsProps extends PrepareUpdateQuotasProps {
|
|
25297
25303
|
tokensToDisable: Array<Asset>;
|
|
25298
25304
|
calls: Array<MultiCall>;
|
|
25305
|
+
creditAccount: CreditAccountDataSlice;
|
|
25299
25306
|
}
|
|
25300
25307
|
interface ChangeDeptProps {
|
|
25301
25308
|
creditAccount: CreditAccountDataSlice;
|
|
@@ -25397,18 +25404,19 @@ declare class CreditAccountsService extends SDKConstruct {
|
|
|
25397
25404
|
* @returns
|
|
25398
25405
|
*/
|
|
25399
25406
|
getUpdateForAccounts(accounts: Array<CreditAccountDataSlice>): Promise<UpdatePriceFeedsResult>;
|
|
25407
|
+
getUpdateForAccount(creditManager: Address, creditAccount: CreditAccountDataSlice | undefined, desiredQuotas: Array<Asset> | undefined): Promise<UpdatePriceFeedsResult>;
|
|
25400
25408
|
/**
|
|
25401
25409
|
* Returns account price updates in a non-encoded format
|
|
25402
25410
|
* @param acc
|
|
25403
25411
|
* @returns
|
|
25404
25412
|
*/
|
|
25405
|
-
getOnDemandPriceUpdates(
|
|
25413
|
+
getOnDemandPriceUpdates(creditManager: Address, creditAccount: CreditAccountDataSlice | undefined, desiredQuotas: Array<Asset> | undefined): Promise<Array<OnDemandPriceUpdate>>;
|
|
25406
25414
|
/**
|
|
25407
25415
|
* Returns price updates in format that is accepted by various credit facade methods (multicall, close/liquidate, etc...)
|
|
25408
25416
|
* @param acc
|
|
25409
25417
|
* @returns
|
|
25410
25418
|
*/
|
|
25411
|
-
getPriceUpdatesForFacade(
|
|
25419
|
+
getPriceUpdatesForFacade(creditManager: Address, creditAccount: CreditAccountDataSlice | undefined, desiredQuotas: Array<Asset> | undefined): Promise<Array<MultiCall>>;
|
|
25412
25420
|
/**
|
|
25413
25421
|
* Returns addresses of pools of attached markets
|
|
25414
25422
|
*/
|
package/dist/esm/sdk/index.d.mts
CHANGED
|
@@ -22374,9 +22374,10 @@ declare class PriceFeedRegister extends SDKConstruct implements IHooks<PriceFeed
|
|
|
22374
22374
|
/**
|
|
22375
22375
|
* Returns RawTxs to update price feeds
|
|
22376
22376
|
* @param priceFeeds top-level price feeds, actual updatable price feeds will be derived. If not provided will use all price feeds that are attached
|
|
22377
|
+
* @param logContext extra information for logging
|
|
22377
22378
|
* @returns
|
|
22378
22379
|
*/
|
|
22379
|
-
generatePriceFeedsUpdateTxs(priceFeeds?: IPriceFeedContract[]): Promise<UpdatePriceFeedsResult>;
|
|
22380
|
+
generatePriceFeedsUpdateTxs(priceFeeds?: IPriceFeedContract[], logContext?: Record<string, any>): Promise<UpdatePriceFeedsResult>;
|
|
22380
22381
|
mustGet(address: Address): IPriceFeedContract;
|
|
22381
22382
|
getOrCreate(data: PriceFeedTreeNode): IPriceFeedContract;
|
|
22382
22383
|
/**
|
|
@@ -25049,7 +25050,7 @@ declare class RouterV3Contract extends BaseContract<abi> implements IHooks<Route
|
|
|
25049
25050
|
getAvailableConnectors(collateralTokens: Array<Address>): Array<Address>;
|
|
25050
25051
|
}
|
|
25051
25052
|
declare function balancesMap(assets: Array<Asset>): AddressMap<bigint>;
|
|
25052
|
-
declare function assetsMap(assets: Array<
|
|
25053
|
+
declare function assetsMap<T extends Asset>(assets: Array<T> | readonly T[]): AddressMap<T>;
|
|
25053
25054
|
|
|
25054
25055
|
interface SDKOptions {
|
|
25055
25056
|
/**
|
|
@@ -25276,26 +25277,32 @@ interface RepayAndLiquidateCreditAccountProps {
|
|
|
25276
25277
|
to: Address;
|
|
25277
25278
|
permits: Record<string, PermitResult>;
|
|
25278
25279
|
}
|
|
25279
|
-
interface
|
|
25280
|
-
creditAccount: CreditAccountDataSlice;
|
|
25280
|
+
interface PrepareUpdateQuotasProps {
|
|
25281
25281
|
minQuota: Array<Asset>;
|
|
25282
25282
|
averageQuota: Array<Asset>;
|
|
25283
25283
|
}
|
|
25284
|
-
interface
|
|
25284
|
+
interface UpdateQuotasProps extends PrepareUpdateQuotasProps {
|
|
25285
|
+
creditAccount: CreditAccountDataSlice;
|
|
25286
|
+
}
|
|
25287
|
+
interface AddCollateralProps extends PrepareUpdateQuotasProps {
|
|
25285
25288
|
asset: Asset;
|
|
25286
25289
|
ethAmount: bigint;
|
|
25287
|
-
permit
|
|
25290
|
+
permit: PermitResult | undefined;
|
|
25291
|
+
creditAccount: CreditAccountDataSlice;
|
|
25288
25292
|
}
|
|
25289
|
-
interface WithdrawCollateralProps extends
|
|
25293
|
+
interface WithdrawCollateralProps extends PrepareUpdateQuotasProps {
|
|
25290
25294
|
assetsToWithdraw: Array<Asset>;
|
|
25291
25295
|
to: Address;
|
|
25296
|
+
creditAccount: CreditAccountDataSlice;
|
|
25292
25297
|
}
|
|
25293
|
-
interface ExecuteSwapProps extends
|
|
25298
|
+
interface ExecuteSwapProps extends PrepareUpdateQuotasProps {
|
|
25294
25299
|
calls: Array<MultiCall>;
|
|
25300
|
+
creditAccount: CreditAccountDataSlice;
|
|
25295
25301
|
}
|
|
25296
|
-
interface ClaimFarmRewardsProps extends
|
|
25302
|
+
interface ClaimFarmRewardsProps extends PrepareUpdateQuotasProps {
|
|
25297
25303
|
tokensToDisable: Array<Asset>;
|
|
25298
25304
|
calls: Array<MultiCall>;
|
|
25305
|
+
creditAccount: CreditAccountDataSlice;
|
|
25299
25306
|
}
|
|
25300
25307
|
interface ChangeDeptProps {
|
|
25301
25308
|
creditAccount: CreditAccountDataSlice;
|
|
@@ -25397,18 +25404,19 @@ declare class CreditAccountsService extends SDKConstruct {
|
|
|
25397
25404
|
* @returns
|
|
25398
25405
|
*/
|
|
25399
25406
|
getUpdateForAccounts(accounts: Array<CreditAccountDataSlice>): Promise<UpdatePriceFeedsResult>;
|
|
25407
|
+
getUpdateForAccount(creditManager: Address, creditAccount: CreditAccountDataSlice | undefined, desiredQuotas: Array<Asset> | undefined): Promise<UpdatePriceFeedsResult>;
|
|
25400
25408
|
/**
|
|
25401
25409
|
* Returns account price updates in a non-encoded format
|
|
25402
25410
|
* @param acc
|
|
25403
25411
|
* @returns
|
|
25404
25412
|
*/
|
|
25405
|
-
getOnDemandPriceUpdates(
|
|
25413
|
+
getOnDemandPriceUpdates(creditManager: Address, creditAccount: CreditAccountDataSlice | undefined, desiredQuotas: Array<Asset> | undefined): Promise<Array<OnDemandPriceUpdate>>;
|
|
25406
25414
|
/**
|
|
25407
25415
|
* Returns price updates in format that is accepted by various credit facade methods (multicall, close/liquidate, etc...)
|
|
25408
25416
|
* @param acc
|
|
25409
25417
|
* @returns
|
|
25410
25418
|
*/
|
|
25411
|
-
getPriceUpdatesForFacade(
|
|
25419
|
+
getPriceUpdatesForFacade(creditManager: Address, creditAccount: CreditAccountDataSlice | undefined, desiredQuotas: Array<Asset> | undefined): Promise<Array<MultiCall>>;
|
|
25412
25420
|
/**
|
|
25413
25421
|
* Returns addresses of pools of attached markets
|
|
25414
25422
|
*/
|
package/dist/esm/sdk/index.mjs
CHANGED
|
@@ -21223,8 +21223,9 @@ var RedstoneUpdater = class extends SDKConstruct {
|
|
|
21223
21223
|
setHistoricalTimestamp(timestampMs) {
|
|
21224
21224
|
this.#historicalTimestampMs = 6e4 * Math.floor(timestampMs / 6e4);
|
|
21225
21225
|
}
|
|
21226
|
-
async getUpdateTxs(feeds) {
|
|
21226
|
+
async getUpdateTxs(feeds, logContext = {}) {
|
|
21227
21227
|
this.#logger?.debug(
|
|
21228
|
+
logContext,
|
|
21228
21229
|
`generating update transactions for ${feeds.length} redstone price feeds`
|
|
21229
21230
|
);
|
|
21230
21231
|
const groupedFeeds = {};
|
|
@@ -21263,6 +21264,7 @@ var RedstoneUpdater = class extends SDKConstruct {
|
|
|
21263
21264
|
}
|
|
21264
21265
|
}
|
|
21265
21266
|
this.#logger?.debug(
|
|
21267
|
+
logContext,
|
|
21266
21268
|
`generated ${results.length} update transactions for redstone price feeds`
|
|
21267
21269
|
);
|
|
21268
21270
|
return results;
|
|
@@ -21471,9 +21473,10 @@ var PriceFeedRegister = class extends SDKConstruct {
|
|
|
21471
21473
|
/**
|
|
21472
21474
|
* Returns RawTxs to update price feeds
|
|
21473
21475
|
* @param priceFeeds top-level price feeds, actual updatable price feeds will be derived. If not provided will use all price feeds that are attached
|
|
21476
|
+
* @param logContext extra information for logging
|
|
21474
21477
|
* @returns
|
|
21475
21478
|
*/
|
|
21476
|
-
async generatePriceFeedsUpdateTxs(priceFeeds) {
|
|
21479
|
+
async generatePriceFeedsUpdateTxs(priceFeeds, logContext = {}) {
|
|
21477
21480
|
const updateables = priceFeeds ? priceFeeds.flatMap((pf) => pf.updatableDependencies()) : this.#feeds.values();
|
|
21478
21481
|
const txs = [];
|
|
21479
21482
|
const redstonePFs = [];
|
|
@@ -21484,7 +21487,10 @@ var PriceFeedRegister = class extends SDKConstruct {
|
|
|
21484
21487
|
}
|
|
21485
21488
|
let maxTimestamp = 0;
|
|
21486
21489
|
if (redstonePFs.length > 0) {
|
|
21487
|
-
const redstoneUpdates = await this.#redstoneUpdater.getUpdateTxs(
|
|
21490
|
+
const redstoneUpdates = await this.#redstoneUpdater.getUpdateTxs(
|
|
21491
|
+
redstonePFs,
|
|
21492
|
+
logContext
|
|
21493
|
+
);
|
|
21488
21494
|
for (const { tx, timestamp } of redstoneUpdates) {
|
|
21489
21495
|
if (timestamp > maxTimestamp) {
|
|
21490
21496
|
maxTimestamp = timestamp;
|
|
@@ -21494,6 +21500,7 @@ var PriceFeedRegister = class extends SDKConstruct {
|
|
|
21494
21500
|
}
|
|
21495
21501
|
const result = { txs, timestamp: maxTimestamp };
|
|
21496
21502
|
this.logger?.debug(
|
|
21503
|
+
logContext,
|
|
21497
21504
|
`generated ${txs.length} price feed update transactions, timestamp: ${maxTimestamp}`
|
|
21498
21505
|
);
|
|
21499
21506
|
if (txs.length) {
|
|
@@ -22741,7 +22748,9 @@ var CreditAccountsService = class extends SDKConstruct {
|
|
|
22741
22748
|
if (raw.success) {
|
|
22742
22749
|
return raw;
|
|
22743
22750
|
}
|
|
22744
|
-
const { txs: priceUpdateTxs, timestamp: _ } = await this.sdk.priceFeeds.generatePriceFeedsUpdateTxs(
|
|
22751
|
+
const { txs: priceUpdateTxs, timestamp: _ } = await this.sdk.priceFeeds.generatePriceFeedsUpdateTxs(void 0, {
|
|
22752
|
+
account
|
|
22753
|
+
});
|
|
22745
22754
|
const resp = await simulateMulticall(this.provider.publicClient, {
|
|
22746
22755
|
contracts: [
|
|
22747
22756
|
...priceUpdateTxs.map(rawTxToMulticallPriceUpdate),
|
|
@@ -22822,7 +22831,11 @@ var CreditAccountsService = class extends SDKConstruct {
|
|
|
22822
22831
|
creditManager: cm.creditManager,
|
|
22823
22832
|
slippage
|
|
22824
22833
|
});
|
|
22825
|
-
const priceUpdates = await this.getPriceUpdatesForFacade(
|
|
22834
|
+
const priceUpdates = await this.getPriceUpdatesForFacade(
|
|
22835
|
+
account.creditManager,
|
|
22836
|
+
account,
|
|
22837
|
+
void 0
|
|
22838
|
+
);
|
|
22826
22839
|
const tx = cm.creditFacade.liquidateCreditAccount(
|
|
22827
22840
|
account.creditAccount,
|
|
22828
22841
|
to,
|
|
@@ -22860,7 +22873,7 @@ var CreditAccountsService = class extends SDKConstruct {
|
|
|
22860
22873
|
...this.#prepareDecreaseDebt(ca),
|
|
22861
22874
|
...this.#prepareDisableTokens(ca),
|
|
22862
22875
|
...assetsToWithdraw.map(
|
|
22863
|
-
(t) => this.#prepareWithdrawToken(ca, t, MAX_UINT256, to)
|
|
22876
|
+
(t) => this.#prepareWithdrawToken(ca.creditFacade, t, MAX_UINT256, to)
|
|
22864
22877
|
)
|
|
22865
22878
|
];
|
|
22866
22879
|
const tx = operation === "close" ? cm.creditFacade.closeCreditAccount(ca.creditAccount, calls) : cm.creditFacade.multicall(ca.creditAccount, calls);
|
|
@@ -22888,13 +22901,13 @@ var CreditAccountsService = class extends SDKConstruct {
|
|
|
22888
22901
|
const cm = this.sdk.marketRegister.findCreditManager(ca.creditManager);
|
|
22889
22902
|
const addCollateral = collateralAssets.filter((a) => a.balance > 0);
|
|
22890
22903
|
const calls = [
|
|
22891
|
-
...this.#prepareAddCollateral(
|
|
22904
|
+
...this.#prepareAddCollateral(ca.creditFacade, addCollateral, permits),
|
|
22892
22905
|
...this.#prepareDisableQuotas(ca),
|
|
22893
22906
|
...this.#prepareDecreaseDebt(ca),
|
|
22894
22907
|
...this.#prepareDisableTokens(ca),
|
|
22895
22908
|
// TODO: probably needs a better way to handle reward tokens
|
|
22896
22909
|
...assetsToWithdraw.map(
|
|
22897
|
-
(t) => this.#prepareWithdrawToken(ca, t, MAX_UINT256, to)
|
|
22910
|
+
(t) => this.#prepareWithdrawToken(ca.creditFacade, t, MAX_UINT256, to)
|
|
22898
22911
|
)
|
|
22899
22912
|
];
|
|
22900
22913
|
const tx = operation === "close" ? cm.creditFacade.closeCreditAccount(ca.creditAccount, calls) : cm.creditFacade.multicall(ca.creditAccount, calls);
|
|
@@ -22917,13 +22930,17 @@ var CreditAccountsService = class extends SDKConstruct {
|
|
|
22917
22930
|
to
|
|
22918
22931
|
}) {
|
|
22919
22932
|
const cm = this.sdk.marketRegister.findCreditManager(ca.creditManager);
|
|
22920
|
-
const priceUpdates = await this.getPriceUpdatesForFacade(
|
|
22933
|
+
const priceUpdates = await this.getPriceUpdatesForFacade(
|
|
22934
|
+
ca.creditManager,
|
|
22935
|
+
ca,
|
|
22936
|
+
void 0
|
|
22937
|
+
);
|
|
22921
22938
|
const addCollateral = collateralAssets.filter((a) => a.balance > 0);
|
|
22922
22939
|
const calls = [
|
|
22923
22940
|
...priceUpdates,
|
|
22924
|
-
...this.#prepareAddCollateral(
|
|
22941
|
+
...this.#prepareAddCollateral(ca.creditFacade, addCollateral, permits),
|
|
22925
22942
|
...assetsToWithdraw.map(
|
|
22926
|
-
(t) => this.#prepareWithdrawToken(ca, t, MAX_UINT256, to)
|
|
22943
|
+
(t) => this.#prepareWithdrawToken(ca.creditFacade, t, MAX_UINT256, to)
|
|
22927
22944
|
)
|
|
22928
22945
|
];
|
|
22929
22946
|
const tx = cm.creditFacade.liquidateCreditAccount(
|
|
@@ -22938,14 +22955,18 @@ var CreditAccountsService = class extends SDKConstruct {
|
|
|
22938
22955
|
const cm = this.sdk.marketRegister.findCreditManager(
|
|
22939
22956
|
creditAccount.creditManager
|
|
22940
22957
|
);
|
|
22941
|
-
const priceUpdates = await this.getPriceUpdatesForFacade(
|
|
22958
|
+
const priceUpdates = await this.getPriceUpdatesForFacade(
|
|
22959
|
+
creditAccount.creditManager,
|
|
22960
|
+
creditAccount,
|
|
22961
|
+
void 0
|
|
22962
|
+
);
|
|
22942
22963
|
const calls = [
|
|
22943
22964
|
...priceUpdates,
|
|
22944
|
-
...this.#prepareUpdateQuotas(props)
|
|
22965
|
+
...this.#prepareUpdateQuotas(props.creditAccount.creditFacade, props)
|
|
22945
22966
|
];
|
|
22946
22967
|
const tx = cm.creditFacade.multicall(creditAccount.creditAccount, [
|
|
22947
22968
|
...priceUpdates,
|
|
22948
|
-
...this.#prepareUpdateQuotas(props)
|
|
22969
|
+
...this.#prepareUpdateQuotas(props.creditAccount.creditFacade, props)
|
|
22949
22970
|
]);
|
|
22950
22971
|
return {
|
|
22951
22972
|
tx,
|
|
@@ -22957,15 +22978,19 @@ var CreditAccountsService = class extends SDKConstruct {
|
|
|
22957
22978
|
const cm = this.sdk.marketRegister.findCreditManager(
|
|
22958
22979
|
creditAccount.creditManager
|
|
22959
22980
|
);
|
|
22960
|
-
const priceUpdatesCalls = await this.getPriceUpdatesForFacade(
|
|
22981
|
+
const priceUpdatesCalls = await this.getPriceUpdatesForFacade(
|
|
22982
|
+
creditAccount.creditManager,
|
|
22983
|
+
creditAccount,
|
|
22984
|
+
props.averageQuota
|
|
22985
|
+
);
|
|
22961
22986
|
const calls = [
|
|
22962
22987
|
...priceUpdatesCalls,
|
|
22963
22988
|
...this.#prepareAddCollateral(
|
|
22989
|
+
creditAccount.creditFacade,
|
|
22964
22990
|
[asset],
|
|
22965
|
-
creditAccount,
|
|
22966
22991
|
permit ? { [asset.token]: permit } : {}
|
|
22967
22992
|
),
|
|
22968
|
-
...this.#prepareUpdateQuotas(props)
|
|
22993
|
+
...this.#prepareUpdateQuotas(creditAccount.creditFacade, props)
|
|
22969
22994
|
];
|
|
22970
22995
|
const tx = cm.creditFacade.multicall(creditAccount.creditAccount, calls);
|
|
22971
22996
|
tx.value = ethAmount.toString(10);
|
|
@@ -22983,20 +23008,19 @@ var CreditAccountsService = class extends SDKConstruct {
|
|
|
22983
23008
|
const cm = this.sdk.marketRegister.findCreditManager(
|
|
22984
23009
|
creditAccount.creditManager
|
|
22985
23010
|
);
|
|
22986
|
-
const priceUpdatesCalls = await this.getPriceUpdatesForFacade(
|
|
23011
|
+
const priceUpdatesCalls = await this.getPriceUpdatesForFacade(
|
|
23012
|
+
creditAccount.creditManager,
|
|
23013
|
+
creditAccount,
|
|
23014
|
+
void 0
|
|
23015
|
+
);
|
|
22987
23016
|
const underlyingEnabled = (creditAccount.enabledTokensMask & 1n) === 1n;
|
|
22988
23017
|
const shouldEnable = !isDecrease && !underlyingEnabled;
|
|
22989
23018
|
const calls = [
|
|
22990
23019
|
...priceUpdatesCalls,
|
|
22991
|
-
...shouldEnable ? this.#prepareEnableTokens(creditAccount, [
|
|
22992
|
-
|
|
22993
|
-
|
|
22994
|
-
|
|
22995
|
-
abi: iCreditFacadeV3MulticallAbi,
|
|
22996
|
-
functionName: isDecrease ? "decreaseDebt" : "increaseDebt",
|
|
22997
|
-
args: [change]
|
|
22998
|
-
})
|
|
22999
|
-
}
|
|
23020
|
+
...shouldEnable ? this.#prepareEnableTokens(creditAccount.creditFacade, [
|
|
23021
|
+
creditAccount.underlying
|
|
23022
|
+
]) : [],
|
|
23023
|
+
this.#prepareChangeDebt(creditAccount.creditFacade, change, isDecrease)
|
|
23000
23024
|
];
|
|
23001
23025
|
const tx = cm.creditFacade.multicall(creditAccount.creditAccount, calls);
|
|
23002
23026
|
return { tx, calls };
|
|
@@ -23006,13 +23030,22 @@ var CreditAccountsService = class extends SDKConstruct {
|
|
|
23006
23030
|
const cm = this.sdk.marketRegister.findCreditManager(
|
|
23007
23031
|
creditAccount.creditManager
|
|
23008
23032
|
);
|
|
23009
|
-
const priceUpdatesCalls = await this.getPriceUpdatesForFacade(
|
|
23033
|
+
const priceUpdatesCalls = await this.getPriceUpdatesForFacade(
|
|
23034
|
+
creditAccount.creditManager,
|
|
23035
|
+
creditAccount,
|
|
23036
|
+
void 0
|
|
23037
|
+
);
|
|
23010
23038
|
const calls = [
|
|
23011
23039
|
...priceUpdatesCalls,
|
|
23012
23040
|
...assetsToWithdraw.map(
|
|
23013
|
-
(a) => this.#prepareWithdrawToken(
|
|
23041
|
+
(a) => this.#prepareWithdrawToken(
|
|
23042
|
+
creditAccount.creditFacade,
|
|
23043
|
+
a.token,
|
|
23044
|
+
a.balance,
|
|
23045
|
+
to
|
|
23046
|
+
)
|
|
23014
23047
|
),
|
|
23015
|
-
...this.#prepareUpdateQuotas(props)
|
|
23048
|
+
...this.#prepareUpdateQuotas(creditAccount.creditFacade, props)
|
|
23016
23049
|
];
|
|
23017
23050
|
const tx = cm.creditFacade.multicall(creditAccount.creditAccount, calls);
|
|
23018
23051
|
return { tx, calls };
|
|
@@ -23023,11 +23056,15 @@ var CreditAccountsService = class extends SDKConstruct {
|
|
|
23023
23056
|
const cm = this.sdk.marketRegister.findCreditManager(
|
|
23024
23057
|
creditAccount.creditManager
|
|
23025
23058
|
);
|
|
23026
|
-
const priceUpdatesCalls = await this.getPriceUpdatesForFacade(
|
|
23059
|
+
const priceUpdatesCalls = await this.getPriceUpdatesForFacade(
|
|
23060
|
+
creditAccount.creditManager,
|
|
23061
|
+
creditAccount,
|
|
23062
|
+
props.averageQuota
|
|
23063
|
+
);
|
|
23027
23064
|
const calls = [
|
|
23028
23065
|
...priceUpdatesCalls,
|
|
23029
23066
|
...swapCalls,
|
|
23030
|
-
...this.#prepareUpdateQuotas(props)
|
|
23067
|
+
...this.#prepareUpdateQuotas(creditAccount.creditFacade, props)
|
|
23031
23068
|
];
|
|
23032
23069
|
const tx = cm.creditFacade.multicall(creditAccount.creditAccount, calls);
|
|
23033
23070
|
return { tx, calls };
|
|
@@ -23038,14 +23075,18 @@ var CreditAccountsService = class extends SDKConstruct {
|
|
|
23038
23075
|
const cm = this.sdk.marketRegister.findCreditManager(
|
|
23039
23076
|
creditAccount.creditManager
|
|
23040
23077
|
);
|
|
23041
|
-
const priceUpdatesCalls = await this.getPriceUpdatesForFacade(
|
|
23078
|
+
const priceUpdatesCalls = await this.getPriceUpdatesForFacade(
|
|
23079
|
+
creditAccount.creditManager,
|
|
23080
|
+
creditAccount,
|
|
23081
|
+
props.averageQuota
|
|
23082
|
+
);
|
|
23042
23083
|
const calls = [
|
|
23043
23084
|
...priceUpdatesCalls,
|
|
23044
23085
|
...claimCalls,
|
|
23045
23086
|
...tokensToDisable.map(
|
|
23046
|
-
(a) => this.#prepareDisableToken(a.token
|
|
23087
|
+
(a) => this.#prepareDisableToken(creditAccount.creditFacade, a.token)
|
|
23047
23088
|
),
|
|
23048
|
-
...this.#prepareUpdateQuotas(props)
|
|
23089
|
+
...this.#prepareUpdateQuotas(creditAccount.creditFacade, props)
|
|
23049
23090
|
];
|
|
23050
23091
|
const tx = cm.creditFacade.multicall(creditAccount.creditAccount, calls);
|
|
23051
23092
|
return { tx, calls };
|
|
@@ -23116,16 +23157,64 @@ var CreditAccountsService = class extends SDKConstruct {
|
|
|
23116
23157
|
}
|
|
23117
23158
|
return this.sdk.priceFeeds.generatePriceFeedsUpdateTxs(priceFeeds);
|
|
23118
23159
|
}
|
|
23160
|
+
async getUpdateForAccount(creditManager, creditAccount, desiredQuotas) {
|
|
23161
|
+
const tokensByPool = /* @__PURE__ */ new Map();
|
|
23162
|
+
const oracleByPool = /* @__PURE__ */ new Map();
|
|
23163
|
+
const quotaRecord = desiredQuotas ? assetsMap(desiredQuotas) : desiredQuotas;
|
|
23164
|
+
const caBalancesRecord = creditAccount ? assetsMap(creditAccount.tokens) : creditAccount;
|
|
23165
|
+
const market = this.sdk.marketRegister.findByCreditManager(creditManager);
|
|
23166
|
+
const cm = this.sdk.marketRegister.findCreditManager(creditManager).creditManager;
|
|
23167
|
+
const pool = market.poolFactory.pool.address;
|
|
23168
|
+
oracleByPool.set(pool, market.priceOracle);
|
|
23169
|
+
const insertToken = (p, t) => {
|
|
23170
|
+
const tokens = tokensByPool.get(p) ?? /* @__PURE__ */ new Set();
|
|
23171
|
+
tokens.add(t);
|
|
23172
|
+
tokensByPool.set(pool, tokens);
|
|
23173
|
+
};
|
|
23174
|
+
for (const t of cm.collateralTokens) {
|
|
23175
|
+
if (creditAccount && caBalancesRecord && quotaRecord) {
|
|
23176
|
+
const balanceAsset = caBalancesRecord.get(t);
|
|
23177
|
+
const balance = balanceAsset?.balance || 0n;
|
|
23178
|
+
const mask = balanceAsset?.mask || 0n;
|
|
23179
|
+
const isEnabled = (mask & creditAccount.enabledTokensMask) !== 0n;
|
|
23180
|
+
const quotaAsset = quotaRecord.get(t);
|
|
23181
|
+
const quotaBalance = quotaAsset?.balance || 0n;
|
|
23182
|
+
if (balance > 10n && isEnabled || quotaBalance > 0)
|
|
23183
|
+
insertToken(pool, t);
|
|
23184
|
+
} else if (creditAccount && caBalancesRecord) {
|
|
23185
|
+
const balanceAsset = caBalancesRecord.get(t);
|
|
23186
|
+
const balance = balanceAsset?.balance || 0n;
|
|
23187
|
+
const mask = balanceAsset?.mask || 0n;
|
|
23188
|
+
const isEnabled = (mask & creditAccount.enabledTokensMask) !== 0n;
|
|
23189
|
+
if (balance > 10n && isEnabled) insertToken(pool, t);
|
|
23190
|
+
} else if (quotaRecord) {
|
|
23191
|
+
const quotaAsset = quotaRecord.get(t);
|
|
23192
|
+
const quotaBalance = quotaAsset?.balance || 0n;
|
|
23193
|
+
if (quotaBalance > 0) insertToken(pool, t);
|
|
23194
|
+
}
|
|
23195
|
+
}
|
|
23196
|
+
const priceFeeds = [];
|
|
23197
|
+
for (const [pool2, priceFeedFactory] of oracleByPool.entries()) {
|
|
23198
|
+
const tokens = Array.from(tokensByPool.get(pool2) ?? []);
|
|
23199
|
+
priceFeeds.push(...priceFeedFactory.priceFeedsForTokens(tokens));
|
|
23200
|
+
}
|
|
23201
|
+
return this.sdk.priceFeeds.generatePriceFeedsUpdateTxs(
|
|
23202
|
+
priceFeeds,
|
|
23203
|
+
creditAccount ? { account: creditAccount.creditAccount } : void 0
|
|
23204
|
+
);
|
|
23205
|
+
}
|
|
23119
23206
|
/**
|
|
23120
23207
|
* Returns account price updates in a non-encoded format
|
|
23121
23208
|
* @param acc
|
|
23122
23209
|
* @returns
|
|
23123
23210
|
*/
|
|
23124
|
-
async getOnDemandPriceUpdates(
|
|
23125
|
-
const market = this.sdk.marketRegister.findByCreditManager(
|
|
23126
|
-
|
|
23211
|
+
async getOnDemandPriceUpdates(creditManager, creditAccount, desiredQuotas) {
|
|
23212
|
+
const market = this.sdk.marketRegister.findByCreditManager(creditManager);
|
|
23213
|
+
const update = await this.getUpdateForAccount(
|
|
23214
|
+
creditManager,
|
|
23215
|
+
creditAccount,
|
|
23216
|
+
desiredQuotas
|
|
23127
23217
|
);
|
|
23128
|
-
const update = await this.getUpdateForAccounts([acc]);
|
|
23129
23218
|
return market.priceOracle.onDemandPriceUpdates(update);
|
|
23130
23219
|
}
|
|
23131
23220
|
/**
|
|
@@ -23133,9 +23222,13 @@ var CreditAccountsService = class extends SDKConstruct {
|
|
|
23133
23222
|
* @param acc
|
|
23134
23223
|
* @returns
|
|
23135
23224
|
*/
|
|
23136
|
-
async getPriceUpdatesForFacade(
|
|
23137
|
-
const cm = this.sdk.marketRegister.findCreditManager(
|
|
23138
|
-
const updates = await this.getOnDemandPriceUpdates(
|
|
23225
|
+
async getPriceUpdatesForFacade(creditManager, creditAccount, desiredQuotas) {
|
|
23226
|
+
const cm = this.sdk.marketRegister.findCreditManager(creditManager);
|
|
23227
|
+
const updates = await this.getOnDemandPriceUpdates(
|
|
23228
|
+
creditManager,
|
|
23229
|
+
creditAccount,
|
|
23230
|
+
desiredQuotas
|
|
23231
|
+
);
|
|
23139
23232
|
return cm.creditFacade.encodeOnDemandPriceUpdates(updates);
|
|
23140
23233
|
}
|
|
23141
23234
|
#prepareDisableQuotas(ca) {
|
|
@@ -23154,17 +23247,13 @@ var CreditAccountsService = class extends SDKConstruct {
|
|
|
23154
23247
|
}
|
|
23155
23248
|
return calls;
|
|
23156
23249
|
}
|
|
23157
|
-
#prepareUpdateQuotas({
|
|
23158
|
-
creditAccount,
|
|
23159
|
-
averageQuota,
|
|
23160
|
-
minQuota
|
|
23161
|
-
}) {
|
|
23250
|
+
#prepareUpdateQuotas(creditFacade, { averageQuota, minQuota }) {
|
|
23162
23251
|
const minRecord = assetsMap(minQuota);
|
|
23163
23252
|
const calls = averageQuota.map((q) => {
|
|
23164
23253
|
const minAsset = minRecord.get(q.token);
|
|
23165
23254
|
const min = minAsset && minAsset?.balance > 0 ? minAsset.balance : 0n;
|
|
23166
23255
|
return {
|
|
23167
|
-
target:
|
|
23256
|
+
target: creditFacade,
|
|
23168
23257
|
callData: encodeFunctionData({
|
|
23169
23258
|
abi: iCreditFacadeV3MulticallAbi,
|
|
23170
23259
|
functionName: "updateQuota",
|
|
@@ -23193,14 +23282,14 @@ var CreditAccountsService = class extends SDKConstruct {
|
|
|
23193
23282
|
const calls = [];
|
|
23194
23283
|
for (const t of ca.tokens) {
|
|
23195
23284
|
if (t.token !== ca.underlying && (t.mask & ca.enabledTokensMask) !== 0n && t.quota === 0n) {
|
|
23196
|
-
calls.push(this.#prepareDisableToken(t.token
|
|
23285
|
+
calls.push(this.#prepareDisableToken(ca.creditFacade, t.token));
|
|
23197
23286
|
}
|
|
23198
23287
|
}
|
|
23199
23288
|
return calls;
|
|
23200
23289
|
}
|
|
23201
|
-
#prepareDisableToken(
|
|
23290
|
+
#prepareDisableToken(creditFacade, token) {
|
|
23202
23291
|
return {
|
|
23203
|
-
target:
|
|
23292
|
+
target: creditFacade,
|
|
23204
23293
|
callData: encodeFunctionData({
|
|
23205
23294
|
abi: iCreditFacadeV3MulticallAbi,
|
|
23206
23295
|
functionName: "disableToken",
|
|
@@ -23208,9 +23297,9 @@ var CreditAccountsService = class extends SDKConstruct {
|
|
|
23208
23297
|
})
|
|
23209
23298
|
};
|
|
23210
23299
|
}
|
|
23211
|
-
#prepareEnableTokens(
|
|
23300
|
+
#prepareEnableTokens(creditFacade, tokens) {
|
|
23212
23301
|
return tokens.map((t) => ({
|
|
23213
|
-
target:
|
|
23302
|
+
target: creditFacade,
|
|
23214
23303
|
callData: encodeFunctionData({
|
|
23215
23304
|
abi: iCreditFacadeV3MulticallAbi,
|
|
23216
23305
|
functionName: "enableToken",
|
|
@@ -23218,9 +23307,9 @@ var CreditAccountsService = class extends SDKConstruct {
|
|
|
23218
23307
|
})
|
|
23219
23308
|
}));
|
|
23220
23309
|
}
|
|
23221
|
-
#prepareWithdrawToken(
|
|
23310
|
+
#prepareWithdrawToken(creditFacade, token, amount, to) {
|
|
23222
23311
|
return {
|
|
23223
|
-
target:
|
|
23312
|
+
target: creditFacade,
|
|
23224
23313
|
callData: encodeFunctionData({
|
|
23225
23314
|
abi: iCreditFacadeV3MulticallAbi,
|
|
23226
23315
|
functionName: "withdrawCollateral",
|
|
@@ -23228,12 +23317,22 @@ var CreditAccountsService = class extends SDKConstruct {
|
|
|
23228
23317
|
})
|
|
23229
23318
|
};
|
|
23230
23319
|
}
|
|
23231
|
-
#
|
|
23320
|
+
#prepareChangeDebt(creditFacade, change, isDecrease) {
|
|
23321
|
+
return {
|
|
23322
|
+
target: creditFacade,
|
|
23323
|
+
callData: encodeFunctionData({
|
|
23324
|
+
abi: iCreditFacadeV3MulticallAbi,
|
|
23325
|
+
functionName: isDecrease ? "decreaseDebt" : "increaseDebt",
|
|
23326
|
+
args: [change]
|
|
23327
|
+
})
|
|
23328
|
+
};
|
|
23329
|
+
}
|
|
23330
|
+
#prepareAddCollateral(creditFacade, assets, permits) {
|
|
23232
23331
|
const calls = assets.map(({ token, balance }) => {
|
|
23233
23332
|
const p = permits[token];
|
|
23234
23333
|
if (p) {
|
|
23235
23334
|
return {
|
|
23236
|
-
target:
|
|
23335
|
+
target: creditFacade,
|
|
23237
23336
|
callData: encodeFunctionData({
|
|
23238
23337
|
abi: iCreditFacadeV3MulticallAbi,
|
|
23239
23338
|
functionName: "addCollateralWithPermit",
|
|
@@ -23242,7 +23341,7 @@ var CreditAccountsService = class extends SDKConstruct {
|
|
|
23242
23341
|
};
|
|
23243
23342
|
}
|
|
23244
23343
|
return {
|
|
23245
|
-
target:
|
|
23344
|
+
target: creditFacade,
|
|
23246
23345
|
callData: encodeFunctionData({
|
|
23247
23346
|
abi: iCreditFacadeV3MulticallAbi,
|
|
23248
23347
|
functionName: "addCollateral",
|