Skip to content

Commit

Permalink
refactor: make tvl in tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
coreyar committed Dec 4, 2024
1 parent a9b5dce commit dfa1082
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 15 deletions.
2 changes: 1 addition & 1 deletion subgraphs/etherfi-promo/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ TVL
"""
type TVL @entity {
id: Bytes!
tvl: BigInt!
tvl: BigDecimal!
}

"""
Expand Down
7 changes: 6 additions & 1 deletion subgraphs/etherfi-promo/src/operations/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { BorrowerAccount, SupplierAccount, TVL } from '../../generated/schema';
import { ERC20 as ERC20Contract } from '../../generated/vWeETH/ERC20';
import { VToken as VTokenContract } from '../../generated/vWeETH/VToken';
import { getBorrowerAccount, getSupplierAccount, getTvl } from './get';
import exponentToBigDecimal from '../utilities/exponentToBigDecimal';

export function updateSupplierAccount(
accountAddress: Address,
Expand Down Expand Up @@ -35,7 +36,11 @@ export function updateTvl(event: ethereum.Event): TVL {
const totalBorrows = vTokenContract.totalBorrowsCurrent();
const totalReserves = vTokenContract.totalReserves();
const vTokenContractBalance = underlyingContract.balanceOf(event.address);
tvl.tvl = vTokenContractBalance.plus(totalBorrows).minus(totalReserves);
tvl.tvl = vTokenContractBalance
.plus(totalBorrows)
.minus(totalReserves)
.toBigDecimal()
.div(exponentToBigDecimal(18));
tvl.save();
return tvl;
}
4 changes: 2 additions & 2 deletions subgraphs/etherfi-promo/src/utilities/exponentToBigDecimal.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { BigDecimal } from '@graphprotocol/graph-ts';

function exponentToBigInt(decimals: i32): BigDecimal {
function exponentToBigDecimal(decimals: i32): BigDecimal {
let bd = BigDecimal.fromString('1');
for (let i = 0; i < decimals; i++) {
bd = bd.times(BigDecimal.fromString('10'));
}
return bd;
}

export default exponentToBigInt;
export default exponentToBigDecimal;
6 changes: 3 additions & 3 deletions subgraphs/etherfi-promo/subgraph-client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ class SubgraphClient {
}
}

export default new SubgraphClient(
'http://127.0.0.1:8000/subgraphs/name/venusprotocol/etherfi-promo',
);
const createSubgraphClient = (url: string) => new SubgraphClient(url);

export default createSubgraphClient;
21 changes: 13 additions & 8 deletions subgraphs/etherfi-promo/tests/invariant.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import assert from 'assert';

import subgraphClient from '../subgraph-client';
import { parseUnits } from 'ethers/lib/utils';
import createSubgraphClient from '../subgraph-client';

const weEths = '0xEF26C64bC06A8dE4CA5D31f119835f9A1d9433b9';
const weEth = '0xb4933AF59868986316Ed37fa865C829Eba2df0C7';

const subgraphClient = createSubgraphClient(
'https://gateway.thegraph.com/api/71ddbc81f1c42517b3929d56bc6ce8e8/subgraphs/id/3mxXR9oB3gc12N3jjs4XfzpuqsRZKt5rWv6fhgYsyDgq',
);

const checkWeEth = async () => {
const { data } = await subgraphClient.getAccounts(20679412, weEth, weEth);
const { data } = await subgraphClient.getAccounts(21324416, weEth, weEth);

const supplierBalanceSum = data!.supplierAccounts.reduce(
(acc: BigInt, curr: { effective_balance: string }) => {
return BigInt(acc.toString()) + BigInt(curr.effective_balance);
return BigInt(acc.toString()) + parseUnits(curr.effective_balance, 18).toBigInt();
},
BigInt(0),
);
Expand All @@ -23,19 +27,20 @@ const checkWeEth = async () => {
};

const checkWeEths = async () => {
const { data } = await subgraphClient.getAccounts(20679412, weEths, weEths);
const { data } = await subgraphClient.getAccounts(21324416, weEths, weEths);

const supplierBalanceSum = data!.supplierAccounts.reduce(
(acc: BigInt, curr: { effective_balance: string }) => {
return BigInt(acc.toString()) + BigInt(curr.effective_balance);
return BigInt(acc.toString()) + parseUnits(curr.effective_balance, 18).toBigInt();
},
BigInt(0),
);

const inRange = (num: bigint) => num >= -10000000000n && num <= 10000000000n;

assert.ok(
inRange(BigInt(data!.tvl!.tvl) - (supplierBalanceSum as bigint)),
`TVL: ${data!.tvl!.tvl} supplierBalanceSum: ${supplierBalanceSum}`,
inRange(parseUnits(data!.tvl!.tvl, 18).toBigInt() - (supplierBalanceSum as bigint)),
`TVL: ${parseUnits(data!.tvl!.tvl, 18).toBigInt()} supplierBalanceSum: ${supplierBalanceSum}`,
);
};

Expand Down

0 comments on commit dfa1082

Please sign in to comment.