Skip to content

Commit 0753a99

Browse files
authored
[SCR-70] Normalize values across rhomarkets and layerbank (#14)
Normalizing values across adapters
1 parent ec77d93 commit 0753a99

File tree

5 files changed

+59
-25
lines changed

5 files changed

+59
-25
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ Please note the following:
5656
- A standard function / entry point that inputs a block number and fetches all user level data at that block. (see below)
5757
- Accepts the standard input defined in `hourly_blocks.csv` (see more details below)
5858
- All values are in the underlying token amount (no lp or output tokens such as, cTokens, aTokens, uni pools)
59-
- Token amounts are normalized
59+
- Token amounts are normalized to the underlying token decimals (not raw values)
6060
- All strings/addresses are lowercase with no spaces
6161

6262
> Note: **Expect multiple entries per user if the protocol has more than one token asset**
@@ -141,8 +141,8 @@ type OutputDataSchemaRow = {
141141
token_symbol: string;
142142

143143
// Financial data
144-
supply_token: bigint;
145-
borrow_token: bigint;
144+
supply_token: number;
145+
borrow_token: number;
146146

147147
// Metadata
148148
block_number: number;

adapters/layerbank/src/index.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ type OutputDataSchemaRow = {
2222
market: string;
2323
token_address: string;
2424
token_symbol: string;
25-
supply_token: bigint;
26-
borrow_token: bigint;
25+
supply_token: number;
26+
borrow_token: number;
2727
block_number: number;
2828
timestamp: number;
2929
protocol: string;
@@ -66,10 +66,10 @@ export const getUserTVLByBlock = async (blocks: BlockData) => {
6666
timestamp: blocks.blockTimestamp,
6767
block_number: blocks.blockNumber,
6868
etl_timestamp: Math.floor(Date.now() / 1000),
69-
token_address: marketInfo.underlyingAddress,
69+
token_address: marketInfo.underlyingAddress.toLowerCase(),
7070
token_symbol: marketInfo.underlyingSymbol,
71-
user_address: state.account,
72-
market: marketInfo.address,
71+
user_address: state.account.toLowerCase(),
72+
market: marketInfo.address.toLowerCase(),
7373
supply_token: state.lentAmount,
7474
borrow_token: state.borrowAmount,
7575
});

adapters/layerbank/src/sdk/marketDetails.ts

+34-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createPublicClient, extractChain, http, getContract } from "viem";
1+
import { createPublicClient, extractChain, http, getContract, formatUnits } from "viem";
22
import { CHAINS, RPC_URLS, WETH_ADDRESS } from "./config";
33
import { scroll } from "viem/chains";
44
import coreAbi from "./abi/core.abi";
@@ -7,8 +7,10 @@ import { AccountState } from "./subgraphDetails";
77

88
export interface MarketInfo {
99
address: string;
10+
decimals: number;
1011
underlyingAddress: string;
1112
underlyingSymbol: string;
13+
underlyingDecimals: number;
1214
exchangeRateStored: bigint;
1315
}
1416

@@ -71,6 +73,14 @@ export const getMarketInfos = async (
7173
})) as any,
7274
});
7375

76+
const underlyingDecimalResults = await publicClient.multicall({
77+
contracts: underlyings.map((m) => ({
78+
address: (m as any).address,
79+
abi: (m as any).abi,
80+
functionName: "decimals",
81+
})) as any,
82+
});
83+
7484
const exchangeRateResults = await publicClient.multicall({
7585
contracts: markets.map((m) => ({
7686
address: m.address,
@@ -80,6 +90,14 @@ export const getMarketInfos = async (
8090
blockNumber,
8191
});
8292

93+
const decimalResults = await publicClient.multicall({
94+
contracts: markets.map((m) => ({
95+
address: m.address,
96+
abi: m.abi,
97+
functionName: "decimals",
98+
})) as any,
99+
});
100+
83101
const marketInfos: MarketInfo[] = [];
84102

85103
for (let i = 0; i < markets.length; i++) {
@@ -88,8 +106,10 @@ export const getMarketInfos = async (
88106

89107
marketInfos.push({
90108
address: marketAddress,
109+
decimals: (decimalResults[i].result as number) || 0,
91110
underlyingAddress,
92111
underlyingSymbol: underlyingSymbolResults[i].result as any,
112+
underlyingDecimals: underlyingDecimalResults[i].result as any,
93113
exchangeRateStored: BigInt(
94114
exchangeRateResults[i].status === "success"
95115
? (exchangeRateResults[i].result as any)
@@ -110,7 +130,13 @@ export const updateBorrowBalances = async (
110130
);
111131
const marketsByUnderlying: any = {};
112132
for (let marketInfo of marketInfos) {
113-
marketsByUnderlying[marketInfo.underlyingAddress] = marketInfo.address;
133+
marketsByUnderlying[marketInfo.underlyingAddress] = {
134+
address: marketInfo.address,
135+
exchangeRate: marketInfo.exchangeRateStored,
136+
decimals: marketInfo.decimals,
137+
tokenAddress: marketInfo.underlyingAddress,
138+
tokenSymbol: marketInfo.underlyingSymbol,
139+
};
114140
}
115141

116142
const publicClient = createPublicClient({
@@ -131,7 +157,7 @@ export const updateBorrowBalances = async (
131157
contracts: subStates
132158
.map((m) => [
133159
{
134-
address: marketsByUnderlying[m.token],
160+
address: marketsByUnderlying[m.token].address,
135161
abi: ltokenAbi,
136162
functionName: "borrowBalanceOf",
137163
args: [m.account],
@@ -142,8 +168,11 @@ export const updateBorrowBalances = async (
142168
});
143169

144170
for (var j = 0; j < subStates.length; j++) {
145-
subStates[j].borrowAmount = BigInt(
146-
borrowBalanceResults[j].result?.toString() ?? 0
171+
subStates[j].borrowAmount = Number(
172+
formatUnits(
173+
(borrowBalanceResults[j]?.result as bigint) || 0n,
174+
marketsByUnderlying[subStates[j].token].underlyingDecimals
175+
)
147176
);
148177
}
149178
}

adapters/layerbank/src/sdk/subgraphDetails.ts

+12-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Account, createPublicClient, extractChain, http } from "viem";
1+
import { formatUnits, createPublicClient, extractChain, http } from "viem";
22
import { scroll } from "viem/chains";
33
import {
44
CHAINS,
@@ -17,8 +17,8 @@ export interface AccountState {
1717
id: string;
1818
account: string;
1919
token: string;
20-
lentAmount: bigint;
21-
borrowAmount: bigint;
20+
lentAmount: number;
21+
borrowAmount: number;
2222
}
2323

2424
export const getAccountStatesForAddressByPoolAtBlock = async (
@@ -94,8 +94,8 @@ export const getAccountStatesForAddressByPoolAtBlock = async (
9494
? WETH_ADDRESS[CHAINS.SCROLL].toLowerCase()
9595
: m.token
9696
].toLowerCase(),
97-
lentAmount: BigInt(m.supplied),
98-
borrowAmount: BigInt(m.borrowed),
97+
lentAmount: m.supplied,
98+
borrowAmount: m.borrowed,
9999
}));
100100

101101
// Push the filtered and mapped states into the states array
@@ -120,8 +120,13 @@ export const getAccountStatesForAddressByPoolAtBlock = async (
120120

121121
return {
122122
...state,
123-
lentAmount:
124-
(state.lentAmount * marketInfo.exchangeRateStored) / BigInt(1e18),
123+
lentAmount: Number(
124+
formatUnits(
125+
((BigInt(state.lentAmount) || 0n) * marketInfo.exchangeRateStored) /
126+
10n ** 18n,
127+
marketInfo.underlyingDecimals
128+
)
129+
)
125130
};
126131
})
127132
.filter((x) => x !== undefined) as AccountState[];

adapters/rhomarkets/src/sdk/index.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ type OutputDataSchemaRow = {
2222
market: string;
2323
token_address: string;
2424
token_symbol: string;
25-
supply_token: bigint;
26-
borrow_token: bigint;
25+
supply_token: number;
26+
borrow_token: number;
2727
block_number: number;
2828
timestamp: number;
2929
protocol: string;
@@ -132,10 +132,10 @@ export const getUserTVLByBlock = async (blocks: BlockData) => {
132132
timestamp: blocks.blockTimestamp,
133133
block_number: blocks.blockNumber,
134134
etl_timestamp: Math.floor(Date.now() / 1000),
135-
token_address: marketInfo.tokenAddress,
135+
token_address: marketInfo.tokenAddress.toLowerCase(),
136136
token_symbol: marketInfo.tokenSymbol,
137-
user_address: item.user_address,
138-
market: item.market,
137+
user_address: item.user_address.toLowerCase(),
138+
market: item.market.toLowerCase(),
139139
supply_token: item.supply_token,
140140
borrow_token: item.borrow_token,
141141
};

0 commit comments

Comments
 (0)