Skip to content

Commit

Permalink
refactor: cash mantissa
Browse files Browse the repository at this point in the history
  • Loading branch information
coreyar committed Sep 20, 2023
1 parent bf53351 commit 7a67b17
Show file tree
Hide file tree
Showing 16 changed files with 23 additions and 39 deletions.
2 changes: 1 addition & 1 deletion subgraphs/isolated-pools/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ type Market @entity {
"Borrow rate per block"
borrowRateMantissa: BigInt!
"The vToken contract balance of BEP20 or BNB"
cash: BigDecimal!
cashMantissa: BigInt!
"Collateral factor determining how much one can borrow"
collateralFactorMantissa: BigInt!
"Exchange rate of tokens / vTokens"
Expand Down
6 changes: 1 addition & 5 deletions subgraphs/isolated-pools/src/operations/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,7 @@ export function createMarket(

market.borrowRateMantissa = vTokenContract.borrowRatePerBlock();

market.cash = vTokenContract
.getCash()
.toBigDecimal()
.div(exponentToBigDecimal(market.underlyingDecimals))
.truncate(market.underlyingDecimals);
market.cashMantissa = vTokenContract.getCash();

market.exchangeRateMantissa = vTokenContract.exchangeRateStored();

Expand Down
7 changes: 2 additions & 5 deletions subgraphs/isolated-pools/src/operations/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ export const updateMarket = (
vTokenAddress,
market.underlyingDecimals,
);
market.underlyingPriceCents = tokenPriceCents.truncate(market.underlyingDecimals);
market.underlyingPriceCents = tokenPriceCents;

market.accrualBlockNumber = valueOrNotAvailableIntIfReverted(
marketContract.try_accrualBlockNumber(),
Expand All @@ -192,10 +192,7 @@ export const updateMarket = (
market.reservesMantissa = valueOrNotAvailableIntIfReverted(marketContract.try_totalReserves());

const cashBigInt = valueOrNotAvailableIntIfReverted(marketContract.try_getCash());
market.cash = cashBigInt
.toBigDecimal()
.div(exponentToBigDecimal(market.underlyingDecimals))
.truncate(market.underlyingDecimals);
market.cashMantissa = cashBigInt;

// calling supplyRatePerBlock & borrowRatePerBlock can fail due to external reasons, so we fall back to 0 in case of an error
market.borrowRateMantissa = valueOrNotAvailableIntIfReverted(
Expand Down
2 changes: 1 addition & 1 deletion subgraphs/isolated-pools/src/utilities/exponentToBigInt.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BigInt } from '@graphprotocol/graph-ts';

function exponentToBigInt(decimals: i32): i32 {
function exponentToBigInt(decimals: i32): BigInt {
let bd = BigInt.fromString('1');
for (let i = 0; i < decimals; i++) {
bd = bd.times(BigInt.fromString('10'));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Address } from '@graphprotocol/graph-ts';
import { Address, BigInt } from '@graphprotocol/graph-ts';

import { PriceOracle } from '../../generated/templates/VToken/PriceOracle';
import { NOT_AVAILABLE_BIG_INT } from '../constants';
Expand All @@ -11,7 +11,7 @@ const getTokenPriceInCents = (
poolAddress: Address,
eventAddress: Address,
underlyingDecimals: i32,
): i32 => {
): BigInt => {
const pool = getPool(poolAddress);
// will return NOT_AVAILABLE if the price cannot be fetched
let underlyingPrice = NOT_AVAILABLE_BIG_INT;
Expand Down
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 @@ -482,7 +482,7 @@ describe('VToken', () => {
assertMarketDocument('borrowIndexMantissa', '300000000000000000000');
assertMarketDocument('reservesMantissa', '5128924555022289393');
assertMarketDocument('totalBorrowsMantissa', '2641234234636158123');
assertMarketDocument('cash', '1.418171344423412457');
assertMarketDocument('cashMantissa', '1418171344423412457');
assertMarketDocument('borrowRateMantissa', '12678493');
assertMarketDocument('supplyRateMantissa', '12678493');
});
Expand Down
2 changes: 1 addition & 1 deletion subgraphs/isolated-pools/tests/integration/pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ describe('Pools', function () {
markets.forEach((m, idx) => {
expect(m.pool.id).to.equal(pool.id);
expect(m.borrowRateMantissa).to.equal('0');
expect(m.cash).to.equal('1');
expect(m.cashMantissa).to.equal('1');
expect(m.collateralFactorMantissa).to.equal('700000000000000000');
expect(m.exchangeRateMantissa).to.equal('10000000000000000000000000000');
expect(m.interestRateModelAddress).to.equal(interestRateModelAddresses[idx]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ query MarketById($id: ID!) {
}
badDebtMantissa
borrowRateMantissa
cash
cashMantissa
collateralFactorMantissa
exchangeRateMantissa
interestRateModelAddress
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ query Markets {
}
badDebtMantissa
borrowRateMantissa
cash
cashMantissa
collateralFactorMantissa
exchangeRateMantissa
interestRateModelAddress
Expand Down
2 changes: 1 addition & 1 deletion subgraphs/venus/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type Market @entity {
"Borrow rate per block"
borrowRateMantissa: BigInt!
"The vToken contract balance of BEP20 or BNB"
cash: BigDecimal!
cashMantissa: BigInt!
"Collateral factor determining how much one can borrow"
collateralFactor: BigDecimal!
"Exchange rate of tokens / vTokens"
Expand Down
8 changes: 4 additions & 4 deletions subgraphs/venus/src/operations/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export function createMarket(marketAddress: string): Market {
market.underlyingDecimals = 18;
market.underlyingName = 'Binance Coin';
market.underlyingSymbol = 'BNB';
market.underlyingPriceCents = zeroBigDecimal;
market.underlyingPriceCents = zeroBigInt32;
// It is all other VBEP20 contracts
} else {
market = new Market(marketAddress);
Expand All @@ -71,8 +71,8 @@ export function createMarket(marketAddress: string): Market {
market.underlyingName = underlyingContract.name();
market.underlyingSymbol = underlyingContract.symbol();

const underlyingValue = getUnderlyingPrice(market.id, market.underlyingDecimals);
market.underlyingPriceCents = underlyingValue.underlyingPriceUsd;
const underlyingPriceCents = getUnderlyingPrice(market.id, market.underlyingDecimals);
market.underlyingPriceCents = underlyingPriceCents;
}

market.vTokenDecimals = contract.decimals();
Expand All @@ -81,7 +81,7 @@ export function createMarket(marketAddress: string): Market {
const reserveFactor = contract.try_reserveFactorMantissa();

market.borrowRateMantissa = zeroBigInt32;
market.cash = zeroBigDecimal;
market.cashMantissa = zeroBigInt32;
market.collateralFactor = zeroBigDecimal;
market.exchangeRateMantissa = zeroBigInt32;
market.interestRateModelAddress = interestRateModelAddress.reverted
Expand Down
13 changes: 2 additions & 11 deletions subgraphs/venus/src/operations/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { AccountVToken, Market } from '../../generated/schema';
import { VToken } from '../../generated/templates/VToken/VToken';
import { zeroBigInt32 } from '../constants';
import { createMarket } from '../operations/create';
import { exponentToBigDecimal } from '../utilities/exponentToBigDecimal';
import { getUnderlyingPrice } from '../utilities/getUnderlyingPrice';
import { createAccountVToken } from './create';
import { getOrCreateAccountVTokenTransaction } from './getOrCreate';
Expand Down Expand Up @@ -43,17 +42,13 @@ export const updateMarket = (
if (market.accrualBlockNumber != blockNumber) {
const contractAddress = Address.fromString(market.id);
const contract = VToken.bind(contractAddress);
const vTokenDecimals = market.vTokenDecimals;
market.accrualBlockNumber = contract.accrualBlockNumber().toI32();
market.blockTimestamp = blockTimestamp;

const underlyingPriceCents = getUnderlyingPrice(market.id, market.underlyingDecimals);
market.underlyingPriceCents = underlyingPriceCents;

market.totalSupplyMantissa = contract
.totalSupply()
.toBigDecimal()
.div(exponentToBigDecimal(vTokenDecimals));
market.totalSupplyMantissa = contract.totalSupply();

/* Exchange rate explanation
In Practice
Expand All @@ -77,11 +72,7 @@ export const updateMarket = (
market.reservesMantissa = contract.totalReserves();
market.totalBorrowsMantissa = contract.totalBorrows();

market.cash = contract
.getCash()
.toBigDecimal()
.div(exponentToBigDecimal(market.underlyingDecimals))
.truncate(market.underlyingDecimals);
market.cashMantissa = contract.getCash();

// Must convert to BigDecimal, and remove 10^18 that is used for Exp in Venus Solidity
const borrowRatePerBlock = contract.try_borrowRatePerBlock();
Expand Down
4 changes: 2 additions & 2 deletions subgraphs/venus/src/utilities/getUnderlyingPrice.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Address } from '@graphprotocol/graph-ts';
import { Address, BigInt } from '@graphprotocol/graph-ts';

import { getTokenPriceCents } from './getTokenPriceCents';

export function getUnderlyingPrice(address: string, underlyingDecimals: i32): i32 {
export function getUnderlyingPrice(address: string, underlyingDecimals: i32): BigInt {
const contractAddress = Address.fromString(address);
const underlyingPriceCents = getTokenPriceCents(contractAddress, underlyingDecimals);

Expand Down
2 changes: 1 addition & 1 deletion subgraphs/venus/src/utilities/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export { getBnbPriceInUsd } from './getBnbPriceInUsd';
export { getTokenPrice } from './getTokenPrice';
export { getTokenPriceCents } from './getTokenPriceCents';
export { exponentToBigDecimal } from './exponentToBigDecimal';
export { ensureComptrollerSynced } from './ensureComptrollerSynced';
export { getUnderlyingPrice } from './getUnderlyingPrice';
2 changes: 1 addition & 1 deletion subgraphs/venus/tests/Comptroller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ describe('handleMarketListing', () => {
assertMarketDocument('underlyingSymbol', 'BNB');
assertMarketDocument('underlyingPriceCents', '0');
assertMarketDocument('borrowRateMantissa', '0');
assertMarketDocument('cash', '0');
assertMarketDocument('cashMantissa', '0');
assertMarketDocument('collateralFactor', '0');
assertMarketDocument('exchangeRateMantissa', '0');
assertMarketDocument('interestRateModelAddress', interestRateModelAddress.toHex());
Expand Down
2 changes: 1 addition & 1 deletion subgraphs/venus/tests/VToken/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ describe('VToken', () => {
assertMarketDocument('borrowIndexMantissa', '300000000000000000000');
assertMarketDocument('reservesMantissa', '5128924555022289393');
assertMarketDocument('totalBorrowsMantissa', '2641234234636158123');
assertMarketDocument('cash', '1.418171344423412457');
assertMarketDocument('cashMantissa', '1418171344423412457');
assertMarketDocument('borrowRateMantissa', '12678493');
assertMarketDocument('supplyRateMantissa', '12678493');
});
Expand Down

0 comments on commit 7a67b17

Please sign in to comment.