Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests mantissa refactor #97

Merged
merged 9 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ The following subgraphs are deployed to the BSC mainnet

You can also run this subgraph locally, if you wish. Instructions for that can be found in [The Graph Documentation](https://thegraph.com/docs/quick-start).

### ABI

The ABI used is `vtoken.json`. It is a stripped down version of the full abi provided by Venus, that satisfies the calls we need to make for both vBNB and vBEP20 contracts. This way we can use 1 ABI file, and one mapping for vBNB and vBEP20.

## Getting started with querying

Below are a few ways to show how to query the Venus V2 Subgraph for data. The queries show most of the information that is queryable, but there are many other filtering options that can be used, just check out the [querying api](https://github.com/graphprotocol/graph-node/blob/master/docs/graphql-api.md).
Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"pretty": "prettier '**/*.ts' --write",
"test": "yarn workspaces foreach run test",
"test:integration": "yarn workspaces foreach run test:integration",
"postinstall": "patch-package && ./copy_contracts.sh"
"postinstall": "./copy_contracts.sh"
},
"devDependencies": {
"@graphprotocol/client-cli": "^3.0.0",
Expand All @@ -51,7 +51,7 @@
"@venusprotocol/governance-contracts": "^1.0.0",
"@venusprotocol/isolated-pools": "1.0.0",
"@venusprotocol/oracle": "^1.4.1",
"@venusprotocol/venus-protocol": "^0.6.0",
"@venusprotocol/venus-protocol": "^3.0.0-dev.7",
"assemblyscript": "0.19.23",
"chai": "^4.3.6",
"eslint": "^8.25.0",
Expand All @@ -69,7 +69,6 @@
"matchstick-as": "^0.5.0",
"module-alias": "^2.2.2",
"mustache": "^4.2.0",
"patch-package": "6.5.1",
"prettier": "^2.5.1",
"prettier-airbnb-config": "^1.0.0",
"solidity-coverage": "^0.7.21",
Expand Down
111 changes: 0 additions & 111 deletions patches/@venusprotocol+venus-protocol+0.6.0.patch

This file was deleted.

10 changes: 5 additions & 5 deletions 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 All @@ -102,9 +102,9 @@ type Market @entity {
"Max token borrow amount allowed"
borrowCapMantissa: BigInt!
"Total borrowed"
treasuryTotalBorrowsMantissa: BigInt!
totalBorrowsMantissa: BigInt!
"Total supplied"
treasuryTotalSupplyMantissa: BigInt!
totalSupplyMantissa: BigInt!

# Fields that are not in Venus api
"Block the market is updated to"
Expand All @@ -115,8 +115,8 @@ type Market @entity {
borrowIndexMantissa: BigInt!
"The factor determining interest that goes to reserves"
reserveFactorMantissa: BigInt!
"Underlying token price in USD"
underlyingPriceUsd: BigDecimal!
"Underlying token price in USD cents"
underlyingPriceCents: BigInt!
"Underlying token decimal length"
underlyingDecimals: Int!
"vToken decimal length"
Expand Down
16 changes: 6 additions & 10 deletions subgraphs/isolated-pools/src/operations/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import {
zeroBigInt32,
} from '../constants';
import { poolLensAddress, poolRegistryAddress } from '../constants/addresses';
import { getTokenPriceInUsd } from '../utilities';
import { getTokenPriceInCents } from '../utilities';
import exponentToBigDecimal from '../utilities/exponentToBigDecimal';
import {
getAccountVTokenId,
Expand Down Expand Up @@ -100,21 +100,17 @@ export function createMarket(
market.symbol = vTokenContract.symbol();

const underlyingDecimals = underlyingContract.decimals();
const underlyingValue = getTokenPriceInUsd(comptroller, vTokenAddress, underlyingDecimals);
const underlyingValue = getTokenPriceInCents(comptroller, vTokenAddress, underlyingDecimals);
market.underlyingAddress = underlyingAddress;
market.underlyingName = underlyingContract.name();
market.underlyingSymbol = underlyingContract.symbol();
market.underlyingPriceUsd = underlyingValue;
market.underlyingPriceCents = underlyingValue;
market.underlyingDecimals = underlyingDecimals;
market.vTokenDecimals = vTokenContract.decimals();

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 All @@ -129,8 +125,8 @@ export function createMarket(

market.reserveFactorMantissa = vTokenContract.reserveFactorMantissa();

market.treasuryTotalBorrowsMantissa = vTokenContract.totalBorrows();
market.treasuryTotalSupplyMantissa = vTokenContract.totalSupply();
market.totalBorrowsMantissa = vTokenContract.totalBorrows();
market.totalSupplyMantissa = vTokenContract.totalSupply();

market.badDebtMantissa = vTokenContract.badDebt();

Expand Down
19 changes: 6 additions & 13 deletions subgraphs/isolated-pools/src/operations/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
getExchangeRateBigDecimal,
valueOrNotAvailableIntIfReverted,
} from '../utilities';
import { getTokenPriceInUsd } from '../utilities';
import { getTokenPriceInCents } from '../utilities';
import { getOrCreateMarket } from './getOrCreate';
import {
getOrCreateAccount,
Expand Down Expand Up @@ -171,12 +171,12 @@ export const updateMarket = (
}
const marketContract = VToken.bind(vTokenAddress);

const tokenPriceUsd = getTokenPriceInUsd(
const tokenPriceCents = getTokenPriceInCents(
marketContract.comptroller(),
vTokenAddress,
market.underlyingDecimals,
);
market.underlyingPriceUsd = tokenPriceUsd.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 All @@ -205,12 +202,8 @@ export const updateMarket = (
marketContract.try_supplyRatePerBlock(),
);

market.treasuryTotalBorrowsMantissa = valueOrNotAvailableIntIfReverted(
marketContract.try_totalBorrows(),
);
market.treasuryTotalSupplyMantissa = valueOrNotAvailableIntIfReverted(
marketContract.try_totalSupply(),
);
market.totalBorrowsMantissa = valueOrNotAvailableIntIfReverted(marketContract.try_totalBorrows());
market.totalSupplyMantissa = valueOrNotAvailableIntIfReverted(marketContract.try_totalSupply());

market.save();
return market as Market;
Expand Down
11 changes: 11 additions & 0 deletions subgraphs/isolated-pools/src/utilities/exponentToBigInt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { BigInt } from '@graphprotocol/graph-ts';

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

export default exponentToBigInt;
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { Address, BigDecimal } from '@graphprotocol/graph-ts';
import { Address, BigInt } from '@graphprotocol/graph-ts';

import { PriceOracle } from '../../generated/templates/VToken/PriceOracle';
import { NOT_AVAILABLE_BIG_DECIMAL } from '../constants';
import { NOT_AVAILABLE_BIG_INT } from '../constants';
import { getPool } from '../operations/get';
import exponentToBigDecimal from '../utilities/exponentToBigDecimal';
import exponentToBigInt from './exponentToBigInt';
import valueOrNotAvailableIntIfReverted from './valueOrNotAvailableIntIfReverted';

// Used for all vBEP20 contracts
const getTokenPrice = (
const getTokenPriceInCents = (
poolAddress: Address,
eventAddress: Address,
underlyingDecimals: i32,
): BigDecimal => {
): BigInt => {
const pool = getPool(poolAddress);
// will return NOT_AVAILABLE if the price cannot be fetched
let underlyingPrice = NOT_AVAILABLE_BIG_DECIMAL;
let underlyingPrice = NOT_AVAILABLE_BIG_INT;
if (pool && pool.priceOracleAddress) {
const oracleAddress = Address.fromBytes(pool.priceOracleAddress);
/* PriceOracle2 is used from starting of Comptroller.
Expand All @@ -23,16 +23,16 @@ const getTokenPrice = (
* Note this returns the value without factoring in token decimals and wei, so we must divide
* the number by (bnbDecimals - tokenDecimals) and again by the mantissa.
*/
const mantissaDecimalFactor = exponentToBigDecimal(36 - underlyingDecimals);
const mantissaDecimalFactor = exponentToBigInt(36 - underlyingDecimals);
const priceOracle = PriceOracle.bind(oracleAddress);

const underlyingPriceBigInt = valueOrNotAvailableIntIfReverted(
priceOracle.try_getUnderlyingPrice(eventAddress),
);
underlyingPrice = underlyingPriceBigInt.toBigDecimal().div(mantissaDecimalFactor);
underlyingPrice = underlyingPriceBigInt.div(mantissaDecimalFactor);
}

return underlyingPrice;
};

export default getTokenPrice;
export default getTokenPriceInCents;
2 changes: 1 addition & 1 deletion subgraphs/isolated-pools/src/utilities/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { default as getExchangeRateBigDecimal } from './getExchangeRateBigDecimal';
export { default as getTokenPriceInUsd } from './getTokenPriceInUsd';
export { default as getTokenPriceInCents } from './getTokenPriceInCents';
export { default as exponentToBigDecimal } from './exponentToBigDecimal';
export { default as valueOrNotAvailableIntIfReverted } from './valueOrNotAvailableIntIfReverted';
6 changes: 3 additions & 3 deletions subgraphs/isolated-pools/tests/VToken/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -477,12 +477,12 @@ describe('VToken', () => {

assertMarketDocument('accrualBlockNumber', '999');
assertMarketDocument('blockTimestamp', accrueInterestEvent.block.timestamp.toString());
assertMarketDocument('treasuryTotalSupplyMantissa', '36504567163409');
assertMarketDocument('totalSupplyMantissa', '36504567163409');
assertMarketDocument('exchangeRateMantissa', '365045823500000000000000');
assertMarketDocument('borrowIndexMantissa', '300000000000000000000');
assertMarketDocument('reservesMantissa', '5128924555022289393');
assertMarketDocument('treasuryTotalBorrowsMantissa', '2641234234636158123');
assertMarketDocument('cash', '1.418171344423412457');
assertMarketDocument('totalBorrowsMantissa', '2641234234636158123');
assertMarketDocument('cashMantissa', '1418171344423412457');
assertMarketDocument('borrowRateMantissa', '12678493');
assertMarketDocument('supplyRateMantissa', '12678493');
});
Expand Down
6 changes: 3 additions & 3 deletions subgraphs/isolated-pools/tests/integration/pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('Pools', function () {
'0xe7f1725e7734ce288f8367e1bb143e90bb3f0512',
];
const underlyingSymbols = ['BSW', 'BNX'];
const underlyingPricesUSD = ['0.208', '159.99'];
const underlyingPricesCents = ['208', '15999'];
const interestRateModelAddresses = [
'0xbf5a316f4303e13ae92c56d2d8c9f7629bef5c6e',
'0xb955b6c65ff69bfe07a557aa385055282b8a5ea3',
Expand Down 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 All @@ -152,7 +152,7 @@ describe('Pools', function () {
expect(m.blockTimestamp).to.not.be.equal('0');
expect(m.borrowIndexMantissa).to.equal('1000000000000000000');
expect(m.reserveFactorMantissa).to.equal('0');
expect(m.underlyingPriceUsd).to.equal(underlyingPricesUSD[idx]);
expect(m.underlyingPriceCents).to.equal(underlyingPricesCents[idx]);
expect(m.underlyingDecimals).to.equal(18);
});
});
Expand Down
Loading
Loading