Skip to content

Commit

Permalink
feat: Mint and Redeem events emit the updated account balance
Browse files Browse the repository at this point in the history
  • Loading branch information
gleiser-oliveira committed Jan 25, 2023
1 parent a6cdbf1 commit 1ec4989
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 23 deletions.
4 changes: 2 additions & 2 deletions contracts/VToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ contract VToken is Ownable2StepUpgradeable, VTokenInterface, ExponentialNoError,
accountTokens[minter] = accountTokens[minter] + mintTokens;

/* We emit a Mint event, and a Transfer event */
emit Mint(minter, actualMintAmount, mintTokens);
emit Mint(minter, actualMintAmount, mintTokens, accountTokens[minter]);
emit Transfer(address(0), minter, mintTokens);
}

Expand Down Expand Up @@ -662,7 +662,7 @@ contract VToken is Ownable2StepUpgradeable, VTokenInterface, ExponentialNoError,

/* We emit a Transfer event, and a Redeem event */
emit Transfer(redeemer, address(this), redeemTokens);
emit Redeem(redeemer, redeemAmount, redeemTokens);
emit Redeem(redeemer, redeemAmount, redeemTokens, accountTokens[redeemer]);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions contracts/VTokenInterfaces.sol
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,12 @@ abstract contract VTokenInterface is VTokenStorage {
/**
* @notice Event emitted when tokens are minted
*/
event Mint(address minter, uint256 mintAmount, uint256 mintTokens);
event Mint(address minter, uint256 mintAmount, uint256 mintTokens, uint256 accountBalance);

/**
* @notice Event emitted when tokens are redeemed
*/
event Redeem(address redeemer, uint256 redeemAmount, uint256 redeemTokens);
event Redeem(address redeemer, uint256 redeemAmount, uint256 redeemTokens, uint256 accountBalance);

/**
* @notice Event emitted when underlying is borrowed
Expand Down
2 changes: 1 addition & 1 deletion contracts/test/Mocks/MockPancakeSwap.sol
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ interface IPancakePair {
bytes32 s
) external;

event Mint(address indexed sender, uint256 amount0, uint256 amount1);
event Mint(address indexed sender, uint256 amount0, uint256 amount1, uint256 amount2);
event Burn(address indexed sender, uint256 amount0, uint256 amount1, address indexed to);
event Swap(
address indexed sender,
Expand Down
2 changes: 1 addition & 1 deletion tests/hardhat/Tokens/mintAndRedeemTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ describe("VToken", function () {

expect(result) // eslint-disable-line @typescript-eslint/no-floating-promises
.to.emit(vToken, "Mint")
.withArgs(minterAddress, mintAmount, mintTokens);
.withArgs(minterAddress, mintAmount, mintTokens, mintAmount);

expect(result) // eslint-disable-line @typescript-eslint/no-floating-promises
.to.emit(vToken, "Transfer")
Expand Down
40 changes: 23 additions & 17 deletions tests/integration/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ describe("Positive Cases", () => {
// //////////
await expect(vBNX.connect(acc2Signer).mint(mintAmount))
.to.emit(vBNX, "Mint")
.withArgs(acc2, mintAmount, mintAmount);
.withArgs(acc2, mintAmount, mintAmount, mintAmount);
[error, balance, borrowBalance] = await vBNX.connect(acc2Signer).getAccountSnapshot(acc2);
expect(error).to.equal(Error.NO_ERROR);
expect(balance).to.equal(mintAmount);
Expand All @@ -193,7 +193,7 @@ describe("Positive Cases", () => {
// Supply WBTC to market from 2nd account
await expect(vBSW.connect(acc1Signer).mint(mintAmount))
.to.emit(vBSW, "Mint")
.withArgs(await acc1Signer.getAddress(), mintAmount, mintAmount);
.withArgs(await acc1Signer.getAddress(), mintAmount, mintAmount, mintAmount);

[error, balance, borrowBalance] = await vBSW
.connect(acc2Signer)
Expand Down Expand Up @@ -223,7 +223,7 @@ describe("Positive Cases", () => {
const redeemAmount = 10e3;
await expect(vBNX.connect(acc2Signer).redeem(redeemAmount))
.to.emit(vBNX, "Redeem")
.withArgs(acc2, redeemAmount, redeemAmount);
.withArgs(acc2, redeemAmount, redeemAmount, mintAmount - redeemAmount);

[error, balance, borrowBalance] = await vBNX.connect(acc2Signer).getAccountSnapshot(acc2);
expect(error).to.equal(Error.NO_ERROR);
Expand Down Expand Up @@ -293,12 +293,12 @@ describe("Straight Cases For Single User Liquidation and healing", () => {

await expect(vBNX.connect(acc2Signer).mint(mintAmount))
.to.emit(vBNX, "Mint")
.withArgs(acc2, mintAmount, mintAmount);
.withArgs(acc2, mintAmount, mintAmount, mintAmount);
// borrow
// Supply WBTC to market from 2nd account
await expect(vBSW.connect(acc1Signer).mint(mintAmount))
.to.emit(vBSW, "Mint")
.withArgs(acc1, mintAmount, mintAmount);
.withArgs(acc1, mintAmount, mintAmount, mintAmount);
// It should revert when try to borrow more than liquidity
await expect(vBSW.connect(acc2Signer).borrow(8e10)).to.be.revertedWithCustomError(
Comptroller,
Expand Down Expand Up @@ -363,12 +363,12 @@ describe("Straight Cases For Single User Liquidation and healing", () => {

await expect(vBNX.connect(acc2Signer).mint(mintAmount))
.to.emit(vBNX, "Mint")
.withArgs(acc2, mintAmount, mintAmount);
.withArgs(acc2, mintAmount, mintAmount, mintAmount);
// borrow
// Supply WBTC to market from 2nd account
await expect(vBSW.connect(acc1Signer).mint(mintAmount))
.to.emit(vBSW, "Mint")
.withArgs(acc1, mintAmount, mintAmount);
.withArgs(acc1, mintAmount, mintAmount, mintAmount);
await expect(vBSW.connect(acc2Signer).borrow(bswBorrowAmount))
.to.emit(vBSW, "Borrow")
.withArgs(acc2, bswBorrowAmount, bswBorrowAmount, bswBorrowAmount);
Expand All @@ -387,12 +387,13 @@ describe("Straight Cases For Single User Liquidation and healing", () => {
it("Should revert when liquidation is called through vToken and no shortfall", async function () {
// Mint and Increase collateral of the user
mintAmount = convertToUnit("1", 18);
const expectedTotalBalance = convertToUnit("1.0000000001", 18);
await BNX.connect(acc2Signer).faucet(mintAmount);
await BNX.connect(acc2Signer).approve(vBNX.address, mintAmount);

await expect(vBNX.connect(acc2Signer).mint(mintAmount))
.to.emit(vBNX, "Mint")
.withArgs(acc2, mintAmount, mintAmount);
.withArgs(acc2, mintAmount, mintAmount, expectedTotalBalance);
// Liquidation
await expect(
vBSW.connect(acc1Signer).liquidateBorrow(acc2, bswBorrowAmount, vBSW.address),
Expand All @@ -402,12 +403,13 @@ describe("Straight Cases For Single User Liquidation and healing", () => {
it("Should revert when liquidation is called through vToken and trying to seize more tokens", async function () {
// Mint and Incrrease collateral of the user
mintAmount = convertToUnit("1", 18);
const expectedTotalBalance = convertToUnit("2", 18);
await BNX.connect(acc2Signer).faucet(mintAmount);
await BNX.connect(acc2Signer).approve(vBNX.address, mintAmount);

await expect(vBNX.connect(acc2Signer).mint(mintAmount))
.to.emit(vBNX, "Mint")
.withArgs(acc2, mintAmount, mintAmount);
.withArgs(acc2, mintAmount, mintAmount, expectedTotalBalance);
// price manipulation and borrow to overcome insufficient shortfall
bswBorrowAmount = convertToUnit("1", 18);
await vBSW.connect(acc2Signer).borrow(bswBorrowAmount);
Expand All @@ -424,12 +426,13 @@ describe("Straight Cases For Single User Liquidation and healing", () => {
it("Should revert when liquidation is called through vToken and trying to pay too much", async function () {
// Mint and Incrrease collateral of the user
mintAmount = convertToUnit("1", 18);
const expectedTotalBalance = convertToUnit("2", 18);
await BNX.connect(acc2Signer).faucet(mintAmount);
await BNX.connect(acc2Signer).approve(vBNX.address, mintAmount);

await expect(vBNX.connect(acc2Signer).mint(mintAmount))
.to.emit(vBNX, "Mint")
.withArgs(acc2, mintAmount, mintAmount);
.withArgs(acc2, mintAmount, mintAmount, expectedTotalBalance);
// price manipulation and borrow to overcome insufficient shortfall
bswBorrowAmount = convertToUnit("1", 18);
await vBSW.connect(acc2Signer).borrow(bswBorrowAmount);
Expand All @@ -446,12 +449,13 @@ describe("Straight Cases For Single User Liquidation and healing", () => {
it("Should success when liquidation is called through vToken", async function () {
// Mint and Incrrease collateral of the user
mintAmount = convertToUnit("1", 18);
const expectedTotalBalance = convertToUnit("2", 18);
await BNX.connect(acc2Signer).faucet(mintAmount);
await BNX.connect(acc2Signer).approve(vBNX.address, mintAmount);

await expect(vBNX.connect(acc2Signer).mint(mintAmount))
.to.emit(vBNX, "Mint")
.withArgs(acc2, mintAmount, mintAmount);
.withArgs(acc2, mintAmount, mintAmount, expectedTotalBalance);

// price manipulation and borrow to overcome insufficient shortfall
bswBorrowAmount = convertToUnit("1", 18);
Expand Down Expand Up @@ -493,12 +497,12 @@ describe("Straight Cases For Single User Liquidation and healing", () => {

await expect(vBNX.connect(acc2Signer).mint(mintAmount))
.to.emit(vBNX, "Mint")
.withArgs(acc2, mintAmount, mintAmount);
.withArgs(acc2, mintAmount, mintAmount, mintAmount);
// borrow
// Supply WBTC to market from 2nd account
await expect(vBSW.connect(acc1Signer).mint(mintAmount))
.to.emit(vBSW, "Mint")
.withArgs(acc1, mintAmount, mintAmount);
.withArgs(acc1, mintAmount, mintAmount, mintAmount);
// It should revert when try to borrow more than liquidity
await expect(vBSW.connect(acc2Signer).borrow(bswBorrowAmount))
.to.emit(vBSW, "Borrow")
Expand All @@ -511,11 +515,13 @@ describe("Straight Cases For Single User Liquidation and healing", () => {

it("Should revert when total collateral is greater then minLiquidation threshold", async function () {
// Increase mint to make collateral large then min threshold liquidation
await BNX.connect(acc2Signer).faucet(convertToUnit("1", 18));
await BNX.connect(acc2Signer).approve(vBNX.address, convertToUnit("1", 18));
await expect(vBNX.connect(acc2Signer).mint(convertToUnit("1", 18)))
const mintAmount = convertToUnit("1", 18);
const expectedTotalBalance = convertToUnit("1.0000000001", 18);
await BNX.connect(acc2Signer).faucet(mintAmount);
await BNX.connect(acc2Signer).approve(vBNX.address, mintAmount);
await expect(vBNX.connect(acc2Signer).mint(mintAmount))
.to.emit(vBNX, "Mint")
.withArgs(acc2, convertToUnit("1", 18), convertToUnit("1", 18));
.withArgs(acc2, mintAmount, mintAmount, expectedTotalBalance);
// heal
await expect(Comptroller.connect(acc1Signer).healAccount(acc2))
.to.be.revertedWithCustomError(Comptroller, "CollateralExceedsThreshold")
Expand Down

0 comments on commit 1ec4989

Please sign in to comment.