Skip to content

Commit

Permalink
Update deposit unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rackstar committed May 9, 2024
1 parent f7c225b commit 22344ea
Showing 1 changed file with 104 additions and 54 deletions.
158 changes: 104 additions & 54 deletions test/unit/YieldDeposit/deposit.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,92 +2,142 @@ const { ethers } = require('hardhat');
const { expect } = require('chai');
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');

const { increasePriceFeedRate } = require('./helper');
const { setup } = require('./setup');

describe('YieldDeposit - deposit', function () {
it('should not be able to deposit if user has already existing deposit', async function () {
const RATE_DENOMINATOR = ethers.BigNumber.from('10').pow(18);

describe.only('YieldDeposit - deposit', function () {
it('should revert InvalidDepositAmount if deposit amount is less than or equal to zero', async function () {
const fixture = await loadFixture(setup);
const { yieldDeposit, weEth, chainLinkPriceFeed } = fixture.contracts;
const { yieldDeposit, weEth } = fixture.contracts;
const [member] = fixture.accounts.members;

const depositAmount = ethers.utils.parseEther('10');
await weEth.connect(member).approve(yieldDeposit.address, depositAmount);
await yieldDeposit.connect(member).deposit(depositAmount);

const priceRate = await chainLinkPriceFeed.latestAnswer();
expect(await yieldDeposit.totalPrincipal()).to.be.equal(depositAmount);
expect(await yieldDeposit.deposits(member.address)).to.be.equal(depositAmount);
expect(await yieldDeposit.coverAmounts(member.address)).to.be.equal('518700000000000000');
expect(await yieldDeposit.initialRates(member.address)).to.be.equal(priceRate);

await weEth.connect(member).approve(yieldDeposit.address, depositAmount);
const depositError = yieldDeposit.connect(member).deposit(depositAmount);
await expect(depositError).to.revertedWithCustomError(yieldDeposit, 'WithdrawBeforeMakingNewDeposit');
const depositError = yieldDeposit.connect(member).deposit(weEth.address, '0');
await expect(depositError).to.revertedWithCustomError(yieldDeposit, 'InvalidDepositAmount');

expect(await yieldDeposit.totalPrincipal()).to.be.equal(depositAmount);
expect(await yieldDeposit.deposits(member.address)).to.be.equal(depositAmount);
expect(await yieldDeposit.coverAmounts(member.address)).to.be.equal('518700000000000000');
expect(await yieldDeposit.initialRates(member.address)).to.be.equal(priceRate);
expect(await yieldDeposit.totalDepositValue()).to.be.equal('0');
expect(await yieldDeposit.userTokenDepositValue(member.address, weEth.address)).to.be.equal('0');
});

it('should revert InvalidDepositAmount if deposit amount is less than or equal to zero', async function () {
it('should revert TokenNotSupported if token is not supported', async function () {
const fixture = await loadFixture(setup);
const { yieldDeposit } = fixture.contracts;
const { yieldDeposit, weEth } = fixture.contracts;
const [member] = fixture.accounts.members;

const depositError = yieldDeposit.connect(member).deposit('0');
await expect(depositError).to.revertedWithCustomError(yieldDeposit, 'InvalidDepositAmount');
const unsupportedToken = '0x3d08cc653ec3df0c039c3a1da15ed0ceea3b0acc';
const depositError = yieldDeposit.connect(member).deposit(unsupportedToken, '1');
await expect(depositError).to.revertedWithCustomError(yieldDeposit, 'TokenNotSupported');

expect(await yieldDeposit.totalPrincipal()).to.be.equal('0');
expect(await yieldDeposit.deposits(member.address)).to.be.equal('0');
expect(await yieldDeposit.coverAmounts(member.address)).to.be.equal('0');
expect(await yieldDeposit.initialRates(member.address)).to.be.equal('0');
expect(await yieldDeposit.totalDepositValue()).to.be.equal('0');
expect(await yieldDeposit.userTokenDepositValue(member.address, weEth.address)).to.be.equal('0');
});

it('should deposit token to contract', async function () {
it('should be able to deposit to contract', async function () {
const fixture = await loadFixture(setup);
const { yieldDeposit, weEth, chainLinkPriceFeed } = fixture.contracts;
const { yieldDeposit, weEth, chainLinkPriceFeedWeEth } = fixture.contracts;
const [member] = fixture.accounts.members;

const userWeEthBalanceBefore = await weEth.balanceOf(member.address);
const contractWeEthBalanceBefore = await weEth.balanceOf(yieldDeposit.address);
const contractTotalPrincipalBefore = await yieldDeposit.totalPrincipal();
const userDepositBefore = await yieldDeposit.deposits(member.address);
const userCoverAmountBefore = await yieldDeposit.coverAmounts(member.address);
const userInitialRateBefore = await yieldDeposit.initialRates(member.address);
expect(contractWeEthBalanceBefore).to.be.equal('0');
expect(contractTotalPrincipalBefore).to.be.equal('0');
expect(userDepositBefore).to.be.equal('0');
expect(userCoverAmountBefore).to.be.equal('0');
expect(userInitialRateBefore).to.be.equal('0');
expect(await weEth.balanceOf(yieldDeposit.address)).to.be.equal('0');
expect(await yieldDeposit.totalDepositValue()).to.be.equal('0');
expect(await yieldDeposit.userTokenDepositValue(member.address, weEth.address)).to.be.equal('0');

const depositAmount = ethers.utils.parseEther('10');
await weEth.connect(member).approve(yieldDeposit.address, depositAmount);
await yieldDeposit.connect(member).deposit(depositAmount);
await yieldDeposit.connect(member).deposit(weEth.address, depositAmount);

const priceRate = await chainLinkPriceFeedWeEth.latestAnswer();
const userDepositValue = depositAmount.mul(priceRate).div(RATE_DENOMINATOR);

const contractWeEthBalanceAfter = await weEth.balanceOf(yieldDeposit.address);
const contractTotalPrincipalAfter = await yieldDeposit.totalPrincipal();
const userWeEthBalanceAfter = await weEth.balanceOf(member.address);
const userDepositAfter = await yieldDeposit.deposits(member.address);
const userCoverAmountAfter = await yieldDeposit.coverAmounts(member.address);
const userInitialRateAfter = await yieldDeposit.initialRates(member.address);
expect(contractTotalPrincipalAfter).to.be.equal(depositAmount);
expect(contractWeEthBalanceAfter).to.be.equal(depositAmount);
expect(userWeEthBalanceAfter).to.be.equal(userWeEthBalanceBefore.sub(depositAmount));
expect(userDepositAfter).to.be.equal(depositAmount);
expect(userCoverAmountAfter).to.be.equal('518700000000000000');
expect(userInitialRateAfter).to.be.equal(await chainLinkPriceFeed.latestAnswer());
expect(await weEth.balanceOf(yieldDeposit.address)).to.be.equal(depositAmount);
expect(await yieldDeposit.totalDepositValue()).to.be.equal(userDepositValue);
expect(await yieldDeposit.userTokenDepositValue(member.address, weEth.address)).to.be.equal(userDepositValue);
});

it('should be able to do a second deposit on top of existing deposit - same token', async function () {
const fixture = await loadFixture(setup);
const { yieldDeposit, weEth, chainLinkPriceFeedWeEth } = fixture.contracts;
const [member] = fixture.accounts.members;

const userWeEthBalanceBefore = await weEth.balanceOf(member.address);

const depositAmount1 = ethers.utils.parseEther('10');
await weEth.connect(member).approve(yieldDeposit.address, depositAmount1);
await yieldDeposit.connect(member).deposit(weEth.address, depositAmount1);

const priceRate1 = await chainLinkPriceFeedWeEth.latestAnswer();
const userDeposit1Value = depositAmount1.mul(priceRate1).div(RATE_DENOMINATOR);

await increasePriceFeedRate(chainLinkPriceFeedWeEth);

const depositAmount2 = ethers.utils.parseEther('20');
await weEth.connect(member).approve(yieldDeposit.address, depositAmount2);
await yieldDeposit.connect(member).deposit(weEth.address, depositAmount2);

const priceRate2 = await chainLinkPriceFeedWeEth.latestAnswer();
const userDeposit2Value = depositAmount2.mul(priceRate2).div(RATE_DENOMINATOR);

const userWeEthBalanceAfter = await weEth.balanceOf(member.address);
const totalUserDepositAmount = depositAmount1.add(depositAmount2);
const totalUserDepositValue = userDeposit1Value.add(userDeposit2Value)

expect(userWeEthBalanceAfter).to.be.equal(userWeEthBalanceBefore.sub(totalUserDepositAmount));
expect(await weEth.balanceOf(yieldDeposit.address)).to.be.equal(totalUserDepositAmount);
expect(await yieldDeposit.totalDepositValue()).to.be.equal(totalUserDepositValue);
expect(await yieldDeposit.userTokenDepositValue(member.address, weEth.address)).to.be.equal(totalUserDepositValue);
});

it('should be able to do a second deposit on top of existing deposit - different token', async function () {
const fixture = await loadFixture(setup);
const { yieldDeposit, weEth, stEth, chainLinkPriceFeedWeEth, chainLinkPriceFeedStEth } = fixture.contracts;
const [member] = fixture.accounts.members;

const userWeEthBalanceBefore = await weEth.balanceOf(member.address);
const userStEthBalanceBefore = await stEth.balanceOf(member.address);

const weEthDepositAmount = ethers.utils.parseEther('10');
await weEth.connect(member).approve(yieldDeposit.address, weEthDepositAmount);
await yieldDeposit.connect(member).deposit(weEth.address, weEthDepositAmount);

expect(await weEth.balanceOf(member.address)).to.equal(userWeEthBalanceBefore.sub(weEthDepositAmount));

const weEthPriceRate = await chainLinkPriceFeedWeEth.latestAnswer();
const weEthDepositValue = weEthDepositAmount.mul(weEthPriceRate).div(RATE_DENOMINATOR);

const stEthDepositAmount = ethers.utils.parseEther('30');
await stEth.connect(member).approve(yieldDeposit.address, stEthDepositAmount);
await yieldDeposit.connect(member).deposit(stEth.address, stEthDepositAmount);

expect(await stEth.balanceOf(member.address)).to.equal(userStEthBalanceBefore.sub(stEthDepositAmount));

const stEthPriceRate = await chainLinkPriceFeedStEth.latestAnswer();
const stEthDepositValue = stEthDepositAmount.mul(stEthPriceRate).div(RATE_DENOMINATOR);
console.log('stEthDepositValue: ', stEthDepositValue);

const totalUserDepositAmount = weEthDepositAmount.add(stEthDepositAmount);
const totalUserDepositValue = weEthDepositValue.add(stEthDepositValue)

expect(await weEth.balanceOf(yieldDeposit.address)).to.be.equal(weEthDepositAmount);
expect(await stEth.balanceOf(yieldDeposit.address)).to.be.equal(stEthDepositAmount);
expect(await yieldDeposit.totalDepositValue()).to.be.equal(totalUserDepositValue);
expect(await yieldDeposit.userTokenDepositValue(member.address, weEth.address)).to.be.equal(weEthDepositValue);
expect(await yieldDeposit.userTokenDepositValue(member.address, stEth.address)).to.be.equal(stEthDepositValue);
});

it('should emit TokenDeposited on successful deposit', async function () {
const fixture = await loadFixture(setup);
const { yieldDeposit, weEth } = fixture.contracts;
const { yieldDeposit, weEth, chainLinkPriceFeedWeEth } = fixture.contracts;
const [member] = fixture.accounts.members;

const depositAmount = ethers.utils.parseEther('10');
const priceRate = await chainLinkPriceFeedWeEth.latestAnswer();

await weEth.connect(member).approve(yieldDeposit.address, depositAmount);
await expect(yieldDeposit.connect(member).deposit(depositAmount))
await expect(yieldDeposit.connect(member).deposit(weEth.address, depositAmount))
.to.emit(yieldDeposit, 'TokenDeposited')
.withArgs(member.address, depositAmount, '518700000000000000');
.withArgs(member.address, depositAmount, priceRate);
});
});

0 comments on commit 22344ea

Please sign in to comment.