Skip to content

Commit

Permalink
rollback to commit f7c06c1 (for layerbank folder)
Browse files Browse the repository at this point in the history
  • Loading branch information
melotik committed Jul 9, 2024
1 parent 8ac9d55 commit 45bd9be
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 54 deletions.
49 changes: 27 additions & 22 deletions adapters/layerbank/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { CHAINS, PROTOCOLS } from "./sdk/config";
import {
getAccountStatesForAddressByPoolAtBlock,
getTimestampAtBlock,
} from "./sdk/subgraphDetails";

(BigInt.prototype as any).toJSON = function () {
Expand All @@ -11,6 +12,8 @@ import fs from "fs";
import csv from "csv-parser";
import { write } from "fast-csv";
import { getMarketInfos, updateBorrowBalances } from "./sdk/marketDetails";
import { bigMath } from "./sdk/abi/helpers";
import { exit } from "process";

interface BlockData {
blockNumber: number;
Expand All @@ -21,9 +24,10 @@ type OutputDataSchemaRow = {
user_address: string;
market: string;
token_address: string;
underlying_decimals: number;
token_symbol: string;
supply_token: number;
borrow_token: number;
supply_token: bigint;
borrow_token: bigint;
block_number: number;
timestamp: number;
protocol: string;
Expand Down Expand Up @@ -56,27 +60,28 @@ export const getUserTVLByBlock = async (blocks: BlockData) => {

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

// Check if marketInfo is defined before pushing to csvRows
if (marketInfo) {
csvRows.push({
protocol: "Layerbank",
timestamp: blocks.blockTimestamp,
block_number: blocks.blockNumber,
etl_timestamp: Math.floor(Date.now() / 1000),
token_address: marketInfo.underlyingAddress.toLowerCase(),
token_symbol: marketInfo.underlyingSymbol,
user_address: state.account.toLowerCase(),
market: marketInfo.address.toLowerCase(),
supply_token: state.lentAmount,
borrow_token: state.borrowAmount,
});
} else {
console.warn(`Market info not found for token: ${state.token}`);
}
});

// Check if marketInfo is defined before pushing to csvRows
if (marketInfo) {
csvRows.push({
protocol: "Layerbank",
timestamp: blocks.blockTimestamp,
block_number: blocks.blockNumber,
etl_timestamp: Math.floor(Date.now() / 1000),
token_address: marketInfo.underlyingAddress.toLowerCase(),
underlying_decimals: marketInfo.underlyingDecimals,
token_symbol: marketInfo.underlyingSymbol,
user_address: state.account.toLowerCase(),
market: marketInfo.address.toLowerCase(),
supply_token: state.lentAmount,
borrow_token: state.borrowAmount,
});
} else {
console.warn(`Market info not found for token: ${state.token}`);
}
});

return csvRows;
};
Expand Down
27 changes: 7 additions & 20 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, formatUnits } from "viem";
import { createPublicClient, extractChain, http, getContract } from "viem";
import { CHAINS, RPC_URLS, WETH_ADDRESS } from "./config";
import { scroll } from "viem/chains";
import coreAbi from "./abi/core.abi";
Expand All @@ -7,7 +7,6 @@ import { AccountState } from "./subgraphDetails";

export interface MarketInfo {
address: string;
decimals: number;
underlyingAddress: string;
underlyingSymbol: string;
underlyingDecimals: number;
Expand Down Expand Up @@ -90,14 +89,6 @@ 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 @@ -106,7 +97,6 @@ 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,
Expand All @@ -130,7 +120,7 @@ export const updateBorrowBalances = async (
);
const marketsByUnderlying: any = {};
for (let marketInfo of marketInfos) {
marketsByUnderlying[marketInfo.underlyingAddress] = marketInfo;
marketsByUnderlying[marketInfo.underlyingAddress] = marketInfo.address;
}

const publicClient = createPublicClient({
Expand All @@ -141,17 +131,17 @@ export const updateBorrowBalances = async (
states = states.filter((x) => x.borrowAmount > 0);

console.log(`Will update all borrow balances for ${states.length} states`);
for (var i = 0; i < states.length; i += 1000) {
for (var i = 0; i < states.length; i += 500) {
const start = i;
const end = i + 1000;
const end = i + 500;
var subStates = states.slice(start, end);
console.log(`Updating borrow balances for ${start} - ${end}`);

const borrowBalanceResults = await publicClient.multicall({
contracts: subStates
.map((m) => [
{
address: marketsByUnderlying[m.token].address,
address: marketsByUnderlying[m.token],
abi: ltokenAbi,
functionName: "borrowBalanceOf",
args: [m.account],
Expand All @@ -162,11 +152,8 @@ export const updateBorrowBalances = async (
});

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

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: m.supplied,
borrowAmount: m.borrowed,
lentAmount: BigInt(m.supplied),
borrowAmount: BigInt(m.borrowed),
}));

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

return {
...state,
lentAmount: Number(
formatUnits(
((BigInt(state.lentAmount) || 0n) * marketInfo.exchangeRateStored) /
10n ** 18n,
marketInfo.underlyingDecimals
)
)
lentAmount:
(state.lentAmount * marketInfo.exchangeRateStored) / BigInt(1e18),
};
})
.filter((x) => x !== undefined) as AccountState[];
Expand Down

0 comments on commit 45bd9be

Please sign in to comment.