Skip to content

Commit

Permalink
refactor: add initialization handler
Browse files Browse the repository at this point in the history
  • Loading branch information
coreyar committed Sep 24, 2024
1 parent 0d9f7dc commit ba3f46d
Show file tree
Hide file tree
Showing 12 changed files with 80 additions and 55 deletions.
25 changes: 19 additions & 6 deletions subgraphs/venus/src/mappings/comptroller.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/* eslint-disable prefer-const */
// to satisfy AS compiler
import { Address } from '@graphprotocol/graph-ts';
import { Address, ethereum } from '@graphprotocol/graph-ts';

import { Comptroller as ComptrollerContract } from '../../generated/Comptroller/Comptroller';
import {
DistributedSupplierVenus,
MarketEntered,
Expand All @@ -12,15 +13,27 @@ import {
NewLiquidationIncentive,
NewPriceOracle,
} from '../../generated/DiamondComptroller/Comptroller';
import { Comptroller } from '../../generated/schema';
import { zeroBigInt32 } from '../constants';
import { getMarket } from '../operations/get';
import { comptrollerAddress } from '../constants/addresses';
import { getComptroller, getMarket } from '../operations/get';
import {
getOrCreateAccount,
getOrCreateAccountVToken,
getOrCreateComptroller,
getOrCreateMarket,
} from '../operations/getOrCreate';

// eslint-disable-next-line @typescript-eslint/no-unused-vars
export const handleInitialization = (block: ethereum.Block): void => {
const comptroller = new Comptroller(comptrollerAddress);
const comptrollerContract = ComptrollerContract.bind(comptrollerAddress);
comptroller.priceOracle = comptrollerContract.oracle();
comptroller.closeFactor = comptrollerContract.closeFactorMantissa();
comptroller.liquidationIncentive = comptrollerContract.liquidationIncentiveMantissa();
comptroller.maxAssets = comptrollerContract.maxAssets();
comptroller.save();
};

export function handleMarketListed(event: MarketListed): void {
// Create the market for initial listing
const market = getOrCreateMarket(event.params.vToken, event);
Expand Down Expand Up @@ -56,7 +69,7 @@ export function handleMarketExited(event: MarketExited): void {
}

export function handleNewCloseFactor(event: NewCloseFactor): void {
const comptroller = getOrCreateComptroller();
const comptroller = getComptroller();
comptroller.closeFactor = event.params.newCloseFactorMantissa;
comptroller.save();
}
Expand All @@ -69,13 +82,13 @@ export function handleNewCollateralFactor(event: NewCollateralFactor): void {

// This should be the first event according to bscscan but it isn't.... price oracle is. weird
export function handleNewLiquidationIncentive(event: NewLiquidationIncentive): void {
const comptroller = getOrCreateComptroller();
const comptroller = getComptroller();
comptroller.liquidationIncentive = event.params.newLiquidationIncentiveMantissa;
comptroller.save();
}

export function handleNewPriceOracle(event: NewPriceOracle): void {
const comptroller = getOrCreateComptroller();
const comptroller = getComptroller();
comptroller.priceOracle = event.params.newPriceOracle;
comptroller.save();
}
Expand Down
Empty file.
8 changes: 7 additions & 1 deletion subgraphs/venus/src/operations/get.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { Address, log } from '@graphprotocol/graph-ts';

import { Market } from '../../generated/schema';
import { Comptroller, Market } from '../../generated/schema';
import { comptrollerAddress } from '../constants/addresses';

export function getComptroller(): Comptroller {
const comptroller = Comptroller.load(comptrollerAddress)!;
return comptroller;
}

export const getMarket = (vTokenAddress: Address): Market | null => {
const market = Market.load(vTokenAddress);
Expand Down
25 changes: 3 additions & 22 deletions subgraphs/venus/src/operations/getOrCreate.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { Address, BigInt, Bytes, ethereum, log } from '@graphprotocol/graph-ts';
import { Address, BigInt, Bytes, ethereum } from '@graphprotocol/graph-ts';

import { Comptroller as ComptrollerContract } from '../../generated/Comptroller/Comptroller';
import { Account, AccountVToken, Comptroller, Market } from '../../generated/schema';
import { Account, AccountVToken, Market } from '../../generated/schema';
import {
VToken as VTokenTemplate,
VTokenUpdatedEvents as VTokenUpdatedEventsTemplate,
} from '../../generated/templates';
import { BEP20 } from '../../generated/templates/VToken/BEP20';
import { VToken } from '../../generated/templates/VToken/VToken';
import { zeroBigInt32 } from '../constants';
import { comptrollerAddress, nullAddress } from '../constants/addresses';
import { nullAddress } from '../constants/addresses';
import {
getUnderlyingPrice,
valueOrNotAvailableAddressIfReverted,
Expand All @@ -20,22 +19,7 @@ import { getMarket } from './get';
import { updateMarketCashMantissa } from './updateMarketCashMantissa';
import { updateMarketRates } from './updateMarketRates';

export function getOrCreateComptroller(): Comptroller {
let comptroller = Comptroller.load(comptrollerAddress);
if (!comptroller) {
comptroller = new Comptroller(comptrollerAddress);
const comptrollerContract = ComptrollerContract.bind(comptrollerAddress);
comptroller.priceOracle = comptrollerContract.oracle();
comptroller.closeFactor = comptrollerContract.closeFactorMantissa();
comptroller.liquidationIncentive = comptrollerContract.liquidationIncentiveMantissa();
comptroller.maxAssets = comptrollerContract.maxAssets();
comptroller.save();
}
return comptroller;
}

export function getOrCreateMarket(marketAddress: Address, event: ethereum.Event): Market {
// @todo add and use market id utility
let market = getMarket(marketAddress);
if (!market) {
const vTokenContract = VToken.bind(marketAddress);
Expand All @@ -58,9 +42,6 @@ export function getOrCreateMarket(marketAddress: Address, event: ethereum.Event)
market.underlyingSymbol = 'BNB';
} else {
market.underlyingAddress = vTokenContract.underlying();
log.debug('[createMarket] market underlying address: {}', [
market.underlyingAddress.toHexString(),
]);
const underlyingContract = BEP20.bind(Address.fromBytes(market.underlyingAddress));
market.underlyingDecimals = underlyingContract.decimals();
market.underlyingName = underlyingContract.name();
Expand Down
4 changes: 2 additions & 2 deletions subgraphs/venus/src/utilities/getTokenPriceCents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { Address, BigInt, log } from '@graphprotocol/graph-ts';

import { valueOrNotAvailableIntIfReverted } from '.';
import { PriceOracle } from '../../generated/templates/VToken/PriceOracle';
import { getOrCreateComptroller } from '../operations/getOrCreate';
import { getComptroller } from '../operations/get';
import { exponentToBigInt } from './exponentToBigInt';

// Used for all vBEP20 contracts
export function getTokenPriceCents(eventAddress: Address, underlyingDecimals: i32): BigInt {
const comptroller = getOrCreateComptroller();
const comptroller = getComptroller();
if (!comptroller.priceOracle) {
log.debug('[getTokenPrice] empty price oracle: {}', ['0']);
return BigInt.zero();
Expand Down
2 changes: 2 additions & 0 deletions subgraphs/venus/src/utilities/ids.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ export const getAccountVTokenTransactionId = (

export const getMarketActionId = (vTokenAddress: Address, action: i32): Bytes =>
vTokenAddress.concatI32(action);

export const getMarketId = (vTokenAddress: Address): Bytes => vTokenAddress;
14 changes: 9 additions & 5 deletions subgraphs/venus/template.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
specVersion: 0.0.4
specVersion: 1.2.0
description: Venus is an open-source protocol for algorithmic, efficient Money Markets on the BSC blockchain.
repository: https://github.com/VenusProtocol/subgraphs
schema:
Expand All @@ -13,7 +13,7 @@ dataSources:
startBlock: {{ startBlock }}
mapping:
kind: ethereum/events
apiVersion: 0.0.5
apiVersion: 0.0.9
language: wasm/assemblyscript
file: ./src/mappings/comptroller.ts
entities:
Expand Down Expand Up @@ -58,7 +58,7 @@ dataSources:
startBlock: {{ startBlock }}
mapping:
kind: ethereum/events
apiVersion: 0.0.5
apiVersion: 0.0.9
language: wasm/assemblyscript
file: ./src/mappings/comptroller.ts
entities:
Expand All @@ -82,6 +82,10 @@ dataSources:
handler: handleMarketEntered
- event: MarketExited(address,address)
handler: handleMarketExited
blockHandlers:
- handler: handleInitialization
filter:
kind: once
templates:
- name: VToken
kind: ethereum/contract
Expand All @@ -90,7 +94,7 @@ templates:
abi: VToken
mapping:
kind: ethereum/events
apiVersion: 0.0.5
apiVersion: 0.0.9
language: wasm/assemblyscript
file: ./src/mappings/vToken.ts
entities:
Expand Down Expand Up @@ -138,7 +142,7 @@ templates:
abi: VToken
mapping:
kind: ethereum/events
apiVersion: 0.0.5
apiVersion: 0.0.9
language: wasm/assemblyscript
file: ./src/mappings/vToken.ts
entities:
Expand Down
9 changes: 2 additions & 7 deletions subgraphs/venus/tests/Comptroller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,7 @@ import {
} from 'matchstick-as/assembly/index';

import { handleMarketListed, handleMarketUnlisted } from '../src/mappings/comptroller';
import {
comptrollerAddress,
interestRateModelAddress,
nullAddress,
vBnbAddress,
} from './constants';
import { interestRateModelAddress, nullAddress, vBnbAddress } from './constants';
import { createMarketListedEvent } from './events';
import { createComptrollerMock, createVBep20AndUnderlyingMock } from './mocks';

Expand All @@ -38,7 +33,7 @@ beforeAll(() => {
interestRateModelAddress,
);

createComptrollerMock(comptrollerAddress);
createComptrollerMock();
});

describe('handleMarketListing', () => {
Expand Down
15 changes: 11 additions & 4 deletions subgraphs/venus/tests/VToken/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import {
} from 'matchstick-as/assembly/index';

import { oneBigInt, zeroBigInt32 } from '../../src/constants';
import { comptrollerAddress } from '../../src/constants/addresses';
import { handleMarketListed } from '../../src/mappings/comptroller';
import { handleInitialization } from '../../src/mappings/comptroller';
import {
handleAccrueInterest,
handleBorrow,
Expand All @@ -32,6 +32,7 @@ import {
} from '../../src/mappings/vToken';
import { getMarket } from '../../src/operations/get';
import { getAccountVTokenId } from '../../src/utilities/ids';
import { createComptrollerMock } from '../mocks';
import {
createAccrueInterestEvent,
createBorrowEvent,
Expand All @@ -50,8 +51,13 @@ import {
createReservesReducedEvent,
createTransferEvent,
} from './events';
import { createAccountVTokenBalanceOfMock, createBorrowBalanceCurrentMock } from './mocks';
import { createPriceOracleMock, createVBep20AndUnderlyingMock } from './mocks';
import {
createAccountVTokenBalanceOfMock,
createBorrowBalanceCurrentMock,
createMockBlock,
createPriceOracleMock,
createVBep20AndUnderlyingMock,
} from './mocks';

const tokenAddress = Address.fromString('0x0000000000000000000000000000000000000b0b');
const user1Address = Address.fromString('0x0000000000000000000000000000000000000101');
Expand All @@ -69,10 +75,10 @@ const cleanup = (): void => {
};

beforeAll(() => {
createComptrollerMock();
createVBep20AndUnderlyingMock(
aaaTokenAddress,
tokenAddress,
comptrollerAddress,
'AAA Coin',
'AAA',
BigInt.fromI32(18),
Expand All @@ -90,6 +96,7 @@ beforeAll(() => {
});

beforeEach(() => {
handleInitialization(createMockBlock());
// Add Market
const marketAddedEvent = createMarketListedEvent(aaaTokenAddress);

Expand Down
25 changes: 23 additions & 2 deletions subgraphs/venus/tests/VToken/mocks.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,35 @@
import { Address, BigInt, ethereum } from '@graphprotocol/graph-ts';
import { Address, BigInt, Bytes, ethereum } from '@graphprotocol/graph-ts';
import { createMockedFunction } from 'matchstick-as';

import { comptrollerAddress } from '../../src/constants/addresses';

export const mockPriceOracleAddress = Address.fromString(
'0xb0b0000000000000000000000000000000000000',
);

export const createMockBlock = (): ethereum.Block => {
return new ethereum.Block(
Bytes.fromHexString('0x'),
Bytes.fromHexString('0x'),
Bytes.fromHexString('0x'),
comptrollerAddress,
Bytes.fromHexString('0x'),
Bytes.fromHexString('0x'),
Bytes.fromHexString('0x'),
BigInt.fromI32(0),
BigInt.fromI32(0),
BigInt.fromI32(0),
BigInt.fromI32(0),
BigInt.fromI32(0),
BigInt.fromI32(0),
null,
null,
);
};

export const createVBep20AndUnderlyingMock = (
contractAddress: Address,
underlyingAddress: Address,
comptrollerAddress: Address,
name: string,
symbol: string,
decimals: BigInt,
Expand Down
1 change: 0 additions & 1 deletion subgraphs/venus/tests/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@ export const vBnbAddress = Address.fromString('0xA07c5b74C9B40447a954e1466938b86
export const interestRateModelAddress = Address.fromString(
'0x594942C0e62eC577889777424CD367545C796A74',
);
export const comptrollerAddress = Address.fromString('0xfd36e2c2a6789db23113685031d7f16329158384');
export const nullAddress = Address.fromString('0x0000000000000000000000000000000000000000');
7 changes: 2 additions & 5 deletions subgraphs/venus/tests/mocks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Address, BigInt, ethereum } from '@graphprotocol/graph-ts';
import { createMockedFunction } from 'matchstick-as';

import { comptrollerAddress } from '../src/constants/addresses';
import { vBnbAddress } from './constants';

export const mockPriceOracleAddress = Address.fromString(
Expand Down Expand Up @@ -98,11 +99,7 @@ export const createVBep20AndUnderlyingMock = (
]);
};

export const createComptrollerMock = (comptrollerAddress: Address): void => {
createMockedFunction(comptrollerAddress, 'oracle', 'oracle():(address)').returns([
ethereum.Value.fromAddress(mockPriceOracleAddress),
]);

export const createComptrollerMock = (): void => {
createMockedFunction(
comptrollerAddress,
'closeFactorMantissa',
Expand Down

0 comments on commit ba3f46d

Please sign in to comment.