@gearbox-protocol/sdk 14.12.0-next.74 → 14.12.0-next.76
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/plugins/accounts-counter/AccountsCounterPlugin.js +1 -1
- package/dist/cjs/sdk/accounts/CreditAccountsServiceV310.js +85 -584
- package/dist/cjs/sdk/accounts/bots/AccountBotsService.js +132 -0
- package/dist/cjs/sdk/accounts/bots/PeripheryCompressorV310Contract.js +31 -0
- package/dist/cjs/sdk/accounts/bots/abi.js +19 -0
- package/dist/cjs/sdk/accounts/bots/index.js +6 -0
- package/dist/cjs/sdk/accounts/bots/types.js +1 -0
- package/dist/cjs/sdk/accounts/credit-account-compressor/CreditAccountCompressor.js +280 -0
- package/dist/cjs/sdk/accounts/credit-account-compressor/CreditAccountCompressorV310Contract.js +141 -0
- package/dist/cjs/sdk/accounts/credit-account-compressor/index.js +6 -0
- package/dist/cjs/sdk/accounts/credit-account-compressor/types.js +1 -0
- package/dist/cjs/sdk/accounts/index.js +10 -0
- package/dist/cjs/sdk/index.js +8 -0
- package/dist/cjs/sdk/market/ZapperRegister.js +1 -1
- package/dist/cjs/sdk/market/credit/CreditSuite.js +31 -0
- package/dist/cjs/sdk/market/oracle/PriceOracleBaseContract.js +41 -1
- package/dist/cjs/sdk/market/oracle/PriceOracleV310Contract.js +0 -23
- package/dist/cjs/sdk/market/pricefeeds/PriceFeedsRegister.js +1 -1
- package/dist/esm/plugins/accounts-counter/AccountsCounterPlugin.js +1 -1
- package/dist/esm/sdk/accounts/CreditAccountsServiceV310.js +87 -586
- package/dist/esm/sdk/accounts/bots/AccountBotsService.js +131 -0
- package/dist/esm/sdk/accounts/bots/PeripheryCompressorV310Contract.js +31 -0
- package/dist/esm/sdk/accounts/bots/abi.js +18 -0
- package/dist/esm/sdk/accounts/bots/index.js +4 -0
- package/dist/esm/sdk/accounts/bots/types.js +1 -0
- package/dist/esm/sdk/accounts/credit-account-compressor/CreditAccountCompressor.js +279 -0
- package/dist/esm/sdk/accounts/credit-account-compressor/CreditAccountCompressorV310Contract.js +141 -0
- package/dist/esm/sdk/accounts/credit-account-compressor/index.js +4 -0
- package/dist/esm/sdk/accounts/credit-account-compressor/types.js +1 -0
- package/dist/esm/sdk/accounts/index.js +7 -1
- package/dist/esm/sdk/index.js +5 -1
- package/dist/esm/sdk/market/ZapperRegister.js +1 -1
- package/dist/esm/sdk/market/credit/CreditSuite.js +31 -0
- package/dist/esm/sdk/market/oracle/PriceOracleBaseContract.js +41 -1
- package/dist/esm/sdk/market/oracle/PriceOracleV310Contract.js +0 -23
- package/dist/esm/sdk/market/pricefeeds/PriceFeedsRegister.js +1 -1
- package/dist/types/new-sdk/AbstractNamespace.d.ts +2 -2
- package/dist/types/new-sdk/opportunities/OpportunitiesNamespace.d.ts +1 -1
- package/dist/types/new-sdk/positions/PositionsNamespace.d.ts +1 -1
- package/dist/types/plugins/accounts/AccountsPlugin.d.ts +1 -1
- package/dist/types/sdk/accounts/CreditAccountsServiceV310.d.ts +17 -58
- package/dist/types/sdk/accounts/bots/AccountBotsService.d.ts +38 -0
- package/dist/types/sdk/accounts/bots/PeripheryCompressorV310Contract.d.ts +236 -0
- package/dist/types/sdk/accounts/bots/abi.d.ts +18 -0
- package/dist/types/sdk/accounts/bots/index.d.ts +4 -0
- package/dist/types/sdk/accounts/bots/types.d.ts +143 -0
- package/dist/types/sdk/accounts/credit-account-compressor/CreditAccountCompressor.d.ts +60 -0
- package/dist/types/sdk/accounts/credit-account-compressor/CreditAccountCompressorV310Contract.d.ts +879 -0
- package/dist/types/sdk/accounts/credit-account-compressor/index.d.ts +4 -0
- package/dist/types/sdk/accounts/credit-account-compressor/types.d.ts +164 -0
- package/dist/types/sdk/accounts/index.d.ts +11 -3
- package/dist/types/sdk/accounts/liquidations/MultichainLiquidationsService.d.ts +2 -2
- package/dist/types/sdk/accounts/liquidations/types.d.ts +1 -1
- package/dist/types/sdk/accounts/types.d.ts +13 -194
- package/dist/types/sdk/base/TokensMeta.d.ts +1 -1
- package/dist/types/sdk/base/index.d.ts +2 -2
- package/dist/types/sdk/base/types.d.ts +6 -1
- package/dist/types/sdk/index.d.ts +11 -5
- package/dist/types/sdk/market/MarketConfiguratorContract.d.ts +1 -1
- package/dist/types/sdk/market/MarketSuite.d.ts +1 -1
- package/dist/types/sdk/market/credit/CreditManagerV310Contract.d.ts +1 -1
- package/dist/types/sdk/market/credit/CreditSuite.d.ts +26 -1
- package/dist/types/sdk/market/credit/types.d.ts +1 -1
- package/dist/types/sdk/market/index.d.ts +2 -2
- package/dist/types/sdk/market/oracle/PriceOracleBaseContract.d.ts +16 -5
- package/dist/types/sdk/market/oracle/PriceOracleV310Contract.d.ts +0 -11
- package/dist/types/sdk/market/oracle/index.d.ts +2 -2
- package/dist/types/sdk/market/oracle/types.d.ts +31 -15
- package/dist/types/sdk/market/pool/LinearInterestRateModelContract.d.ts +1 -1
- package/dist/types/sdk/market/pool/PoolQuotaKeeperV310Contract.d.ts +1 -1
- package/dist/types/sdk/market/pool/PoolSuite.d.ts +1 -1
- package/dist/types/sdk/market/pool/PoolV310Contract.d.ts +1 -1
- package/dist/types/sdk/market/pool/types.d.ts +1 -1
- package/dist/types/sdk/market/pricefeeds/AbstractPriceFeed.d.ts +1 -1
- package/dist/types/sdk/market/pricefeeds/types.d.ts +1 -1
- package/dist/types/sdk/opportunities/MultichainOpportunitiesService.d.ts +2 -2
- package/dist/types/sdk/pools/types.d.ts +1 -1
- package/dist/types/sdk/positions/MultichainPositionsService.d.ts +2 -2
- package/package.json +1 -1
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { iBotListV310Abi } from "../../../abi/310/generated.js";
|
|
2
|
+
import { AP_PERIPHERY_COMPRESSOR } from "../../constants/address-provider.js";
|
|
3
|
+
import { VERSION_RANGE_310 } from "../../constants/versions.js";
|
|
4
|
+
import "../../constants/index.js";
|
|
5
|
+
import { SDKConstruct } from "../../base/SDKConstruct.js";
|
|
6
|
+
import "../../base/index.js";
|
|
7
|
+
import { iBotAbi } from "./abi.js";
|
|
8
|
+
import { PeripheryCompressorV310Contract } from "./PeripheryCompressorV310Contract.js";
|
|
9
|
+
//#region src/sdk/accounts/bots/AccountBotsService.ts
|
|
10
|
+
/**
|
|
11
|
+
* Bots of credit accounts.
|
|
12
|
+
*
|
|
13
|
+
* Reads which bots are connected to an account and with which permissions,
|
|
14
|
+
* and builds the transactions that connect or disconnect one.
|
|
15
|
+
**/
|
|
16
|
+
var AccountBotsService = class extends SDKConstruct {
|
|
17
|
+
/**
|
|
18
|
+
* Reads the bots connected to each of the given credit accounts.
|
|
19
|
+
*
|
|
20
|
+
* All reads go out as a single multicall, split into three groups: the
|
|
21
|
+
* periphery compressor's view of each account, the status of the legacy
|
|
22
|
+
* migration bot on each account, and the status of every bot in
|
|
23
|
+
* `additionalBots` on every account.
|
|
24
|
+
*
|
|
25
|
+
* @param accountsToCheck - Accounts to read, with their credit managers.
|
|
26
|
+
* @param legacyMigrationBot - Legacy migration bot to check, if any.
|
|
27
|
+
* @param additionalBots - Bots to check on every account.
|
|
28
|
+
**/
|
|
29
|
+
async getConnectedBots(accountsToCheck, legacyMigrationBot, additionalBots) {
|
|
30
|
+
const compressor = this.#compressor;
|
|
31
|
+
const compressorCalls = accountsToCheck.map(({ creditManager, creditAccount }) => {
|
|
32
|
+
const { configurator } = this.sdk.marketRegister.findByCreditManager(creditManager);
|
|
33
|
+
return compressor.connectedBotsCall(configurator.address, creditAccount);
|
|
34
|
+
});
|
|
35
|
+
const migrationCalls = legacyMigrationBot ? accountsToCheck.map((account) => this.#botStatusCall(account, legacyMigrationBot)) : [];
|
|
36
|
+
const additionalCalls = accountsToCheck.flatMap((account) => additionalBots.map((bot) => this.#botStatusCall(account, bot)));
|
|
37
|
+
const responses = await this.client.multicall({
|
|
38
|
+
contracts: [
|
|
39
|
+
...compressorCalls,
|
|
40
|
+
...migrationCalls,
|
|
41
|
+
...additionalCalls
|
|
42
|
+
],
|
|
43
|
+
allowFailure: true,
|
|
44
|
+
batchSize: 0
|
|
45
|
+
});
|
|
46
|
+
const migrationStart = compressorCalls.length;
|
|
47
|
+
const additionalStart = migrationStart + migrationCalls.length;
|
|
48
|
+
return {
|
|
49
|
+
legacy: responses.slice(0, migrationStart),
|
|
50
|
+
legacyMigration: this.#migrationBotStatuses(legacyMigrationBot, accountsToCheck.length, responses.slice(migrationStart, additionalStart)),
|
|
51
|
+
additionalBots: this.#botStatusesPerAccount(accountsToCheck.length, additionalBots.length, responses.slice(additionalStart))
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Connects or disconnects a bot, and updates prices when the bot is set on a
|
|
56
|
+
* credit account.
|
|
57
|
+
*
|
|
58
|
+
* @param props - {@link SetBotProps}
|
|
59
|
+
* @returns Everything needed to execute the operation. Setting a bot on a
|
|
60
|
+
* credit manager only yields calls, since it is not a standalone transaction.
|
|
61
|
+
**/
|
|
62
|
+
async setBot({ botAddress, permissions: defaultPermissions, targetContract }) {
|
|
63
|
+
const cm = this.sdk.marketRegister.findCreditManager(targetContract.creditManager);
|
|
64
|
+
const permissions = defaultPermissions !== null ? defaultPermissions : await this.client.readContract({
|
|
65
|
+
address: botAddress,
|
|
66
|
+
abi: iBotAbi,
|
|
67
|
+
functionName: "requiredPermissions"
|
|
68
|
+
});
|
|
69
|
+
const addBotCall = cm.creditFacade.prepareSetBotPermissions(botAddress, permissions);
|
|
70
|
+
if (targetContract.type !== "creditAccount") return {
|
|
71
|
+
calls: [addBotCall],
|
|
72
|
+
creditFacade: cm.creditFacade
|
|
73
|
+
};
|
|
74
|
+
const calls = await this.sdk.accounts.prependPriceUpdates(targetContract.creditManager, [addBotCall], targetContract);
|
|
75
|
+
return {
|
|
76
|
+
tx: cm.multicallTx(targetContract.creditAccount, calls),
|
|
77
|
+
calls,
|
|
78
|
+
creditFacade: cm.creditFacade
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Descriptor of a `getBotStatus` call on the bot list of an account's
|
|
83
|
+
* credit facade.
|
|
84
|
+
**/
|
|
85
|
+
#botStatusCall({ creditManager, creditAccount }, bot) {
|
|
86
|
+
const { creditFacade } = this.sdk.marketRegister.findCreditManager(creditManager);
|
|
87
|
+
return {
|
|
88
|
+
abi: iBotListV310Abi,
|
|
89
|
+
address: creditFacade.botList,
|
|
90
|
+
functionName: "getBotStatus",
|
|
91
|
+
args: [bot, creditAccount]
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Regroups the flat `additionalBots` responses into one entry per account,
|
|
96
|
+
* keeping the order the bots were requested in.
|
|
97
|
+
**/
|
|
98
|
+
#botStatusesPerAccount(accounts, botsPerAccount, responses) {
|
|
99
|
+
this.#checkLength(responses, accounts * botsPerAccount, "bots");
|
|
100
|
+
const perAccount = [];
|
|
101
|
+
for (let i = 0; i < accounts; i++) perAccount.push({ result: responses.slice(i * botsPerAccount, (i + 1) * botsPerAccount) });
|
|
102
|
+
return perAccount;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Migration bot statuses, or `undefined` when no migration bot was queried.
|
|
106
|
+
**/
|
|
107
|
+
#migrationBotStatuses(bot, accounts, responses) {
|
|
108
|
+
if (!bot) return;
|
|
109
|
+
this.#checkLength(responses, accounts, "migration bots");
|
|
110
|
+
return {
|
|
111
|
+
result: responses,
|
|
112
|
+
botAddress: bot
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
#checkLength(responses, expected, what) {
|
|
116
|
+
if (responses.length !== expected) this.logger?.error(`result length mismatch for ${what}: got ${responses.length}, expected ${expected}`);
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Periphery compressor of the current chain.
|
|
120
|
+
*
|
|
121
|
+
* Resolved on every access, because the address provider is only populated
|
|
122
|
+
* once the SDK is attached or hydrated; the contracts register acts as the
|
|
123
|
+
* cache.
|
|
124
|
+
**/
|
|
125
|
+
get #compressor() {
|
|
126
|
+
const [address] = this.sdk.addressProvider.mustGetLatest(AP_PERIPHERY_COMPRESSOR, VERSION_RANGE_310);
|
|
127
|
+
return this.sdk.getContract(address) ?? new PeripheryCompressorV310Contract(this.sdk, address);
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
//#endregion
|
|
131
|
+
export { AccountBotsService };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { BaseContract } from "../../base/BaseContract.js";
|
|
2
|
+
import "../../base/index.js";
|
|
3
|
+
import { peripheryCompressorAbi } from "../../../abi/compressors/peripheryCompressor.js";
|
|
4
|
+
//#region src/sdk/accounts/bots/PeripheryCompressorV310Contract.ts
|
|
5
|
+
const abi = peripheryCompressorAbi;
|
|
6
|
+
var PeripheryCompressorV310Contract = class extends BaseContract {
|
|
7
|
+
constructor(sdk, address) {
|
|
8
|
+
super(sdk, {
|
|
9
|
+
addr: address,
|
|
10
|
+
name: "PeripheryCompressorV310",
|
|
11
|
+
abi,
|
|
12
|
+
version: 310
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Descriptor of a `getConnectedBots` call on this compressor.
|
|
17
|
+
*
|
|
18
|
+
* @param marketConfigurator - Configurator that governs the account's market.
|
|
19
|
+
* @param creditAccount - Credit account to list the connected bots of.
|
|
20
|
+
**/
|
|
21
|
+
connectedBotsCall(marketConfigurator, creditAccount) {
|
|
22
|
+
return {
|
|
23
|
+
abi,
|
|
24
|
+
address: this.address,
|
|
25
|
+
functionName: "getConnectedBots",
|
|
26
|
+
args: [marketConfigurator, creditAccount]
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
//#endregion
|
|
31
|
+
export { PeripheryCompressorV310Contract };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
//#region src/sdk/accounts/bots/abi.ts
|
|
2
|
+
/**
|
|
3
|
+
* Minimal bot interface: the permissions a bot needs to operate on a credit
|
|
4
|
+
* account, which the SDK reads when the caller does not specify them.
|
|
5
|
+
**/
|
|
6
|
+
const iBotAbi = [{
|
|
7
|
+
type: "function",
|
|
8
|
+
name: "requiredPermissions",
|
|
9
|
+
inputs: [],
|
|
10
|
+
outputs: [{
|
|
11
|
+
name: "",
|
|
12
|
+
type: "uint192",
|
|
13
|
+
internalType: "uint192"
|
|
14
|
+
}],
|
|
15
|
+
stateMutability: "view"
|
|
16
|
+
}];
|
|
17
|
+
//#endregion
|
|
18
|
+
export { iBotAbi };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
import { AddressMap } from "../../utils/AddressMap.js";
|
|
2
|
+
import { AddressSet } from "../../utils/AddressSet.js";
|
|
3
|
+
import { AP_CREDIT_ACCOUNT_COMPRESSOR } from "../../constants/address-provider.js";
|
|
4
|
+
import { ADDRESS_0X0 } from "../../constants/addresses.js";
|
|
5
|
+
import { MAX_UINT256 } from "../../constants/math.js";
|
|
6
|
+
import { VERSION_RANGE_310 } from "../../constants/versions.js";
|
|
7
|
+
import "../../constants/index.js";
|
|
8
|
+
import { hexEq } from "../../utils/hex.js";
|
|
9
|
+
import "../../utils/index.js";
|
|
10
|
+
import { SDKConstruct } from "../../base/SDKConstruct.js";
|
|
11
|
+
import "../../base/index.js";
|
|
12
|
+
import { iRWAFactoryAbi } from "../../../abi/rwa/iRWAFactory.js";
|
|
13
|
+
import { borrowApyBps, healthFactorBps, positionLeverage, usdToNumber } from "../../market/math.js";
|
|
14
|
+
import { dominantCollateral } from "../../market/credit/dominantCollateral.js";
|
|
15
|
+
import { simulateWithPriceUpdates } from "../../utils/viem/simulateWithPriceUpdates.js";
|
|
16
|
+
import "../../utils/viem/index.js";
|
|
17
|
+
import "../../market/index.js";
|
|
18
|
+
import { CreditAccountCompressorV310Contract } from "./CreditAccountCompressorV310Contract.js";
|
|
19
|
+
//#region src/sdk/accounts/credit-account-compressor/CreditAccountCompressor.ts
|
|
20
|
+
/**
|
|
21
|
+
* Reads credit accounts of the current chain.
|
|
22
|
+
*
|
|
23
|
+
* Stitches the credit account compressor together with the RWA factories (for
|
|
24
|
+
* accounts owned via an investor EOA) and with the withdrawal compressor (for
|
|
25
|
+
* assets that are on their way out of an account), and describes the result
|
|
26
|
+
* either as raw account data or as {@link StrategyPosition}s.
|
|
27
|
+
*
|
|
28
|
+
* TODO: create and deploy new compressor contract onchain to avoid all this stitching
|
|
29
|
+
**/
|
|
30
|
+
var CreditAccountCompressor = class extends SDKConstruct {
|
|
31
|
+
/**
|
|
32
|
+
* Reads data of a single credit account.
|
|
33
|
+
*
|
|
34
|
+
* When the compressor cannot value the account with current prices, the read
|
|
35
|
+
* is retried with price feed updates applied.
|
|
36
|
+
*
|
|
37
|
+
* @param account - Credit account address.
|
|
38
|
+
* @param blockNumber - Block to read at, defaults to the latest block.
|
|
39
|
+
* @returns Account data, or `undefined` if the account does not exist.
|
|
40
|
+
**/
|
|
41
|
+
async getCreditAccountData(account, blockNumber) {
|
|
42
|
+
const contract = this.#contract;
|
|
43
|
+
const raw = await contract.getCreditAccountData(account, blockNumber);
|
|
44
|
+
if (!raw) return;
|
|
45
|
+
const marketSuite = this.sdk.marketRegister.findByCreditManager(raw.creditManager);
|
|
46
|
+
const factory = marketSuite.rwaFactory;
|
|
47
|
+
let ca;
|
|
48
|
+
let investor;
|
|
49
|
+
if (raw.success) {
|
|
50
|
+
ca = raw;
|
|
51
|
+
investor = await factory?.getInvestor(raw.creditAccount, false);
|
|
52
|
+
} else {
|
|
53
|
+
const { txs: priceUpdateTxs } = await marketSuite.priceOracle.priceUpdateTxsForAccount(raw);
|
|
54
|
+
[ca, investor] = await simulateWithPriceUpdates(this.client, {
|
|
55
|
+
priceUpdates: priceUpdateTxs,
|
|
56
|
+
contracts: [contract.dataCall(account), ...factory ? [{
|
|
57
|
+
abi: iRWAFactoryAbi,
|
|
58
|
+
address: factory.address,
|
|
59
|
+
functionName: "getInvestor",
|
|
60
|
+
args: [raw.creditAccount]
|
|
61
|
+
}] : []],
|
|
62
|
+
blockNumber,
|
|
63
|
+
gas: this.sdk.gasLimit
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
return {
|
|
67
|
+
...ca,
|
|
68
|
+
investor
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Reads all credit accounts matching the options, sorted by health factor
|
|
73
|
+
* ascending.
|
|
74
|
+
*
|
|
75
|
+
* @param options - {@link GetCreditAccountsOptions}
|
|
76
|
+
* @param blockNumber - Block to read at, defaults to the latest block.
|
|
77
|
+
**/
|
|
78
|
+
async getCreditAccounts(options, blockNumber) {
|
|
79
|
+
const { creditManager, includeZeroDebt = false, maxHealthFactor = MAX_UINT256, minHealthFactor = 0n, owner = ADDRESS_0X0, ignoreReservePrices = false } = options ?? {};
|
|
80
|
+
const target = creditManager ?? {
|
|
81
|
+
configurators: this.#marketConfigurators,
|
|
82
|
+
creditManagers: [],
|
|
83
|
+
pools: [],
|
|
84
|
+
underlying: "0x0000000000000000000000000000000000000000"
|
|
85
|
+
};
|
|
86
|
+
const { txs: priceUpdateTxs } = await this.sdk.priceFeeds.generatePriceFeedsUpdateTxs(ignoreReservePrices ? { main: true } : void 0);
|
|
87
|
+
return (await this.#contract.getCreditAccounts(target, {
|
|
88
|
+
owner,
|
|
89
|
+
includeZeroDebt,
|
|
90
|
+
minHealthFactor,
|
|
91
|
+
maxHealthFactor
|
|
92
|
+
}, {
|
|
93
|
+
blockNumber,
|
|
94
|
+
priceUpdateTxs
|
|
95
|
+
})).sort((a, b) => Number(a.healthFactor - b.healthFactor));
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Reads all credit accounts of a borrower, sorted by health factor
|
|
99
|
+
* ascending.
|
|
100
|
+
*
|
|
101
|
+
* Covers accounts the borrower owns directly and RWA accounts they own as an
|
|
102
|
+
* investor, which are owned on-chain by an RWA factory.
|
|
103
|
+
*
|
|
104
|
+
* @param borrower - Wallet address.
|
|
105
|
+
* @param options - {@link GetCreditAccountsOptions}
|
|
106
|
+
* @param blockNumber - Block to read at, defaults to the latest block.
|
|
107
|
+
**/
|
|
108
|
+
async getBorrowerCreditAccounts(borrower, options, blockNumber) {
|
|
109
|
+
const { creditManager, includeZeroDebt = false, maxHealthFactor = MAX_UINT256, minHealthFactor = 0n, ignoreReservePrices = false } = options ?? {};
|
|
110
|
+
const { txs: priceUpdateTxs } = await this.sdk.priceFeeds.generatePriceFeedsUpdateTxs(ignoreReservePrices ? { main: true } : void 0);
|
|
111
|
+
const rwaAccountAddresses = (await this.sdk.rwa.getInvestorData(borrower)).flatMap((d) => d.creditAccounts.map((ca) => ca.creditAccount));
|
|
112
|
+
const cmFilter = creditManager ? {
|
|
113
|
+
configurators: [],
|
|
114
|
+
creditManagers: [creditManager],
|
|
115
|
+
pools: [],
|
|
116
|
+
underlying: ADDRESS_0X0
|
|
117
|
+
} : {
|
|
118
|
+
configurators: this.#marketConfigurators,
|
|
119
|
+
creditManagers: [],
|
|
120
|
+
pools: [],
|
|
121
|
+
underlying: ADDRESS_0X0
|
|
122
|
+
};
|
|
123
|
+
const permissiveFilter = {
|
|
124
|
+
owner: borrower,
|
|
125
|
+
includeZeroDebt: true,
|
|
126
|
+
minHealthFactor: 0n,
|
|
127
|
+
maxHealthFactor: MAX_UINT256,
|
|
128
|
+
reverting: false
|
|
129
|
+
};
|
|
130
|
+
const contract = this.#contract;
|
|
131
|
+
const rwaContracts = rwaAccountAddresses.map((account) => contract.dataCall(account));
|
|
132
|
+
const getCreditAccountsContracts = [false, true].map((reverting) => contract.accountsCall([
|
|
133
|
+
cmFilter,
|
|
134
|
+
{
|
|
135
|
+
...permissiveFilter,
|
|
136
|
+
reverting
|
|
137
|
+
},
|
|
138
|
+
0n
|
|
139
|
+
]));
|
|
140
|
+
const allContracts = [...rwaContracts, ...getCreditAccountsContracts];
|
|
141
|
+
const results = await simulateWithPriceUpdates(this.client, {
|
|
142
|
+
priceUpdates: priceUpdateTxs,
|
|
143
|
+
contracts: allContracts,
|
|
144
|
+
blockNumber,
|
|
145
|
+
gas: this.sdk.gasLimit
|
|
146
|
+
});
|
|
147
|
+
const rwaResults = results.slice(0, rwaAccountAddresses.length);
|
|
148
|
+
const normalResults = results.slice(rwaAccountAddresses.length);
|
|
149
|
+
const seen = new AddressSet();
|
|
150
|
+
const allCAs = [];
|
|
151
|
+
for (const ca of rwaResults) if (!seen.has(ca.creditAccount)) {
|
|
152
|
+
seen.add(ca.creditAccount);
|
|
153
|
+
allCAs.push({
|
|
154
|
+
...ca,
|
|
155
|
+
investor: borrower
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
for (const [accounts] of normalResults) for (const ca of accounts) if (!seen.has(ca.creditAccount)) {
|
|
159
|
+
seen.add(ca.creditAccount);
|
|
160
|
+
allCAs.push({
|
|
161
|
+
...ca,
|
|
162
|
+
investor: void 0
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
const filtered = allCAs.filter((ca) => {
|
|
166
|
+
if (!includeZeroDebt && ca.debt === 0n) return false;
|
|
167
|
+
if (ca.healthFactor < minHealthFactor) return false;
|
|
168
|
+
if (ca.healthFactor > maxHealthFactor) return false;
|
|
169
|
+
if (creditManager && !hexEq(ca.creditManager, creditManager)) return false;
|
|
170
|
+
return true;
|
|
171
|
+
});
|
|
172
|
+
this.logger?.debug(`loaded ${allCAs.length} borrower credit accounts (${rwaResults.length} RWA, ${filtered.length} after filter)`);
|
|
173
|
+
return filtered.sort((a, b) => Number(a.healthFactor - b.healthFactor));
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Describes all credit accounts of a wallet as strategy positions.
|
|
177
|
+
*
|
|
178
|
+
* @param props - {@link ListStrategyPositionsProps}
|
|
179
|
+
**/
|
|
180
|
+
async listPositions(props) {
|
|
181
|
+
const { owner, includeZeroDebt } = props;
|
|
182
|
+
const [accounts] = await Promise.all([this.getBorrowerCreditAccounts(owner, { includeZeroDebt }), this.sdk.withdrawalCompressor?.loadWithdrawableAssets()]);
|
|
183
|
+
const describable = accounts.filter((ca) => {
|
|
184
|
+
if (!ca.success) this.logger?.warn(`cannot describe position of ${this.labelAddress(ca.creditAccount)}: collateral computation failed`);
|
|
185
|
+
return ca.success;
|
|
186
|
+
});
|
|
187
|
+
const withdrawals = await Promise.all(describable.map((ca) => this.#accountWithdrawals(ca)));
|
|
188
|
+
return describable.map((ca, i) => this.#toStrategyPosition(ca, withdrawals[i] ?? new AddressMap()));
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Builds one strategy position from an account snapshot.
|
|
192
|
+
*
|
|
193
|
+
* @param withdrawals - Delayed withdrawals of the account, keyed by the
|
|
194
|
+
* phantom token that represents them on it.
|
|
195
|
+
**/
|
|
196
|
+
#toStrategyPosition(ca, withdrawals) {
|
|
197
|
+
const suite = this.sdk.marketRegister.findCreditManager(ca.creditManager);
|
|
198
|
+
const { market } = suite;
|
|
199
|
+
const { priceOracle } = market;
|
|
200
|
+
const { pool } = market.pool;
|
|
201
|
+
const token = this.sdk.tokensMeta.mustGetToken(market.unwrappedUnderlying);
|
|
202
|
+
const totalDebtValue = ca.debt + ca.accruedInterest + ca.accruedFees;
|
|
203
|
+
const collateral = dominantCollateral(ca, market);
|
|
204
|
+
return {
|
|
205
|
+
kind: "strategy",
|
|
206
|
+
chainId: this.sdk.chainId,
|
|
207
|
+
creditManager: ca.creditManager,
|
|
208
|
+
creditAccount: ca.creditAccount,
|
|
209
|
+
name: collateral ? suite.strategyName(collateral) : token.symbol,
|
|
210
|
+
targetCollateral: collateral ? this.sdk.tokensMeta.mustGetToken(collateral) : null,
|
|
211
|
+
leverage: positionLeverage(totalDebtValue, ca.totalValue),
|
|
212
|
+
borrowApy: borrowApyBps(pool.baseInterestRate, suite.creditManager.feeInterest),
|
|
213
|
+
totalDebt: {
|
|
214
|
+
token,
|
|
215
|
+
value: totalDebtValue,
|
|
216
|
+
valueUsd: usdToNumber(ca.totalDebtUSD)
|
|
217
|
+
},
|
|
218
|
+
totalValue: {
|
|
219
|
+
token,
|
|
220
|
+
value: ca.totalValue,
|
|
221
|
+
valueUsd: usdToNumber(ca.totalValueUSD)
|
|
222
|
+
},
|
|
223
|
+
healthFactor: healthFactorBps(ca.healthFactor),
|
|
224
|
+
collaterals: ca.tokens.flatMap((t) => {
|
|
225
|
+
if ((t.mask & ca.enabledTokensMask) === 0n || t.balance <= 10n) return [];
|
|
226
|
+
return [{
|
|
227
|
+
collateral: priceOracle.toTokenAmount(t.token, t.balance),
|
|
228
|
+
quota: priceOracle.toTokenAmount(market.underlying, t.quota),
|
|
229
|
+
withdrawals: withdrawals.get(t.token) ?? []
|
|
230
|
+
}];
|
|
231
|
+
})
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Delayed withdrawals of one account, keyed by the phantom token that
|
|
236
|
+
* represents them on it, so that each collateral row can pick up its own.
|
|
237
|
+
**/
|
|
238
|
+
async #accountWithdrawals(ca) {
|
|
239
|
+
const compressor = this.sdk.withdrawalCompressor;
|
|
240
|
+
const byPhantomToken = new AddressMap(void 0, "accountWithdrawals");
|
|
241
|
+
const holdsPhantomToken = ca.tokens.some((t) => t.balance > 10n && compressor?.getWithdrawalSourceToken(t.token) !== void 0);
|
|
242
|
+
if (!compressor || !holdsPhantomToken) return byPhantomToken;
|
|
243
|
+
const { priceOracle } = this.sdk.marketRegister.findByCreditManager(ca.creditManager);
|
|
244
|
+
const { claimable, pending } = await compressor.getCurrentWithdrawals(ca.creditAccount);
|
|
245
|
+
const add = (w, outputs, claimableAt) => {
|
|
246
|
+
const assets = outputs.map((o) => ({
|
|
247
|
+
isDelayed: true,
|
|
248
|
+
...priceOracle.toTokenAmount(o.token, o.amount),
|
|
249
|
+
redeemer: w.redeemer,
|
|
250
|
+
claimableAt: claimableAt === void 0 ? void 0 : Number(claimableAt)
|
|
251
|
+
}));
|
|
252
|
+
byPhantomToken.upsert(w.withdrawalPhantomToken, [...byPhantomToken.get(w.withdrawalPhantomToken) ?? [], ...assets]);
|
|
253
|
+
};
|
|
254
|
+
for (const w of claimable) add(w, w.outputs);
|
|
255
|
+
for (const w of pending) add(w, w.expectedOutputs, w.claimableAt);
|
|
256
|
+
return byPhantomToken;
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* Credit account compressor contract of the current chain.
|
|
260
|
+
*
|
|
261
|
+
* Resolved on every access, because the address provider is only populated
|
|
262
|
+
* once the SDK is attached or hydrated. The contracts register acts as the
|
|
263
|
+
* cache: instances register themselves there on construction, and a
|
|
264
|
+
* re-hydrated SDK with a different compressor address simply misses and
|
|
265
|
+
* builds a new wrapper.
|
|
266
|
+
**/
|
|
267
|
+
get #contract() {
|
|
268
|
+
const [address] = this.sdk.addressProvider.mustGetLatest(AP_CREDIT_ACCOUNT_COMPRESSOR, VERSION_RANGE_310);
|
|
269
|
+
return this.sdk.getContract(address) ?? new CreditAccountCompressorV310Contract(this.sdk, address);
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* Addresses of market configurators the SDK is attached to.
|
|
273
|
+
**/
|
|
274
|
+
get #marketConfigurators() {
|
|
275
|
+
return this.sdk.marketRegister.marketConfigurators.map((mc) => mc.address);
|
|
276
|
+
}
|
|
277
|
+
};
|
|
278
|
+
//#endregion
|
|
279
|
+
export { CreditAccountCompressor };
|
package/dist/esm/sdk/accounts/credit-account-compressor/CreditAccountCompressorV310Contract.js
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { BaseContract } from "../../base/BaseContract.js";
|
|
2
|
+
import "../../base/index.js";
|
|
3
|
+
import { simulateWithPriceUpdates } from "../../utils/viem/simulateWithPriceUpdates.js";
|
|
4
|
+
import "../../utils/viem/index.js";
|
|
5
|
+
import { creditAccountCompressorAbi } from "../../../abi/compressors/creditAccountCompressor.js";
|
|
6
|
+
//#region src/sdk/accounts/credit-account-compressor/CreditAccountCompressorV310Contract.ts
|
|
7
|
+
const abi = creditAccountCompressorAbi;
|
|
8
|
+
/**
|
|
9
|
+
* V3.10 credit account compressor.
|
|
10
|
+
*
|
|
11
|
+
* Wraps the compressor ABI and nothing else: single-account reads, paginated
|
|
12
|
+
* multi-account reads, and call descriptors for callers that need to batch
|
|
13
|
+
* these reads with calls to other contracts.
|
|
14
|
+
**/
|
|
15
|
+
var CreditAccountCompressorV310Contract = class extends BaseContract {
|
|
16
|
+
#sdk;
|
|
17
|
+
constructor(sdk, address) {
|
|
18
|
+
super(sdk, {
|
|
19
|
+
addr: address,
|
|
20
|
+
name: "CreditAccountCompressorV310",
|
|
21
|
+
abi,
|
|
22
|
+
version: 310
|
|
23
|
+
});
|
|
24
|
+
this.#sdk = sdk;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Reads data of a single credit account.
|
|
28
|
+
*
|
|
29
|
+
* @param account - Credit account address.
|
|
30
|
+
* @param blockNumber - Block to read at, defaults to the latest block.
|
|
31
|
+
* @returns Account data, or `undefined` if the compressor reverted, which it
|
|
32
|
+
* does when the account does not exist.
|
|
33
|
+
**/
|
|
34
|
+
async getCreditAccountData(account, blockNumber) {
|
|
35
|
+
try {
|
|
36
|
+
return await this.client.readContract({
|
|
37
|
+
abi,
|
|
38
|
+
address: this.address,
|
|
39
|
+
functionName: "getCreditAccountData",
|
|
40
|
+
args: [account],
|
|
41
|
+
blockNumber,
|
|
42
|
+
gas: this.#sdk.gasLimit
|
|
43
|
+
});
|
|
44
|
+
} catch (_e) {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Reads all credit accounts matching the filters, in the order the
|
|
50
|
+
* compressor returns them.
|
|
51
|
+
*
|
|
52
|
+
* Two compressor quirks are handled here: results are paginated, and the
|
|
53
|
+
* `reverting` account filter is exclusive, so accounts whose collateral
|
|
54
|
+
* computation reverts are only returned by a second pass.
|
|
55
|
+
*
|
|
56
|
+
* @param target - Credit managers to query.
|
|
57
|
+
* @param query - Account-level filters, without `reverting`.
|
|
58
|
+
* @param options - Block, price updates and page size.
|
|
59
|
+
**/
|
|
60
|
+
async getCreditAccounts(target, query, options) {
|
|
61
|
+
const { batchSize, blockNumber, priceUpdateTxs } = options ?? {};
|
|
62
|
+
const allCAs = [];
|
|
63
|
+
let revertingOffset = 0;
|
|
64
|
+
for (const reverting of [false, true]) {
|
|
65
|
+
let offset = 0n;
|
|
66
|
+
revertingOffset = allCAs.length;
|
|
67
|
+
do {
|
|
68
|
+
const [accounts, newOffset] = await this.#getCreditAccounts(batchSize ? [
|
|
69
|
+
target,
|
|
70
|
+
{
|
|
71
|
+
...query,
|
|
72
|
+
reverting
|
|
73
|
+
},
|
|
74
|
+
offset,
|
|
75
|
+
batchSize
|
|
76
|
+
] : [
|
|
77
|
+
target,
|
|
78
|
+
{
|
|
79
|
+
...query,
|
|
80
|
+
reverting
|
|
81
|
+
},
|
|
82
|
+
offset
|
|
83
|
+
], priceUpdateTxs, blockNumber);
|
|
84
|
+
allCAs.push(...accounts);
|
|
85
|
+
offset = newOffset;
|
|
86
|
+
} while (offset !== 0n);
|
|
87
|
+
}
|
|
88
|
+
this.logger?.debug(`loaded ${allCAs.length} credit accounts (${allCAs.length - revertingOffset} reverting)`);
|
|
89
|
+
return allCAs;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Descriptor of a `getCreditAccountData` call on this compressor.
|
|
93
|
+
**/
|
|
94
|
+
dataCall(account) {
|
|
95
|
+
return {
|
|
96
|
+
abi,
|
|
97
|
+
address: this.address,
|
|
98
|
+
functionName: "getCreditAccountData",
|
|
99
|
+
args: [account]
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Descriptor of a `getCreditAccounts` call on this compressor.
|
|
104
|
+
**/
|
|
105
|
+
accountsCall(args) {
|
|
106
|
+
return {
|
|
107
|
+
abi,
|
|
108
|
+
address: this.address,
|
|
109
|
+
functionName: "getCreditAccounts",
|
|
110
|
+
args
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* One page of `getCreditAccounts`, with price updates applied when the
|
|
115
|
+
* accounts hold tokens with on-demand price feeds.
|
|
116
|
+
**/
|
|
117
|
+
async #getCreditAccounts(args, priceUpdateTxs, blockNumber) {
|
|
118
|
+
let resp;
|
|
119
|
+
if (priceUpdateTxs?.length) [resp] = await simulateWithPriceUpdates(this.client, {
|
|
120
|
+
priceUpdates: priceUpdateTxs,
|
|
121
|
+
contracts: [this.accountsCall(args)],
|
|
122
|
+
blockNumber,
|
|
123
|
+
gas: this.#sdk.gasLimit
|
|
124
|
+
});
|
|
125
|
+
else resp = await this.client.readContract({
|
|
126
|
+
abi,
|
|
127
|
+
address: this.address,
|
|
128
|
+
functionName: "getCreditAccounts",
|
|
129
|
+
args,
|
|
130
|
+
blockNumber,
|
|
131
|
+
gas: this.#sdk.gasLimit
|
|
132
|
+
});
|
|
133
|
+
this.logger?.debug({
|
|
134
|
+
accounts: resp[0]?.length ?? 0,
|
|
135
|
+
nextOffset: Number(resp[1])
|
|
136
|
+
}, "got credit accounts");
|
|
137
|
+
return resp;
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
//#endregion
|
|
141
|
+
export { CreditAccountCompressorV310Contract };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
import { PeripheryCompressorV310Contract } from "./bots/PeripheryCompressorV310Contract.js";
|
|
2
|
+
import { AccountBotsService } from "./bots/AccountBotsService.js";
|
|
3
|
+
import "./bots/index.js";
|
|
4
|
+
import { CreditAccountCompressorV310Contract } from "./credit-account-compressor/CreditAccountCompressorV310Contract.js";
|
|
5
|
+
import { CreditAccountCompressor } from "./credit-account-compressor/CreditAccountCompressor.js";
|
|
6
|
+
import "./credit-account-compressor/index.js";
|
|
1
7
|
import { CreditAccountsServiceV310 } from "./CreditAccountsServiceV310.js";
|
|
2
8
|
import { primaryInstantOutput } from "./intents/operations/claim-delayed/index.js";
|
|
3
9
|
import { CreditAccountOperationsService } from "./intents/index.js";
|
|
@@ -18,4 +24,4 @@ import { toWithdrawalStatus } from "./withdrawal-compressor/types.js";
|
|
|
18
24
|
import { WithdrawalCompressorV313Contract } from "./withdrawal-compressor/WithdrawalCompressorV313Contract.js";
|
|
19
25
|
import { createWithdrawalCompressor } from "./withdrawal-compressor/createWithdrawalCompressor.js";
|
|
20
26
|
import "./withdrawal-compressor/index.js";
|
|
21
|
-
export { AbstractWithdrawalCompressorContract, CreditAccountOperationsService, CreditAccountsServiceV310, DELAYED_INTENT_TYPES, DELAYED_INTENT_VERSION, InvalidDelayedIntentError, LIQUIDATION_APPROVAL_BUFFER, LIQUIDATION_COMPRESSOR_V313_ADDRESS, LiquidationsService, MultichainLiquidationsService, RedemptionLoggerV310Contract, WithdrawalCompressorV310Contract, WithdrawalCompressorV311Contract, WithdrawalCompressorV313Contract, createRedemptionLogger, createWithdrawalCompressor, decodeDelayedIntent, encodeDelayedIntent, getWithdrawalCompressorAddress, iCreditAccountAbi, primaryInstantOutput, toClaimableWithdrawal, toPendingWithdrawal, toRequestableWithdrawal, toWithdrawalStatus };
|
|
27
|
+
export { AbstractWithdrawalCompressorContract, AccountBotsService, CreditAccountCompressor, CreditAccountCompressorV310Contract, CreditAccountOperationsService, CreditAccountsServiceV310, DELAYED_INTENT_TYPES, DELAYED_INTENT_VERSION, InvalidDelayedIntentError, LIQUIDATION_APPROVAL_BUFFER, LIQUIDATION_COMPRESSOR_V313_ADDRESS, LiquidationsService, MultichainLiquidationsService, PeripheryCompressorV310Contract, RedemptionLoggerV310Contract, WithdrawalCompressorV310Contract, WithdrawalCompressorV311Contract, WithdrawalCompressorV313Contract, createRedemptionLogger, createWithdrawalCompressor, decodeDelayedIntent, encodeDelayedIntent, getWithdrawalCompressorAddress, iCreditAccountAbi, primaryInstantOutput, toClaimableWithdrawal, toPendingWithdrawal, toRequestableWithdrawal, toWithdrawalStatus };
|