@defisaver/positions-sdk 2.1.169-custom-fetching-dev → 2.1.169-custom-fetching-3-dev
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/cjs/aaveV2/index.d.ts +4 -4
- package/cjs/aaveV2/index.js +7 -7
- package/cjs/aaveV3/index.d.ts +4 -3
- package/cjs/aaveV3/index.js +14 -10
- package/cjs/aaveV3/merkl.d.ts +5 -1
- package/cjs/aaveV3/merkl.js +8 -3
- package/cjs/aaveV4/index.d.ts +4 -3
- package/cjs/aaveV4/index.js +19 -16
- package/cjs/aaveV4/merkl.d.ts +10 -2
- package/cjs/aaveV4/merkl.js +15 -6
- package/cjs/claiming/aaveV3.d.ts +2 -2
- package/cjs/claiming/aaveV3.js +5 -4
- package/cjs/claiming/king.d.ts +2 -2
- package/cjs/claiming/king.js +3 -2
- package/cjs/claiming/spark.d.ts +3 -3
- package/cjs/claiming/spark.js +8 -7
- package/cjs/claiming/uniswap.d.ts +2 -2
- package/cjs/claiming/uniswap.js +3 -2
- package/cjs/compoundV2/index.d.ts +4 -4
- package/cjs/compoundV2/index.js +9 -9
- package/cjs/compoundV3/index.d.ts +5 -4
- package/cjs/compoundV3/index.js +17 -13
- package/cjs/compoundV3/merkl.d.ts +1 -1
- package/cjs/compoundV3/merkl.js +2 -5
- package/cjs/curveUsd/index.d.ts +5 -5
- package/cjs/curveUsd/index.js +17 -17
- package/cjs/fluid/index.d.ts +23 -22
- package/cjs/fluid/index.js +75 -72
- package/cjs/fluid/merkl.d.ts +5 -3
- package/cjs/fluid/merkl.js +11 -6
- package/cjs/liquity/index.d.ts +4 -4
- package/cjs/liquity/index.js +23 -23
- package/cjs/liquityV2/index.d.ts +10 -10
- package/cjs/liquityV2/index.js +59 -59
- package/cjs/llamaLend/index.d.ts +5 -5
- package/cjs/llamaLend/index.js +13 -13
- package/cjs/maker/index.d.ts +6 -6
- package/cjs/maker/index.js +16 -16
- package/cjs/morphoBlue/index.d.ts +7 -7
- package/cjs/morphoBlue/index.js +28 -26
- package/cjs/morphoBlue/merkl.d.ts +5 -3
- package/cjs/morphoBlue/merkl.js +6 -8
- package/cjs/morphoMidnight/index.d.ts +5 -5
- package/cjs/morphoMidnight/index.js +18 -18
- package/cjs/portfolio/customDeFiData.js +45 -40
- package/cjs/services/merkl.d.ts +36 -2
- package/cjs/services/merkl.js +142 -19
- package/cjs/services/priceService.d.ts +12 -12
- package/cjs/services/priceService.js +30 -29
- package/cjs/services/viem.d.ts +10 -2
- package/cjs/services/viem.js +27 -1
- package/cjs/spark/index.d.ts +4 -4
- package/cjs/spark/index.js +9 -9
- package/cjs/types/merkl.d.ts +18 -0
- package/cjs/types/portfolio.d.ts +2 -0
- package/cjs/umbrella/index.d.ts +2 -2
- package/cjs/umbrella/index.js +5 -4
- package/esm/aaveV2/index.d.ts +4 -4
- package/esm/aaveV2/index.js +8 -8
- package/esm/aaveV3/index.d.ts +4 -3
- package/esm/aaveV3/index.js +15 -11
- package/esm/aaveV3/merkl.d.ts +5 -1
- package/esm/aaveV3/merkl.js +9 -4
- package/esm/aaveV4/index.d.ts +4 -3
- package/esm/aaveV4/index.js +20 -17
- package/esm/aaveV4/merkl.d.ts +10 -2
- package/esm/aaveV4/merkl.js +16 -7
- package/esm/claiming/aaveV3.d.ts +2 -2
- package/esm/claiming/aaveV3.js +5 -4
- package/esm/claiming/king.d.ts +2 -2
- package/esm/claiming/king.js +3 -2
- package/esm/claiming/spark.d.ts +3 -3
- package/esm/claiming/spark.js +8 -7
- package/esm/claiming/uniswap.d.ts +2 -2
- package/esm/claiming/uniswap.js +3 -2
- package/esm/compoundV2/index.d.ts +4 -4
- package/esm/compoundV2/index.js +9 -9
- package/esm/compoundV3/index.d.ts +5 -4
- package/esm/compoundV3/index.js +19 -15
- package/esm/compoundV3/merkl.d.ts +1 -1
- package/esm/compoundV3/merkl.js +3 -6
- package/esm/curveUsd/index.d.ts +5 -5
- package/esm/curveUsd/index.js +17 -17
- package/esm/fluid/index.d.ts +23 -22
- package/esm/fluid/index.js +76 -73
- package/esm/fluid/merkl.d.ts +5 -3
- package/esm/fluid/merkl.js +12 -7
- package/esm/liquity/index.d.ts +4 -4
- package/esm/liquity/index.js +23 -23
- package/esm/liquityV2/index.d.ts +10 -10
- package/esm/liquityV2/index.js +60 -60
- package/esm/llamaLend/index.d.ts +5 -5
- package/esm/llamaLend/index.js +13 -13
- package/esm/maker/index.d.ts +6 -6
- package/esm/maker/index.js +16 -16
- package/esm/morphoBlue/index.d.ts +7 -7
- package/esm/morphoBlue/index.js +29 -27
- package/esm/morphoBlue/merkl.d.ts +5 -3
- package/esm/morphoBlue/merkl.js +7 -9
- package/esm/morphoMidnight/index.d.ts +5 -5
- package/esm/morphoMidnight/index.js +18 -18
- package/esm/portfolio/customDeFiData.js +47 -42
- package/esm/services/merkl.d.ts +36 -2
- package/esm/services/merkl.js +136 -18
- package/esm/services/priceService.d.ts +12 -12
- package/esm/services/priceService.js +31 -30
- package/esm/services/viem.d.ts +10 -2
- package/esm/services/viem.js +25 -0
- package/esm/spark/index.d.ts +4 -4
- package/esm/spark/index.js +10 -10
- package/esm/types/merkl.d.ts +18 -0
- package/esm/types/portfolio.d.ts +2 -0
- package/esm/umbrella/index.d.ts +2 -2
- package/esm/umbrella/index.js +5 -4
- package/package.json +1 -1
- package/src/aaveV2/index.ts +7 -7
- package/src/aaveV3/index.ts +15 -11
- package/src/aaveV3/merkl.ts +9 -4
- package/src/aaveV4/index.ts +19 -16
- package/src/aaveV4/merkl.ts +16 -7
- package/src/claiming/aaveV3.ts +5 -3
- package/src/claiming/king.ts +6 -3
- package/src/claiming/spark.ts +9 -6
- package/src/claiming/uniswap.ts +4 -3
- package/src/compoundV2/index.ts +10 -9
- package/src/compoundV3/index.ts +20 -14
- package/src/compoundV3/merkl.ts +5 -4
- package/src/curveUsd/index.ts +19 -17
- package/src/fluid/index.ts +88 -70
- package/src/fluid/merkl.ts +12 -7
- package/src/liquity/index.ts +23 -23
- package/src/liquityV2/index.ts +64 -56
- package/src/llamaLend/index.ts +15 -13
- package/src/maker/index.ts +19 -16
- package/src/morphoBlue/index.ts +23 -20
- package/src/morphoBlue/merkl.ts +9 -6
- package/src/morphoMidnight/index.ts +13 -13
- package/src/portfolio/customDeFiData.ts +48 -41
- package/src/services/merkl.ts +152 -19
- package/src/services/priceService.ts +33 -30
- package/src/services/viem.ts +16 -0
- package/src/spark/index.ts +11 -8
- package/src/types/merkl.ts +20 -0
- package/src/types/portfolio.ts +2 -0
- package/src/umbrella/index.ts +6 -5
package/src/fluid/merkl.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import memoize from 'memoizee';
|
|
2
2
|
import { aprToApy } from '../moneymarket';
|
|
3
|
-
import {
|
|
3
|
+
import { fetchMerklOpportunities } from '../services/merkl';
|
|
4
4
|
import {
|
|
5
5
|
FluidMarketData,
|
|
6
6
|
FluidMerklRewardMap,
|
|
@@ -66,22 +66,27 @@ export const buildFluidMerklRewardMap = (opportunities: MerklOpportunity[], chai
|
|
|
66
66
|
};
|
|
67
67
|
|
|
68
68
|
/**
|
|
69
|
-
* Fetches live vault-scoped Fluid campaigns for the chain, keyed by vault address.
|
|
70
|
-
*
|
|
69
|
+
* Fetches live vault-scoped Fluid campaigns for the chain, keyed by vault address. With
|
|
70
|
+
* `asOfTimestamp` (unix seconds) the campaigns and APRs are the ones Merkl had at that moment
|
|
71
|
+
* instead. Never throws — on any failure it returns an empty map, so Merkl being down can't break
|
|
72
|
+
* market data.
|
|
71
73
|
*/
|
|
72
|
-
export const getFluidMerklCampaigns = memoize(async (chainId: NetworkNumber): Promise<FluidMerklRewardMap> => {
|
|
74
|
+
export const getFluidMerklCampaigns = memoize(async (chainId: NetworkNumber, asOfTimestamp?: number): Promise<FluidMerklRewardMap> => {
|
|
73
75
|
try {
|
|
74
|
-
const opportunities = await
|
|
76
|
+
const opportunities = await fetchMerklOpportunities({
|
|
75
77
|
mainProtocolId: 'fluid',
|
|
78
|
+
chainId: chainId.toString(),
|
|
76
79
|
type: Object.values(FluidMerklOpportunityType).join(','),
|
|
77
80
|
status: OpportunityStatus.LIVE,
|
|
78
|
-
});
|
|
81
|
+
}, asOfTimestamp);
|
|
79
82
|
return buildFluidMerklRewardMap(opportunities, chainId);
|
|
80
83
|
} catch (e) {
|
|
81
84
|
console.error('Failed to fetch Fluid Merkl campaigns', e);
|
|
82
85
|
return {};
|
|
83
86
|
}
|
|
84
|
-
|
|
87
|
+
// memoizee keys on the declared arity, which excludes optional params — `length: 2` keeps the
|
|
88
|
+
// timestamp in the cache key so a historical lookup never serves (or is served by) the live one
|
|
89
|
+
}, { promise: true, maxAge: 60 * 1000, length: 2 });
|
|
85
90
|
|
|
86
91
|
/**
|
|
87
92
|
* Appends the vault's Merkl campaigns to its assets' incentive arrays — supply rewards on every
|
package/src/liquity/index.ts
CHANGED
|
@@ -50,21 +50,21 @@ export const _getLiquityAccountBalances = async (provider: Client, network: Netw
|
|
|
50
50
|
|
|
51
51
|
export const getLiquityAccountBalances = async (provider: EthereumProvider, network: NetworkNumber, block: Blockish, addressMapping: boolean, address: EthAddress): Promise<PositionBalances> => _getLiquityAccountBalances(getViemProvider(provider, network), network, block, addressMapping, address);
|
|
52
52
|
|
|
53
|
-
const _getDebtInFront = async (viewContract: any, address: EthAddress, accumulatedSum = '0', iterations = 2000): Promise<string> => {
|
|
54
|
-
const res = await viewContract.read.getDebtInFront([address, accumulatedSum, iterations]);
|
|
53
|
+
const _getDebtInFront = async (viewContract: any, address: EthAddress, accumulatedSum = '0', iterations = 2000, blockNumber: Blockish = 'latest'): Promise<string> => {
|
|
54
|
+
const res = await viewContract.read.getDebtInFront([address, accumulatedSum, iterations], setViemBlockNumber(blockNumber));
|
|
55
55
|
const debt = res[1].toString();
|
|
56
56
|
const next = res[0];
|
|
57
57
|
if (next === ZERO_ADDRESS) return assetAmountInEth(debt, 'LUSD');
|
|
58
|
-
return _getDebtInFront(viewContract, next, debt, iterations);
|
|
58
|
+
return _getDebtInFront(viewContract, next, debt, iterations, blockNumber);
|
|
59
59
|
};
|
|
60
60
|
|
|
61
|
-
export const getDebtInFront = async (provider: EthereumProvider, address: EthAddress): Promise<string> => {
|
|
61
|
+
export const getDebtInFront = async (provider: EthereumProvider, address: EthAddress, blockNumber: Blockish = 'latest'): Promise<string> => {
|
|
62
62
|
const client = getViemProvider(provider, NetworkNumber.Eth);
|
|
63
63
|
const viewContract = LiquityViewContractViem(client, NetworkNumber.Eth);
|
|
64
|
-
return _getDebtInFront(viewContract, address);
|
|
64
|
+
return _getDebtInFront(viewContract, address, '0', 2000, blockNumber);
|
|
65
65
|
};
|
|
66
66
|
|
|
67
|
-
export const _getLiquityTroveInfo = async (provider: Client, network: NetworkNumber, address: EthAddress): Promise<LiquityTroveInfo> => {
|
|
67
|
+
export const _getLiquityTroveInfo = async (provider: Client, network: NetworkNumber, address: EthAddress, blockNumber: Blockish = 'latest'): Promise<LiquityTroveInfo> => {
|
|
68
68
|
const viewContract = LiquityViewContractViem(provider, network);
|
|
69
69
|
const collSurplusPoolContract = LiquityCollSurplusPoolContractViem(provider, network);
|
|
70
70
|
const troveManagerContract = LiquityTroveManagerContractViem(provider, network);
|
|
@@ -80,13 +80,13 @@ export const _getLiquityTroveInfo = async (provider: Client, network: NetworkNum
|
|
|
80
80
|
totalLUSD,
|
|
81
81
|
debtInFront,
|
|
82
82
|
] = await Promise.all([
|
|
83
|
-
viewContract.read.getTroveInfo([address]),
|
|
84
|
-
collSurplusPoolContract.read.getCollateral([address]),
|
|
85
|
-
troveManagerContract.read.getBorrowingRateWithDecay(),
|
|
86
|
-
priceFeedContract.read.fetchPrice(),
|
|
87
|
-
activePoolContract.read.getETH(),
|
|
88
|
-
activePoolContract.read.getLUSDDebt(),
|
|
89
|
-
_getDebtInFront(viewContract, address),
|
|
83
|
+
viewContract.read.getTroveInfo([address], setViemBlockNumber(blockNumber)),
|
|
84
|
+
collSurplusPoolContract.read.getCollateral([address], setViemBlockNumber(blockNumber)),
|
|
85
|
+
troveManagerContract.read.getBorrowingRateWithDecay(setViemBlockNumber(blockNumber)),
|
|
86
|
+
priceFeedContract.read.fetchPrice(setViemBlockNumber(blockNumber)),
|
|
87
|
+
activePoolContract.read.getETH(setViemBlockNumber(blockNumber)),
|
|
88
|
+
activePoolContract.read.getLUSDDebt(setViemBlockNumber(blockNumber)),
|
|
89
|
+
_getDebtInFront(viewContract, address, '0', 2000, blockNumber),
|
|
90
90
|
]);
|
|
91
91
|
|
|
92
92
|
const recoveryMode = troveInfo[6];
|
|
@@ -125,9 +125,9 @@ export const _getLiquityTroveInfo = async (provider: Client, network: NetworkNum
|
|
|
125
125
|
return payload;
|
|
126
126
|
};
|
|
127
127
|
|
|
128
|
-
export const getLiquityTroveInfo = async (provider: EthereumProvider, network: NetworkNumber, address: EthAddress): Promise<LiquityTroveInfo> => _getLiquityTroveInfo(getViemProvider(provider, network, { batch: { multicall: true } }), network, address);
|
|
128
|
+
export const getLiquityTroveInfo = async (provider: EthereumProvider, network: NetworkNumber, address: EthAddress, blockNumber: Blockish = 'latest'): Promise<LiquityTroveInfo> => _getLiquityTroveInfo(getViemProvider(provider, network, { batch: { multicall: true } }), network, address, blockNumber);
|
|
129
129
|
|
|
130
|
-
export const getLiquityStakingData = async (provider: Client, network: NetworkNumber, address: EthAddress) => {
|
|
130
|
+
export const getLiquityStakingData = async (provider: Client, network: NetworkNumber, address: EthAddress, blockNumber: Blockish = 'latest') => {
|
|
131
131
|
const lqtyStakingView = LiquityLQTYStakingViem(provider, network);
|
|
132
132
|
const stabilityPoolView = LiquityStabilityPoolViem(provider, network);
|
|
133
133
|
const [
|
|
@@ -140,14 +140,14 @@ export const getLiquityStakingData = async (provider: Client, network: NetworkNu
|
|
|
140
140
|
compoundedLUSDDeposit,
|
|
141
141
|
totalLUSDDeposits,
|
|
142
142
|
] = await Promise.all([
|
|
143
|
-
lqtyStakingView.read.stakes([address]),
|
|
144
|
-
lqtyStakingView.read.getPendingETHGain([address]),
|
|
145
|
-
lqtyStakingView.read.getPendingLUSDGain([address]),
|
|
146
|
-
lqtyStakingView.read.totalLQTYStaked(),
|
|
147
|
-
stabilityPoolView.read.getDepositorETHGain([address]),
|
|
148
|
-
stabilityPoolView.read.getDepositorLQTYGain([address]),
|
|
149
|
-
stabilityPoolView.read.getCompoundedLUSDDeposit([address]),
|
|
150
|
-
stabilityPoolView.read.getTotalLUSDDeposits(),
|
|
143
|
+
lqtyStakingView.read.stakes([address], setViemBlockNumber(blockNumber)),
|
|
144
|
+
lqtyStakingView.read.getPendingETHGain([address], setViemBlockNumber(blockNumber)),
|
|
145
|
+
lqtyStakingView.read.getPendingLUSDGain([address], setViemBlockNumber(blockNumber)),
|
|
146
|
+
lqtyStakingView.read.totalLQTYStaked(setViemBlockNumber(blockNumber)),
|
|
147
|
+
stabilityPoolView.read.getDepositorETHGain([address], setViemBlockNumber(blockNumber)),
|
|
148
|
+
stabilityPoolView.read.getDepositorLQTYGain([address], setViemBlockNumber(blockNumber)),
|
|
149
|
+
stabilityPoolView.read.getCompoundedLUSDDeposit([address], setViemBlockNumber(blockNumber)),
|
|
150
|
+
stabilityPoolView.read.getTotalLUSDDeposits(setViemBlockNumber(blockNumber)),
|
|
151
151
|
]);
|
|
152
152
|
const totalLUSDDeposited = getEthAmountForDecimals(totalLUSDDeposits as string, 18);
|
|
153
153
|
const totalLQTYStaked = getEthAmountForDecimals(totalLQTYStakes as string, 18);
|
package/src/liquityV2/index.ts
CHANGED
|
@@ -8,7 +8,7 @@ import {
|
|
|
8
8
|
LiquityV2ViewContractViem,
|
|
9
9
|
} from '../contracts';
|
|
10
10
|
import {
|
|
11
|
-
EthAddress, EthereumProvider, IncentiveKind, NetworkNumber, HexString,
|
|
11
|
+
Blockish, EthAddress, EthereumProvider, IncentiveKind, NetworkNumber, HexString,
|
|
12
12
|
} from '../types/common';
|
|
13
13
|
import {
|
|
14
14
|
LIQUITY_V2_TROVE_STATUS_ENUM,
|
|
@@ -25,7 +25,7 @@ import {
|
|
|
25
25
|
} from '../services/utils';
|
|
26
26
|
import { SECONDS_PER_YEAR, ZERO_ADDRESS } from '../constants';
|
|
27
27
|
import { LiquityV2Markets } from '../markets';
|
|
28
|
-
import { getViemProvider } from '../services/viem';
|
|
28
|
+
import { getViemProvider, setViemBlockNumber } from '../services/viem';
|
|
29
29
|
|
|
30
30
|
export type SPYieldGainParameters = {
|
|
31
31
|
P: string;
|
|
@@ -42,13 +42,13 @@ const getLiquityV2ViewContract = (provider: Client, network: NetworkNumber, isLe
|
|
|
42
42
|
};
|
|
43
43
|
|
|
44
44
|
|
|
45
|
-
export const _getLiquityV2MarketData = async (provider: Client, network: NetworkNumber, selectedMarket: LiquityV2MarketInfo): Promise<LiquityV2MarketData> => {
|
|
45
|
+
export const _getLiquityV2MarketData = async (provider: Client, network: NetworkNumber, selectedMarket: LiquityV2MarketInfo, blockNumber: Blockish = 'latest'): Promise<LiquityV2MarketData> => {
|
|
46
46
|
const {
|
|
47
47
|
marketAddress, debtToken, collateralToken, isLegacy,
|
|
48
48
|
} = selectedMarket;
|
|
49
49
|
const data = isLegacy
|
|
50
|
-
? await LiquityV2LegacyViewContractViem(provider, network).read.getMarketData([marketAddress])
|
|
51
|
-
: (await LiquityV2ViewContractViem(provider, network).simulate.getMarketData([marketAddress])).result;
|
|
50
|
+
? await LiquityV2LegacyViewContractViem(provider, network).read.getMarketData([marketAddress], setViemBlockNumber(blockNumber))
|
|
51
|
+
: (await LiquityV2ViewContractViem(provider, network).simulate.getMarketData([marketAddress], setViemBlockNumber(blockNumber))).result;
|
|
52
52
|
const hintHelperAddress = data.hintHelpers;
|
|
53
53
|
const troveNFTAddress = data.troveNFT;
|
|
54
54
|
const borrowerOperationsAddress = data.borrowerOperations;
|
|
@@ -131,7 +131,8 @@ export const getLiquityV2MarketData = async (
|
|
|
131
131
|
provider: EthereumProvider,
|
|
132
132
|
network: NetworkNumber,
|
|
133
133
|
selectedMarket: LiquityV2MarketInfo,
|
|
134
|
-
|
|
134
|
+
blockNumber: Blockish = 'latest',
|
|
135
|
+
): Promise<LiquityV2MarketData> => _getLiquityV2MarketData(getViemProvider(provider, network), network, selectedMarket, blockNumber);
|
|
135
136
|
|
|
136
137
|
const getUserTroves = async (
|
|
137
138
|
provider: Client,
|
|
@@ -142,14 +143,15 @@ const getUserTroves = async (
|
|
|
142
143
|
startIndex = 0,
|
|
143
144
|
endIndex = 100,
|
|
144
145
|
troves: { troveId: string, ownedByUser: boolean }[] = [],
|
|
146
|
+
blockNumber: Blockish = 'latest',
|
|
145
147
|
): Promise<{ troves: { troveId: string, ownedByUser: boolean }[], nextFreeTroveIndex: string }> => {
|
|
146
148
|
const viewContract = getLiquityV2ViewContract(provider, network, isLegacy);
|
|
147
|
-
const result = await viewContract.read.getUserTroves([account, marketAddress, BigInt(startIndex), BigInt(endIndex)]);
|
|
149
|
+
const result = await viewContract.read.getUserTroves([account, marketAddress, BigInt(startIndex), BigInt(endIndex)], setViemBlockNumber(blockNumber));
|
|
148
150
|
const newStartIndex = endIndex + 1;
|
|
149
151
|
const nextFreeTroveIndex = result[1].toString();
|
|
150
152
|
const existingTroves = [...troves, ...result[0].map((trove: any) => ({ ...trove, troveId: trove.troveId.toString() }))];
|
|
151
153
|
if (nextFreeTroveIndex !== '-1') return { troves: existingTroves.filter((trove) => trove.ownedByUser), nextFreeTroveIndex };
|
|
152
|
-
return getUserTroves(provider, network, isLegacy, account, marketAddress, newStartIndex, newStartIndex + 100, existingTroves);
|
|
154
|
+
return getUserTroves(provider, network, isLegacy, account, marketAddress, newStartIndex, newStartIndex + 100, existingTroves, blockNumber);
|
|
153
155
|
};
|
|
154
156
|
|
|
155
157
|
export const nftContractCreationBlockMapping = {
|
|
@@ -216,13 +218,14 @@ const getDebtInFrontForSingleMarketLiquityV2 = async (
|
|
|
216
218
|
troveId: string,
|
|
217
219
|
accumulatedSum = '0',
|
|
218
220
|
iterations = 2000,
|
|
221
|
+
blockNumber: Blockish = 'latest',
|
|
219
222
|
): Promise<string> => {
|
|
220
223
|
const viewContract = getLiquityV2ViewContract(provider, network, isLegacy);
|
|
221
|
-
const res = await viewContract.read.getDebtInFront([marketAddress, BigInt(troveId), BigInt(accumulatedSum), BigInt(iterations)]);
|
|
224
|
+
const res = await viewContract.read.getDebtInFront([marketAddress, BigInt(troveId), BigInt(accumulatedSum), BigInt(iterations)], setViemBlockNumber(blockNumber));
|
|
222
225
|
const next = res[0].toString();
|
|
223
226
|
const debt = res[1].toString();
|
|
224
227
|
if (next === '0') return assetAmountInEth(debt);
|
|
225
|
-
return getDebtInFrontForSingleMarketLiquityV2(provider, network, isLegacy, marketAddress, next, debt, iterations);
|
|
228
|
+
return getDebtInFrontForSingleMarketLiquityV2(provider, network, isLegacy, marketAddress, next, debt, iterations, blockNumber);
|
|
226
229
|
};
|
|
227
230
|
|
|
228
231
|
const getDebtInFrontForInterestRateSingleMarketLiquityV2 = async (
|
|
@@ -234,31 +237,32 @@ const getDebtInFrontForInterestRateSingleMarketLiquityV2 = async (
|
|
|
234
237
|
troveId = '0',
|
|
235
238
|
accumulatedSum = '0',
|
|
236
239
|
iterations = 2000,
|
|
240
|
+
blockNumber: Blockish = 'latest',
|
|
237
241
|
): Promise<string> => {
|
|
238
242
|
const viewContract = getLiquityV2ViewContract(provider, network, isLegacy);
|
|
239
243
|
if (+interestRate === 0 || !interestRate) return '0';
|
|
240
244
|
const interestRateWei = new Dec(interestRate).times(1e16).toFixed(0).toString();
|
|
241
|
-
const res = await viewContract.read.getDebtInFrontByInterestRate([marketAddress, BigInt(troveId), BigInt(accumulatedSum), BigInt(iterations), BigInt(interestRateWei)]);
|
|
245
|
+
const res = await viewContract.read.getDebtInFrontByInterestRate([marketAddress, BigInt(troveId), BigInt(accumulatedSum), BigInt(iterations), BigInt(interestRateWei)], setViemBlockNumber(blockNumber));
|
|
242
246
|
const next = res[0].toString();
|
|
243
247
|
const debt = res[1].toString();
|
|
244
248
|
if (next === '0') return assetAmountInEth(debt);
|
|
245
|
-
return getDebtInFrontForInterestRateSingleMarketLiquityV2(provider, network, isLegacy, marketAddress, interestRate, next, debt, iterations);
|
|
249
|
+
return getDebtInFrontForInterestRateSingleMarketLiquityV2(provider, network, isLegacy, marketAddress, interestRate, next, debt, iterations, blockNumber);
|
|
246
250
|
};
|
|
247
251
|
|
|
248
|
-
const getUnbackedDebtForSingleMarket = async (totalBorrowed: string, provider: Client, network: NetworkNumber, stabilityPoolAddress: EthAddress) => {
|
|
252
|
+
const getUnbackedDebtForSingleMarket = async (totalBorrowed: string, provider: Client, network: NetworkNumber, stabilityPoolAddress: EthAddress, blockNumber: Blockish = 'latest') => {
|
|
249
253
|
const stabilityPoolContract = createViemContractFromConfigFunc('LiquityV2StabilityPool', stabilityPoolAddress)(provider, network);
|
|
250
|
-
const totalBoldDeposits = await stabilityPoolContract.read.getTotalBoldDeposits();
|
|
254
|
+
const totalBoldDeposits = await stabilityPoolContract.read.getTotalBoldDeposits(setViemBlockNumber(blockNumber));
|
|
251
255
|
const totalBoldDepositsInEth = assetAmountInEth(totalBoldDeposits.toString());
|
|
252
256
|
|
|
253
257
|
return Dec.max(new Dec(totalBorrowed).sub(totalBoldDepositsInEth), 0).toString();
|
|
254
258
|
};
|
|
255
259
|
|
|
256
|
-
const getAllMarketsUnbackedDebts = async (markets: Record<LiquityV2Versions, LiquityV2MarketData>, isLegacy: boolean, provider: Client, network: NetworkNumber): Promise<Record<LiquityV2Versions, string>> => {
|
|
260
|
+
const getAllMarketsUnbackedDebts = async (markets: Record<LiquityV2Versions, LiquityV2MarketData>, isLegacy: boolean, provider: Client, network: NetworkNumber, blockNumber: Blockish = 'latest'): Promise<Record<LiquityV2Versions, string>> => {
|
|
257
261
|
const allMarketsUnbackedDebt = await Promise.all(Object.entries(markets).map(async ([version, market]) => {
|
|
258
262
|
const { assetsData, marketData } = market;
|
|
259
263
|
const { debtToken, isLegacy: isLegacyMarket } = LiquityV2Markets(network)[version as LiquityV2Versions];
|
|
260
264
|
if (isLegacyMarket !== isLegacy) return [version, '0'];
|
|
261
|
-
const unbackedDebt = await getUnbackedDebtForSingleMarket(assetsData[debtToken].totalBorrow, provider, network, marketData.stabilityPoolAddress);
|
|
265
|
+
const unbackedDebt = await getUnbackedDebtForSingleMarket(assetsData[debtToken].totalBorrow, provider, network, marketData.stabilityPoolAddress, blockNumber);
|
|
262
266
|
return [version, unbackedDebt];
|
|
263
267
|
}));
|
|
264
268
|
|
|
@@ -317,23 +321,23 @@ export const calculateDebtInFrontLiquityV2 = (markets: Record<LiquityV2Versions,
|
|
|
317
321
|
// @dev The amount redeemed on each branch depends on the unbacked debt of every branch (the difference between total borrow and stability pool deposits).
|
|
318
322
|
// When new debt is generated on the selected market, the unbacked debt will increase, resulting in a higher redemption amount on that branch.
|
|
319
323
|
// This function accepts the new debt that's about to be generated (e.g., trove creation) and estimates the debt in front based on the new state.
|
|
320
|
-
export const getDebtInFrontForInterestRateIncludingNewDebtLiquityV2 = async (newDebt: string, markets: Record<LiquityV2Versions, LiquityV2MarketData>, selectedMarket: LiquityV2Versions, provider: Client, network: NetworkNumber, interestRate: string) => {
|
|
324
|
+
export const getDebtInFrontForInterestRateIncludingNewDebtLiquityV2 = async (newDebt: string, markets: Record<LiquityV2Versions, LiquityV2MarketData>, selectedMarket: LiquityV2Versions, provider: Client, network: NetworkNumber, interestRate: string, blockNumber: Blockish = 'latest') => {
|
|
321
325
|
const marketsWithNewDebt = structuredClone(markets);
|
|
322
326
|
const selectedMarketDebtToken = LiquityV2Markets(network)[selectedMarket].debtToken;
|
|
323
327
|
const currentTotalBorrow = new Dec(marketsWithNewDebt[selectedMarket].assetsData[selectedMarketDebtToken].totalBorrow);
|
|
324
328
|
marketsWithNewDebt[selectedMarket].assetsData[selectedMarketDebtToken].totalBorrow = currentTotalBorrow.add(newDebt).toString();
|
|
325
329
|
|
|
326
330
|
const { isLegacy } = LiquityV2Markets(NetworkNumber.Eth)[selectedMarket];
|
|
327
|
-
const allMarketsUnbackedDebts = await getAllMarketsUnbackedDebts(marketsWithNewDebt, isLegacy, provider, network);
|
|
328
|
-
const interestRateDebtInFront = new Dec(await getDebtInFrontForInterestRateSingleMarketLiquityV2(provider, network, isLegacy, LiquityV2Markets(network)[selectedMarket].marketAddress, interestRate));
|
|
331
|
+
const allMarketsUnbackedDebts = await getAllMarketsUnbackedDebts(marketsWithNewDebt, isLegacy, provider, network, blockNumber);
|
|
332
|
+
const interestRateDebtInFront = new Dec(await getDebtInFrontForInterestRateSingleMarketLiquityV2(provider, network, isLegacy, LiquityV2Markets(network)[selectedMarket].marketAddress, interestRate, '0', '0', 2000, blockNumber));
|
|
329
333
|
|
|
330
334
|
return calculateDebtInFrontLiquityV2(marketsWithNewDebt, selectedMarket, allMarketsUnbackedDebts, interestRateDebtInFront.toString());
|
|
331
335
|
};
|
|
332
336
|
|
|
333
|
-
const getDebtInFrontLiquityV2 = async (markets: Record<LiquityV2Versions, LiquityV2MarketData>, selectedMarket: LiquityV2Versions, provider: Client, network: NetworkNumber, viewContract: any, troveId: string) => {
|
|
337
|
+
const getDebtInFrontLiquityV2 = async (markets: Record<LiquityV2Versions, LiquityV2MarketData>, selectedMarket: LiquityV2Versions, provider: Client, network: NetworkNumber, viewContract: any, troveId: string, blockNumber: Blockish = 'latest') => {
|
|
334
338
|
const { isLegacy } = LiquityV2Markets(NetworkNumber.Eth)[selectedMarket];
|
|
335
|
-
const allMarketsUnbackedDebts = await getAllMarketsUnbackedDebts(markets, isLegacy, provider, network);
|
|
336
|
-
const interestRateDebtInFront = await getDebtInFrontForSingleMarketLiquityV2(provider, network, isLegacy, LiquityV2Markets(network)[selectedMarket].marketAddress, troveId);
|
|
339
|
+
const allMarketsUnbackedDebts = await getAllMarketsUnbackedDebts(markets, isLegacy, provider, network, blockNumber);
|
|
340
|
+
const interestRateDebtInFront = await getDebtInFrontForSingleMarketLiquityV2(provider, network, isLegacy, LiquityV2Markets(network)[selectedMarket].marketAddress, troveId, '0', 2000, blockNumber);
|
|
337
341
|
|
|
338
342
|
return calculateDebtInFrontLiquityV2(markets, selectedMarket, allMarketsUnbackedDebts, interestRateDebtInFront.toString());
|
|
339
343
|
};
|
|
@@ -347,9 +351,9 @@ const getDebtInFrontLiquityV2 = async (markets: Record<LiquityV2Versions, Liquit
|
|
|
347
351
|
* @param interestRate
|
|
348
352
|
* @param debtInFrontBeingMoved - amound of debt being repositioned if interest rate is being increased (prevents including it as debt in front)
|
|
349
353
|
*/
|
|
350
|
-
const _getDebtInFrontForInterestRateLiquityV2 = async (markets: Record<LiquityV2Versions, LiquityV2MarketData>, selectedMarket: LiquityV2Versions, provider: Client, network: NetworkNumber, isLegacy: boolean, interestRate: string, debtInFrontBeingMoved: string = '0') => {
|
|
351
|
-
const allMarketsUnbackedDebts = await getAllMarketsUnbackedDebts(markets, isLegacy, provider, network);
|
|
352
|
-
const interestRateDebtInFront = new Dec(await getDebtInFrontForInterestRateSingleMarketLiquityV2(provider, network, isLegacy, LiquityV2Markets(network)[selectedMarket].marketAddress, interestRate))
|
|
354
|
+
const _getDebtInFrontForInterestRateLiquityV2 = async (markets: Record<LiquityV2Versions, LiquityV2MarketData>, selectedMarket: LiquityV2Versions, provider: Client, network: NetworkNumber, isLegacy: boolean, interestRate: string, debtInFrontBeingMoved: string = '0', blockNumber: Blockish = 'latest') => {
|
|
355
|
+
const allMarketsUnbackedDebts = await getAllMarketsUnbackedDebts(markets, isLegacy, provider, network, blockNumber);
|
|
356
|
+
const interestRateDebtInFront = new Dec(await getDebtInFrontForInterestRateSingleMarketLiquityV2(provider, network, isLegacy, LiquityV2Markets(network)[selectedMarket].marketAddress, interestRate, '0', '0', 2000, blockNumber))
|
|
353
357
|
.sub(debtInFrontBeingMoved);
|
|
354
358
|
|
|
355
359
|
return calculateDebtInFrontLiquityV2(markets, selectedMarket, allMarketsUnbackedDebts, interestRateDebtInFront.toString());
|
|
@@ -363,7 +367,8 @@ export const getDebtInFrontForInterestRateLiquityV2 = async (
|
|
|
363
367
|
isLegacy: boolean,
|
|
364
368
|
interestRate: string,
|
|
365
369
|
debtInFrontBeingMoved: string = '0',
|
|
366
|
-
|
|
370
|
+
blockNumber: Blockish = 'latest',
|
|
371
|
+
) => _getDebtInFrontForInterestRateLiquityV2(markets, selectedMarket, getViemProvider(provider, network), network, isLegacy, interestRate, debtInFrontBeingMoved, blockNumber);
|
|
367
372
|
|
|
368
373
|
export const _getLiquityV2TroveData = async (
|
|
369
374
|
provider: Client,
|
|
@@ -381,17 +386,18 @@ export const _getLiquityV2TroveData = async (
|
|
|
381
386
|
allMarketsData: Record<LiquityV2Versions, LiquityV2MarketData>,
|
|
382
387
|
},
|
|
383
388
|
fetchDebtInFront: boolean = true,
|
|
389
|
+
blockNumber: Blockish = 'latest',
|
|
384
390
|
): Promise<LiquityV2TroveData> => {
|
|
385
391
|
const { minCollRatio, batchCollRatio } = allMarketsData[selectedMarket.value].marketData;
|
|
386
392
|
const { collateralToken, marketAddress, debtToken } = selectedMarket;
|
|
387
393
|
const troveInfoPromise = selectedMarket.isLegacy
|
|
388
|
-
? LiquityV2LegacyViewContractViem(provider, network).read.getTroveInfo([marketAddress, BigInt(troveId)])
|
|
394
|
+
? LiquityV2LegacyViewContractViem(provider, network).read.getTroveInfo([marketAddress, BigInt(troveId)], setViemBlockNumber(blockNumber))
|
|
389
395
|
: LiquityV2ViewContractViem(provider, network).simulate
|
|
390
|
-
.getTroveInfo([marketAddress, BigInt(troveId)])
|
|
396
|
+
.getTroveInfo([marketAddress, BigInt(troveId)], setViemBlockNumber(blockNumber))
|
|
391
397
|
.then(({ result }) => result);
|
|
392
398
|
const [_data, debtInFront] = await Promise.all([
|
|
393
399
|
troveInfoPromise,
|
|
394
|
-
fetchDebtInFront ? getDebtInFrontLiquityV2(allMarketsData, selectedMarket.value, provider, network, selectedMarket.isLegacy, troveId) : Promise.resolve('0'),
|
|
400
|
+
fetchDebtInFront ? getDebtInFrontLiquityV2(allMarketsData, selectedMarket.value, provider, network, selectedMarket.isLegacy, troveId, blockNumber) : Promise.resolve('0'),
|
|
395
401
|
]);
|
|
396
402
|
const data = {
|
|
397
403
|
..._data,
|
|
@@ -467,6 +473,7 @@ export const getLiquityV2TroveData = async (
|
|
|
467
473
|
allMarketsData: Record<LiquityV2Versions, LiquityV2MarketData>,
|
|
468
474
|
},
|
|
469
475
|
fetchDebtInFront: boolean = true,
|
|
476
|
+
blockNumber: Blockish = 'latest',
|
|
470
477
|
): Promise<LiquityV2TroveData> => _getLiquityV2TroveData(
|
|
471
478
|
getViemProvider(provider, network),
|
|
472
479
|
network,
|
|
@@ -477,12 +484,13 @@ export const getLiquityV2TroveData = async (
|
|
|
477
484
|
allMarketsData,
|
|
478
485
|
},
|
|
479
486
|
fetchDebtInFront,
|
|
487
|
+
blockNumber,
|
|
480
488
|
);
|
|
481
489
|
|
|
482
|
-
export const getLiquityV2ClaimableCollateral = async (collSurplusPoolAddress: EthAddress, account: EthAddress, provider: EthereumProvider, network: NetworkNumber): Promise<string> => {
|
|
490
|
+
export const getLiquityV2ClaimableCollateral = async (collSurplusPoolAddress: EthAddress, account: EthAddress, provider: EthereumProvider, network: NetworkNumber, blockNumber: Blockish = 'latest'): Promise<string> => {
|
|
483
491
|
const client = getViemProvider(provider, network);
|
|
484
492
|
const collSurplusPoolContract = createViemContractFromConfigFunc('LiquityV2CollSurplusPool', collSurplusPoolAddress)(client, network);
|
|
485
|
-
const claimableCollateral = await collSurplusPoolContract.read.getCollateral([account]);
|
|
493
|
+
const claimableCollateral = await collSurplusPoolContract.read.getCollateral([account], setViemBlockNumber(blockNumber));
|
|
486
494
|
return claimableCollateral.toString();
|
|
487
495
|
};
|
|
488
496
|
|
|
@@ -579,7 +587,7 @@ const calculateSPApy = (
|
|
|
579
587
|
return apy;
|
|
580
588
|
};
|
|
581
589
|
|
|
582
|
-
export const getLiquityV2Staking = async (provider: Client, network: NetworkNumber, market: LiquityV2Versions, user: EthAddress) => {
|
|
590
|
+
export const getLiquityV2Staking = async (provider: Client, network: NetworkNumber, market: LiquityV2Versions, user: EthAddress, blockNumber: Blockish = 'latest') => {
|
|
583
591
|
const stabilityPoolView = createViemContractFromConfigFunc('LiquityV2StabilityPool', stabilityPoolAddrForMarket[market])(provider, network);
|
|
584
592
|
const activePoolView = createViemContractFromConfigFunc('LiquityV2ActivePool', activePoolAddrForMarket[market])(provider, network);
|
|
585
593
|
|
|
@@ -599,17 +607,17 @@ export const getLiquityV2Staking = async (provider: Client, network: NetworkNumb
|
|
|
599
607
|
aggWeightedDebtSum,
|
|
600
608
|
lastAggUpdateTime,
|
|
601
609
|
] = await Promise.all([
|
|
602
|
-
stabilityPoolView.read.getDepositorCollGain([user]),
|
|
603
|
-
stabilityPoolView.read.getDepositorYieldGain([user]),
|
|
604
|
-
stabilityPoolView.read.getCompoundedBoldDeposit([user]),
|
|
605
|
-
stabilityPoolView.read.getTotalBoldDeposits(),
|
|
606
|
-
stabilityPoolView.read.P(),
|
|
607
|
-
stabilityPoolView.read.currentScale(),
|
|
608
|
-
stabilityPoolView.read.getYieldGainsPending(),
|
|
609
|
-
debtTokenContract.read.balanceOf([user]),
|
|
610
|
-
|
|
611
|
-
activePoolView.read.aggWeightedDebtSum(),
|
|
612
|
-
activePoolView.read.lastAggUpdateTime(),
|
|
610
|
+
stabilityPoolView.read.getDepositorCollGain([user], setViemBlockNumber(blockNumber)),
|
|
611
|
+
stabilityPoolView.read.getDepositorYieldGain([user], setViemBlockNumber(blockNumber)),
|
|
612
|
+
stabilityPoolView.read.getCompoundedBoldDeposit([user], setViemBlockNumber(blockNumber)),
|
|
613
|
+
stabilityPoolView.read.getTotalBoldDeposits(setViemBlockNumber(blockNumber)),
|
|
614
|
+
stabilityPoolView.read.P(setViemBlockNumber(blockNumber)),
|
|
615
|
+
stabilityPoolView.read.currentScale(setViemBlockNumber(blockNumber)),
|
|
616
|
+
stabilityPoolView.read.getYieldGainsPending(setViemBlockNumber(blockNumber)),
|
|
617
|
+
debtTokenContract.read.balanceOf([user], setViemBlockNumber(blockNumber)),
|
|
618
|
+
|
|
619
|
+
activePoolView.read.aggWeightedDebtSum(setViemBlockNumber(blockNumber)),
|
|
620
|
+
activePoolView.read.lastAggUpdateTime(setViemBlockNumber(blockNumber)),
|
|
613
621
|
]);
|
|
614
622
|
|
|
615
623
|
const stabilityPoolYieldParams: SPYieldGainParameters = {
|
|
@@ -638,7 +646,7 @@ export const getLiquityV2Staking = async (provider: Client, network: NetworkNumb
|
|
|
638
646
|
};
|
|
639
647
|
};
|
|
640
648
|
|
|
641
|
-
export const getLiquitySAndYBold = async (provider: Client, network: NetworkNumber, markets: any, user: EthAddress) => {
|
|
649
|
+
export const getLiquitySAndYBold = async (provider: Client, network: NetworkNumber, markets: any, user: EthAddress, blockNumber: Blockish = 'latest') => {
|
|
642
650
|
const sBold = LiquityV2sBoldVaultViem(provider, network);
|
|
643
651
|
const yBold = createViemContractFromConfigFunc('Erc4626', '0x9F4330700a36B29952869fac9b33f45EEdd8A3d8')(provider, network);
|
|
644
652
|
const stYBold = createViemContractFromConfigFunc('Erc4626', '0x23346B04a7f55b8760E5860AA5A77383D63491cD')(provider, network);
|
|
@@ -667,22 +675,22 @@ export const getLiquitySAndYBold = async (provider: Client, network: NetworkNumb
|
|
|
667
675
|
yBoldBalance,
|
|
668
676
|
stYBoldBalance,
|
|
669
677
|
] = await Promise.all([
|
|
670
|
-
sBold.read.totalAssets(),
|
|
671
|
-
sBold.read.convertToShares([BigInt(1e18)]),
|
|
672
|
-
sBold.read.maxWithdraw([user]),
|
|
678
|
+
sBold.read.totalAssets(setViemBlockNumber(blockNumber)),
|
|
679
|
+
sBold.read.convertToShares([BigInt(1e18)], setViemBlockNumber(blockNumber)),
|
|
680
|
+
sBold.read.maxWithdraw([user], setViemBlockNumber(blockNumber)),
|
|
673
681
|
|
|
674
|
-
sBold.read.sps([BigInt(0)]),
|
|
675
|
-
sBold.read.sps([BigInt(1)]),
|
|
676
|
-
sBold.read.sps([BigInt(2)]),
|
|
682
|
+
sBold.read.sps([BigInt(0)], setViemBlockNumber(blockNumber)),
|
|
683
|
+
sBold.read.sps([BigInt(1)], setViemBlockNumber(blockNumber)),
|
|
684
|
+
sBold.read.sps([BigInt(2)], setViemBlockNumber(blockNumber)),
|
|
677
685
|
|
|
678
|
-
yBold.read.totalAssets(),
|
|
679
|
-
yBold.read.maxWithdraw([user]),
|
|
680
|
-
stYBold.read.convertToShares([BigInt(1e18)]),
|
|
686
|
+
yBold.read.totalAssets(setViemBlockNumber(blockNumber)),
|
|
687
|
+
yBold.read.maxWithdraw([user], setViemBlockNumber(blockNumber)),
|
|
688
|
+
stYBold.read.convertToShares([BigInt(1e18)], setViemBlockNumber(blockNumber)),
|
|
681
689
|
getYBoldApyApi(),
|
|
682
690
|
|
|
683
|
-
sBold.read.balanceOf([user]),
|
|
684
|
-
yBold.read.balanceOf([user]),
|
|
685
|
-
stYBold.read.balanceOf([user]),
|
|
691
|
+
sBold.read.balanceOf([user], setViemBlockNumber(blockNumber)),
|
|
692
|
+
yBold.read.balanceOf([user], setViemBlockNumber(blockNumber)),
|
|
693
|
+
stYBold.read.balanceOf([user], setViemBlockNumber(blockNumber)),
|
|
686
694
|
]);
|
|
687
695
|
|
|
688
696
|
const spMarketRes = {
|
package/src/llamaLend/index.ts
CHANGED
|
@@ -15,7 +15,7 @@ import { getLlamaLendMarketFromControllerAddress } from '../markets/llamaLend';
|
|
|
15
15
|
import { getStakingApy, STAKING_ASSETS } from '../staking';
|
|
16
16
|
import { getViemProvider, setViemBlockNumber } from '../services/viem';
|
|
17
17
|
|
|
18
|
-
const getAndFormatBands = async (provider: Client, network: NetworkNumber, selectedMarket: LlamaLendMarketData, _minBand: string, _maxBand: string) => {
|
|
18
|
+
const getAndFormatBands = async (provider: Client, network: NetworkNumber, selectedMarket: LlamaLendMarketData, _minBand: string, _maxBand: string, blockNumber: Blockish = 'latest') => {
|
|
19
19
|
const contract = LlamaLendViewContractViem(provider, network);
|
|
20
20
|
const minBand = parseInt(_minBand, 10);
|
|
21
21
|
const maxBand = parseInt(_maxBand, 10);
|
|
@@ -39,7 +39,7 @@ const getAndFormatBands = async (provider: Client, network: NetworkNumber, selec
|
|
|
39
39
|
} else {
|
|
40
40
|
start = pivots[index - 1] + 1;
|
|
41
41
|
}
|
|
42
|
-
const pivotedBandsData = await contract.read.getBandsData([selectedMarket.controllerAddress, BigInt(start), BigInt(pivot)]);
|
|
42
|
+
const pivotedBandsData = await contract.read.getBandsData([selectedMarket.controllerAddress, BigInt(start), BigInt(pivot)], setViemBlockNumber(blockNumber));
|
|
43
43
|
return pivotedBandsData;
|
|
44
44
|
}))).flat();
|
|
45
45
|
|
|
@@ -52,13 +52,13 @@ const getAndFormatBands = async (provider: Client, network: NetworkNumber, selec
|
|
|
52
52
|
}));
|
|
53
53
|
};
|
|
54
54
|
|
|
55
|
-
export const _getLlamaLendGlobalData = async (provider: Client, network: NetworkNumber, selectedMarket: LlamaLendMarketData): Promise<LlamaLendGlobalMarketData> => {
|
|
55
|
+
export const _getLlamaLendGlobalData = async (provider: Client, network: NetworkNumber, selectedMarket: LlamaLendMarketData, blockNumber: Blockish = 'latest'): Promise<LlamaLendGlobalMarketData> => {
|
|
56
56
|
const contract = LlamaLendViewContractViem(provider, network);
|
|
57
57
|
|
|
58
58
|
const collAsset = selectedMarket.collAsset;
|
|
59
59
|
const debtAsset = selectedMarket.baseAsset;
|
|
60
60
|
|
|
61
|
-
const data = await contract.read.globalData([selectedMarket.controllerAddress]);
|
|
61
|
+
const data = await contract.read.globalData([selectedMarket.controllerAddress], setViemBlockNumber(blockNumber));
|
|
62
62
|
|
|
63
63
|
// all prices are in 18 decimals
|
|
64
64
|
const oraclePrice = getEthAmountForDecimals(data.oraclePrice.toString(), 18);
|
|
@@ -82,7 +82,7 @@ export const _getLlamaLendGlobalData = async (provider: Client, network: Network
|
|
|
82
82
|
const futureBorrowRate = new Dec(new Dec(2.718281828459).pow(exponentFutureRate).minus(1)).mul(100)
|
|
83
83
|
.toString();
|
|
84
84
|
|
|
85
|
-
const bandsData = await getAndFormatBands(provider, network, selectedMarket, data.minBand.toString(), data.maxBand.toString());
|
|
85
|
+
const bandsData = await getAndFormatBands(provider, network, selectedMarket, data.minBand.toString(), data.maxBand.toString(), blockNumber);
|
|
86
86
|
const cap = assetAmountInEth(data.debtTokenTotalSupply.toString(), debtAsset);
|
|
87
87
|
const leftToBorrow = getEthAmountForDecimals(data.debtTokenLeftToBorrow.toString(), 18);
|
|
88
88
|
|
|
@@ -152,7 +152,8 @@ export const getLlamaLendGlobalData = async (
|
|
|
152
152
|
provider: EthereumProvider,
|
|
153
153
|
network: NetworkNumber,
|
|
154
154
|
selectedMarket: LlamaLendMarketData,
|
|
155
|
-
|
|
155
|
+
blockNumber: Blockish = 'latest',
|
|
156
|
+
): Promise<LlamaLendGlobalMarketData> => _getLlamaLendGlobalData(getViemProvider(provider, network), network, selectedMarket, blockNumber);
|
|
156
157
|
|
|
157
158
|
const getStatusForUser = (bandRange: string[], activeBand: string, debtSupplied: string, collSupplied: string, healthPercent: string) => {
|
|
158
159
|
// if bands are equal, that can only be [0,0] which means user doesn't have loan (min number of bands is 4)
|
|
@@ -209,11 +210,11 @@ export const getLlamaLendAccountBalances = async (
|
|
|
209
210
|
controllerAddress: EthAddress,
|
|
210
211
|
): Promise<PositionBalances> => _getLlamaLendAccountBalances(getViemProvider(provider, network), network, block, addressMapping, address, controllerAddress);
|
|
211
212
|
|
|
212
|
-
export const _getLlamaLendUserData = async (provider: Client, network: NetworkNumber, address: EthAddress, selectedMarket: LlamaLendMarketData, marketData: LlamaLendGlobalMarketData): Promise<LlamaLendUserData> => {
|
|
213
|
+
export const _getLlamaLendUserData = async (provider: Client, network: NetworkNumber, address: EthAddress, selectedMarket: LlamaLendMarketData, marketData: LlamaLendGlobalMarketData, blockNumber: Blockish = 'latest'): Promise<LlamaLendUserData> => {
|
|
213
214
|
const contract = LlamaLendViewContractViem(provider, network);
|
|
214
215
|
const { assetsData } = marketData;
|
|
215
216
|
|
|
216
|
-
const data = await contract.read.userData([selectedMarket.controllerAddress, address]);
|
|
217
|
+
const data = await contract.read.userData([selectedMarket.controllerAddress, address], setViemBlockNumber(blockNumber));
|
|
217
218
|
const collAsset = selectedMarket.collAsset;
|
|
218
219
|
const debtAsset = selectedMarket.baseAsset;
|
|
219
220
|
|
|
@@ -267,7 +268,7 @@ export const _getLlamaLendUserData = async (provider: Client, network: NetworkNu
|
|
|
267
268
|
const priceHigh = assetAmountInEth(data.priceHigh.toString());
|
|
268
269
|
const priceLow = assetAmountInEth(data.priceLow.toString());
|
|
269
270
|
|
|
270
|
-
const _userBands = data.loanExists ? (await getAndFormatBands(provider, network, selectedMarket, data.bandRange[0].toString(), data.bandRange[1].toString())) : [];
|
|
271
|
+
const _userBands = data.loanExists ? (await getAndFormatBands(provider, network, selectedMarket, data.bandRange[0].toString(), data.bandRange[1].toString(), blockNumber)) : [];
|
|
271
272
|
|
|
272
273
|
const status = data.loanExists ? getStatusForUser(data.bandRange.map(b => b.toString()), marketData.activeBand, debtSupplied, collSupplied, healthPercent) : LlamaLendStatus.Nonexistant;
|
|
273
274
|
|
|
@@ -301,10 +302,11 @@ export const getLlamaLendUserData = async (
|
|
|
301
302
|
address: EthAddress,
|
|
302
303
|
selectedMarket: LlamaLendMarketData,
|
|
303
304
|
marketData: LlamaLendGlobalMarketData,
|
|
304
|
-
|
|
305
|
+
blockNumber: Blockish = 'latest',
|
|
306
|
+
): Promise<LlamaLendUserData> => _getLlamaLendUserData(getViemProvider(provider, network), network, address, selectedMarket, marketData, blockNumber);
|
|
305
307
|
|
|
306
|
-
export const getLlamaLendFullPositionData = async (provider: EthereumProvider, network: NetworkNumber, address: EthAddress, selectedMarket: LlamaLendMarketData): Promise<LlamaLendUserData> => {
|
|
307
|
-
const marketData = await getLlamaLendGlobalData(provider, network, selectedMarket);
|
|
308
|
-
const positionData = await getLlamaLendUserData(provider, network, address, selectedMarket, marketData);
|
|
308
|
+
export const getLlamaLendFullPositionData = async (provider: EthereumProvider, network: NetworkNumber, address: EthAddress, selectedMarket: LlamaLendMarketData, blockNumber: Blockish = 'latest'): Promise<LlamaLendUserData> => {
|
|
309
|
+
const marketData = await getLlamaLendGlobalData(provider, network, selectedMarket, blockNumber);
|
|
310
|
+
const positionData = await getLlamaLendUserData(provider, network, address, selectedMarket, marketData, blockNumber);
|
|
309
311
|
return positionData;
|
|
310
312
|
};
|
package/src/maker/index.ts
CHANGED
|
@@ -106,13 +106,13 @@ export const getMakerAccountBalances = async (
|
|
|
106
106
|
_managerAddress?: EthAddress,
|
|
107
107
|
): Promise<PositionBalances> => _getMakerAccountBalances(getViemProvider(provider, network, { batch: { multicall: true } }), network, block, addressMapping, cdpId, _managerAddress);
|
|
108
108
|
|
|
109
|
-
export const _getUserCdps = async (provider: Client, network: NetworkNumber, userAddress: EthAddress): Promise<CdpInfo[]> => {
|
|
109
|
+
export const _getUserCdps = async (provider: Client, network: NetworkNumber, userAddress: EthAddress, blockNumber: Blockish = 'latest'): Promise<CdpInfo[]> => {
|
|
110
110
|
if (!userAddress) return [];
|
|
111
111
|
const mcdGetCdpsContract = McdGetCdpsContractViem(provider, network);
|
|
112
112
|
|
|
113
113
|
const mcdCdpManagerAddress = getConfigContractAddress('McdCdpManager', network);
|
|
114
114
|
|
|
115
|
-
const standardCdps = await mcdGetCdpsContract.read.getCdpsAsc([mcdCdpManagerAddress, userAddress]);
|
|
115
|
+
const standardCdps = await mcdGetCdpsContract.read.getCdpsAsc([mcdCdpManagerAddress, userAddress], setViemBlockNumber(blockNumber));
|
|
116
116
|
|
|
117
117
|
const parsedStandardCdps = standardCdps[0].map((id, i) => ({
|
|
118
118
|
id: parseInt(id.toString(), 10),
|
|
@@ -131,15 +131,16 @@ export const getUserCdps = async (
|
|
|
131
131
|
provider: EthereumProvider,
|
|
132
132
|
network: NetworkNumber,
|
|
133
133
|
userAddress: EthAddress,
|
|
134
|
-
|
|
134
|
+
blockNumber: Blockish = 'latest',
|
|
135
|
+
): Promise<CdpInfo[]> => _getUserCdps(getViemProvider(provider, network), network, userAddress, blockNumber);
|
|
135
136
|
|
|
136
|
-
export const _getMakerIlksData = async (provider: Client, network: NetworkNumber, ilkLabels: string[]): Promise<Record<string, IlkInfo>> => {
|
|
137
|
+
export const _getMakerIlksData = async (provider: Client, network: NetworkNumber, ilkLabels: string[], blockNumber: Blockish = 'latest'): Promise<Record<string, IlkInfo>> => {
|
|
137
138
|
const vatContract = McdVatContractViem(provider, network);
|
|
138
139
|
const spotterContract = McdSpotterContractViem(provider, network);
|
|
139
140
|
const dogContract = McdDogContractViem(provider, network);
|
|
140
141
|
const jugContract = McdJugContractViem(provider, network);
|
|
141
142
|
|
|
142
|
-
const par = await spotterContract.read.par();
|
|
143
|
+
const par = await spotterContract.read.par(setViemBlockNumber(blockNumber));
|
|
143
144
|
|
|
144
145
|
const ilksInfo = await Promise.all(ilkLabels.map(async (ilkLabel) => {
|
|
145
146
|
const ilk = stringToBytes(ilkLabel) as HexString;
|
|
@@ -150,11 +151,11 @@ export const _getMakerIlksData = async (provider: Client, network: NetworkNumber
|
|
|
150
151
|
futureRate,
|
|
151
152
|
chop,
|
|
152
153
|
] = await Promise.all([
|
|
153
|
-
spotterContract.read.ilks([ilk]),
|
|
154
|
-
vatContract.read.ilks([ilk]),
|
|
155
|
-
jugContract.read.ilks([ilk]),
|
|
156
|
-
jugContract.read.drip([ilk]),
|
|
157
|
-
dogContract.read.chop([ilk]),
|
|
154
|
+
spotterContract.read.ilks([ilk], setViemBlockNumber(blockNumber)),
|
|
155
|
+
vatContract.read.ilks([ilk], setViemBlockNumber(blockNumber)),
|
|
156
|
+
jugContract.read.ilks([ilk], setViemBlockNumber(blockNumber)),
|
|
157
|
+
jugContract.read.drip([ilk], setViemBlockNumber(blockNumber)),
|
|
158
|
+
dogContract.read.chop([ilk], setViemBlockNumber(blockNumber)),
|
|
158
159
|
]);
|
|
159
160
|
|
|
160
161
|
return parseCollateralInfo(
|
|
@@ -179,12 +180,13 @@ export const getMakerIlksData = async (
|
|
|
179
180
|
provider: EthereumProvider,
|
|
180
181
|
network: NetworkNumber,
|
|
181
182
|
ilkLabels: string[],
|
|
182
|
-
|
|
183
|
+
blockNumber: Blockish = 'latest',
|
|
184
|
+
): Promise<Record<string, IlkInfo>> => _getMakerIlksData(getViemProvider(provider, network, { batch: { multicall: true } }), network, ilkLabels, blockNumber);
|
|
183
185
|
|
|
184
186
|
/**
|
|
185
187
|
* @param ilkInfo optional precomputed ilk data (from `_getMakerIlksData`); when provided the per-ilk reads are skipped
|
|
186
188
|
*/
|
|
187
|
-
export const _getMakerCdpData = async (provider: Client, network: NetworkNumber, cdp: CdpInfo, ilkInfo?: IlkInfo): Promise<CdpData> => {
|
|
189
|
+
export const _getMakerCdpData = async (provider: Client, network: NetworkNumber, cdp: CdpInfo, ilkInfo?: IlkInfo, blockNumber: Blockish = 'latest'): Promise<CdpData> => {
|
|
188
190
|
const vatContract = McdVatContractViem(provider, network);
|
|
189
191
|
|
|
190
192
|
const [
|
|
@@ -193,9 +195,9 @@ export const _getMakerCdpData = async (provider: Client, network: NetworkNumber,
|
|
|
193
195
|
fetchedIlkInfo,
|
|
194
196
|
collStakingApy,
|
|
195
197
|
] = await Promise.all([
|
|
196
|
-
vatContract.read.urns([cdp.ilk, cdp.urn]),
|
|
197
|
-
vatContract.read.gem([cdp.ilk, cdp.urn]),
|
|
198
|
-
ilkInfo || _getMakerIlksData(provider, network, [cdp.ilkLabel]).then((ilks) => ilks[cdp.ilkLabel]),
|
|
198
|
+
vatContract.read.urns([cdp.ilk, cdp.urn], setViemBlockNumber(blockNumber)),
|
|
199
|
+
vatContract.read.gem([cdp.ilk, cdp.urn], setViemBlockNumber(blockNumber)),
|
|
200
|
+
ilkInfo || _getMakerIlksData(provider, network, [cdp.ilkLabel], blockNumber).then((ilks) => ilks[cdp.ilkLabel]),
|
|
199
201
|
STAKING_ASSETS.includes(cdp.asset) ? getStakingApy(cdp.asset) : Promise.resolve('0'),
|
|
200
202
|
]);
|
|
201
203
|
|
|
@@ -270,4 +272,5 @@ export const getMakerCdpData = async (
|
|
|
270
272
|
provider: EthereumProvider,
|
|
271
273
|
network: NetworkNumber,
|
|
272
274
|
cdp: CdpInfo,
|
|
273
|
-
|
|
275
|
+
blockNumber: Blockish = 'latest',
|
|
276
|
+
): Promise<CdpData> => _getMakerCdpData(getViemProvider(provider, network), network, cdp, undefined, blockNumber);
|