@gearbox-protocol/sdk 3.0.0-vfourabi.1 → 3.0.0-vfourabi.3
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/.eslintignore +3 -0
- package/.github/workflows/lint_pr.yml +18 -0
- package/.github/workflows/pr.yml +39 -0
- package/.github/workflows/release.yml +47 -0
- package/.gitignore +19 -0
- package/.husky/commit-msg +1 -0
- package/.husky/pre-commit +1 -0
- package/.nvmrc +1 -0
- package/.releaserc.json +34 -0
- package/.vscode/extensions.json +7 -0
- package/.vscode/settings.json +16 -0
- package/.yarn/install-state.gz +0 -0
- package/.yarnrc.yml +1 -0
- package/ContributionAgreement +72 -0
- package/dist/cjs/abi/iZapper.cjs +85 -0
- package/dist/cjs/abi/iZapper.d.ts +123 -0
- package/dist/cjs/sdk/index.cjs +19830 -19764
- package/dist/cjs/sdk/index.d.ts +3746 -3061
- package/dist/esm/abi/iZapper.d.mts +123 -0
- package/dist/esm/abi/iZapper.mjs +83 -0
- package/dist/esm/sdk/index.d.mts +3746 -3061
- package/dist/esm/sdk/index.mjs +19807 -19765
- package/jest.config.js +7 -0
- package/package.json +32 -2
- package/scripts/example.ts +30 -0
- package/src/abi/compressors.ts +3962 -0
- package/src/abi/errors.ts +196 -0
- package/src/abi/iERC20.ts +122 -0
- package/src/abi/iPausable.ts +35 -0
- package/src/abi/iPriceFeedStore.ts +227 -0
- package/src/abi/iUpdatablePriceFeed.ts +16 -0
- package/src/abi/iVersion.ts +28 -0
- package/src/abi/iZapper.ts +84 -0
- package/src/abi/routerV300.ts +378 -0
- package/src/abi/v300.ts +4151 -0
- package/src/abi/v310.ts +5179 -0
- package/src/adapters/AbstractAdapter.ts +28 -0
- package/src/adapters/BalancerV2VaultAdapterContract.ts +19 -0
- package/src/adapters/BalancerV3RouterAdapterContract.ts +19 -0
- package/src/adapters/CamelotV3AdapterContract.ts +20 -0
- package/src/adapters/ConvexV1BaseRewardPoolAdapterContract.ts +20 -0
- package/src/adapters/ConvexV1BoosterAdapterContract.ts +20 -0
- package/src/adapters/Curve2AssetsAdapterContract.ts +20 -0
- package/src/adapters/Curve3AssetsAdapterContract.ts +20 -0
- package/src/adapters/Curve4AssetsAdapterContract.ts +20 -0
- package/src/adapters/CurveV1AdapterStETHContract.ts +20 -0
- package/src/adapters/CurveV1StableNGAdapterContract.ts +20 -0
- package/src/adapters/DaiUsdsAdapterContract.ts +21 -0
- package/src/adapters/ERC4626AdapterContract.ts +20 -0
- package/src/adapters/MellowERC4626VaultAdapterContract.ts +20 -0
- package/src/adapters/MellowVaultAdapterContract.ts +20 -0
- package/src/adapters/PendleRouterAdapterContract.ts +21 -0
- package/src/adapters/StakingRewardsAdapterContract.ts +21 -0
- package/src/adapters/UniswapV2AdapterContract.ts +20 -0
- package/src/adapters/UniswapV3AdapterContract.ts +170 -0
- package/src/adapters/VelodromeV2AdapterContract.ts +20 -0
- package/src/adapters/WstETHV1AdapterContract.ts +20 -0
- package/src/adapters/YearnV2AdapterContract.ts +20 -0
- package/src/adapters/abi/adapters.ts +4325 -0
- package/src/adapters/abi/iBalancerV3RouterAdapter.ts +126 -0
- package/src/adapters/abi/index.ts +2 -0
- package/src/adapters/index.ts +2 -0
- package/src/adapters/plugin.ts +90 -0
- package/src/adapters/types.ts +62 -0
- package/src/dev/AccountOpener.ts +611 -0
- package/src/dev/PriceFeedStore.ts +95 -0
- package/src/dev/SDKExample.ts +167 -0
- package/src/dev/abi.ts +14 -0
- package/src/dev/calcLiquidatableLTs.ts +77 -0
- package/src/dev/createAnvilClient.ts +155 -0
- package/src/dev/index.ts +7 -0
- package/src/dev/setLTZero.ts +127 -0
- package/src/dev/setLTs.ts +68 -0
- package/src/sdk/GearboxSDK.ts +488 -0
- package/src/sdk/abi/index.ts +1 -0
- package/src/sdk/abi/oracles.ts +4614 -0
- package/src/sdk/accounts/CreditAccountsService.ts +1123 -0
- package/src/sdk/accounts/index.ts +1 -0
- package/src/sdk/base/AddressLabeller.ts +35 -0
- package/src/sdk/base/BaseContract.ts +237 -0
- package/src/sdk/base/IAddressLabeller.ts +13 -0
- package/src/sdk/base/PlaceholderContract.ts +12 -0
- package/src/sdk/base/SDKConstruct.ts +35 -0
- package/src/sdk/base/TokensMeta.ts +45 -0
- package/src/sdk/base/index.ts +6 -0
- package/src/sdk/base/types.ts +102 -0
- package/src/sdk/bots/BotsService.ts +92 -0
- package/src/sdk/bots/index.ts +2 -0
- package/src/sdk/bots/utils.ts +19 -0
- package/src/sdk/chain/Provider.ts +99 -0
- package/src/sdk/chain/chains.ts +41 -0
- package/src/sdk/chain/detectChain.ts +21 -0
- package/src/sdk/chain/index.ts +3 -0
- package/src/sdk/constants/address-provider.ts +47 -0
- package/src/sdk/constants/addresses.ts +82 -0
- package/src/sdk/constants/bot-permissions.ts +36 -0
- package/src/sdk/constants/index.ts +6 -0
- package/src/sdk/constants/math.ts +23 -0
- package/src/sdk/constants/networks.ts +38 -0
- package/src/sdk/constants/periphery.ts +7 -0
- package/src/sdk/core/BotListV3Contract.ts +103 -0
- package/src/sdk/core/GearStakingV3Contract.ts +84 -0
- package/src/sdk/core/address-provider/AbstractAddressProviderContract.ts +101 -0
- package/src/sdk/core/address-provider/AddressProviderV3Contract.ts +105 -0
- package/src/sdk/core/address-provider/AddressProviderV3_1Contract.ts +92 -0
- package/src/sdk/core/address-provider/getAddressProvider.ts +45 -0
- package/src/sdk/core/address-provider/index.ts +4 -0
- package/src/sdk/core/address-provider/types.ts +17 -0
- package/src/sdk/core/index.ts +3 -0
- package/src/sdk/gauges/GaugeStakingService.ts +100 -0
- package/src/sdk/gauges/index.ts +2 -0
- package/src/sdk/gauges/utils.ts +25 -0
- package/src/sdk/index.ts +17 -0
- package/src/sdk/market/MarketConfiguratorContract.ts +78 -0
- package/src/sdk/market/MarketRegister.ts +330 -0
- package/src/sdk/market/MarketSuite.ts +83 -0
- package/src/sdk/market/adapters/PlaceholderAdapterContracts.ts +18 -0
- package/src/sdk/market/adapters/factory.ts +34 -0
- package/src/sdk/market/adapters/index.ts +3 -0
- package/src/sdk/market/adapters/types.ts +7 -0
- package/src/sdk/market/credit/CreditConfiguratorV300Contract.ts +164 -0
- package/src/sdk/market/credit/CreditConfiguratorV310Contract.ts +139 -0
- package/src/sdk/market/credit/CreditFacadeV300Contract.ts +206 -0
- package/src/sdk/market/credit/CreditFacadeV310Contract.ts +190 -0
- package/src/sdk/market/credit/CreditManagerV300Contract.ts +122 -0
- package/src/sdk/market/credit/CreditManagerV310Contract.ts +122 -0
- package/src/sdk/market/credit/CreditSuite.ts +65 -0
- package/src/sdk/market/credit/createCreditConfigurator.ts +18 -0
- package/src/sdk/market/credit/createCreditFacade.ts +18 -0
- package/src/sdk/market/credit/createCreditManager.ts +18 -0
- package/src/sdk/market/credit/index.ts +8 -0
- package/src/sdk/market/credit/types.ts +42 -0
- package/src/sdk/market/index.ts +7 -0
- package/src/sdk/market/oracle/PriceOracleBaseContract.ts +376 -0
- package/src/sdk/market/oracle/PriceOracleV300Contract.ts +71 -0
- package/src/sdk/market/oracle/PriceOracleV310Contract.ts +43 -0
- package/src/sdk/market/oracle/createPriceOracle.ts +22 -0
- package/src/sdk/market/oracle/index.ts +4 -0
- package/src/sdk/market/oracle/types.ts +48 -0
- package/src/sdk/market/pool/GaugeContract.ts +120 -0
- package/src/sdk/market/pool/LinearInterestRateModelContract.ts +75 -0
- package/src/sdk/market/pool/PoolQuotaKeeperV300Contract.ts +89 -0
- package/src/sdk/market/pool/PoolQuotaKeeperV310Contract.ts +89 -0
- package/src/sdk/market/pool/PoolSuite.ts +76 -0
- package/src/sdk/market/pool/PoolV300Contract.ts +144 -0
- package/src/sdk/market/pool/PoolV310Contract.ts +144 -0
- package/src/sdk/market/pool/TumblerContract.ts +73 -0
- package/src/sdk/market/pool/createInterestRateModel.ts +33 -0
- package/src/sdk/market/pool/createPool.ts +19 -0
- package/src/sdk/market/pool/createPoolQuotaKeeper.ts +20 -0
- package/src/sdk/market/pool/createRateKeeper.ts +25 -0
- package/src/sdk/market/pool/index.ts +6 -0
- package/src/sdk/market/pool/types.ts +28 -0
- package/src/sdk/market/pricefeeds/AbstractLPPriceFeed.ts +71 -0
- package/src/sdk/market/pricefeeds/AbstractPriceFeed.ts +137 -0
- package/src/sdk/market/pricefeeds/BalancerStablePriceFeed.ts +24 -0
- package/src/sdk/market/pricefeeds/BalancerWeightedPriceFeed.ts +70 -0
- package/src/sdk/market/pricefeeds/BoundedPriceFeed.ts +34 -0
- package/src/sdk/market/pricefeeds/ChainlinkPriceFeed.ts +16 -0
- package/src/sdk/market/pricefeeds/CompositePriceFeed.ts +25 -0
- package/src/sdk/market/pricefeeds/CurveCryptoPriceFeed.ts +24 -0
- package/src/sdk/market/pricefeeds/CurveStablePriceFeed.ts +24 -0
- package/src/sdk/market/pricefeeds/CurveUSDPriceFeed.ts +24 -0
- package/src/sdk/market/pricefeeds/Erc4626PriceFeed.ts +36 -0
- package/src/sdk/market/pricefeeds/MellowLRTPriceFeed.ts +26 -0
- package/src/sdk/market/pricefeeds/PendleTWAPPTPriceFeed.ts +44 -0
- package/src/sdk/market/pricefeeds/PriceFeedRef.ts +34 -0
- package/src/sdk/market/pricefeeds/PriceFeedsRegister.ts +293 -0
- package/src/sdk/market/pricefeeds/RedstonePriceFeed.ts +65 -0
- package/src/sdk/market/pricefeeds/RedstoneUpdater.ts +304 -0
- package/src/sdk/market/pricefeeds/WstETHPriceFeed.ts +24 -0
- package/src/sdk/market/pricefeeds/YearnPriceFeed.ts +24 -0
- package/src/sdk/market/pricefeeds/ZeroPriceFeed.ts +17 -0
- package/src/sdk/market/pricefeeds/index.ts +20 -0
- package/src/sdk/market/pricefeeds/types.ts +82 -0
- package/src/sdk/market/pricefeeds/utils.ts +24 -0
- package/src/sdk/plugins/index.ts +10 -0
- package/src/sdk/router/PathOptionFactory.ts +147 -0
- package/src/sdk/router/RouterV3Contract.ts +558 -0
- package/src/sdk/router/index.ts +2 -0
- package/src/sdk/router/types.ts +48 -0
- package/src/sdk/sdk-gov-legacy/contracts/adapters.ts +39 -0
- package/src/sdk/sdk-gov-legacy/contracts/contracts.ts +647 -0
- package/src/sdk/sdk-gov-legacy/contracts/index.ts +2 -0
- package/src/sdk/sdk-gov-legacy/core/constants.ts +11 -0
- package/src/sdk/sdk-gov-legacy/index.ts +4 -0
- package/src/sdk/sdk-gov-legacy/oracles/PriceFeedType.ts +24 -0
- package/src/sdk/sdk-gov-legacy/oracles/index.ts +1 -0
- package/src/sdk/sdk-gov-legacy/tokens/aave.ts +101 -0
- package/src/sdk/sdk-gov-legacy/tokens/aura.ts +284 -0
- package/src/sdk/sdk-gov-legacy/tokens/balancer.ts +421 -0
- package/src/sdk/sdk-gov-legacy/tokens/compound.ts +79 -0
- package/src/sdk/sdk-gov-legacy/tokens/convex.ts +561 -0
- package/src/sdk/sdk-gov-legacy/tokens/curveLP.ts +480 -0
- package/src/sdk/sdk-gov-legacy/tokens/decimals.ts +301 -0
- package/src/sdk/sdk-gov-legacy/tokens/erc4626.ts +75 -0
- package/src/sdk/sdk-gov-legacy/tokens/gear.ts +273 -0
- package/src/sdk/sdk-gov-legacy/tokens/index.ts +18 -0
- package/src/sdk/sdk-gov-legacy/tokens/normal.ts +613 -0
- package/src/sdk/sdk-gov-legacy/tokens/quoted.ts +10 -0
- package/src/sdk/sdk-gov-legacy/tokens/stakingRewards.ts +41 -0
- package/src/sdk/sdk-gov-legacy/tokens/token.ts +2354 -0
- package/src/sdk/sdk-gov-legacy/tokens/tokenData.ts +44 -0
- package/src/sdk/sdk-gov-legacy/tokens/tokenType.ts +33 -0
- package/src/sdk/sdk-gov-legacy/tokens/wrapped.ts +28 -0
- package/src/sdk/sdk-gov-legacy/tokens/yearn.ts +122 -0
- package/src/sdk/sdk-gov-legacy/tokens/zircuit.ts +44 -0
- package/src/sdk/sdk-legacy/apy/index.ts +149 -0
- package/src/sdk/sdk-legacy/core/assets.ts +200 -0
- package/src/sdk/sdk-legacy/core/creditAccount.spec.ts +1966 -0
- package/src/sdk/sdk-legacy/core/creditAccount.ts +816 -0
- package/src/sdk/sdk-legacy/core/creditManager.ts +316 -0
- package/src/sdk/sdk-legacy/core/creditSession.ts +352 -0
- package/src/sdk/sdk-legacy/core/endpoint.ts +131 -0
- package/src/sdk/sdk-legacy/core/eventOrTx.ts +101 -0
- package/src/sdk/sdk-legacy/core/pool.ts +439 -0
- package/src/sdk/sdk-legacy/core/strategy.ts +82 -0
- package/src/sdk/sdk-legacy/core/transactions.ts +865 -0
- package/src/sdk/sdk-legacy/gearboxRewards/api.ts +704 -0
- package/src/sdk/sdk-legacy/gearboxRewards/apy.ts +267 -0
- package/src/sdk/sdk-legacy/gearboxRewards/extraAPY.ts +165 -0
- package/src/sdk/sdk-legacy/gearboxRewards/index.ts +3 -0
- package/src/sdk/sdk-legacy/gearboxRewards/merklAPI.ts +151 -0
- package/src/sdk/sdk-legacy/index.ts +21 -0
- package/src/sdk/sdk-legacy/pathfinder/core.ts +18 -0
- package/src/sdk/sdk-legacy/pathfinder/index.ts +1 -0
- package/src/sdk/sdk-legacy/payload/creditAccount.ts +48 -0
- package/src/sdk/sdk-legacy/payload/creditManager.ts +133 -0
- package/src/sdk/sdk-legacy/payload/creditSession.ts +143 -0
- package/src/sdk/sdk-legacy/payload/graphPayload.ts +10 -0
- package/src/sdk/sdk-legacy/payload/pool.ts +198 -0
- package/src/sdk/sdk-legacy/payload/token.ts +13 -0
- package/src/sdk/sdk-legacy/tokens/tokenData.ts +57 -0
- package/src/sdk/sdk-legacy/types/IAirdropDistributor.ts +103 -0
- package/src/sdk/sdk-legacy/types/IBaseRewardPool.ts +249 -0
- package/src/sdk/sdk-legacy/types/IDataCompressorV3.ts +957 -0
- package/src/sdk/sdk-legacy/types/IDegenDistributor.ts +70 -0
- package/src/sdk/sdk-legacy/types/IERC20Permit.ts +35 -0
- package/src/sdk/sdk-legacy/types/IERC20ZapperDeposits.ts +99 -0
- package/src/sdk/sdk-legacy/types/IETHZapperDeposits.ts +27 -0
- package/src/sdk/sdk-legacy/types/IFarmingPool.ts +229 -0
- package/src/sdk/sdk-legacy/types/IInterestRateModel.ts +23 -0
- package/src/sdk/sdk-legacy/types/IMulticall3.ts +242 -0
- package/src/sdk/sdk-legacy/types/IOffchainOracle.ts +31 -0
- package/src/sdk/sdk-legacy/types/index.ts +11 -0
- package/src/sdk/sdk-legacy/utils/formatter.ts +46 -0
- package/src/sdk/sdk-legacy/utils/index.ts +3 -0
- package/src/sdk/sdk-legacy/utils/math.ts +6 -0
- package/src/sdk/sdk-legacy/utils/price.ts +27 -0
- package/src/sdk/types/index.ts +5 -0
- package/src/sdk/types/logger.ts +13 -0
- package/src/sdk/types/state-human.ts +224 -0
- package/src/sdk/types/state.ts +8 -0
- package/src/sdk/types/transactions.ts +22 -0
- package/src/sdk/types/tvl.ts +4 -0
- package/src/sdk/utils/AddressMap.ts +111 -0
- package/src/sdk/utils/bytes32ToString.ts +6 -0
- package/src/sdk/utils/childLogger.ts +8 -0
- package/src/sdk/utils/createRawTx.ts +86 -0
- package/src/sdk/utils/etherscan.ts +42 -0
- package/src/sdk/utils/filterDust.ts +20 -0
- package/src/sdk/utils/formatter.ts +124 -0
- package/src/sdk/utils/index.ts +10 -0
- package/src/sdk/utils/internal/Hooks.ts +51 -0
- package/src/sdk/utils/internal/index.ts +1 -0
- package/src/sdk/utils/json.ts +27 -0
- package/src/sdk/utils/mappers.ts +38 -0
- package/src/sdk/utils/retry.ts +24 -0
- package/src/sdk/utils/viem/detectNetwork.ts +27 -0
- package/src/sdk/utils/viem/index.ts +3 -0
- package/src/sdk/utils/viem/sendRawTx.ts +36 -0
- package/src/sdk/utils/viem/simulateMulticall.ts +262 -0
- package/tsconfig.build.json +4 -0
- package/tsconfig.json +16 -0
- package/tsup.config.ts +77 -0
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import type { Address, ContractEventName, Log } from "viem";
|
|
2
|
+
|
|
3
|
+
import { iCreditManagerV310Abi } from "../../../abi/v310";
|
|
4
|
+
import type { CreditManagerData, CreditManagerState } from "../../base";
|
|
5
|
+
import { BaseContract } from "../../base";
|
|
6
|
+
import type { GearboxSDK } from "../../GearboxSDK";
|
|
7
|
+
import type { CreditManagerStateHuman } from "../../types";
|
|
8
|
+
import { AddressMap, fmtBinaryMask, percentFmt } from "../../utils";
|
|
9
|
+
import type { IAdapterContract } from "../adapters";
|
|
10
|
+
import { createAdapter } from "../adapters";
|
|
11
|
+
import type { ICreditManagerContract } from "./types";
|
|
12
|
+
|
|
13
|
+
const abi = iCreditManagerV310Abi;
|
|
14
|
+
type abi = typeof iCreditManagerV310Abi;
|
|
15
|
+
|
|
16
|
+
// Augmenting contract class with interface of compressor data object
|
|
17
|
+
export interface CreditManagerV310Contract
|
|
18
|
+
extends Omit<
|
|
19
|
+
CreditManagerState,
|
|
20
|
+
"baseParams" | "collateralTokens" | "liquidationThresholds"
|
|
21
|
+
>,
|
|
22
|
+
BaseContract<abi> {}
|
|
23
|
+
|
|
24
|
+
export class CreditManagerV310Contract
|
|
25
|
+
extends BaseContract<abi>
|
|
26
|
+
implements ICreditManagerContract
|
|
27
|
+
{
|
|
28
|
+
/**
|
|
29
|
+
* Mapping targetContract => adapter
|
|
30
|
+
*/
|
|
31
|
+
public readonly adapters: AddressMap<IAdapterContract>;
|
|
32
|
+
/**
|
|
33
|
+
* Mapping token address => liquidation threshold
|
|
34
|
+
*/
|
|
35
|
+
public readonly liquidationThresholds: AddressMap<number>;
|
|
36
|
+
|
|
37
|
+
constructor(sdk: GearboxSDK, { creditManager, adapters }: CreditManagerData) {
|
|
38
|
+
const { baseParams, collateralTokens, ...rest } = creditManager;
|
|
39
|
+
super(sdk, {
|
|
40
|
+
...baseParams,
|
|
41
|
+
name: `CreditManagerV310(${creditManager.name})`,
|
|
42
|
+
abi,
|
|
43
|
+
});
|
|
44
|
+
Object.assign(this, rest);
|
|
45
|
+
this.liquidationThresholds = new AddressMap(
|
|
46
|
+
collateralTokens.map(ct => [ct.token, ct.liquidationThreshold]),
|
|
47
|
+
"liquidationThresholds",
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
this.adapters = new AddressMap(undefined, "adapters");
|
|
51
|
+
for (const adapterData of adapters) {
|
|
52
|
+
try {
|
|
53
|
+
const adapter = createAdapter(this.sdk, adapterData);
|
|
54
|
+
adapter.name = `${adapter.name}(${this.name})`;
|
|
55
|
+
this.adapters.upsert(adapter.targetContract, adapter);
|
|
56
|
+
} catch (e) {
|
|
57
|
+
throw new Error(`cannot attach adapter: ${e}`, { cause: e });
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
public override stateHuman(raw?: boolean): CreditManagerStateHuman {
|
|
63
|
+
return {
|
|
64
|
+
...super.stateHuman(raw),
|
|
65
|
+
name: this.name,
|
|
66
|
+
accountFactory: this.labelAddress(this.accountFactory),
|
|
67
|
+
underlying: this.labelAddress(this.underlying),
|
|
68
|
+
pool: this.labelAddress(this.pool),
|
|
69
|
+
creditFacade: this.labelAddress(this.creditFacade),
|
|
70
|
+
creditConfigurator: this.labelAddress(this.creditConfigurator),
|
|
71
|
+
maxEnabledTokens: this.maxEnabledTokens,
|
|
72
|
+
collateralTokens: Object.fromEntries(
|
|
73
|
+
this.liquidationThresholds
|
|
74
|
+
.entries()
|
|
75
|
+
.map(([k, v]) => [
|
|
76
|
+
this.labelAddress(k as Address),
|
|
77
|
+
percentFmt(v, raw),
|
|
78
|
+
]),
|
|
79
|
+
) as Record<Address, string>,
|
|
80
|
+
feeInterest: percentFmt(this.feeInterest, raw),
|
|
81
|
+
feeLiquidation: percentFmt(this.feeLiquidation, raw),
|
|
82
|
+
liquidationDiscount: percentFmt(this.liquidationDiscount, raw),
|
|
83
|
+
feeLiquidationExpired: percentFmt(this.feeLiquidationExpired, raw),
|
|
84
|
+
liquidationDiscountExpired: percentFmt(
|
|
85
|
+
this.liquidationDiscountExpired,
|
|
86
|
+
raw,
|
|
87
|
+
),
|
|
88
|
+
quotedTokensMask: fmtBinaryMask(0n), // TODO: ?
|
|
89
|
+
contractsToAdapters: Object.fromEntries(
|
|
90
|
+
this.adapters
|
|
91
|
+
.entries()
|
|
92
|
+
.map(([k, v]) => [
|
|
93
|
+
this.labelAddress(k),
|
|
94
|
+
this.labelAddress(v.address),
|
|
95
|
+
]),
|
|
96
|
+
),
|
|
97
|
+
creditAccounts: [], // TODO: ?
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
public get collateralTokens(): Address[] {
|
|
102
|
+
return this.liquidationThresholds.keys();
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
public override processLog(
|
|
106
|
+
log: Log<
|
|
107
|
+
bigint,
|
|
108
|
+
number,
|
|
109
|
+
false,
|
|
110
|
+
undefined,
|
|
111
|
+
undefined,
|
|
112
|
+
abi,
|
|
113
|
+
ContractEventName<abi>
|
|
114
|
+
>,
|
|
115
|
+
): void {
|
|
116
|
+
switch (log.eventName) {
|
|
117
|
+
case "SetCreditConfigurator":
|
|
118
|
+
this.dirty = true;
|
|
119
|
+
break;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import type { Address } from "viem";
|
|
2
|
+
|
|
3
|
+
import { type MarketData, SDKConstruct } from "../../base";
|
|
4
|
+
import type { GearboxSDK } from "../../GearboxSDK";
|
|
5
|
+
import type { CreditSuiteStateHuman, TVL } from "../../types";
|
|
6
|
+
import createCreditConfigurator from "./createCreditConfigurator";
|
|
7
|
+
import createCreditFacade from "./createCreditFacade";
|
|
8
|
+
import createCreditManager from "./createCreditManager";
|
|
9
|
+
import type {
|
|
10
|
+
CreditFacadeContract,
|
|
11
|
+
ICreditConfiguratorContract,
|
|
12
|
+
ICreditManagerContract,
|
|
13
|
+
} from "./types";
|
|
14
|
+
|
|
15
|
+
export class CreditSuite extends SDKConstruct {
|
|
16
|
+
public readonly name: string;
|
|
17
|
+
public readonly pool: Address;
|
|
18
|
+
public readonly underlying: Address;
|
|
19
|
+
|
|
20
|
+
public readonly creditManager: ICreditManagerContract;
|
|
21
|
+
public readonly creditFacade: CreditFacadeContract;
|
|
22
|
+
public readonly creditConfigurator: ICreditConfiguratorContract;
|
|
23
|
+
|
|
24
|
+
constructor(sdk: GearboxSDK, marketData: MarketData, index: number) {
|
|
25
|
+
super(sdk);
|
|
26
|
+
const { creditManagers, pool } = marketData;
|
|
27
|
+
const creditManager = creditManagers[index];
|
|
28
|
+
const { name } = creditManager.creditManager;
|
|
29
|
+
|
|
30
|
+
this.name = name;
|
|
31
|
+
this.pool = pool.baseParams.addr;
|
|
32
|
+
this.underlying = pool.underlying;
|
|
33
|
+
|
|
34
|
+
this.creditManager = createCreditManager(sdk, creditManager);
|
|
35
|
+
this.creditFacade = createCreditFacade(sdk, creditManager);
|
|
36
|
+
this.creditConfigurator = createCreditConfigurator(sdk, creditManager);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async tvl(): Promise<TVL> {
|
|
40
|
+
// const cas =
|
|
41
|
+
// await this.service.peripheryFactory.dataCompressorV3Contract.getCreditsAccountByCreditManager(
|
|
42
|
+
// this.creditManager.address,
|
|
43
|
+
// );
|
|
44
|
+
const tvl = 0n; // cas.reduce((acc, ca) => acc + BigInt(ca.totalValue), 0n);
|
|
45
|
+
const tvlUSD = 0n; // cas.reduce((acc, ca) => acc + BigInt(ca.totalValueUSD), 0n);
|
|
46
|
+
return { tvl, tvlUSD };
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
override get dirty(): boolean {
|
|
50
|
+
// TODO: any other ways to get dirty, adapters maybe?
|
|
51
|
+
return (
|
|
52
|
+
this.creditFacade.dirty ||
|
|
53
|
+
this.creditManager.dirty ||
|
|
54
|
+
this.creditConfigurator.dirty
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
public stateHuman(raw = true): CreditSuiteStateHuman {
|
|
59
|
+
return {
|
|
60
|
+
creditFacade: this.creditFacade.stateHuman(raw),
|
|
61
|
+
creditManager: this.creditManager.stateHuman(raw),
|
|
62
|
+
creditConfigurator: this.creditConfigurator.stateHuman(raw),
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { CreditManagerData } from "../../base";
|
|
2
|
+
import type { GearboxSDK } from "../../GearboxSDK";
|
|
3
|
+
import { CreditConfiguratorV300Contract } from "./CreditConfiguratorV300Contract";
|
|
4
|
+
import { CreditConfiguratorV310Contract } from "./CreditConfiguratorV310Contract";
|
|
5
|
+
import type { ICreditConfiguratorContract } from "./types";
|
|
6
|
+
|
|
7
|
+
export default function createCreditConfigurator(
|
|
8
|
+
sdk: GearboxSDK,
|
|
9
|
+
data: CreditManagerData,
|
|
10
|
+
): ICreditConfiguratorContract {
|
|
11
|
+
const v = data.creditConfigurator.baseParams.version;
|
|
12
|
+
if (v >= 300 && v < 310) {
|
|
13
|
+
return new CreditConfiguratorV300Contract(sdk, data);
|
|
14
|
+
} else if (v === 310n) {
|
|
15
|
+
return new CreditConfiguratorV310Contract(sdk, data);
|
|
16
|
+
}
|
|
17
|
+
throw new Error(`Unsupported credit configurator version: ${v}`);
|
|
18
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { CreditManagerData } from "../../base";
|
|
2
|
+
import type { GearboxSDK } from "../../GearboxSDK";
|
|
3
|
+
import { CreditFacadeV300Contract } from "./CreditFacadeV300Contract";
|
|
4
|
+
import { CreditFacadeV310Contract } from "./CreditFacadeV310Contract";
|
|
5
|
+
import type { CreditFacadeContract } from "./types";
|
|
6
|
+
|
|
7
|
+
export default function createCreditFacade(
|
|
8
|
+
sdk: GearboxSDK,
|
|
9
|
+
data: CreditManagerData,
|
|
10
|
+
): CreditFacadeContract {
|
|
11
|
+
const v = data.creditFacade.baseParams.version;
|
|
12
|
+
if (v >= 300 && v < 310) {
|
|
13
|
+
return new CreditFacadeV300Contract(sdk, data);
|
|
14
|
+
} else if (v === 310n) {
|
|
15
|
+
return new CreditFacadeV310Contract(sdk, data);
|
|
16
|
+
}
|
|
17
|
+
throw new Error(`Unsupported credit facade version: ${v}`);
|
|
18
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { CreditManagerData } from "../../base";
|
|
2
|
+
import type { GearboxSDK } from "../../GearboxSDK";
|
|
3
|
+
import { CreditManagerV300Contract } from "./CreditManagerV300Contract";
|
|
4
|
+
import { CreditManagerV310Contract } from "./CreditManagerV310Contract";
|
|
5
|
+
import type { ICreditManagerContract } from "./types";
|
|
6
|
+
|
|
7
|
+
export default function createCreditManager(
|
|
8
|
+
sdk: GearboxSDK,
|
|
9
|
+
data: CreditManagerData,
|
|
10
|
+
): ICreditManagerContract {
|
|
11
|
+
const v = data.creditManager.baseParams.version;
|
|
12
|
+
if (v >= 300 && v < 310) {
|
|
13
|
+
return new CreditManagerV300Contract(sdk, data);
|
|
14
|
+
} else if (v === 310n) {
|
|
15
|
+
return new CreditManagerV310Contract(sdk, data);
|
|
16
|
+
}
|
|
17
|
+
throw new Error(`Unsupported credit manager version: ${v}`);
|
|
18
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export * from "./CreditConfiguratorV300Contract";
|
|
2
|
+
export * from "./CreditConfiguratorV310Contract";
|
|
3
|
+
export * from "./CreditFacadeV300Contract";
|
|
4
|
+
export * from "./CreditFacadeV310Contract";
|
|
5
|
+
export * from "./CreditManagerV300Contract";
|
|
6
|
+
export * from "./CreditManagerV310Contract";
|
|
7
|
+
export * from "./CreditSuite";
|
|
8
|
+
export * from "./types";
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { Address } from "abitype";
|
|
2
|
+
|
|
3
|
+
import type { CreditManagerState, IBaseContract } from "../../base";
|
|
4
|
+
import type {
|
|
5
|
+
CreditConfiguratorStateHuman,
|
|
6
|
+
CreditManagerStateHuman,
|
|
7
|
+
} from "../../types";
|
|
8
|
+
import type { AddressMap } from "../../utils";
|
|
9
|
+
import type { CreditFacadeV300Contract, CreditFacadeV310Contract } from "..";
|
|
10
|
+
import type { IAdapterContract } from "../adapters";
|
|
11
|
+
import type { RampEvent } from "./CreditConfiguratorV300Contract";
|
|
12
|
+
|
|
13
|
+
export interface ICreditConfiguratorContract extends IBaseContract {
|
|
14
|
+
adapters: Address[];
|
|
15
|
+
isPaused: boolean;
|
|
16
|
+
|
|
17
|
+
checkRamps: () => Promise<RampEvent[]>;
|
|
18
|
+
stateHuman: (raw?: boolean) => CreditConfiguratorStateHuman;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface ICreditManagerContract
|
|
22
|
+
extends IBaseContract,
|
|
23
|
+
Omit<CreditManagerState, "baseParams" | "collateralTokens"> {
|
|
24
|
+
/**
|
|
25
|
+
* Mapping targetContract => adapter
|
|
26
|
+
*/
|
|
27
|
+
adapters: AddressMap<IAdapterContract>;
|
|
28
|
+
/**
|
|
29
|
+
* Mapping collateral token address => liquidation threshold
|
|
30
|
+
*/
|
|
31
|
+
liquidationThresholds: AddressMap<number>;
|
|
32
|
+
/**
|
|
33
|
+
* List of collateral tokens
|
|
34
|
+
*/
|
|
35
|
+
collateralTokens: Address[];
|
|
36
|
+
|
|
37
|
+
stateHuman: (raw?: boolean) => CreditManagerStateHuman;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export type CreditFacadeContract =
|
|
41
|
+
| CreditFacadeV300Contract
|
|
42
|
+
| CreditFacadeV310Contract;
|
|
@@ -0,0 +1,376 @@
|
|
|
1
|
+
import type { Abi, Address } from "viem";
|
|
2
|
+
import { decodeFunctionData } from "viem";
|
|
3
|
+
|
|
4
|
+
import { iPriceFeedCompressorAbi } from "../../../abi/compressors";
|
|
5
|
+
import { iUpdatablePriceFeedAbi } from "../../../abi/iUpdatablePriceFeed";
|
|
6
|
+
import type {
|
|
7
|
+
PriceFeedMapEntry,
|
|
8
|
+
PriceFeedTreeNode,
|
|
9
|
+
PriceOracleData,
|
|
10
|
+
} from "../../base";
|
|
11
|
+
import { BaseContract } from "../../base";
|
|
12
|
+
import type { BaseContractOptions } from "../../base/BaseContract";
|
|
13
|
+
import { AP_PRICE_FEED_COMPRESSOR } from "../../constants";
|
|
14
|
+
import type { GearboxSDK } from "../../GearboxSDK";
|
|
15
|
+
import type { PriceOracleV3StateHuman } from "../../types";
|
|
16
|
+
import { AddressMap } from "../../utils";
|
|
17
|
+
import type {
|
|
18
|
+
IPriceFeedContract,
|
|
19
|
+
PriceFeedUsageType,
|
|
20
|
+
UpdatePriceFeedsResult,
|
|
21
|
+
} from "../pricefeeds";
|
|
22
|
+
import { PriceFeedRef } from "../pricefeeds";
|
|
23
|
+
import type {
|
|
24
|
+
IPriceOracleContract,
|
|
25
|
+
OnDemandPriceUpdate,
|
|
26
|
+
PriceFeedsForTokensOptions,
|
|
27
|
+
} from "./types";
|
|
28
|
+
|
|
29
|
+
export class PriceOracleBaseContract<abi extends Abi | readonly unknown[]>
|
|
30
|
+
extends BaseContract<abi>
|
|
31
|
+
implements IPriceOracleContract
|
|
32
|
+
{
|
|
33
|
+
/**
|
|
34
|
+
* Underlying token of market to which this price oracle belongs
|
|
35
|
+
*/
|
|
36
|
+
public readonly underlying: Address;
|
|
37
|
+
/**
|
|
38
|
+
* Mapping Token => [PriceFeed Address, stalenessPeriod]
|
|
39
|
+
*/
|
|
40
|
+
public readonly mainPriceFeeds = new AddressMap<PriceFeedRef>(
|
|
41
|
+
undefined,
|
|
42
|
+
"mainPriceFeeds",
|
|
43
|
+
);
|
|
44
|
+
/**
|
|
45
|
+
* Mapping Token => [PriceFeed Address, stalenessPeriod]
|
|
46
|
+
*/
|
|
47
|
+
public readonly reservePriceFeeds = new AddressMap<PriceFeedRef>(
|
|
48
|
+
undefined,
|
|
49
|
+
"reservePriceFeeds",
|
|
50
|
+
);
|
|
51
|
+
/**
|
|
52
|
+
* Mapping Token => Price in underlying
|
|
53
|
+
*/
|
|
54
|
+
public readonly mainPrices = new AddressMap<bigint>(undefined, "mainPrices");
|
|
55
|
+
/**
|
|
56
|
+
* Mapping Token => Price in underlying
|
|
57
|
+
*/
|
|
58
|
+
public readonly reservePrices = new AddressMap<bigint>(
|
|
59
|
+
undefined,
|
|
60
|
+
"reservePrices",
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
#priceFeedTree: readonly PriceFeedTreeNode[] = [];
|
|
64
|
+
|
|
65
|
+
constructor(
|
|
66
|
+
sdk: GearboxSDK,
|
|
67
|
+
args: BaseContractOptions<abi>,
|
|
68
|
+
data: PriceOracleData,
|
|
69
|
+
underlying: Address,
|
|
70
|
+
) {
|
|
71
|
+
super(sdk, args);
|
|
72
|
+
this.underlying = underlying;
|
|
73
|
+
const { priceFeedMapping, priceFeedStructure } = data;
|
|
74
|
+
this.#loadState(priceFeedMapping, priceFeedStructure);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Returns main and reserve price feeds for given tokens
|
|
79
|
+
* @param tokens
|
|
80
|
+
* @param opts Option to include main/reserve feeds only, defaults to both
|
|
81
|
+
* @returns
|
|
82
|
+
*/
|
|
83
|
+
public priceFeedsForTokens(
|
|
84
|
+
tokens: Address[],
|
|
85
|
+
opts?: PriceFeedsForTokensOptions,
|
|
86
|
+
): IPriceFeedContract[] {
|
|
87
|
+
const main = opts?.main ?? true;
|
|
88
|
+
const reserve = opts?.reserve ?? true;
|
|
89
|
+
return tokens
|
|
90
|
+
.flatMap(t => [
|
|
91
|
+
main ? this.mainPriceFeeds.get(t)?.priceFeed : undefined,
|
|
92
|
+
reserve ? this.reservePriceFeeds.get(t)?.priceFeed : undefined,
|
|
93
|
+
])
|
|
94
|
+
.filter((f): f is IPriceFeedContract => !!f);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Generates updates for all updateable price feeds in this oracle (including dependencies)
|
|
99
|
+
* @returns
|
|
100
|
+
*/
|
|
101
|
+
public async updatePriceFeeds(): Promise<UpdatePriceFeedsResult> {
|
|
102
|
+
const updatables: IPriceFeedContract[] = [];
|
|
103
|
+
for (const node of this.#priceFeedTree) {
|
|
104
|
+
if (node.updatable) {
|
|
105
|
+
updatables.push(this.sdk.priceFeeds.mustGet(node.baseParams.addr));
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return this.sdk.priceFeeds.generatePriceFeedsUpdateTxs(updatables);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Converts previously obtained price updates into CreditFacade multicall entries
|
|
113
|
+
* @param creditFacade
|
|
114
|
+
* @param updates
|
|
115
|
+
* @returns
|
|
116
|
+
*/
|
|
117
|
+
public onDemandPriceUpdates(
|
|
118
|
+
updates?: UpdatePriceFeedsResult,
|
|
119
|
+
): OnDemandPriceUpdate[] {
|
|
120
|
+
// TODO: really here I'm doing lots of reverse processing:
|
|
121
|
+
// decoding RawTx into Redstone calldata
|
|
122
|
+
// and then finding token + reserve value for a price feed
|
|
123
|
+
// it would be much nicer to have intermediate format and get RawTx/OnDemandPriceUpdate/ViemMulticall from it (as it's done in liquidator)
|
|
124
|
+
const result: OnDemandPriceUpdate[] = [];
|
|
125
|
+
if (!updates) {
|
|
126
|
+
this.logger?.debug("empty updates list");
|
|
127
|
+
return result;
|
|
128
|
+
}
|
|
129
|
+
const { txs } = updates;
|
|
130
|
+
|
|
131
|
+
for (const tx of txs) {
|
|
132
|
+
const { to: priceFeed, callData, description } = tx;
|
|
133
|
+
const [token, reserve] = this.findTokenForPriceFeed(priceFeed);
|
|
134
|
+
// this situation happend when we have combined updates from multiple markrts,
|
|
135
|
+
// but this particular feed is not added to this particular oracle
|
|
136
|
+
if (!token) {
|
|
137
|
+
this.logger?.debug(
|
|
138
|
+
`skipping onDemandPriceUpdate ${description}): token not found for price feed ${priceFeed} in oracle ${this.address}`,
|
|
139
|
+
);
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
const { args } = decodeFunctionData({
|
|
143
|
+
abi: iUpdatablePriceFeedAbi,
|
|
144
|
+
data: callData,
|
|
145
|
+
});
|
|
146
|
+
const data = args[0]!;
|
|
147
|
+
result.push({
|
|
148
|
+
priceFeed,
|
|
149
|
+
token,
|
|
150
|
+
reserve,
|
|
151
|
+
data,
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
this.logger?.debug(
|
|
155
|
+
`got ${result.length} onDemandPriceUpdates from ${txs.length} txs`,
|
|
156
|
+
);
|
|
157
|
+
return result;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Returns true if oracle's price feed tree contains given price feed
|
|
162
|
+
* @param priceFeed
|
|
163
|
+
* @returns
|
|
164
|
+
*/
|
|
165
|
+
public usesPriceFeed(priceFeed: Address): boolean {
|
|
166
|
+
return this.#priceFeedTree.some(
|
|
167
|
+
node => node.baseParams.addr.toLowerCase() === priceFeed.toLowerCase(),
|
|
168
|
+
);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Tries to convert amount of token into underlying of current market
|
|
173
|
+
* @param token
|
|
174
|
+
* @param amount
|
|
175
|
+
* @param reserve
|
|
176
|
+
* @returns
|
|
177
|
+
*/
|
|
178
|
+
public convertToUnderlying(
|
|
179
|
+
token: Address,
|
|
180
|
+
amount: bigint,
|
|
181
|
+
reserve = false,
|
|
182
|
+
): bigint {
|
|
183
|
+
return this.convert(token, this.underlying, amount, reserve);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Tries to convert amount of from one token to another, using latest known prices
|
|
188
|
+
* @param from
|
|
189
|
+
* @param to
|
|
190
|
+
* @param amount
|
|
191
|
+
* @param reserve
|
|
192
|
+
*/
|
|
193
|
+
public convert(
|
|
194
|
+
from: Address,
|
|
195
|
+
to: Address,
|
|
196
|
+
amount: bigint,
|
|
197
|
+
reserve = false,
|
|
198
|
+
): bigint {
|
|
199
|
+
if (from === to) {
|
|
200
|
+
return amount;
|
|
201
|
+
}
|
|
202
|
+
const fromPrice = reserve
|
|
203
|
+
? this.reservePrices.mustGet(from)
|
|
204
|
+
: this.mainPrices.mustGet(from);
|
|
205
|
+
const fromScale = 10n ** BigInt(this.sdk.tokensMeta.decimals(from));
|
|
206
|
+
const toPrice = reserve
|
|
207
|
+
? this.reservePrices.mustGet(to)
|
|
208
|
+
: this.mainPrices.mustGet(to);
|
|
209
|
+
const toScale = 10n ** BigInt(this.sdk.tokensMeta.decimals(to));
|
|
210
|
+
|
|
211
|
+
return (amount * fromPrice * toScale) / (toPrice * fromScale);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Tries to convert amount of token to USD, using latest known prices
|
|
216
|
+
* @param from
|
|
217
|
+
* @param to
|
|
218
|
+
* @param amount
|
|
219
|
+
* @param reserve
|
|
220
|
+
*/
|
|
221
|
+
public convertToUSD(from: Address, amount: bigint, reserve = false): bigint {
|
|
222
|
+
const price = reserve
|
|
223
|
+
? this.reservePrices.mustGet(from)
|
|
224
|
+
: this.mainPrices.mustGet(from);
|
|
225
|
+
const scale = 10n ** BigInt(this.sdk.tokensMeta.decimals(from));
|
|
226
|
+
return (amount * price) / scale;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Loads new prices for this oracle from PriceFeedCompressor
|
|
231
|
+
* Does not update price feeds, only updates prices
|
|
232
|
+
*/
|
|
233
|
+
public async updatePrices(): Promise<void> {
|
|
234
|
+
await this.sdk.marketRegister.updatePrices([this.address]);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
public syncStateMulticall() {
|
|
238
|
+
const args: any[] = [this.address];
|
|
239
|
+
if (this.version === 300) {
|
|
240
|
+
args.push(
|
|
241
|
+
Array.from(
|
|
242
|
+
new Set([
|
|
243
|
+
this.underlying,
|
|
244
|
+
...this.mainPriceFeeds.keys(),
|
|
245
|
+
...this.reservePriceFeeds.keys(),
|
|
246
|
+
]),
|
|
247
|
+
),
|
|
248
|
+
);
|
|
249
|
+
}
|
|
250
|
+
return {
|
|
251
|
+
call: {
|
|
252
|
+
abi: iPriceFeedCompressorAbi,
|
|
253
|
+
address: this.sdk.addressProvider.getLatestVersion(
|
|
254
|
+
AP_PRICE_FEED_COMPRESSOR,
|
|
255
|
+
),
|
|
256
|
+
functionName: "getPriceFeeds",
|
|
257
|
+
args,
|
|
258
|
+
},
|
|
259
|
+
onResult: ([entries, tree]: [
|
|
260
|
+
PriceFeedMapEntry[],
|
|
261
|
+
PriceFeedTreeNode[],
|
|
262
|
+
]) => {
|
|
263
|
+
this.#loadState(entries, tree);
|
|
264
|
+
},
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
#loadState(
|
|
269
|
+
entries: readonly PriceFeedMapEntry[],
|
|
270
|
+
tree: readonly PriceFeedTreeNode[],
|
|
271
|
+
): void {
|
|
272
|
+
this.#priceFeedTree = tree;
|
|
273
|
+
this.mainPriceFeeds.clear();
|
|
274
|
+
this.reservePriceFeeds.clear();
|
|
275
|
+
this.mainPrices.clear();
|
|
276
|
+
this.reservePrices.clear();
|
|
277
|
+
|
|
278
|
+
for (const node of tree) {
|
|
279
|
+
this.sdk.priceFeeds.getOrCreate(node);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
entries.forEach(entry => {
|
|
283
|
+
const { token, priceFeed, reserve, stalenessPeriod } = entry;
|
|
284
|
+
const ref = new PriceFeedRef(this.sdk, priceFeed, stalenessPeriod);
|
|
285
|
+
const node = this.#priceFeedTree.find(
|
|
286
|
+
n => n.baseParams.addr === priceFeed,
|
|
287
|
+
);
|
|
288
|
+
const price = node?.answer?.price;
|
|
289
|
+
if (reserve) {
|
|
290
|
+
this.reservePriceFeeds.upsert(token, ref);
|
|
291
|
+
if (price) {
|
|
292
|
+
this.reservePrices.upsert(token, price);
|
|
293
|
+
} else {
|
|
294
|
+
this.logger?.warn(
|
|
295
|
+
`answer not found for reserve price feed ${this.labelAddress(priceFeed)}, success: ${node?.answer?.success}`,
|
|
296
|
+
);
|
|
297
|
+
}
|
|
298
|
+
} else {
|
|
299
|
+
this.mainPriceFeeds.upsert(token, ref);
|
|
300
|
+
if (price) {
|
|
301
|
+
this.mainPrices.upsert(token, price);
|
|
302
|
+
} else {
|
|
303
|
+
this.logger?.warn(
|
|
304
|
+
`answer not found for main price feed ${this.labelAddress(priceFeed)}, success: ${node?.answer?.success}`,
|
|
305
|
+
);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
this.#labelPriceFeed(priceFeed, reserve ? "Reserve" : "Main", token);
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
this.logger?.debug(
|
|
312
|
+
`Got ${this.mainPriceFeeds.size} main and ${this.reservePriceFeeds.size} reserve price feeds`,
|
|
313
|
+
);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
#labelPriceFeed(
|
|
317
|
+
address: Address,
|
|
318
|
+
usage: PriceFeedUsageType,
|
|
319
|
+
token: Address,
|
|
320
|
+
): void {
|
|
321
|
+
this.sdk.provider.addressLabels.set(address, oldLabel => {
|
|
322
|
+
const symbol = this.sdk.tokensMeta.symbol(token);
|
|
323
|
+
let pricefeedTag = `${symbol}.${usage}`;
|
|
324
|
+
|
|
325
|
+
if (oldLabel) {
|
|
326
|
+
const oldLabelParts = new Set(oldLabel.split(", "));
|
|
327
|
+
oldLabelParts.add(pricefeedTag);
|
|
328
|
+
pricefeedTag = Array.from(oldLabelParts).join(", ");
|
|
329
|
+
}
|
|
330
|
+
return pricefeedTag;
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* Helper method to find "attachment point" of price feed (makes sense for updatable price feeds only) -
|
|
336
|
+
* returns token (in v3.0 can be ticker) and main/reserve flag
|
|
337
|
+
*
|
|
338
|
+
* @param priceFeed
|
|
339
|
+
* @returns
|
|
340
|
+
*/
|
|
341
|
+
protected findTokenForPriceFeed(
|
|
342
|
+
priceFeed: Address,
|
|
343
|
+
): [token: Address | undefined, reserve: boolean] {
|
|
344
|
+
for (const [token, pf] of this.mainPriceFeeds.entries()) {
|
|
345
|
+
if (pf.address === priceFeed) {
|
|
346
|
+
return [token, false];
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
for (const [token, pf] of this.reservePriceFeeds.entries()) {
|
|
350
|
+
if (pf.address === priceFeed) {
|
|
351
|
+
return [token, true];
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
return [undefined, false];
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
public override stateHuman(raw = true): PriceOracleV3StateHuman {
|
|
358
|
+
return {
|
|
359
|
+
...super.stateHuman(raw),
|
|
360
|
+
mainPriceFeeds: Object.fromEntries(
|
|
361
|
+
this.mainPriceFeeds
|
|
362
|
+
.entries()
|
|
363
|
+
.map(([token, v]) => [this.labelAddress(token), v.stateHuman(raw)]),
|
|
364
|
+
),
|
|
365
|
+
reservePriceFeeds: Object.fromEntries(
|
|
366
|
+
this.reservePriceFeeds
|
|
367
|
+
.entries()
|
|
368
|
+
.map(([token, v]) => [this.labelAddress(token), v.stateHuman(raw)]),
|
|
369
|
+
),
|
|
370
|
+
};
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
protected get priceFeedTree(): readonly PriceFeedTreeNode[] {
|
|
374
|
+
return this.#priceFeedTree;
|
|
375
|
+
}
|
|
376
|
+
}
|