Skip to content

Commit

Permalink
refactor(core-pool): dont mix math and balance calls for the same value
Browse files Browse the repository at this point in the history
  • Loading branch information
coreyar committed Oct 24, 2024
1 parent 3dd03f2 commit 59fec43
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 72 deletions.
80 changes: 38 additions & 42 deletions subgraphs/venus/src/mappings/vToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,6 @@ export function handleLiquidateBorrow(event: LiquidateBorrow): void {
// the underwater borrower. So we must get that address from the event, and
// the repay token is the event.address
createLiquidationEvent<LiquidateBorrow>(event);
const collateralMarket = getOrCreateMarket(event.params.vTokenCollateral, event);
const borrowMarket = getOrCreateMarket(event.address, event);

const borrowedVTokenContract = VToken.bind(event.address);
const collateralContract = VToken.bind(event.params.vTokenCollateral);
Expand All @@ -299,30 +297,19 @@ export function handleLiquidateBorrow(event: LiquidateBorrow): void {
);
const borrowerCollateralAccountVToken = borrowerSupplyAccountVTokenResult.entity;

borrowerCollateralAccountVToken.vTokenBalanceMantissa =
borrowerCollateralAccountVToken.vTokenBalanceMantissa.minus(event.params.seizeTokens);
borrowerCollateralAccountVToken.save();

const collateralBalance = collateralContract.balanceOf(event.params.borrower);
// Check if borrower is still supplying liquidated asset
if (collateralBalance.equals(zeroBigInt32)) {
collateralMarket.supplierCount = borrowMarket.supplierCount.minus(oneBigInt);
collateralMarket.save();
}
borrowerCollateralAccountVToken.vTokenBalanceMantissa = collateralBalance;
borrowerCollateralAccountVToken.save();

// Check if liquidator is new supplier
const resultLiquidatorAccountVToken = getOrCreateAccountVToken(
event.params.vTokenCollateral,
event.params.liquidator,
);
const liquidatorAccountVToken = resultLiquidatorAccountVToken.entity;
liquidatorAccountVToken.vTokenBalanceMantissa.plus(event.params.seizeTokens);
liquidatorAccountVToken.vTokenBalanceMantissa = collateralContract.balanceOf(
event.params.liquidator,
);
liquidatorAccountVToken.save();

if (liquidatorAccountVToken.vTokenBalanceMantissa.equals(event.params.seizeTokens)) {
collateralMarket.supplierCount = collateralMarket.supplierCount.plus(oneBigInt);
collateralMarket.save();
}
}

/* Transferring of vTokens
Expand All @@ -340,6 +327,7 @@ export function handleTransfer(event: Transfer): void {
// with normal transfers, since mint, redeem, and seize transfers will already run updateMarket()
let accountFromAddress = event.params.from;
let accountToAddress = event.params.to;
const vTokenContract = VToken.bind(event.address);
// Checking if the event is FROM the vToken contract or null (i.e. this will not run when minting)
// Checking if the event is TO the vToken contract (i.e. this will not run when redeeming)
// @TODO Edge case where someone who accidentally sends vTokens to a vToken contract, where it will not get recorded.
Expand All @@ -351,20 +339,30 @@ export function handleTransfer(event: Transfer): void {
getOrCreateAccount(accountFromAddress);
const resultFrom = getOrCreateAccountVToken(event.address, accountFromAddress);
const accountFromVToken = resultFrom.entity;

// Creation updates balance
accountFromVToken.vTokenBalanceMantissa = accountFromVToken.vTokenBalanceMantissa.minus(
event.params.amount,
);
const fromBalance = vTokenContract.balanceOf(accountFromAddress);
accountFromVToken.vTokenBalanceMantissa = fromBalance;
accountFromVToken.save();

// Decrease if no longer minter
if (fromBalance.equals(zeroBigInt32)) {
const market = getMarket(event.address)!;
market.supplierCount = market.supplierCount.minus(oneBigInt);
market.save();
}

getOrCreateAccount(accountToAddress);
const resultTo = getOrCreateAccountVToken(event.address, accountToAddress);
const accountToVToken = resultTo.entity;

accountToVToken.vTokenBalanceMantissa = accountToVToken.vTokenBalanceMantissa.plus(
event.params.amount,
);
const toBalance = vTokenContract.balanceOf(accountToAddress);
accountToVToken.vTokenBalanceMantissa = toBalance;
accountToVToken.save();

// Increase balance if now minter
if (toBalance.equals(event.params.amount)) {
const market = getMarket(event.address)!;
market.supplierCount = market.supplierCount.plus(oneBigInt);
market.save();
}
}
createTransferEvent<Transfer>(event);
const market = getMarket(event.address)!;
Expand Down Expand Up @@ -427,9 +425,6 @@ export function handleMintV1(event: MintV1): void {
const result = getOrCreateAccountVToken(marketAddress, event.params.minter);
const accountVToken = result.entity;

if (event.params.mintTokens.equals(accountVToken.vTokenBalanceMantissa)) {
market.supplierCount = market.supplierCount.plus(oneBigInt);
}
// we'll first update the cash value of the market and then the rates, since they depend on it
updateMarketCashMantissa(market, vTokenContract);

Expand All @@ -443,10 +438,13 @@ export function handleMintV1(event: MintV1): void {
getOrCreateAccount(event.params.minter);

// Creation updates balance
accountVToken.vTokenBalanceMantissa = accountVToken.vTokenBalanceMantissa.plus(
event.params.mintTokens,
);
accountVToken.vTokenBalanceMantissa = vTokenContract.balanceOf(event.params.minter);
accountVToken.save();

if (event.params.mintTokens.equals(accountVToken.vTokenBalanceMantissa)) {
market.supplierCount = market.supplierCount.plus(oneBigInt);
market.save();
}
}

export function handleMintBehalfV1(event: MintBehalfV1): void {
Expand All @@ -459,9 +457,6 @@ export function handleMintBehalfV1(event: MintBehalfV1): void {
const result = getOrCreateAccountVToken(marketAddress, event.params.receiver);
const accountVToken = result.entity;

if (event.params.mintTokens.equals(accountVToken.vTokenBalanceMantissa)) {
market.supplierCount = market.supplierCount.plus(oneBigInt);
}
// we'll first update the cash value of the market
updateMarketCashMantissa(market, vTokenContract);

Expand All @@ -475,10 +470,13 @@ export function handleMintBehalfV1(event: MintBehalfV1): void {
getOrCreateAccount(event.params.receiver);

// Creation updates balance
accountVToken.vTokenBalanceMantissa = accountVToken.vTokenBalanceMantissa.plus(
event.params.mintTokens,
);
accountVToken.vTokenBalanceMantissa = vTokenContract.balanceOf(event.params.receiver);
accountVToken.save();

if (event.params.mintTokens.equals(accountVToken.vTokenBalanceMantissa)) {
market.supplierCount = market.supplierCount.plus(oneBigInt);
market.save();
}
}

export function handleRedeemV1(event: RedeemV1): void {
Expand All @@ -503,9 +501,7 @@ export function handleRedeemV1(event: RedeemV1): void {
const result = getOrCreateAccountVToken(marketAddress, event.params.redeemer);
const accountVToken = result.entity;

accountVToken.vTokenBalanceMantissa = accountVToken.vTokenBalanceMantissa.minus(
event.params.redeemTokens,
);
accountVToken.vTokenBalanceMantissa = vTokenContract.balanceOf(event.params.redeemer);
accountVToken.totalUnderlyingRedeemedMantissa =
accountVToken.totalUnderlyingRedeemedMantissa.plus(event.params.redeemAmount);
accountVToken.save();
Expand Down
42 changes: 12 additions & 30 deletions subgraphs/venus/tests/VToken/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,18 +382,9 @@ describe('VToken', () => {

/** Setup test */
const transferEvent = createTransferEvent(aaaTokenAddress, from, to, amount);
createMockedFunction(
aaaTokenAddress,
'getAccountSnapshot',
'getAccountSnapshot(address):(uint256,uint256,uint256,uint256)',
)
.withArgs([ethereum.Value.fromAddress(from)])
.returns([
ethereum.Value.fromSignedBigInt(zeroBigInt32),
ethereum.Value.fromSignedBigInt(balanceOf),
ethereum.Value.fromSignedBigInt(zeroBigInt32),
ethereum.Value.fromSignedBigInt(oneBigInt),
]);

createAccountVTokenBalanceOfMock(aaaTokenAddress, from, balanceOf.minus(amount));
createAccountVTokenBalanceOfMock(aaaTokenAddress, to, balanceOf.plus(amount));

const accountVTokenId = getAccountVTokenId(aaaTokenAddress, from).toHexString();
/** AccountVToken */
Expand All @@ -412,7 +403,7 @@ describe('VToken', () => {
'AccountVToken',
accountVTokenId,
'vTokenBalanceMantissa',
mintTokens.minus(amount).toString(),
balanceOf.minus(amount).toString(),
);
});

Expand All @@ -425,19 +416,9 @@ describe('VToken', () => {

/** Setup test */
const transferEvent = createTransferEvent(aaaTokenAddress, from, to, amount);
createAccountVTokenBalanceOfMock(aaaTokenAddress, to, amount.plus(balanceOf));
createMockedFunction(
aaaTokenAddress,
'getAccountSnapshot',
'getAccountSnapshot(address):(uint256,uint256,uint256,uint256)',
)
.withArgs([ethereum.Value.fromAddress(to)])
.returns([
ethereum.Value.fromSignedBigInt(zeroBigInt32),
ethereum.Value.fromSignedBigInt(balanceOf),
ethereum.Value.fromSignedBigInt(zeroBigInt32),
ethereum.Value.fromSignedBigInt(oneBigInt),
]);

createAccountVTokenBalanceOfMock(aaaTokenAddress, from, balanceOf.minus(amount));
createAccountVTokenBalanceOfMock(aaaTokenAddress, to, amount);

/** Fire Event */
handleTransfer(transferEvent);
Expand All @@ -449,8 +430,9 @@ describe('VToken', () => {
'AccountVToken',
accountVTokenId,
'vTokenBalanceMantissa',
amount.plus(balanceOf).toString(),
amount.toString(),
);
assert.fieldEquals('Market', aaaTokenAddress.toHexString(), 'supplierCount', '1');
});

test('registers new interest rate model', () => {
Expand Down Expand Up @@ -484,7 +466,6 @@ describe('VToken', () => {
const actualMintAmount = BigInt.fromI64(12);
const halfActualMintAmount = actualMintAmount.div(BigInt.fromI64(2));
const mintTokens = BigInt.fromI64(10);
const accountBalance = mintTokens;
const halfMintTokens = mintTokens.div(BigInt.fromI64(2));

const supplier01 = user1Address;
Expand All @@ -493,7 +474,7 @@ describe('VToken', () => {
supplier01,
actualMintAmount,
mintTokens,
accountBalance,
mintTokens,
);

handleMint(mintEvent);
Expand All @@ -507,6 +488,7 @@ describe('VToken', () => {
actualMintAmount,
mintTokens,
);

createAccountVTokenBalanceOfMock(aaaTokenAddress, supplier02, mintTokens);

handleMintV1(mintEventV1);
Expand All @@ -519,7 +501,7 @@ describe('VToken', () => {
supplier03,
actualMintAmount,
mintTokens,
accountBalance,
mintTokens,
);
createAccountVTokenBalanceOfMock(aaaTokenAddress, supplier03, mintTokens);

Expand Down

0 comments on commit 59fec43

Please sign in to comment.