Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: handle supply event
Browse files Browse the repository at this point in the history
coreyar committed Sep 19, 2023
1 parent 73519a2 commit f523748
Showing 10 changed files with 139 additions and 51 deletions.
2 changes: 0 additions & 2 deletions subgraphs/isolated-pools/schema.graphql
Original file line number Diff line number Diff line change
@@ -167,8 +167,6 @@ type AccountVToken @entity {
id: ID!
"Relation to market"
market: Market!
"Symbol of the vToken"
symbol: String!
"Relation to user"
account: Account!
"Transactions data"
19 changes: 2 additions & 17 deletions subgraphs/isolated-pools/src/mappings/pool.ts
Original file line number Diff line number Diff line change
@@ -27,20 +27,12 @@ import {
import Box from '../utilities/box';

export function handleMarketEntered(event: MarketEntered): void {
const poolAddress = event.address;
const vTokenAddress = event.params.vToken;
const accountAddress = event.params.account;

const market = getOrCreateMarket(vTokenAddress, poolAddress);
getOrCreateAccount(accountAddress);

updateOrCreateAccountVToken(
accountAddress,
vTokenAddress,
market.symbol,
event.block.number,
new Box(true),
);
updateOrCreateAccountVToken(accountAddress, vTokenAddress, event.block.number, new Box(true));
getOrCreateAccountVTokenTransaction(
accountAddress,
event.transaction.hash,
@@ -55,16 +47,9 @@ export function handleMarketExited(event: MarketExited): void {
const vTokenAddress = event.params.vToken;
const accountAddress = event.params.account;

const market = getOrCreateMarket(vTokenAddress);
getOrCreateAccount(accountAddress);

updateOrCreateAccountVToken(
accountAddress,
vTokenAddress,
market.symbol,
event.block.number,
new Box(false),
);
updateOrCreateAccountVToken(accountAddress, vTokenAddress, event.block.number, new Box(false));
getOrCreateAccountVTokenTransaction(
accountAddress,
event.transaction.hash,
23 changes: 19 additions & 4 deletions subgraphs/isolated-pools/src/mappings/vToken.ts
Original file line number Diff line number Diff line change
@@ -28,6 +28,7 @@ import { getOrCreateAccount, getOrCreateMarket } from '../operations/getOrCreate
import {
updateAccountVTokenBorrow,
updateAccountVTokenRepayBorrow,
updateAccountVTokenSupply,
updateAccountVTokenTransferFrom,
updateAccountVTokenTransferTo,
updateMarket,
@@ -54,6 +55,15 @@ export function handleMint(event: Mint): void {

// we read the current total amount of supplied tokens by this account in the market
const suppliedTotal = event.params.accountBalance;
updateAccountVTokenSupply(
vTokenAddress,
event.params.minter,
event.transaction.hash,
event.block.timestamp,
event.block.number,
event.logIndex,
suppliedTotal,
);
if (suppliedTotal == event.params.mintTokens) {
// and if they are the same, it means it's a new supplier
market.supplierCount = market.supplierCount.plus(oneBigInt);
@@ -80,6 +90,15 @@ export function handleRedeem(event: Redeem): void {

// we read the account's balance and...
const currentBalance = event.params.accountBalance;
updateAccountVTokenSupply(
vTokenAddress,
event.params.redeemer,
event.transaction.hash,
event.block.timestamp,
event.block.number,
event.logIndex,
currentBalance,
);
if (currentBalance == zeroBigInt32) {
// if the current balance is 0 then the user has withdrawn all their assets from this market
market.supplierCount = market.supplierCount.minus(oneBigInt);
@@ -102,7 +121,6 @@ export function handleBorrow(event: Borrow): void {

updateAccountVTokenBorrow(
vTokenAddress,
market.symbol,
event.params.borrower,
event.transaction.hash,
event.block.timestamp,
@@ -141,7 +159,6 @@ export function handleRepayBorrow(event: RepayBorrow): void {

updateAccountVTokenRepayBorrow(
vTokenAddress,
market.symbol,
event.params.borrower,
event.transaction.hash,
event.block.timestamp,
@@ -234,7 +251,6 @@ export function handleTransfer(event: Transfer): void {

updateAccountVTokenTransferFrom(
vTokenAddress,
market.symbol,
accountFromAddress,
event.transaction.hash,
event.block.timestamp,
@@ -256,7 +272,6 @@ export function handleTransfer(event: Transfer): void {

updateAccountVTokenTransferTo(
vTokenAddress,
market.symbol,
accountToAddress,
event.transaction.hash,
event.block.timestamp,
2 changes: 0 additions & 2 deletions subgraphs/isolated-pools/src/operations/getOrCreate.ts
Original file line number Diff line number Diff line change
@@ -86,7 +86,6 @@ export const getOrCreateAccountVTokenTransaction = (
};

export const getOrCreateAccountVToken = (
marketSymbol: string,
accountAddress: Address,
marketAddress: Address,
enteredMarket: boolean = false, // eslint-disable-line @typescript-eslint/no-inferrable-types
@@ -95,7 +94,6 @@ export const getOrCreateAccountVToken = (
let accountVToken = AccountVToken.load(accountVTokenId);
if (!accountVToken) {
accountVToken = new AccountVToken(accountVTokenId);
accountVToken.symbol = marketSymbol;
accountVToken.account = accountAddress.toHexString();
accountVToken.market = marketAddress.toHexString();
accountVToken.enteredMarket = enteredMarket;
38 changes: 23 additions & 15 deletions subgraphs/isolated-pools/src/operations/update.ts
Original file line number Diff line number Diff line change
@@ -19,20 +19,14 @@ import { getOrCreatePool } from './getOrCreate';

const updateAccountVToken = (
marketAddress: Address,
marketSymbol: string,
accountAddress: Address,
txHash: Bytes,
timestamp: BigInt,
blockNumber: BigInt,
logIndex: BigInt,
): AccountVToken => {
getOrCreateAccount(accountAddress);
const accountVToken = getOrCreateAccountVToken(
marketSymbol,
accountAddress,
marketAddress,
false,
);
const accountVToken = getOrCreateAccountVToken(accountAddress, marketAddress, false);
getOrCreateAccountVTokenTransaction(
accountAddress,
txHash,
@@ -45,9 +39,30 @@ const updateAccountVToken = (
return accountVToken as AccountVToken;
};

export const updateAccountVTokenSupply = (
marketAddress: Address,
accountAddress: Address,
txHash: Bytes,
timestamp: BigInt,
blockNumber: BigInt,
logIndex: BigInt,
accountSupplyBalanceMantissa: BigInt,
): AccountVToken => {
const accountVToken = updateAccountVToken(
marketAddress,
accountAddress,
txHash,
timestamp,
blockNumber,
logIndex,
);
accountVToken.accountSupplyBalanceMantissa = accountSupplyBalanceMantissa;
accountVToken.save();
return accountVToken as AccountVToken;
};

export const updateAccountVTokenBorrow = (
marketAddress: Address,
marketSymbol: string,
accountAddress: Address,
txHash: Bytes,
timestamp: BigInt,
@@ -58,7 +73,6 @@ export const updateAccountVTokenBorrow = (
): AccountVToken => {
const accountVToken = updateAccountVToken(
marketAddress,
marketSymbol,
accountAddress,
txHash,
timestamp,
@@ -73,7 +87,6 @@ export const updateAccountVTokenBorrow = (

export const updateAccountVTokenRepayBorrow = (
marketAddress: Address,
marketSymbol: string,
accountAddress: Address,
txHash: Bytes,
timestamp: BigInt,
@@ -84,7 +97,6 @@ export const updateAccountVTokenRepayBorrow = (
): AccountVToken => {
const accountVToken = updateAccountVToken(
marketAddress,
marketSymbol,
accountAddress,
txHash,
timestamp,
@@ -99,7 +111,6 @@ export const updateAccountVTokenRepayBorrow = (

export const updateAccountVTokenTransferFrom = (
marketAddress: Address,
marketSymbol: string,
accountAddress: Address,
txHash: Bytes,
timestamp: BigInt,
@@ -121,7 +132,6 @@ export const updateAccountVTokenTransferFrom = (

const accountVToken = updateAccountVToken(
marketAddress,
marketSymbol,
accountAddress,
txHash,
timestamp,
@@ -137,7 +147,6 @@ export const updateAccountVTokenTransferFrom = (

export const updateAccountVTokenTransferTo = (
marketAddress: Address,
marketSymbol: string,
accountAddress: Address,
txHash: Bytes,
timestamp: BigInt,
@@ -146,7 +155,6 @@ export const updateAccountVTokenTransferTo = (
): AccountVToken => {
const accountVToken = updateAccountVToken(
marketAddress,
marketSymbol,
accountAddress,
txHash,
timestamp,
8 changes: 1 addition & 7 deletions subgraphs/isolated-pools/src/operations/updateOrCreate.ts
Original file line number Diff line number Diff line change
@@ -9,7 +9,6 @@ import { getOrCreateAccountVToken } from './getOrCreate';
export const updateOrCreateAccountVToken = (
accountAddress: Address,
marketAddress: Address,
marketSymbol: string,
blockNumber: BigInt,
enteredMarket: Box<boolean> | null = null,
): AccountVToken => {
@@ -18,12 +17,7 @@ export const updateOrCreateAccountVToken = (
enteredMarketBool = enteredMarket.value;
}

const accountVToken = getOrCreateAccountVToken(
marketSymbol,
accountAddress,
marketAddress,
enteredMarketBool,
);
const accountVToken = getOrCreateAccountVToken(accountAddress, marketAddress, enteredMarketBool);
accountVToken.enteredMarket = enteredMarketBool;
accountVToken.accrualBlockNumber = blockNumber;
accountVToken.save();
95 changes: 94 additions & 1 deletion subgraphs/isolated-pools/tests/VToken/index.test.ts
Original file line number Diff line number Diff line change
@@ -137,6 +137,19 @@ describe('VToken', () => {
mintTokens,
accountBalance,
);
createMockedFunction(
aaaTokenAddress,
'getAccountSnapshot',
'getAccountSnapshot(address):(uint256,uint256,uint256,uint256)',
)
.withArgs([ethereum.Value.fromAddress(minter)])
.returns([
ethereum.Value.fromSignedBigInt(zeroBigInt32),
ethereum.Value.fromSignedBigInt(mintTokens),
ethereum.Value.fromSignedBigInt(zeroBigInt32),
ethereum.Value.fromSignedBigInt(oneBigInt),
]);

const market = getMarket(aaaTokenAddress);
assert.assertNotNull(market);
if (!market) {
@@ -168,20 +181,66 @@ describe('VToken', () => {
.truncate(market.underlyingDecimals)
.toString(),
);
// AccountVToken
const accountVTokenId = getAccountVTokenId(aaaTokenAddress, minter);
assert.fieldEquals('AccountVToken', accountVTokenId, 'account', minter.toHexString());
assert.fieldEquals('AccountVToken', accountVTokenId, 'market', aaaTokenAddress.toHexString());
assert.fieldEquals(
'AccountVToken',
accountVTokenId,
'accrualBlockNumber',
oneBigInt.toString(),
);
assert.fieldEquals(
'AccountVToken',
accountVTokenId,
'accountSupplyBalanceMantissa',
accountBalance.toString(),
);
assert.fieldEquals(
'AccountVToken',
accountVTokenId,
'accountBorrowBalanceMantissa',
zeroBigInt32.toString(),
);
assert.fieldEquals(
'AccountVToken',
accountVTokenId,
'totalUnderlyingRedeemedMantissa',
zeroBigInt32.toString(),
);
assert.fieldEquals(
'AccountVToken',
accountVTokenId,
'accountBorrowIndexMantissa',
zeroBigInt32.toString(),
);
});

test('registers redeem event', () => {
const redeemer = user2Address;
const actualRedeemAmount = BigInt.fromString('124620530798726345');
const redeemTokens = BigInt.fromString('37035970026454');
const accountBalance = redeemTokens;
const accountBalance = zeroBigInt32;
const redeemEvent = createRedeemEvent(
aaaTokenAddress,
redeemer,
actualRedeemAmount,
redeemTokens,
accountBalance,
);
createMockedFunction(
aaaTokenAddress,
'getAccountSnapshot',
'getAccountSnapshot(address):(uint256,uint256,uint256,uint256)',
)
.withArgs([ethereum.Value.fromAddress(redeemer)])
.returns([
ethereum.Value.fromSignedBigInt(zeroBigInt32),
ethereum.Value.fromSignedBigInt(zeroBigInt32),
ethereum.Value.fromSignedBigInt(zeroBigInt32),
ethereum.Value.fromSignedBigInt(oneBigInt),
]);
const market = getMarket(aaaTokenAddress);
assert.assertNotNull(market);
if (!market) {
@@ -213,6 +272,40 @@ describe('VToken', () => {
.truncate(market.underlyingDecimals)
.toString(),
);
// AccountVToken
const accountVTokenId = getAccountVTokenId(aaaTokenAddress, redeemer);
assert.fieldEquals('AccountVToken', accountVTokenId, 'account', redeemer.toHexString());
assert.fieldEquals('AccountVToken', accountVTokenId, 'market', aaaTokenAddress.toHexString());
assert.fieldEquals(
'AccountVToken',
accountVTokenId,
'accrualBlockNumber',
oneBigInt.toString(),
);
assert.fieldEquals(
'AccountVToken',
accountVTokenId,
'accountSupplyBalanceMantissa',
zeroBigInt32.toString(),
);
assert.fieldEquals(
'AccountVToken',
accountVTokenId,
'accountBorrowBalanceMantissa',
zeroBigInt32.toString(),
);
assert.fieldEquals(
'AccountVToken',
accountVTokenId,
'totalUnderlyingRedeemedMantissa',
zeroBigInt32.toString(),
);
assert.fieldEquals(
'AccountVToken',
accountVTokenId,
'accountBorrowIndexMantissa',
zeroBigInt32.toString(),
);
});

test('registers borrow event', () => {
Loading

0 comments on commit f523748

Please sign in to comment.