Skip to content

Commit

Permalink
Merge pull request #120 from VenusProtocol/update-core-supplier-and-b…
Browse files Browse the repository at this point in the history
…orrower-count

feat: add supply and borrower count to core pool
  • Loading branch information
coreyar authored Nov 14, 2023
2 parents e926fb7 + b19bcf2 commit 718eb40
Show file tree
Hide file tree
Showing 19 changed files with 816 additions and 543 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
"@venusprotocol/governance-contracts": "^1.3.0",
"@venusprotocol/isolated-pools": "2.1.0-dev.2",
"@venusprotocol/oracle": "^1.7.3-dev.1",
"@venusprotocol/venus-protocol": "3.1.0",
"assemblyscript": "0.19.23",
"chai": "^4.3.6",
"eslint": "^8.25.0",
Expand Down
6 changes: 3 additions & 3 deletions subgraphs/isolated-pools/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,12 @@ type AccountVToken @entity {
"True if user is entered, false if they are exited"
enteredMarket: Boolean!

"Total amount of underling redeemed"
totalUnderlyingRedeemedMantissa: BigDecimal!
"Total amount of underlying redeemed"
totalUnderlyingRedeemedMantissa: BigInt!
"The value of the borrow index upon users last interaction"
accountBorrowIndexMantissa: BigInt!
"Total amount underlying repaid"
totalUnderlyingRepaidMantissa: BigDecimal!
totalUnderlyingRepaidMantissa: BigInt!
}

"""
Expand Down
2 changes: 0 additions & 2 deletions subgraphs/isolated-pools/src/mappings/vToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,6 @@ export function handleTransfer(event: Transfer): void {
event.logIndex,
event.params.amount,
market.exchangeRateMantissa,
market.underlyingDecimals,
market.vTokenDecimals,
);
}

Expand Down
6 changes: 3 additions & 3 deletions subgraphs/isolated-pools/src/operations/getOrCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
} from '../../generated/schema';
import { Comptroller } from '../../generated/templates/Pool/Comptroller';
import { RewardsDistributor as RewardDistributorContract } from '../../generated/templates/RewardsDistributor/RewardsDistributor';
import { zeroBigDecimal, zeroBigInt32 } from '../constants';
import { zeroBigInt32 } from '../constants';
import {
getAccountVTokenId,
getAccountVTokenTransactionId,
Expand Down Expand Up @@ -108,9 +108,9 @@ export const getOrCreateAccountVToken = (
accountVToken.accountSupplyBalanceMantissa = suppliedAmountMantissa;
accountVToken.accountBorrowBalanceMantissa = borrowedAmountMantissa;

accountVToken.totalUnderlyingRedeemedMantissa = zeroBigDecimal;
accountVToken.totalUnderlyingRedeemedMantissa = zeroBigInt32;
accountVToken.accountBorrowIndexMantissa = zeroBigInt32;
accountVToken.totalUnderlyingRepaidMantissa = zeroBigDecimal;
accountVToken.totalUnderlyingRepaidMantissa = zeroBigInt32;
}
return accountVToken;
};
Expand Down
17 changes: 2 additions & 15 deletions subgraphs/isolated-pools/src/operations/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ import { Address, BigInt, Bytes } from '@graphprotocol/graph-ts';
import { PoolMetadataUpdatedNewMetadataStruct } from '../../generated/PoolRegistry/PoolRegistry';
import { AccountVToken, Market } from '../../generated/schema';
import { VToken } from '../../generated/templates/VToken/VToken';
import {
exponentToBigDecimal,
getExchangeRateBigDecimal,
valueOrNotAvailableIntIfReverted,
} from '../utilities';
import { exponentToBigInt, valueOrNotAvailableIntIfReverted } from '../utilities';
import { getTokenPriceInCents } from '../utilities';
import { getOrCreateMarket } from './getOrCreate';
import {
Expand Down Expand Up @@ -118,17 +114,8 @@ export const updateAccountVTokenTransferFrom = (
logIndex: BigInt,
amount: BigInt,
exchangeRate: BigInt,
underlyingDecimals: i32,
vTokenDecimals: i32,
): AccountVToken => {
const exchangeRateBigDecimal = getExchangeRateBigDecimal(
exchangeRate,
underlyingDecimals,
vTokenDecimals,
);
const amountUnderlyingMantissa = exchangeRateBigDecimal
.times(exponentToBigDecimal(underlyingDecimals))
.times(amount.toBigDecimal());
const amountUnderlyingMantissa = exchangeRate.div(exponentToBigInt(18)).times(amount);

const accountVToken = updateAccountVToken(
marketAddress,
Expand Down
1 change: 1 addition & 0 deletions subgraphs/isolated-pools/src/utilities/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { default as getExchangeRateBigDecimal } from './getExchangeRateBigDecimal';
export { default as getTokenPriceInCents } from './getTokenPriceInCents';
export { default as exponentToBigDecimal } from './exponentToBigDecimal';
export { default as exponentToBigInt } from './exponentToBigInt';
export { default as valueOrNotAvailableIntIfReverted } from './valueOrNotAvailableIntIfReverted';
2 changes: 1 addition & 1 deletion subgraphs/isolated-pools/tests/VToken/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ describe('VToken', () => {
'AccountVToken',
accountVTokenId,
'totalUnderlyingRedeemedMantissa',
'53371670178204461670107500000000000000',
'53371549778058610525',
);
});

Expand Down
4 changes: 4 additions & 0 deletions subgraphs/venus/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,9 @@
"prepare:bsc": "npx mustache config/bsc.json template.yaml > subgraph.yaml && npx mustache config/bsc.json src/constants/config-template > src/constants/config.ts",
"test": "graph test",
"test:integration": "true"
},
"dependencies": {
"@venusprotocol/venus-protocol": "3.1.0",
"@venusprotocol/venus-protocol-orig-events": "npm:@venusprotocol/[email protected]"
}
}
82 changes: 32 additions & 50 deletions subgraphs/venus/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type Market @entity {
"The vToken contract balance of BEP20 or BNB"
cashMantissa: BigInt!
"Collateral factor determining how much one can borrow"
collateralFactor: BigDecimal!
collateralFactorMantissa: BigInt!
"Exchange rate of tokens / vTokens"
exchangeRateMantissa: BigInt!
"Address of the interest rate model"
Expand Down Expand Up @@ -67,6 +67,15 @@ type Market @entity {
totalXvsDistributedMantissa: BigInt!
"vToken decimal length"
vTokenDecimals: Int!

"Number of accounts currently supplying to this market"
supplierCount: BigInt!

"Number of accounts with reasonable borrow positions in this market (excludes accounts with dust (potentially left over from liquidations))"
borrowerCountAdjusted: BigInt!

"Number of accounts currently borrowing from this market"
borrowerCount: BigInt!
}

"""
Expand All @@ -84,17 +93,6 @@ type Account @entity {
countLiquidator: Int!
"True if user has ever borrowed"
hasBorrowed: Boolean!

# The following values are added by the JS Wrapper, and must be calculated with the most up
# to date values based on the block delta for market.exchangeRate and market.borrowIndex
# They do not need to be in the schema, but they will show up in the explorer playground

# "If less than 1, the account can be liquidated
# health: BigDecimal!
# "Total assets supplied by user"
# totalBorrowValueInEth: BigDecimal!
# "Total assets borrowed from user"
# totalCollateralValueInEth: BigDecimal!
}

"""
Expand All @@ -118,35 +116,19 @@ type AccountVToken @entity {
enteredMarket: Boolean!

"VToken balance of the user"
vTokenBalance: BigDecimal!
vTokenBalanceMantissa: BigInt!
"Total amount of underlying supplied"
totalUnderlyingSuppliedMantissa: BigInt!
"Total amount of underling redeemed"
"Total amount of underlying redeemed"
totalUnderlyingRedeemedMantissa: BigInt!
"The value of the borrow index upon users last interaction"
accountBorrowIndexMantissa: BigInt!
"Total amount underlying borrowed, exclusive of interest"
totalUnderlyingBorrowed: BigDecimal!
totalUnderlyingBorrowedMantissa: BigInt!
"Total amount underlying repaid"
totalUnderlyingRepaid: BigDecimal!
totalUnderlyingRepaidMantissa: BigInt!
"Current borrow balance stored in contract (exclusive of interest since accrualBlockNumber)"
storedBorrowBalance: BigDecimal!

# The following values are added by the JS Wrapper, and must be calculated with the most up
# to date values based on the block delta for market.exchangeRate and market.borrowIndex
# They do not need to be in the schema, but they will show up in the explorer playground

# supplyBalanceUnderlying: BigDecimal!
# FORMULA: supplyBalanceUnderlying = vTokenBalance * market.exchangeRate

# lifetimeSupplyInterestAccrued: BigDecimal!
# FORMULA: lifetimeSupplyInterestAccrued = supplyBalanceUnderlying - totalUnderlyingSupplied + totalUnderlyingRedeemed

# borrowBalanceUnderlying: BigDecimal!
# FORMULA: borrowBalanceUnderlying = storedBorrowBalance * market.borrowIndex / accountBorrowIndex

# lifetimeBorrowInterestAccrued: BigDecimal!
# FORMULA: lifetimeBorrowInterestAccrued = borrowBalanceUnderlying - totalUnderlyingBorrowed + totalUnderlyingRepaid
storedBorrowBalanceMantissa: BigInt!
}

"""
Expand Down Expand Up @@ -195,7 +177,7 @@ type TransferEvent implements VTokenTransfer @entity {
"Transaction hash concatenated with log index"
id: ID!
"vTokens transferred"
amount: BigDecimal!
amountMantissa: BigInt!
"Account that received tokens"
to: Bytes!
"Account that sent tokens"
Expand All @@ -216,7 +198,7 @@ type MintEvent implements VTokenTransfer @entity {
"Transaction hash concatenated with log index"
id: ID!
"vTokens transferred"
amount: BigDecimal!
amountMantissa: BigInt!
"Account that received tokens (minter)"
to: Bytes!
"Account that sent tokens (VToken contract)"
Expand All @@ -225,10 +207,10 @@ type MintEvent implements VTokenTransfer @entity {
blockNumber: Int!
"Block time"
blockTime: Int!
"Symbol of the vToken transferred"
vTokenSymbol: String!
"Address of the vToken transferred"
vTokenAddress: Bytes!
"Underlying token amount transferred"
underlyingAmount: BigDecimal
underlyingAmountMantissa: BigInt!
}

"""
Expand All @@ -239,7 +221,7 @@ type RedeemEvent implements VTokenTransfer @entity {
"Transaction hash concatenated with log index"
id: ID!
"vTokens transferred"
amount: BigDecimal!
amountMantissa: BigInt!
"Account that received tokens (VToken contract)"
to: Bytes!
"Account that sent tokens (redeemer)"
Expand All @@ -248,10 +230,10 @@ type RedeemEvent implements VTokenTransfer @entity {
blockNumber: Int!
"Block time"
blockTime: Int!
"Symbol of the vToken transferred"
vTokenSymbol: String!
"Address of the vToken transferred"
vTokenAddress: Bytes!
"Underlying token amount transferred"
underlyingAmount: BigDecimal
underlyingAmountMantissa: BigInt!
}

"""
Expand All @@ -262,7 +244,7 @@ type LiquidationEvent implements VTokenTransfer @entity {
"Transaction hash concatenated with log index"
id: ID!
"vTokens seized"
amount: BigDecimal!
amountMantissa: BigInt!
"Liquidator receiving tokens"
to: Bytes!
"Account being liquidated (borrower)"
Expand All @@ -276,7 +258,7 @@ type LiquidationEvent implements VTokenTransfer @entity {
"Symbol of the underlying asset repaid through liquidation"
underlyingSymbol: String!
"Underlying vToken amount that was repaid by liquidator"
underlyingRepayAmount: BigDecimal!
underlyingRepayAmountMantissa: BigInt!
}

"""
Expand All @@ -287,9 +269,9 @@ interface UnderlyingTransfer {
"Transaction hash concatenated with log index"
id: ID!
"Amount of underlying borrowed"
amount: BigDecimal!
amountMantissa: BigInt!
"Total borrows of this asset the account has"
accountBorrows: BigDecimal!
accountBorrowsMantissa: BigInt!
"Account that borrowed the tokens"
borrower: Bytes!
"Block number"
Expand All @@ -307,9 +289,9 @@ type BorrowEvent implements UnderlyingTransfer @entity {
"Transaction hash concatenated with log index"
id: ID!
"Amount of underlying borrowed"
amount: BigDecimal!
amountMantissa: BigInt!
"Total borrows of this asset the account has"
accountBorrows: BigDecimal!
accountBorrowsMantissa: BigInt!
"Account that borrowed the tokens"
borrower: Bytes!
"Block number"
Expand All @@ -328,9 +310,9 @@ type RepayEvent implements UnderlyingTransfer @entity {
"Transaction hash concatenated with log index"
id: ID!
"Amount of underlying repaid"
amount: BigDecimal!
amountMantissa: BigInt!
"Total borrows of this asset the account has"
accountBorrows: BigDecimal!
accountBorrowsMantissa: BigInt!
"Account that borrowed the tokens"
borrower: Bytes!
"Block number"
Expand Down
20 changes: 11 additions & 9 deletions subgraphs/venus/src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ export const zeroBigDecimal = BigDecimal.fromString('0');
export const zeroBigInt32 = BigInt.fromString('0');
export const oneBigInt = BigInt.fromString('1');

export const DUST_THRESHOLD = BigInt.fromString('10');

export const Actions = [
MINT,
REDEEM,
BORROW,
REPAY,
SEIZE,
LIQUIDATE,
TRANSFER,
ENTER_MARKET,
EXIT_MARKET,
'MINT',
'REDEEM',
'BORROW',
'REPAY',
'SEIZE',
'LIQUIDATE',
'TRANSFER',
'ENTER_MARKET',
'EXIT_MARKET',
];
5 changes: 1 addition & 4 deletions subgraphs/venus/src/mappings/comptroller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
} from '../../generated/Comptroller/Comptroller';
import { Account, Market } from '../../generated/schema';
import { VToken } from '../../generated/templates';
import { mantissaFactorBigDecimal } from '../constants';
import { createAccount, createMarket } from '../operations/create';
import { getOrCreateComptroller } from '../operations/getOrCreate';
import { updateCommonVTokenStats } from '../operations/update';
Expand Down Expand Up @@ -110,9 +109,7 @@ export const handleNewCollateralFactor = (event: NewCollateralFactor): void => {
// sources can source from the contract creation block and not the time the
// comptroller adds the market, we can avoid this altogether
if (market != null) {
market.collateralFactor = event.params.newCollateralFactorMantissa
.toBigDecimal()
.div(mantissaFactorBigDecimal);
market.collateralFactorMantissa = event.params.newCollateralFactorMantissa;
market.save();
}
};
Expand Down
Loading

0 comments on commit 718eb40

Please sign in to comment.