@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
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
|
|
2
|
+
export const query = (first: number, skip: number, chainId: string) => `
|
|
3
|
+
query GetMarkets {
|
|
4
|
+
markets(first: ${first}, skip: ${skip}, where: {
|
|
5
|
+
chainId_in: [${chainId}],
|
|
6
|
+
whitelisted: true
|
|
7
|
+
},
|
|
8
|
+
orderBy: SupplyAssetsUsd,
|
|
9
|
+
orderDirection: Desc
|
|
10
|
+
) {
|
|
11
|
+
items {
|
|
12
|
+
uniqueKey
|
|
13
|
+
irmAddress
|
|
14
|
+
oracleAddress
|
|
15
|
+
lltv
|
|
16
|
+
loanAsset {
|
|
17
|
+
address
|
|
18
|
+
name
|
|
19
|
+
symbol
|
|
20
|
+
decimals
|
|
21
|
+
priceUsd
|
|
22
|
+
}
|
|
23
|
+
collateralAsset {
|
|
24
|
+
address
|
|
25
|
+
name
|
|
26
|
+
symbol
|
|
27
|
+
decimals
|
|
28
|
+
priceUsd
|
|
29
|
+
}
|
|
30
|
+
state {
|
|
31
|
+
borrowApy
|
|
32
|
+
supplyApy
|
|
33
|
+
price
|
|
34
|
+
rewards {
|
|
35
|
+
asset {
|
|
36
|
+
address
|
|
37
|
+
name
|
|
38
|
+
symbol
|
|
39
|
+
decimals
|
|
40
|
+
}
|
|
41
|
+
amountPerBorrowedToken
|
|
42
|
+
amountPerSuppliedToken
|
|
43
|
+
}
|
|
44
|
+
borrowAssets
|
|
45
|
+
borrowAssetsUsd
|
|
46
|
+
supplyAssets
|
|
47
|
+
supplyAssetsUsd
|
|
48
|
+
collateralAssets
|
|
49
|
+
collateralAssetsUsd
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
`
|
|
55
|
+
|
|
56
|
+
const BASE_URL = "https://blue-api.morpho.org/graphql"
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
export async function fetchMorphoMarkets(chainId: string) {
|
|
60
|
+
const requestBody = {
|
|
61
|
+
query: query(200, 0, chainId),
|
|
62
|
+
variables: {},
|
|
63
|
+
};
|
|
64
|
+
const response = await fetch(BASE_URL, {
|
|
65
|
+
method: 'POST',
|
|
66
|
+
headers: {
|
|
67
|
+
'Content-Type': 'application/json',
|
|
68
|
+
},
|
|
69
|
+
body: JSON.stringify(requestBody),
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
if (!response.ok) {
|
|
73
|
+
throw new Error(`Network error: ${response.status} - ${response.statusText}`);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const data: any = await response.json();
|
|
77
|
+
|
|
78
|
+
return data.data
|
|
79
|
+
|
|
80
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { AdditionalYields } from "../../types"
|
|
2
|
+
import { convertMarketsToMorphoResponse } from "./convertPublic"
|
|
3
|
+
import { fetchMorphoMarkets } from "./fetchPublic"
|
|
4
|
+
|
|
5
|
+
export async function fetchMorphoPublicData(
|
|
6
|
+
chainId: string,
|
|
7
|
+
yields: AdditionalYields = { intrinsicYields: {}, lenderRewards: {}, loaded: true }
|
|
8
|
+
) {
|
|
9
|
+
const mbData = await fetchMorphoMarkets(chainId)
|
|
10
|
+
return convertMarketsToMorphoResponse(mbData, chainId, yields)
|
|
11
|
+
}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { GenericCurrency } from "../types";
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
export type GetMarketsResponse = {
|
|
5
|
+
markets: {
|
|
6
|
+
items: {
|
|
7
|
+
uniqueKey: string;
|
|
8
|
+
irmAddress: string;
|
|
9
|
+
oracleAddress: string;
|
|
10
|
+
lltv: string;
|
|
11
|
+
loanAsset: {
|
|
12
|
+
address: string;
|
|
13
|
+
name: string;
|
|
14
|
+
symbol: string;
|
|
15
|
+
decimals: number;
|
|
16
|
+
priceUsd: string;
|
|
17
|
+
};
|
|
18
|
+
collateralAsset: {
|
|
19
|
+
address: string;
|
|
20
|
+
name: string;
|
|
21
|
+
symbol: string;
|
|
22
|
+
decimals: number;
|
|
23
|
+
priceUsd: string;
|
|
24
|
+
};
|
|
25
|
+
state: {
|
|
26
|
+
borrowApy: string;
|
|
27
|
+
supplyApy: string;
|
|
28
|
+
price: string;
|
|
29
|
+
rewards: {
|
|
30
|
+
asset: {
|
|
31
|
+
address: string
|
|
32
|
+
name: string
|
|
33
|
+
symbol: string
|
|
34
|
+
decimals: number
|
|
35
|
+
}
|
|
36
|
+
amountPerBorrowedToken: string;
|
|
37
|
+
amountPerSuppliedToken: string;
|
|
38
|
+
}[];
|
|
39
|
+
borrowAssets: string;
|
|
40
|
+
borrowAssetsUsd: string;
|
|
41
|
+
supplyAssets: string
|
|
42
|
+
supplyAssetsUsd: string
|
|
43
|
+
collateralAssets: string;
|
|
44
|
+
collateralAssetsUsd: string;
|
|
45
|
+
};
|
|
46
|
+
}[];
|
|
47
|
+
};
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
export interface BaseYields {
|
|
51
|
+
// borrow interest rates
|
|
52
|
+
variableBorrowRate: number
|
|
53
|
+
stableBorrowRate: number
|
|
54
|
+
// deposit interest rate
|
|
55
|
+
depositRate: number
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export type RewardsMap = { [rewardId: string]: BaseYields }
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
export interface MorphoMarket {
|
|
63
|
+
/** the 1delta lender enum */
|
|
64
|
+
lender: string
|
|
65
|
+
collateralDecimals?: number
|
|
66
|
+
loanDecimals?: number
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
/** the market hash */
|
|
70
|
+
id: string,
|
|
71
|
+
/** market params */
|
|
72
|
+
lltv: string;
|
|
73
|
+
oracle: string;
|
|
74
|
+
irm: string;
|
|
75
|
+
collateralAddress: string;
|
|
76
|
+
loanAddress: string;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
export interface MorphoGeneralPublicResponse {
|
|
81
|
+
data: {
|
|
82
|
+
[tokenSymbol: string]: {
|
|
83
|
+
poolId: string
|
|
84
|
+
underlying: string
|
|
85
|
+
asset: GenericCurrency
|
|
86
|
+
// token amounts
|
|
87
|
+
totalDeposits: number;
|
|
88
|
+
totalDebtStable: 0;
|
|
89
|
+
totalDebt: number;
|
|
90
|
+
// USD amounts
|
|
91
|
+
totalDepositsUSD: number;
|
|
92
|
+
totalDebtStableUSD: 0;
|
|
93
|
+
totalDebtUSD: number;
|
|
94
|
+
// rates
|
|
95
|
+
depositRate: number;
|
|
96
|
+
variableBorrowRate: number;
|
|
97
|
+
stakingYield: number;
|
|
98
|
+
stableBorrowRate: 0;
|
|
99
|
+
|
|
100
|
+
// rewards
|
|
101
|
+
rewards?: RewardsMap
|
|
102
|
+
|
|
103
|
+
// config part
|
|
104
|
+
decimals: number;
|
|
105
|
+
|
|
106
|
+
config: {
|
|
107
|
+
[0]: {
|
|
108
|
+
category: number
|
|
109
|
+
// collateral factors
|
|
110
|
+
borrowCollateralFactor: number
|
|
111
|
+
collateralFactor: number
|
|
112
|
+
borrowFactor: 1
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// flags
|
|
117
|
+
collateralActive: boolean;
|
|
118
|
+
borrowingEnabled: boolean;
|
|
119
|
+
hasStable: false;
|
|
120
|
+
isActive: true;
|
|
121
|
+
isFrozen: false;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
/** morpho market info */
|
|
125
|
+
params: {
|
|
126
|
+
market: MorphoMarket;
|
|
127
|
+
}
|
|
128
|
+
chainId: string
|
|
129
|
+
}
|
|
@@ -1,18 +1,14 @@
|
|
|
1
1
|
import { Lender } from "@1delta/asset-registry"
|
|
2
2
|
import { Call } from "../../../utils/multicall"
|
|
3
|
-
import { getAaveAssets,
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
const fallbackDebtToken = '0x52A1CeB68Ee6b7B5D13E0376A1E0E4423A8cE26e'
|
|
3
|
+
import { getAaveAssets, getAaveTypePoolDataProviderAddress, getAaveTypePoolAddress } from "../../../assets"
|
|
4
|
+
import { isYLDR } from "../../../utils"
|
|
7
5
|
|
|
8
6
|
const getBaseAaveV3Calls = (
|
|
9
7
|
chainId: string,
|
|
10
8
|
account: string,
|
|
11
9
|
lender: Lender,
|
|
12
10
|
assetsToQuery: string[],
|
|
13
|
-
aaveTokenMap: any
|
|
14
11
|
) => {
|
|
15
|
-
const broker = getComposerAddress(chainId)
|
|
16
12
|
return assetsToQuery.flatMap((tk) => [
|
|
17
13
|
{
|
|
18
14
|
address: getAaveTypePoolDataProviderAddress(chainId, lender),
|
|
@@ -24,7 +20,6 @@ const getBaseAaveV3Calls = (
|
|
|
24
20
|
|
|
25
21
|
export const buildAaveV3UserCall = (chainId: string, lender: Lender, account: string): Call[] => {
|
|
26
22
|
const assetsToQuery = getAaveAssets(chainId, lender)
|
|
27
|
-
const aaveTokenMap = getAaveStyleProtocolTokenMap(chainId, lender)
|
|
28
23
|
|
|
29
24
|
const calls = [
|
|
30
25
|
...getBaseAaveV3Calls(
|
|
@@ -32,7 +27,6 @@ export const buildAaveV3UserCall = (chainId: string, lender: Lender, account: st
|
|
|
32
27
|
account,
|
|
33
28
|
lender,
|
|
34
29
|
assetsToQuery,
|
|
35
|
-
aaveTokenMap
|
|
36
30
|
),
|
|
37
31
|
]
|
|
38
32
|
if (!isYLDR(lender)) {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Lender } from "@1delta/asset-registry"
|
|
2
|
-
import { isAaveV2Type, isAaveV3Type, isCompoundV3, isInit, isAaveV32Type, isYLDR } from "../../utils"
|
|
2
|
+
import { isAaveV2Type, isAaveV3Type, isCompoundV3, isInit, isAaveV32Type, isYLDR, isMorphoType } from "../../utils"
|
|
3
3
|
import { IncentivesControllerAbi } from "../../abis/lendle/IncentivesController"
|
|
4
4
|
import { RewarderAbi } from "../../abis/aurelius/Rewarder"
|
|
5
5
|
import { AavePoolV3UpgradedAbi } from "../../abis/aave/AavePoolV3Upgraded"
|
|
@@ -15,6 +15,8 @@ import { MultiFeeDistributionAbi } from "../../abis/lendle/MultiFeeDistribution"
|
|
|
15
15
|
import { PullRewardsIncentivesController } from "../../abis/meridian/PullRewardsIncentivesController"
|
|
16
16
|
import { CometRewardsAbi } from "../../abis/compound-v3/CometRewards"
|
|
17
17
|
import { YLDRAbi } from "../../abis/aave-v3/YLDR"
|
|
18
|
+
import { MorphoBlueAbi } from "../../abis/morpho/blue"
|
|
19
|
+
import { MorphoLensAbi } from "../../abis/morpho/lens"
|
|
18
20
|
|
|
19
21
|
export const getAbi = (lender: Lender) => {
|
|
20
22
|
if (isAaveV2Type(lender as any)) return [
|
|
@@ -45,6 +47,7 @@ export const getAbi = (lender: Lender) => {
|
|
|
45
47
|
...CometExtAbi,
|
|
46
48
|
...CometRewardsAbi,
|
|
47
49
|
]
|
|
50
|
+
if(isMorphoType(lender)) return [...MorphoBlueAbi, ...MorphoLensAbi]
|
|
48
51
|
if (isInit(lender as any)) return InitLensAbi
|
|
49
52
|
return []
|
|
50
53
|
}
|
|
@@ -52,7 +52,8 @@ export const getCompoundV3UserDataConverter = (
|
|
|
52
52
|
deposits,
|
|
53
53
|
depositsUSD: Number(deposits) * price,
|
|
54
54
|
debtStableUSD: 0,
|
|
55
|
-
debt: 0,
|
|
55
|
+
debt: "0",
|
|
56
|
+
debtStable: "0",
|
|
56
57
|
debtUSD: 0,
|
|
57
58
|
collateralActive: true,
|
|
58
59
|
// user depos raw
|
|
@@ -85,7 +86,7 @@ export const getCompoundV3UserDataConverter = (
|
|
|
85
86
|
depositsUSD: Number(deposits) * priceBase,
|
|
86
87
|
debtUSD: Number(debt) * priceBase,
|
|
87
88
|
// populate values for totals
|
|
88
|
-
debtStable: 0,
|
|
89
|
+
debtStable: "0",
|
|
89
90
|
debtStableUSD: 0,
|
|
90
91
|
collateralActive: true,
|
|
91
92
|
// user depos raw
|
|
@@ -1,27 +1,39 @@
|
|
|
1
|
-
import { Call, multicallViemAbiArray } from
|
|
2
|
-
import { Lender } from
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
13
|
-
import {
|
|
14
|
-
import {
|
|
15
|
-
import {
|
|
1
|
+
import { Call, multicallViemAbiArray } from '../../utils/multicall'
|
|
2
|
+
import { Lender } from '@1delta/asset-registry'
|
|
3
|
+
import {
|
|
4
|
+
isAaveV2Type,
|
|
5
|
+
isAaveV3Type,
|
|
6
|
+
isInit,
|
|
7
|
+
isMorphoType,
|
|
8
|
+
isMultiMarket,
|
|
9
|
+
isYLDR,
|
|
10
|
+
} from '../../utils'
|
|
11
|
+
import { LenderUserQuery, organizeUserQueries } from './types'
|
|
12
|
+
import { GetEvmClientFunction } from '../../types'
|
|
13
|
+
import { buildAaveV2UserCall } from './aave-v2-type/userCallBuild'
|
|
14
|
+
import { buildAaveV3UserCall } from './aave-v3-type/userCallBuild'
|
|
15
|
+
import { buildInitUserCall } from './init/userCallBuild'
|
|
16
|
+
import { buildCompoundV3UserCall } from './compound-v3/userCallBuild'
|
|
17
|
+
import { getAbi } from './abis'
|
|
18
|
+
import { getAaveV2UserDataConverter } from './aave-v2-type/userCallParse'
|
|
19
|
+
import { getAaveV3UserDataConverter } from './aave-v3-type/userCallParse'
|
|
20
|
+
import { getInitUserDataConverter } from './init/userCallParse'
|
|
21
|
+
import { getCompoundV3UserDataConverter } from './compound-v3/userCallParse'
|
|
22
|
+
import { getYldrUserDataConverter } from './aave-v3-type/userCallParseYldr'
|
|
23
|
+
import { buildMorphoUserCallWithLens } from './morpho/userCallBuild'
|
|
24
|
+
import { getMorphoUserDataConverterWithlens } from './morpho/userCallParse'
|
|
16
25
|
|
|
17
26
|
function buildUserCall(
|
|
18
27
|
chainId: string,
|
|
19
28
|
lender: Lender,
|
|
20
|
-
account: string
|
|
29
|
+
account: string,
|
|
30
|
+
params?: any,
|
|
21
31
|
) {
|
|
22
32
|
if (isAaveV2Type(lender)) return buildAaveV2UserCall(chainId, lender, account)
|
|
23
33
|
if (isAaveV3Type(lender)) return buildAaveV3UserCall(chainId, lender, account)
|
|
24
34
|
if (isInit(lender)) return buildInitUserCall(chainId, lender, account)
|
|
35
|
+
if (isMorphoType(lender))
|
|
36
|
+
return buildMorphoUserCallWithLens(chainId, account, lender, params)
|
|
25
37
|
return buildCompoundV3UserCall(chainId, lender, account)
|
|
26
38
|
}
|
|
27
39
|
|
|
@@ -31,13 +43,63 @@ function getUserDataConverter(
|
|
|
31
43
|
account: string,
|
|
32
44
|
prices: { [a: string]: number },
|
|
33
45
|
pricesHist: { [a: string]: number },
|
|
34
|
-
lenderPublicState: any
|
|
46
|
+
lenderPublicState: any,
|
|
47
|
+
params?: any,
|
|
35
48
|
) {
|
|
36
|
-
if (isYLDR(lender))
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
49
|
+
if (isYLDR(lender))
|
|
50
|
+
return getYldrUserDataConverter(
|
|
51
|
+
lender,
|
|
52
|
+
chainId,
|
|
53
|
+
account,
|
|
54
|
+
prices,
|
|
55
|
+
pricesHist,
|
|
56
|
+
lenderPublicState?.data?.[lender]?.data,
|
|
57
|
+
)
|
|
58
|
+
if (isAaveV2Type(lender))
|
|
59
|
+
return getAaveV2UserDataConverter(
|
|
60
|
+
lender,
|
|
61
|
+
chainId,
|
|
62
|
+
account,
|
|
63
|
+
prices,
|
|
64
|
+
pricesHist,
|
|
65
|
+
lenderPublicState?.data?.[lender]?.data,
|
|
66
|
+
)
|
|
67
|
+
if (isAaveV3Type(lender))
|
|
68
|
+
return getAaveV3UserDataConverter(
|
|
69
|
+
lender,
|
|
70
|
+
chainId,
|
|
71
|
+
account,
|
|
72
|
+
prices,
|
|
73
|
+
pricesHist,
|
|
74
|
+
lenderPublicState?.data?.[lender]?.data,
|
|
75
|
+
)
|
|
76
|
+
if (isInit(lender))
|
|
77
|
+
return getInitUserDataConverter(
|
|
78
|
+
lender,
|
|
79
|
+
chainId,
|
|
80
|
+
account,
|
|
81
|
+
prices,
|
|
82
|
+
pricesHist,
|
|
83
|
+
lenderPublicState?.data?.[lender]?.data,
|
|
84
|
+
)
|
|
85
|
+
if (isMorphoType(lender))
|
|
86
|
+
return getMorphoUserDataConverterWithlens(
|
|
87
|
+
lender,
|
|
88
|
+
chainId,
|
|
89
|
+
account,
|
|
90
|
+
params,
|
|
91
|
+
prices,
|
|
92
|
+
pricesHist,
|
|
93
|
+
lenderPublicState?.data,
|
|
94
|
+
)
|
|
95
|
+
return getCompoundV3UserDataConverter(
|
|
96
|
+
lender,
|
|
97
|
+
chainId,
|
|
98
|
+
account,
|
|
99
|
+
prices,
|
|
100
|
+
pricesHist,
|
|
101
|
+
lenderPublicState,
|
|
102
|
+
)
|
|
41
103
|
}
|
|
42
104
|
|
|
43
105
|
/**
|
|
@@ -49,17 +111,23 @@ function getUserDataConverter(
|
|
|
49
111
|
*/
|
|
50
112
|
export const getLenderUserDataResult = async (
|
|
51
113
|
chainId: string,
|
|
52
|
-
|
|
53
|
-
getEvmClient: GetEvmClientFunction
|
|
114
|
+
queriesRaw: LenderUserQuery[],
|
|
115
|
+
getEvmClient: GetEvmClientFunction,
|
|
54
116
|
): Promise<any[]> => {
|
|
55
117
|
let calls: {
|
|
56
118
|
call: Call
|
|
57
119
|
abi: any
|
|
58
120
|
}[] = []
|
|
59
121
|
|
|
122
|
+
const queries = organizeUserQueries(queriesRaw)
|
|
60
123
|
for (const query of queries) {
|
|
61
124
|
const abi = getAbi(query.lender)
|
|
62
|
-
const callData = buildUserCall(
|
|
125
|
+
const callData = buildUserCall(
|
|
126
|
+
chainId,
|
|
127
|
+
query.lender,
|
|
128
|
+
query.account,
|
|
129
|
+
query.params,
|
|
130
|
+
)
|
|
63
131
|
const mappedCalls = callData.map((call) => ({ call, abi }))
|
|
64
132
|
calls = [...calls, ...mappedCalls]
|
|
65
133
|
}
|
|
@@ -71,7 +139,7 @@ export const getLenderUserDataResult = async (
|
|
|
71
139
|
getEvmClient,
|
|
72
140
|
true,
|
|
73
141
|
0,
|
|
74
|
-
3
|
|
142
|
+
3,
|
|
75
143
|
)
|
|
76
144
|
}
|
|
77
145
|
|
|
@@ -87,12 +155,13 @@ export const getLenderUserDataResult = async (
|
|
|
87
155
|
*/
|
|
88
156
|
export const convertLenderUserDataResult = (
|
|
89
157
|
chainId: string,
|
|
90
|
-
|
|
158
|
+
queriesRaw: LenderUserQuery[],
|
|
91
159
|
rawResults: any[],
|
|
92
160
|
prices: any,
|
|
93
161
|
pricesHist: any,
|
|
94
|
-
lenderState: any
|
|
162
|
+
lenderState: any,
|
|
95
163
|
): { [lender: string]: any } => {
|
|
164
|
+
const queries = organizeUserQueries(queriesRaw)
|
|
96
165
|
|
|
97
166
|
const lenderData: { [lender: string]: any } = {}
|
|
98
167
|
let currentSlice = 0
|
|
@@ -103,7 +172,8 @@ export const convertLenderUserDataResult = (
|
|
|
103
172
|
query.account,
|
|
104
173
|
prices,
|
|
105
174
|
pricesHist,
|
|
106
|
-
lenderState[chainId]
|
|
175
|
+
lenderState[chainId],
|
|
176
|
+
query.params,
|
|
107
177
|
)
|
|
108
178
|
|
|
109
179
|
try {
|
|
@@ -111,14 +181,25 @@ export const convertLenderUserDataResult = (
|
|
|
111
181
|
const convertedData = converter(data)
|
|
112
182
|
|
|
113
183
|
if (convertedData) {
|
|
114
|
-
|
|
115
|
-
|
|
184
|
+
if (isMultiMarket(query.lender)) {
|
|
185
|
+
Object.keys(convertedData).forEach((market) => {
|
|
186
|
+
lenderData[market] = {
|
|
187
|
+
// @ts-ignore
|
|
188
|
+
[query.account.toLowerCase()]: convertedData[market],
|
|
189
|
+
}
|
|
190
|
+
})
|
|
191
|
+
} else {
|
|
192
|
+
// map it to the user address
|
|
193
|
+
lenderData[query.lender] = {
|
|
194
|
+
[query.account.toLowerCase()]: convertedData,
|
|
195
|
+
}
|
|
196
|
+
}
|
|
116
197
|
}
|
|
117
198
|
} catch (e) {
|
|
118
|
-
console.log(
|
|
199
|
+
console.log('Error parsing for', query.lender, 'on', chainId, e)
|
|
119
200
|
}
|
|
120
201
|
currentSlice += sliceLength
|
|
121
202
|
}
|
|
122
203
|
|
|
123
204
|
return lenderData
|
|
124
|
-
}
|
|
205
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
export interface BalanceInfo {
|
|
2
|
+
index: number; // uint16 (2 bytes)
|
|
3
|
+
supplyShares: bigint; // uint256 (32 bytes)
|
|
4
|
+
borrowShares: bigint; // uint128 (16 bytes)
|
|
5
|
+
supplyAssets: bigint; // uint256 (32 bytes)
|
|
6
|
+
borrowAssets: bigint; // uint256 (32 bytes)
|
|
7
|
+
collateral: bigint; // uint128 (16 bytes)
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const WORD = 32;
|
|
11
|
+
const HALF = 16;
|
|
12
|
+
|
|
13
|
+
/** Read `len` bytes at `off` as a big‐endian BigInt. */
|
|
14
|
+
function readBigIntBE(buf: Uint8Array, off: number, len: number): bigint {
|
|
15
|
+
let v = 0n;
|
|
16
|
+
for (let i = 0; i < len; i++) {
|
|
17
|
+
v = (v << 8n) | BigInt(buf[off + i]);
|
|
18
|
+
}
|
|
19
|
+
return v;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Decode:
|
|
24
|
+
* [ uint256 count (32 bytes) ]
|
|
25
|
+
* then for each of `count` records:
|
|
26
|
+
* uint16 index (2 bytes),
|
|
27
|
+
* uint256 supplyShares (32 bytes),
|
|
28
|
+
* uint128 borrowShares (16 bytes),
|
|
29
|
+
* uint256 supplyAssets (32 bytes),
|
|
30
|
+
* uint256 borrowAssets (32 bytes),
|
|
31
|
+
* uint128 collateral (16 bytes)
|
|
32
|
+
*/
|
|
33
|
+
export function decodePackedDataset(hex: string): BalanceInfo[] {
|
|
34
|
+
if (hex.startsWith("0x")) hex = hex.slice(2);
|
|
35
|
+
const buf = Uint8Array.from(hex.match(/.{1,2}/g)!.map((b) => parseInt(b, 16)));
|
|
36
|
+
|
|
37
|
+
// 1) count (uint256)
|
|
38
|
+
const count = Number(readBigIntBE(buf, 0, 2));
|
|
39
|
+
const out: BalanceInfo[] = [];
|
|
40
|
+
|
|
41
|
+
let off = 2;
|
|
42
|
+
for (let i = 0; i < count; i++) {
|
|
43
|
+
// 2) index (uint16)
|
|
44
|
+
const index = Number(readBigIntBE(buf, off, 2));
|
|
45
|
+
off += 2;
|
|
46
|
+
|
|
47
|
+
// 3) supplyShares (uint256)
|
|
48
|
+
const supplyShares = readBigIntBE(buf, off, WORD);
|
|
49
|
+
off += WORD;
|
|
50
|
+
|
|
51
|
+
// 4) borrowShares (uint128)
|
|
52
|
+
const borrowShares = readBigIntBE(buf, off, HALF);
|
|
53
|
+
off += HALF;
|
|
54
|
+
|
|
55
|
+
// 5) supplyAssets (uint256)
|
|
56
|
+
const supplyAssets = readBigIntBE(buf, off, WORD);
|
|
57
|
+
off += WORD;
|
|
58
|
+
|
|
59
|
+
// 6) borrowAssets (uint256)
|
|
60
|
+
const borrowAssets = readBigIntBE(buf, off, WORD);
|
|
61
|
+
off += WORD;
|
|
62
|
+
|
|
63
|
+
// 7) collateral (uint128)
|
|
64
|
+
const collateral = readBigIntBE(buf, off, HALF);
|
|
65
|
+
off += HALF;
|
|
66
|
+
|
|
67
|
+
out.push({ index, supplyShares, borrowShares, supplyAssets, borrowAssets, collateral });
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return out;
|
|
71
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
// Constants
|
|
2
|
+
const VIRTUAL_SHARES = 10n ** 6n;
|
|
3
|
+
const VIRTUAL_ASSETS = 1n;
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
function mulDivDown(x: bigint, y: bigint, d: bigint): bigint {
|
|
7
|
+
return (x * y) / d;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function mulDivUp(x: bigint, y: bigint, d: bigint): bigint {
|
|
11
|
+
return (x * y + (d - 1n)) / d;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
export function toSharesDown(
|
|
16
|
+
assets: bigint,
|
|
17
|
+
totalAssets: bigint,
|
|
18
|
+
totalShares: bigint
|
|
19
|
+
): bigint {
|
|
20
|
+
return mulDivDown(
|
|
21
|
+
assets,
|
|
22
|
+
totalShares + VIRTUAL_SHARES,
|
|
23
|
+
totalAssets + VIRTUAL_ASSETS
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function toAssetsDown(
|
|
28
|
+
shares: bigint,
|
|
29
|
+
totalAssets: bigint,
|
|
30
|
+
totalShares: bigint
|
|
31
|
+
): bigint {
|
|
32
|
+
return mulDivDown(
|
|
33
|
+
shares,
|
|
34
|
+
totalAssets + VIRTUAL_ASSETS,
|
|
35
|
+
totalShares + VIRTUAL_SHARES
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function toSharesUp(
|
|
40
|
+
assets: bigint,
|
|
41
|
+
totalAssets: bigint,
|
|
42
|
+
totalShares: bigint
|
|
43
|
+
): bigint {
|
|
44
|
+
return mulDivUp(
|
|
45
|
+
assets,
|
|
46
|
+
totalShares + VIRTUAL_SHARES,
|
|
47
|
+
totalAssets + VIRTUAL_ASSETS
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function toAssetsUp(
|
|
52
|
+
shares: bigint,
|
|
53
|
+
totalAssets: bigint,
|
|
54
|
+
totalShares: bigint
|
|
55
|
+
): bigint {
|
|
56
|
+
return mulDivUp(
|
|
57
|
+
shares,
|
|
58
|
+
totalAssets + VIRTUAL_ASSETS,
|
|
59
|
+
totalShares + VIRTUAL_SHARES
|
|
60
|
+
);
|
|
61
|
+
}
|