Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SCR-70] Normalize values across rhomarkets and layerbank #14

Merged
merged 1 commit into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Please note the following:
- A standard function / entry point that inputs a block number and fetches all user level data at that block. (see below)
- Accepts the standard input defined in `hourly_blocks.csv` (see more details below)
- All values are in the underlying token amount (no lp or output tokens such as, cTokens, aTokens, uni pools)
- Token amounts are normalized
- Token amounts are normalized to the underlying token decimals (not raw values)
- All strings/addresses are lowercase with no spaces

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

// Financial data
supply_token: bigint;
borrow_token: bigint;
supply_token: number;
borrow_token: number;

// Metadata
block_number: number;
Expand Down
10 changes: 5 additions & 5 deletions adapters/layerbank/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ type OutputDataSchemaRow = {
market: string;
token_address: string;
token_symbol: string;
supply_token: bigint;
borrow_token: bigint;
supply_token: number;
borrow_token: number;
block_number: number;
timestamp: number;
protocol: string;
Expand Down Expand Up @@ -66,10 +66,10 @@ export const getUserTVLByBlock = async (blocks: BlockData) => {
timestamp: blocks.blockTimestamp,
block_number: blocks.blockNumber,
etl_timestamp: Math.floor(Date.now() / 1000),
token_address: marketInfo.underlyingAddress,
token_address: marketInfo.underlyingAddress.toLowerCase(),
token_symbol: marketInfo.underlyingSymbol,
user_address: state.account,
market: marketInfo.address,
user_address: state.account.toLowerCase(),
market: marketInfo.address.toLowerCase(),
supply_token: state.lentAmount,
borrow_token: state.borrowAmount,
});
Expand Down
39 changes: 34 additions & 5 deletions adapters/layerbank/src/sdk/marketDetails.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createPublicClient, extractChain, http, getContract } from "viem";
import { createPublicClient, extractChain, http, getContract, formatUnits } from "viem";
import { CHAINS, RPC_URLS, WETH_ADDRESS } from "./config";
import { scroll } from "viem/chains";
import coreAbi from "./abi/core.abi";
Expand All @@ -7,8 +7,10 @@ import { AccountState } from "./subgraphDetails";

export interface MarketInfo {
address: string;
decimals: number;
underlyingAddress: string;
underlyingSymbol: string;
underlyingDecimals: number;
exchangeRateStored: bigint;
}

Expand Down Expand Up @@ -71,6 +73,14 @@ export const getMarketInfos = async (
})) as any,
});

const underlyingDecimalResults = await publicClient.multicall({
contracts: underlyings.map((m) => ({
address: (m as any).address,
abi: (m as any).abi,
functionName: "decimals",
})) as any,
});

const exchangeRateResults = await publicClient.multicall({
contracts: markets.map((m) => ({
address: m.address,
Expand All @@ -80,6 +90,14 @@ export const getMarketInfos = async (
blockNumber,
});

const decimalResults = await publicClient.multicall({
contracts: markets.map((m) => ({
address: m.address,
abi: m.abi,
functionName: "decimals",
})) as any,
});

const marketInfos: MarketInfo[] = [];

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

marketInfos.push({
address: marketAddress,
decimals: (decimalResults[i].result as number) || 0,
underlyingAddress,
underlyingSymbol: underlyingSymbolResults[i].result as any,
underlyingDecimals: underlyingDecimalResults[i].result as any,
exchangeRateStored: BigInt(
exchangeRateResults[i].status === "success"
? (exchangeRateResults[i].result as any)
Expand All @@ -110,7 +130,13 @@ export const updateBorrowBalances = async (
);
const marketsByUnderlying: any = {};
for (let marketInfo of marketInfos) {
marketsByUnderlying[marketInfo.underlyingAddress] = marketInfo.address;
marketsByUnderlying[marketInfo.underlyingAddress] = {
address: marketInfo.address,
exchangeRate: marketInfo.exchangeRateStored,
decimals: marketInfo.decimals,
tokenAddress: marketInfo.underlyingAddress,
tokenSymbol: marketInfo.underlyingSymbol,
};
}

const publicClient = createPublicClient({
Expand All @@ -131,7 +157,7 @@ export const updateBorrowBalances = async (
contracts: subStates
.map((m) => [
{
address: marketsByUnderlying[m.token],
address: marketsByUnderlying[m.token].address,
abi: ltokenAbi,
functionName: "borrowBalanceOf",
args: [m.account],
Expand All @@ -142,8 +168,11 @@ export const updateBorrowBalances = async (
});

for (var j = 0; j < subStates.length; j++) {
subStates[j].borrowAmount = BigInt(
borrowBalanceResults[j].result?.toString() ?? 0
subStates[j].borrowAmount = Number(
formatUnits(
(borrowBalanceResults[j]?.result as bigint) || 0n,
marketsByUnderlying[subStates[j].token].underlyingDecimals
)
);
}
}
Expand Down
19 changes: 12 additions & 7 deletions adapters/layerbank/src/sdk/subgraphDetails.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Account, createPublicClient, extractChain, http } from "viem";
import { formatUnits, createPublicClient, extractChain, http } from "viem";
import { scroll } from "viem/chains";
import {
CHAINS,
Expand All @@ -17,8 +17,8 @@ export interface AccountState {
id: string;
account: string;
token: string;
lentAmount: bigint;
borrowAmount: bigint;
lentAmount: number;
borrowAmount: number;
}

export const getAccountStatesForAddressByPoolAtBlock = async (
Expand Down Expand Up @@ -94,8 +94,8 @@ export const getAccountStatesForAddressByPoolAtBlock = async (
? WETH_ADDRESS[CHAINS.SCROLL].toLowerCase()
: m.token
].toLowerCase(),
lentAmount: BigInt(m.supplied),
borrowAmount: BigInt(m.borrowed),
lentAmount: m.supplied,
borrowAmount: m.borrowed,
}));

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

return {
...state,
lentAmount:
(state.lentAmount * marketInfo.exchangeRateStored) / BigInt(1e18),
lentAmount: Number(
formatUnits(
((BigInt(state.lentAmount) || 0n) * marketInfo.exchangeRateStored) /
10n ** 18n,
marketInfo.underlyingDecimals
)
)
};
})
.filter((x) => x !== undefined) as AccountState[];
Expand Down
10 changes: 5 additions & 5 deletions adapters/rhomarkets/src/sdk/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ type OutputDataSchemaRow = {
market: string;
token_address: string;
token_symbol: string;
supply_token: bigint;
borrow_token: bigint;
supply_token: number;
borrow_token: number;
block_number: number;
timestamp: number;
protocol: string;
Expand Down Expand Up @@ -132,10 +132,10 @@ export const getUserTVLByBlock = async (blocks: BlockData) => {
timestamp: blocks.blockTimestamp,
block_number: blocks.blockNumber,
etl_timestamp: Math.floor(Date.now() / 1000),
token_address: marketInfo.tokenAddress,
token_address: marketInfo.tokenAddress.toLowerCase(),
token_symbol: marketInfo.tokenSymbol,
user_address: item.user_address,
market: item.market,
user_address: item.user_address.toLowerCase(),
market: item.market.toLowerCase(),
supply_token: item.supply_token,
borrow_token: item.borrow_token,
};
Expand Down
Loading