Skip to content

Commit

Permalink
(runs locally) adding decimal normalization on balances in layerbank
Browse files Browse the repository at this point in the history
  • Loading branch information
melotik committed Jul 17, 2024
1 parent dc9335c commit bec0441
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 16 deletions.
6 changes: 3 additions & 3 deletions adapters/layerbank/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ type OutputDataSchemaRow = {
token_address: string;
underlying_decimals: number;
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 @@ -60,7 +60,7 @@ export const getUserTVLByBlock = async (blocks: BlockData) => {

states.forEach((state) => {
const marketInfo = marketInfos.find(
(mi) => mi.underlyingAddress == state.token.toLowerCase()
(mi) => mi.underlyingAddress.toLowerCase() == state.token.toLowerCase()
);

// Check if marketInfo is defined before pushing to csvRows
Expand Down
15 changes: 9 additions & 6 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 Down Expand Up @@ -99,7 +99,7 @@ export const getMarketInfos = async (
address: marketAddress,
underlyingAddress,
underlyingSymbol: underlyingSymbolResults[i].result as any,
underlyingDecimals: underlyingDecimalResults[i].result as any,
underlyingDecimals: (underlyingDecimalResults[i].result as number) || 0,
exchangeRateStored: BigInt(
exchangeRateResults[i].status === "success"
? (exchangeRateResults[i].result as any)
Expand All @@ -120,7 +120,7 @@ export const updateBorrowBalances = async (
);
const marketsByUnderlying: any = {};
for (let marketInfo of marketInfos) {
marketsByUnderlying[marketInfo.underlyingAddress] = marketInfo.address;
marketsByUnderlying[marketInfo.underlyingAddress] = marketInfo;
}

const publicClient = createPublicClient({
Expand All @@ -141,7 +141,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 @@ -152,8 +152,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

0 comments on commit bec0441

Please sign in to comment.