@1delta/margin-fetcher 0.0.10 → 0.0.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/lending/aave-v2-type/types.d.ts +2 -0
- package/dist/lending/aave-v2-type/types.d.ts.map +1 -1
- package/dist/lending/compound-v3/publicCallParse.d.ts +4 -4
- package/dist/lending/compound-v3/publicCallParse.d.ts.map +1 -1
- package/dist/lending/compound-v3/publicCallParse.js +37 -22
- package/dist/lending/fetchLender.d.ts.map +1 -1
- package/dist/lending/fetchLender.js +25 -29
- package/dist/lending/fetchLenderAll.d.ts +2 -1
- package/dist/lending/fetchLenderAll.d.ts.map +1 -1
- package/dist/lending/fetchLenderAll.js +3 -3
- package/dist/lending/fetchLenderExt.d.ts +2 -1
- package/dist/lending/fetchLenderExt.d.ts.map +1 -1
- package/dist/lending/fetchLenderExt.js +28 -14
- package/dist/lending/index.d.ts +2 -0
- package/dist/lending/index.d.ts.map +1 -1
- package/dist/lending/index.js +2 -0
- package/dist/lending/morpho/convertPublic.d.ts +3 -5
- package/dist/lending/morpho/convertPublic.d.ts.map +1 -1
- package/dist/lending/morpho/convertPublic.js +16 -12
- package/dist/lending/morpho/fetchPublic.js +1 -1
- package/dist/lending/morpho/publicCallBuild.d.ts +1 -4
- package/dist/lending/morpho/publicCallBuild.d.ts.map +1 -1
- package/dist/lending/morpho/types.d.ts +2 -0
- package/dist/lending/morpho/types.d.ts.map +1 -1
- package/dist/lending/user-data/aave-v3-type/userCallBuild.d.ts.map +1 -1
- package/dist/lending/user-data/aave-v3-type/userCallBuild.js +4 -7
- package/dist/lending/user-data/abis.d.ts +88 -0
- package/dist/lending/user-data/abis.d.ts.map +1 -1
- package/dist/lending/user-data/abis.js +5 -1
- package/dist/lending/user-data/compound-v3/userCallParse.d.ts.map +1 -1
- package/dist/lending/user-data/compound-v3/userCallParse.js +3 -2
- package/dist/lending/user-data/fetchUserData.d.ts +4 -4
- package/dist/lending/user-data/fetchUserData.d.ts.map +1 -1
- package/dist/lending/user-data/fetchUserData.js +46 -25
- package/dist/lending/user-data/init/userCallParse.js +1 -1
- package/dist/lending/user-data/morpho/userCallBuild.d.ts +4 -5
- package/dist/lending/user-data/morpho/userCallBuild.d.ts.map +1 -1
- package/dist/lending/user-data/morpho/userCallBuild.js +15 -9
- package/dist/lending/user-data/morpho/userCallParse.d.ts +1 -2
- package/dist/lending/user-data/morpho/userCallParse.d.ts.map +1 -1
- package/dist/lending/user-data/morpho/userCallParse.js +15 -12
- package/dist/lending/user-data/types.d.ts +30 -19
- package/dist/lending/user-data/types.d.ts.map +1 -1
- package/dist/lending/user-data/types.js +12 -0
- package/dist/lending/user-data/utils/createGeneralUserState.d.ts +4 -4
- package/dist/lending/user-data/utils/createGeneralUserState.d.ts.map +1 -1
- package/dist/lending/user-data/utils/createGeneralUserState.js +24 -17
- package/dist/utils/index.d.ts +5 -3
- package/dist/utils/index.d.ts.map +1 -1
- package/dist/utils/index.js +20 -11
- package/dist/utils/parsing.d.ts +1 -0
- package/dist/utils/parsing.d.ts.map +1 -1
- package/dist/utils/parsing.js +3 -0
- package/dist/yields/index.d.ts.map +1 -1
- package/dist/yields/index.js +12 -5
- package/package.json +1 -1
- package/src/abis/morpho/blue.ts +99 -0
- package/src/abis/morpho/lens.ts +31 -0
- package/src/lending/aave-v2-type/types.ts +2 -0
- package/src/lending/compound-v3/publicCallParse.ts +93 -37
- package/src/lending/fetchLender.ts +86 -49
- package/src/lending/fetchLenderAll.ts +30 -0
- package/src/lending/fetchLenderExt.ts +82 -0
- package/src/lending/index.ts +3 -1
- package/src/lending/morpho/convertPublic.ts +153 -0
- package/src/lending/morpho/fetchPublic.ts +80 -0
- package/src/lending/morpho/publicCallBuild.ts +11 -0
- package/src/lending/morpho/types.ts +129 -0
- package/src/lending/user-data/aave-v3-type/userCallBuild.ts +2 -8
- package/src/lending/user-data/abis.ts +4 -1
- package/src/lending/user-data/compound-v3/userCallParse.ts +3 -2
- package/src/lending/user-data/fetchUserData.ts +114 -33
- package/src/lending/user-data/init/userCallParse.ts +1 -1
- package/src/lending/user-data/morpho/decoder.ts +71 -0
- package/src/lending/user-data/morpho/morphoLib.ts +61 -0
- package/src/lending/user-data/morpho/types.ts +12 -0
- package/src/lending/user-data/morpho/userCallBuild.ts +63 -0
- package/src/lending/user-data/morpho/userCallParse.ts +150 -0
- package/src/lending/user-data/morpho/userCallParseNoDeploy.ts +168 -0
- package/src/lending/user-data/types.ts +64 -42
- package/src/lending/user-data/utils/createGeneralUserState.ts +56 -32
- package/src/utils/index.ts +32 -14
- package/src/utils/parsing.ts +5 -0
- package/src/yields/index.ts +13 -4
- package/test/lenderData.test.ts +13251 -13649
- package/test/mainPrices.test.ts +1 -13
- package/test/morpho.test.ts +20 -0
- package/test/userDataMorpho.test.ts +79 -0
- package/test/utils.ts +13 -0
|
@@ -1,17 +1,27 @@
|
|
|
1
|
-
import { divideAccrualsToAprs } from
|
|
2
|
-
import {
|
|
3
|
-
|
|
1
|
+
import { divideAccrualsToAprs } from '.'
|
|
2
|
+
import {
|
|
3
|
+
AaveV2Public,
|
|
4
|
+
AaveV2UserReserveResponse,
|
|
5
|
+
AaveV3Public,
|
|
6
|
+
AaveV3UserReserveResponse,
|
|
7
|
+
CompoundV3Public,
|
|
8
|
+
CompoundV3UserReserveResponse,
|
|
9
|
+
} from '../types'
|
|
10
|
+
import { safeDivide } from './formatting'
|
|
4
11
|
|
|
5
|
-
type LenderUserData =
|
|
12
|
+
type LenderUserData =
|
|
13
|
+
| AaveV3UserReserveResponse
|
|
14
|
+
| AaveV2UserReserveResponse
|
|
15
|
+
| CompoundV3UserReserveResponse
|
|
6
16
|
type LenderState = AaveV3Public | AaveV2Public | CompoundV3Public
|
|
7
17
|
|
|
8
18
|
export function createBaseTypeUserState(
|
|
9
19
|
payload: LenderUserData,
|
|
10
20
|
lenderData: { [a: string]: LenderState },
|
|
11
21
|
totalDeposits24h: number = 0,
|
|
12
|
-
totalDebt24h: number = 0
|
|
22
|
+
totalDebt24h: number = 0,
|
|
13
23
|
) {
|
|
14
|
-
let assetKeys = Object.keys(payload.lendingPositions[
|
|
24
|
+
let assetKeys = Object.keys(payload.lendingPositions['0'] ?? {})
|
|
15
25
|
const { chainId, account } = payload
|
|
16
26
|
|
|
17
27
|
const mode = Number((payload as any).userEMode ?? 0)
|
|
@@ -35,11 +45,15 @@ export function createBaseTypeUserState(
|
|
|
35
45
|
// main user data
|
|
36
46
|
for (let i = 0; i < assetKeys.length; i++) {
|
|
37
47
|
const asset = assetKeys[i]
|
|
38
|
-
if (!lenderData?.[asset]) continue
|
|
39
|
-
const { depositsUSD, debtStableUSD, debtUSD, collateralActive } =
|
|
48
|
+
if (!lenderData?.[asset]) continue
|
|
49
|
+
const { depositsUSD, debtStableUSD, debtUSD, collateralActive } =
|
|
50
|
+
payload.lendingPositions['0'][asset]
|
|
40
51
|
const {
|
|
41
|
-
depositRate,
|
|
42
|
-
|
|
52
|
+
depositRate,
|
|
53
|
+
stakingYield,
|
|
54
|
+
variableBorrowRate,
|
|
55
|
+
stableBorrowRate,
|
|
56
|
+
rewards,
|
|
43
57
|
} = lenderData[asset]
|
|
44
58
|
|
|
45
59
|
// amounts
|
|
@@ -47,37 +61,47 @@ export function createBaseTypeUserState(
|
|
|
47
61
|
debt += debtStableUSD
|
|
48
62
|
debt += debtUSD
|
|
49
63
|
|
|
50
|
-
// rewards
|
|
51
|
-
Object.entries(rewards ?? {}).forEach(
|
|
52
|
-
|
|
53
|
-
|
|
64
|
+
// rewards
|
|
65
|
+
Object.entries(rewards ?? {}).forEach(
|
|
66
|
+
([key, rewardData]: [string, any]) => {
|
|
67
|
+
rewardDepositAccrual += rewardData.depositRate * depositsUSD
|
|
68
|
+
rewardBorrowAccrual +=
|
|
69
|
+
rewardData.variableBorrowRate * debtUSD +
|
|
70
|
+
(rewardData.stableBorrowRate ?? 0) * debtStableUSD
|
|
54
71
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
72
|
+
// totals
|
|
73
|
+
const rewDepo = rewardData.depositRate * depositsUSD
|
|
74
|
+
const rewDebt =
|
|
75
|
+
rewardData.variableBorrowRate * debtUSD +
|
|
76
|
+
(rewardData.stableBorrowRate ?? 0) * (debtStableUSD ?? 0)
|
|
58
77
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
78
|
+
if (!rewardsPerAsset[key])
|
|
79
|
+
rewardsPerAsset[key] = { depositApr: 0, borrowApr: 0 }
|
|
80
|
+
if (rewDepo > 0) rewardsPerAsset[key].depositApr += rewDepo
|
|
81
|
+
if (rewDebt > 0) rewardsPerAsset[key].borrowApr += rewDebt
|
|
82
|
+
},
|
|
83
|
+
)
|
|
63
84
|
|
|
64
85
|
// staking
|
|
65
86
|
stakingDepositAccrual += (stakingYield ?? 0) * depositsUSD
|
|
66
87
|
stakingBorrowAccrual += (stakingYield ?? 0) * (debtStableUSD + debtUSD)
|
|
67
88
|
if (collateralActive) {
|
|
68
89
|
// risk adjusted
|
|
69
|
-
collateral +=
|
|
70
|
-
|
|
90
|
+
collateral +=
|
|
91
|
+
(lenderData[asset].config[mode]?.collateralFactor ?? 1) * depositsUSD
|
|
92
|
+
borrowDiscountedCollateral +=
|
|
93
|
+
(lenderData[asset].config[mode]?.borrowCollateralFactor ?? 1) *
|
|
94
|
+
depositsUSD
|
|
71
95
|
}
|
|
72
96
|
// IRs
|
|
73
97
|
depositInterest += depositRate * depositsUSD
|
|
74
|
-
borrowInterest +=
|
|
75
|
-
|
|
98
|
+
borrowInterest +=
|
|
99
|
+
debtStableUSD * stableBorrowRate + debtUSD * variableBorrowRate
|
|
76
100
|
}
|
|
77
101
|
const nav = deposits - debt
|
|
78
102
|
// aggregated balance data
|
|
79
103
|
const balanceData = {
|
|
80
|
-
|
|
104
|
+
'0': {
|
|
81
105
|
borrowDiscountedCollateral,
|
|
82
106
|
collateral,
|
|
83
107
|
deposits,
|
|
@@ -87,13 +111,13 @@ export function createBaseTypeUserState(
|
|
|
87
111
|
deposits24h: totalDeposits24h,
|
|
88
112
|
debt24h: totalDebt24h,
|
|
89
113
|
nav24h: totalDeposits24h - totalDebt24h,
|
|
90
|
-
...(payload.rewards ? { rewards: payload.rewards } : {})
|
|
91
|
-
}
|
|
114
|
+
...(payload.rewards ? { rewards: payload.rewards } : {}),
|
|
115
|
+
},
|
|
92
116
|
}
|
|
93
117
|
|
|
94
118
|
// aggregated apr data
|
|
95
119
|
const aprData = {
|
|
96
|
-
|
|
120
|
+
'0': {
|
|
97
121
|
apr: safeDivide(depositInterest - borrowInterest, nav),
|
|
98
122
|
borrowApr: safeDivide(borrowInterest, debt),
|
|
99
123
|
depositApr: safeDivide(depositInterest, deposits),
|
|
@@ -104,9 +128,9 @@ export function createBaseTypeUserState(
|
|
|
104
128
|
stakingApr: safeDivide(stakingDepositAccrual - stakingBorrowAccrual, nav),
|
|
105
129
|
stakingDepositApr: safeDivide(stakingDepositAccrual, deposits),
|
|
106
130
|
stakingBorrowApr: safeDivide(stakingBorrowAccrual, debt),
|
|
107
|
-
}
|
|
131
|
+
},
|
|
108
132
|
}
|
|
109
|
-
const userConfigs = {
|
|
133
|
+
const userConfigs = { '0': { selectedMode: mode, id: account } }
|
|
110
134
|
|
|
111
135
|
return {
|
|
112
136
|
account,
|
|
@@ -115,4 +139,4 @@ export function createBaseTypeUserState(
|
|
|
115
139
|
aprData,
|
|
116
140
|
userConfigs,
|
|
117
141
|
}
|
|
118
|
-
}
|
|
142
|
+
}
|
package/src/utils/index.ts
CHANGED
|
@@ -9,12 +9,26 @@ import {
|
|
|
9
9
|
Lender,
|
|
10
10
|
SWAYLEND_RESERVE_ASSETS,
|
|
11
11
|
INIT_CONFIG_DATA,
|
|
12
|
+
MORPHO_BLUE_POOL_DATA,
|
|
12
13
|
} from "@1delta/asset-registry"
|
|
13
14
|
import { uniq } from "lodash"
|
|
14
15
|
import { COMPOSERS } from "./constants"
|
|
15
16
|
|
|
16
17
|
const AAVE_V3_KEY = "AAVE_V3"
|
|
17
18
|
const COMPOUND_V3_KEY = "COMPOUND_V3"
|
|
19
|
+
|
|
20
|
+
export function isCompoundV3(lender: string) { return lender.startsWith(COMPOUND_V3_KEY) }
|
|
21
|
+
|
|
22
|
+
export function isCompoundV3Type(lender: string) { return lender.startsWith(COMPOUND_V3_KEY) }
|
|
23
|
+
|
|
24
|
+
export function isInit(lender: string) { return lender === Lender.INIT }
|
|
25
|
+
|
|
26
|
+
export function isMorphoType(lender: string) {
|
|
27
|
+
return [
|
|
28
|
+
Lender.MORPHO_BLUE
|
|
29
|
+
].includes(lender as any)
|
|
30
|
+
}
|
|
31
|
+
|
|
18
32
|
const SWAYLEND_KEY = "SWAYLEND"
|
|
19
33
|
|
|
20
34
|
export const AAVE_V3_2_PROTOCOLS = [Lender.AAVE_V3, Lender.AAVE_V3_ETHER_FI, Lender.AAVE_V3_PRIME]
|
|
@@ -43,18 +57,6 @@ export function isAaveType(lender: string) {
|
|
|
43
57
|
return [...AAVE_V3_LENDERS, ...AAVE_V2_LENDERS].includes(lender as any)
|
|
44
58
|
}
|
|
45
59
|
|
|
46
|
-
export function isCompoundV3(lender: string) {
|
|
47
|
-
return lender.startsWith(COMPOUND_V3_KEY)
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export function isCompoundV3Type(lender: string) {
|
|
51
|
-
return lender.startsWith(COMPOUND_V3_KEY)
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
export function isInit(lender: string) {
|
|
55
|
-
return lender === Lender.INIT
|
|
56
|
-
}
|
|
57
|
-
|
|
58
60
|
export function isSwaylend(lender: string) {
|
|
59
61
|
return lender.startsWith(SWAYLEND_KEY)
|
|
60
62
|
}
|
|
@@ -79,6 +81,13 @@ export const getLendersForChain = (c: string) => {
|
|
|
79
81
|
if (chains.includes(c)) lenders.push(l)
|
|
80
82
|
})
|
|
81
83
|
|
|
84
|
+
// morpho
|
|
85
|
+
Object.entries(MORPHO_BLUE_POOL_DATA).forEach(([l, data]) => {
|
|
86
|
+
const chains = Object.keys(data)
|
|
87
|
+
if (chains.includes(c)) lenders.push(l)
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
|
|
82
91
|
return lenders
|
|
83
92
|
}
|
|
84
93
|
|
|
@@ -89,8 +98,9 @@ export const filterLendersByProtocol = (allLenders: string[], protocolList?: str
|
|
|
89
98
|
return allLenders
|
|
90
99
|
}
|
|
91
100
|
|
|
101
|
+
const hasMorpho = protocolList.includes(Lender.MORPHO_BLUE)
|
|
92
102
|
// Filter lenders based on protocol list
|
|
93
|
-
return allLenders.filter((lender) => protocolList.includes(lender))
|
|
103
|
+
return allLenders.filter((lender) => protocolList.includes(lender) || (hasMorpho && lender.startsWith("MORPHO_BLUE")))
|
|
94
104
|
}
|
|
95
105
|
|
|
96
106
|
const getAavesForChain = () => {
|
|
@@ -111,7 +121,10 @@ export const getComposerAddress = (chainId: string): string => {
|
|
|
111
121
|
return COMPOSERS[chainId as keyof typeof COMPOSERS]
|
|
112
122
|
}
|
|
113
123
|
|
|
114
|
-
export const getLenderAssets = (
|
|
124
|
+
export const getLenderAssets = (
|
|
125
|
+
chainId: string | number | undefined,
|
|
126
|
+
lendingProtocol = Lender.AAVE_V3
|
|
127
|
+
): string[] => {
|
|
115
128
|
if (!chainId) return []
|
|
116
129
|
if (isAaveType(lendingProtocol)) return AAVE_STYLE_RESERVE_ASSETS[lendingProtocol]?.[chainId] ?? []
|
|
117
130
|
if (isCompoundV3(lendingProtocol)) return COMPOUND_STYLE_RESERVE_ASSETS[lendingProtocol]?.[chainId] ?? []
|
|
@@ -119,3 +132,8 @@ export const getLenderAssets = (chainId: string | number | undefined, lendingPro
|
|
|
119
132
|
if (isSwaylend(lendingProtocol)) return SWAYLEND_RESERVE_ASSETS[lendingProtocol]?.[chainId ?? 0] ?? []
|
|
120
133
|
return []
|
|
121
134
|
}
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
export function isMultiMarket(lender: string) {
|
|
138
|
+
return lender.startsWith("MORPHO_BLUE")
|
|
139
|
+
}
|
package/src/utils/parsing.ts
CHANGED
|
@@ -36,6 +36,11 @@ export const apyToApr = (apy: number) => {
|
|
|
36
36
|
return ((apy + 1) ** (1 / SECONDS_PER_YEAR) - 1) * SECONDS_PER_YEAR;
|
|
37
37
|
};
|
|
38
38
|
|
|
39
|
+
export const apyToAprPercent = (apy: number) => {
|
|
40
|
+
return 100 * ((apy / 100 + 1) ** (1 / SECONDS_PER_YEAR) - 1) * SECONDS_PER_YEAR;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
|
|
39
44
|
export const formatAaveYieldToNumber = (n?: string | bigint): number => {
|
|
40
45
|
if (!n) return 0;
|
|
41
46
|
return Number(formatEther(BigInt(n) / BigInt(1e7)));
|
package/src/yields/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { apyToApr } from "../utils/parsing"
|
|
1
|
+
import { apyToAprPercent, apyToApr } from "../utils/parsing"
|
|
2
2
|
|
|
3
3
|
const FeedData = {
|
|
4
4
|
WSTETH: 'https://eth-api.lido.fi/v1/protocol/steth/apr/sma',
|
|
@@ -12,6 +12,7 @@ const FeedData = {
|
|
|
12
12
|
WEETH: 'https://www.ether.fi/api/apr',
|
|
13
13
|
SAVAX: "https://api.benqi.fi/liquidstaking/apr",
|
|
14
14
|
RSETH: "https://universe.kelpdao.xyz/rseth/totalApy",
|
|
15
|
+
TRUMATIC: "https://api.trufin.io/staker/apy?staker=MATIC"
|
|
15
16
|
}
|
|
16
17
|
|
|
17
18
|
const LenderAPIs = {
|
|
@@ -105,7 +106,7 @@ export const fetchGeneralYields = async () => {
|
|
|
105
106
|
const susdePromise = safeFetch('SUSDE', async () => {
|
|
106
107
|
try {
|
|
107
108
|
const res = await fetch(FeedData.SUSDE).then(r => r.json())
|
|
108
|
-
return
|
|
109
|
+
return apyToAprPercent(res?.data[0]?.apy ?? 0)
|
|
109
110
|
} catch (e) {
|
|
110
111
|
return 0
|
|
111
112
|
}
|
|
@@ -113,7 +114,12 @@ export const fetchGeneralYields = async () => {
|
|
|
113
114
|
|
|
114
115
|
const rsethPromise = safeFetch('RSETH', async () => {
|
|
115
116
|
const res = await fetch(FeedData.RSETH).then(r => r.json())
|
|
116
|
-
return
|
|
117
|
+
return apyToAprPercent(res.totalAPY)
|
|
118
|
+
})
|
|
119
|
+
|
|
120
|
+
const trumaticPromise = safeFetch('TRUMATIC', async () => {
|
|
121
|
+
const res = await fetch(FeedData.TRUMATIC).then(r => r.json())
|
|
122
|
+
return apyToAprPercent(res.apy)
|
|
117
123
|
})
|
|
118
124
|
|
|
119
125
|
const cbethPromise = safeFetch('CBETH', async () => {
|
|
@@ -124,7 +130,7 @@ export const fetchGeneralYields = async () => {
|
|
|
124
130
|
"Content-Type": "application/json"
|
|
125
131
|
}
|
|
126
132
|
}).then(r => r.json())
|
|
127
|
-
return
|
|
133
|
+
return apyToAprPercent(res.apy) * 100
|
|
128
134
|
} catch (e) {
|
|
129
135
|
console.log("CBETH failed", e)
|
|
130
136
|
return 0
|
|
@@ -155,6 +161,7 @@ export const fetchGeneralYields = async () => {
|
|
|
155
161
|
savaxData,
|
|
156
162
|
cbethData,
|
|
157
163
|
rsethData,
|
|
164
|
+
trumaticData
|
|
158
165
|
] = await Promise.all([
|
|
159
166
|
wstethPromise,
|
|
160
167
|
ezethPromise,
|
|
@@ -168,6 +175,7 @@ export const fetchGeneralYields = async () => {
|
|
|
168
175
|
savaxPromise,
|
|
169
176
|
cbethPromise,
|
|
170
177
|
rsethPromise,
|
|
178
|
+
trumaticPromise,
|
|
171
179
|
])
|
|
172
180
|
|
|
173
181
|
const data: YieldDataWithTimestamp = {
|
|
@@ -186,6 +194,7 @@ export const fetchGeneralYields = async () => {
|
|
|
186
194
|
SAVAX: savaxData,
|
|
187
195
|
CBETH: cbethData,
|
|
188
196
|
RSETH: rsethData,
|
|
197
|
+
TRUMATIC: trumaticData,
|
|
189
198
|
},
|
|
190
199
|
lenderRewards: {
|
|
191
200
|
"167000": {
|