Skip to content

Commit

Permalink
feat: remove redstone, use defillama prices for v3
Browse files Browse the repository at this point in the history
  • Loading branch information
doomsower committed Feb 26, 2024
1 parent 01d759f commit c62ee5a
Show file tree
Hide file tree
Showing 17 changed files with 120 additions and 631 deletions.
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
"@gearbox-protocol/eslint-config": "2.0.0-next.2",
"@gearbox-protocol/prettier-config": "2.0.0-next.0",
"@gearbox-protocol/sdk-gov": "^1.31.2",
"@redstone-finance/protocol": "^0.3.6",
"@redstone-finance/sdk": "^0.3.6",
"@semantic-release/exec": "^6.0.3",
"eslint": "^8.56.0",
"ethers": "^6.0.0",
Expand Down
12 changes: 7 additions & 5 deletions src/adapter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,27 @@ async function tvl(
block: number,
_: unknown,
{ api }: ApiParameter,
) {
): Promise<void> {
// Pool TVL (Current token balances)
const tokensAndOwners = await getPools(block, api);

// CreditAccounts TVL
// v1 and v2:
// return sum of balances of all credit accounts by credit manager in underlying
const v1Balances = await getV1TVL(block, api);
const v2Balances = await getV2TVL(block, api);
// v3 is different:
// return balances of each credit account
const v3Balances = await getV3TVL(block, api);

// Merge all balances for each token
[...v1Balances, ...v2Balances, ...v3Balances].forEach(i => {
api.add(i.token, i.bal);
tokensAndOwners.push([i.token, i.addr]);
});

return api.sumTokens({ tokensAndOwners });
await api.sumTokens({ tokensAndOwners });
}

module.exports = {
export default {
hallmarks: [[1666569600, "LM begins"]],
ethereum: {
tvl,
Expand Down
5 changes: 4 additions & 1 deletion src/adapter/pools/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import type { ChainApi } from "@defillama/sdk";
import { ADDRESS_PROVIDER_V3 } from "../constants";
import { poolAbis } from "./abi";

export async function getPools(block: number, api: ChainApi) {
export async function getPools(
block: number,
api: ChainApi,
): Promise<Array<[token: string, balance: string]>> {
const contractsRegisterAddr: string = await api.call({
block,
abi: poolAbis["getAddressOrRevert"],
Expand Down
12 changes: 10 additions & 2 deletions src/adapter/v1/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,17 @@ import { ADDRESS_PROVIDER_V3 } from "../constants";
// @ts-ignore
import { getLogs } from "../helper/cache/getLogs";
import { v1Abis } from "./abi";
import type { CreditAccountEvent, CreditManagerData, Log } from "./types";
import type {
CreditAccountEvent,
CreditManagerData,
Log,
TokenAndOwner,
} from "./types";

export async function getV1TVL(block: number, api: ChainApi) {
export async function getV1TVL(
block: number,
api: ChainApi,
): Promise<TokenAndOwner[]> {
const creditManagers = await getCreditManagersV1(block, api);

// Silently throw if no V1 CAs available
Expand Down
6 changes: 6 additions & 0 deletions src/adapter/v1/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,9 @@ export interface CreditManagerData {
addr: string;
underlying: string;
}

export interface TokenAndOwner {
addr: string;
token: string;
bal: string;
}
8 changes: 6 additions & 2 deletions src/adapter/v2/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ChainApi } from "@defillama/sdk";
import { Contract, id } from "ethers";
import { Contract } from "ethers";

import { ADDRESS_PROVIDER_V3 } from "../constants";
// @ts-ignore
Expand All @@ -10,9 +10,13 @@ import type {
CreditManagerData,
Log,
ParsedLog,
TokenAndOwner,
} from "./types";

export async function getV2TVL(block: number, api: ChainApi) {
export async function getV2TVL(
block: number,
api: ChainApi,
): Promise<TokenAndOwner[]> {
// Get Current CMs
const creditManagers = await getCreditManagersV210(block, api);
// Silently throw if no V2 CAs available
Expand Down
6 changes: 6 additions & 0 deletions src/adapter/v2/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,9 @@ export interface CreditAccountEvent {
ca: string | null;
cf: string;
}

export interface TokenAndOwner {
addr: string;
token: string;
bal: string;
}
53 changes: 26 additions & 27 deletions src/adapter/v3/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@ import type { ChainApi } from "@defillama/sdk";

import { ADDRESS_PROVIDER_V3 } from "../constants";
import { v3Abis } from "./abi";
import { getPriceUpdates } from "./redstone";
import type { CreditAccountData, CreditManagerData } from "./types";
import type {
CreditAccountData,
CreditManagerData,
TokenAndOwner,
} from "./types";

export async function getV3TVL(block: number, api: ChainApi) {
export async function getV3TVL(
block: number,
api: ChainApi,
): Promise<TokenAndOwner[]> {
const dc300 = await api.call({
abi: v3Abis["getAddressOrRevert"],
target: ADDRESS_PROVIDER_V3,
Expand All @@ -26,11 +32,7 @@ export async function getV3TVL(block: number, api: ChainApi) {
creditManagers.map(cm => getV3CAs(dc300, cm.addr, block, api)),
);

return creditManagers.map((cm, i) => ({
addr: cm.addr,
token: cm.underlying,
bal: caValues[i],
}));
return caValues.flat();
}

export async function getCreditManagersV3(
Expand All @@ -51,29 +53,26 @@ async function getV3CAs(
creditManager: string,
block: number,
api: ChainApi,
): Promise<string> {
let accs: CreditAccountData[] = await api.call({
): Promise<TokenAndOwner[]> {
const accs: CreditAccountData[] = await api.call({
// IDataCompressorV3_00__factory.createInterface().getFunction("getCreditAccountsByCreditManager").format(ethers.utils.FormatTypes.full)
target: dc300,
abi: v3Abis["getCreditAccountsByCreditManager"],
params: [creditManager, []] as any,
block,
});
const priceFeedsNeeded = new Set<string>();
accs.forEach(acc => {
acc.priceFeedsNeeded.forEach(pf => priceFeedsNeeded.add(pf.toLowerCase()));
});
const priceUpdates = await getPriceUpdates(priceFeedsNeeded, block, api);
accs = await api.call({
target: dc300,
abi: v3Abis["getCreditAccountsByCreditManager"],
params: [creditManager, priceUpdates] as any,
block,
});
let totalValue = 0n;
accs.forEach(acc => {
totalValue += BigInt(acc.totalValue);
});

return totalValue.toString();
const result: TokenAndOwner[] = [];
for (const acc of accs) {
for (const { balance, token } of acc.balances) {
// reduce noize
if (balance !== "0" && balance !== "1") {
result.push({
addr: acc.addr,
bal: balance,
token: token,
});
}
}
}
return result;
}
7 changes: 0 additions & 7 deletions src/adapter/v3/redstone/abi.ts

This file was deleted.

Loading

0 comments on commit c62ee5a

Please sign in to comment.