From 3602e27e21c03b5f9bfa202e1a9e3af0998ec89e Mon Sep 17 00:00:00 2001 From: Corey Rice Date: Mon, 4 Sep 2023 12:15:13 -0300 Subject: [PATCH] feat: add totalXvsDistributedWei to market --- subgraphs/venus/schema.graphql | 2 ++ subgraphs/venus/src/constants/index.ts | 3 +++ subgraphs/venus/src/mappings/comptroller.ts | 16 +++++++++++++++- subgraphs/venus/src/operations/create.ts | 2 ++ subgraphs/venus/template.yaml | 4 ++++ 5 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 subgraphs/venus/src/constants/index.ts diff --git a/subgraphs/venus/schema.graphql b/subgraphs/venus/schema.graphql index 3800320e..da274d04 100644 --- a/subgraphs/venus/schema.graphql +++ b/subgraphs/venus/schema.graphql @@ -65,6 +65,8 @@ type Market @entity { underlyingPriceUSD: BigDecimal! "Underlying token decimal length" underlyingDecimals: Int! + "Total XVS Distributed for this market" + totalXvsDistributedMantissa: BigInt! } """ diff --git a/subgraphs/venus/src/constants/index.ts b/subgraphs/venus/src/constants/index.ts new file mode 100644 index 00000000..1672fa9f --- /dev/null +++ b/subgraphs/venus/src/constants/index.ts @@ -0,0 +1,3 @@ +import { BigInt } from '@graphprotocol/graph-ts'; + +export const zeroBigInt32 = BigInt.fromString('0'); diff --git a/subgraphs/venus/src/mappings/comptroller.ts b/subgraphs/venus/src/mappings/comptroller.ts index 95d9e10d..574c6c30 100644 --- a/subgraphs/venus/src/mappings/comptroller.ts +++ b/subgraphs/venus/src/mappings/comptroller.ts @@ -1,8 +1,9 @@ /* eslint-disable prefer-const */ // to satisfy AS compiler -import { log } from '@graphprotocol/graph-ts'; +import { BigInt, log } from '@graphprotocol/graph-ts'; import { + DistributedSupplierVenus, MarketEntered, MarketExited, MarketListed, @@ -128,3 +129,16 @@ export const handleNewPriceOracle = (event: NewPriceOracle): void => { comptroller.priceOracle = event.params.newPriceOracle; comptroller.save(); }; + +// Also handles DistributedBorrowerVenus with same signature +export function handleXvsDistributed(event: DistributedSupplierVenus): void { + let vTokenAddress = event.params.vToken.toHex(); + let venusDelta = event.params.venusDelta.toHex(); + let market = Market.load(vTokenAddress); + if (market == null) { + market = createMarket(vTokenAddress); + } + market.totalXvsDistributedMantissa = market.totalXvsDistributedMantissa.plus( + BigInt.fromString(venusDelta), + ); +} diff --git a/subgraphs/venus/src/operations/create.ts b/subgraphs/venus/src/operations/create.ts index 53fddbc0..ee282e28 100644 --- a/subgraphs/venus/src/operations/create.ts +++ b/subgraphs/venus/src/operations/create.ts @@ -3,6 +3,7 @@ import { Address, BigDecimal, BigInt, log } from '@graphprotocol/graph-ts'; import { Account, AccountVToken, Market } from '../../generated/schema'; import { BEP20 } from '../../generated/templates/VToken/BEP20'; import { VToken } from '../../generated/templates/VToken/VToken'; +import { zeroBigInt32 } from '../constants'; import { nullAddress, vBnbAddress } from '../constants/addresses'; import { zeroBD } from '../utilities/exponentToBigDecimal'; import { getUnderlyingPrice } from '../utilities/getUnderlyingPrice'; @@ -96,6 +97,7 @@ export const createMarket = (marketAddress: string): Market => { market.blockTimestamp = 0; market.borrowIndex = zeroBD; market.reserveFactor = reserveFactor.reverted ? BigInt.fromI32(0) : reserveFactor.value; + market.totalXvsDistributedMantissa = zeroBigInt32; return market; }; diff --git a/subgraphs/venus/template.yaml b/subgraphs/venus/template.yaml index db62399c..5dd06492 100644 --- a/subgraphs/venus/template.yaml +++ b/subgraphs/venus/template.yaml @@ -43,6 +43,10 @@ dataSources: handler: handleNewLiquidationIncentive - event: NewPriceOracle(address,address) handler: handleNewPriceOracle + - event: DistributedBorrowerVenus(indexed address,indexed address,uint256,uint256) + handler: handleXvsDistributed + - event: DistributedSupplierVenus(indexed address,indexed address,uint256,uint256) + handler: handleXvsDistributed templates: - name: VToken kind: ethereum/contract