From 4b9f9ed58398e5854fd852007fec831d31f3bdf8 Mon Sep 17 00:00:00 2001 From: Thrilok Kumar Date: Mon, 31 Aug 2020 23:37:54 +0530 Subject: [PATCH] Done with erc20 and eth pool test cases --- contracts/tests/ethLogic.sol | 2 + test/2_daiPool.js | 100 ++++++++++++++++++++++++++--- test/3_ethPool.js | 121 +++++++++++++++++++++++++++++++++++ 3 files changed, 214 insertions(+), 9 deletions(-) create mode 100644 test/3_ethPool.js diff --git a/contracts/tests/ethLogic.sol b/contracts/tests/ethLogic.sol index 17864f8..1972c17 100644 --- a/contracts/tests/ethLogic.sol +++ b/contracts/tests/ethLogic.sol @@ -17,4 +17,6 @@ contract EthRateLogic { constructor (address ethPool) public { poolToken = address(ethPool); } + + receive() external payable {} } \ No newline at end of file diff --git a/test/2_daiPool.js b/test/2_daiPool.js index f240f71..899991f 100644 --- a/test/2_daiPool.js +++ b/test/2_daiPool.js @@ -51,6 +51,37 @@ contract('DAI Pool', async accounts => { expect(new BN(ethBalance)).to.be.bignumber.least(new BN(ether('1'))); }); + it('should send ether to the master address', async () => { + // Send 1 eth to userAddress to have gas to send an ERC20 tx. + await web3.eth.sendTransaction({ + from: accounts[0], + to: masterAddr, + value: ether('1') + }); + const ethBalance = await balance.current(masterAddr); + expect(new BN(ethBalance)).to.be.bignumber.least(new BN(ether('1'))); + }); + + it('should send DAI to the account[0] address', async () => { + await daiContract.methods + .transfer(accounts[0], ether('1000').toString()) + .send({ from: userAddress}); + const daiBalance = await daiContract.methods.balanceOf(accounts[0]).call(); + expect(new BN(daiBalance)).to.be.bignumber.least(ether('1000')); + }); + + it('should add DAI pool in registry', async () => { + await addPool(registryInstance, daiPoolInstance.address, daiAddr); + }); + + it('should enable DAI pool in registry', async () => { + await enablePool(registryInstance, daiPoolInstance.address); + }); + + it('should update DAI Logic contract in registry', async () => { + await updateRateLogic(registryInstance, daiPoolInstance.address, daiRateLogicInstance.address); + }); + it('should give DAI allowance for DAI pool', async () => { await daiContract.methods .approve(daiPoolInstance.address, ether('1000').toString()) @@ -60,23 +91,74 @@ contract('DAI Pool', async accounts => { }); it('should deposit 100 DAI in DAI pool', async () => { - var amountInWei = new BN(ether(100)).toString() + var amountInWei = (ether("100")).toString() await daiPoolInstance.deposit(amountInWei, {from: userAddress}); const daiBalance = await daiContract.methods.balanceOf(daiPoolInstance.address).call(); expect(new BN(daiBalance)).to.be.bignumber.least(amountInWei); - var totalSupply = await poolInstance.totalSupply(); + var totalSupply = await daiPoolInstance.totalSupply(); expect(new BN(totalSupply)).to.be.bignumber.least(amountInWei); }); it('should add profit 10 DAI and calculate exchange rate', async () => { - var amountInWei = new BN(ether(10)).toString() + var amountInWei = new BN(ether("10")).toString() await daiContract.methods .transfer(daiRateLogicInstance.address, amountInWei) .send({ from: userAddress}); - await daiPoolInstance.deposit(amountInWei, {from: userAddress}); - const daiBalance = await daiContract.methods.balanceOf(daiPoolInstance.address).call(); - expect(new BN(daiBalance)).to.be.bignumber.least(amountInWei); - var totalSupply = await poolInstance.totalSupply(); - expect(new BN(totalSupply)).to.be.bignumber.least(amountInWei); + var exchangeRateInit = await daiPoolInstance.exchangeRate() + await daiPoolInstance.setExchangeRate({from: masterAddr}); + var exchangeRateFinal = await daiPoolInstance.exchangeRate() + expect(exchangeRateInit).to.not.equal(exchangeRateFinal); }); -}); \ No newline at end of file + + it('should give DAI allowance for DAI pool(accounts[0])', async () => { + await daiContract.methods + .approve(daiPoolInstance.address, ether('1000').toString()) + .send({ from: accounts[0]}); + const daiBalance = await daiContract.methods.allowance(accounts[0], daiPoolInstance.address).call(); + expect(new BN(daiBalance)).to.be.bignumber.least(ether('1000')); + }); + + it('should deposit 100 DAI in DAI pool(accounts[0])', async () => { + var amountInWei = (ether("100")).toString() + await daiPoolInstance.deposit(amountInWei, {from: accounts[0]}); + const wrapDaiBalance = await daiPoolInstance.balanceOf(accounts[0]) + expect(new BN(wrapDaiBalance)).to.be.bignumber.least((ether("90")).toString()); + }); + + it('should withdraw 10 DAI in DAI pool', async () => { + var amountInWei = (ether("10")).toString() + await daiPoolInstance.withdraw(amountInWei, accounts[1], {from: userAddress}); + const daiBalance = await daiContract.methods.balanceOf(accounts[1]).call(); + expect(new BN(daiBalance)).to.be.bignumber.least(amountInWei); + }); + + it('should withdraw total DAI in DAI pool', async () => { + var amountInWei = (ether("1000")).toString() + var checkAmt = (ether("90")).toString() + await daiPoolInstance.withdraw(amountInWei, accounts[2], {from: userAddress}); + const daiBalance = await daiContract.methods.balanceOf(accounts[3]).call(); + expect(new BN(daiBalance)).to.be.bignumber.least(checkAmt); + }); +}); + + +async function addPool(registryInstance, poolAddr, tokenAddr) { + await registryInstance.addPool(tokenAddr, poolAddr, {from: masterAddr}); + + var _poolAddr = await registryInstance.poolToken(tokenAddr); + expect(_poolAddr).to.equal(poolAddr); +} + +async function enablePool(registryInstance, poolAddr) { + await registryInstance.updatePool(poolAddr, {from: masterAddr}); + + var _isPool = await registryInstance.isPool(poolAddr); + expect(_isPool).to.equal(true); +} + +async function updateRateLogic(registryInstance, poolAddr, logicAddr) { + await registryInstance.updatePoolLogic(poolAddr, logicAddr, {from: masterAddr}); + + var _logicAddr = await registryInstance.poolLogic(poolAddr); + expect(_logicAddr).to.equal(logicAddr); +} \ No newline at end of file diff --git a/test/3_ethPool.js b/test/3_ethPool.js new file mode 100644 index 0000000..136906b --- /dev/null +++ b/test/3_ethPool.js @@ -0,0 +1,121 @@ +const { BN, ether, balance } = require('@openzeppelin/test-helpers'); +const { expect } = require('chai'); + +const RegistryContract = artifacts.require("Registry"); +const PoolTokenContract = artifacts.require("PoolToken"); +const PoolETHContract = artifacts.require("PoolETH"); + +const DaiRateLogic = artifacts.require("DaiRateLogic"); +const EthRateLogic = artifacts.require("EthRateLogic"); + + +const masterAddr = "0xfCD22438AD6eD564a1C26151Df73F6B33B817B56" + +// ABI + +contract('ETH Pool', async accounts => { + let ethAddr = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE"; + + let accountA = accounts[0]; + let accountB = accounts[1]; + + let ethPoolInstance; + let registryInstance; + + let ethRateLogicInstance; + before(async() => { + registryInstance = await RegistryContract.deployed(); + ethPoolInstance = await PoolETHContract.deployed(); + + ethRateLogicInstance = await EthRateLogic.deployed(); + }) + + it('should send ether to the master address', async () => { + // Send 1 eth to userAddress to have gas to send an ERC20 tx. + await web3.eth.sendTransaction({ + from: accounts[0], + to: masterAddr, + value: ether('1') + }); + const ethBalance = await balance.current(masterAddr); + expect(new BN(ethBalance)).to.be.bignumber.least(new BN(ether('1'))); + }); + + it('should add ETH pool in registry', async () => { + await addPool(registryInstance, ethPoolInstance.address, ethAddr); + }); + + it('should enable DAI pool in registry', async () => { + await enablePool(registryInstance, ethPoolInstance.address); + }); + + it('should update DAI Logic contract in registry', async () => { + await updateRateLogic(registryInstance, ethPoolInstance.address, ethRateLogicInstance.address); + }); + + it('should deposit 5 ETH in ETH pool', async () => { + var amountInWei = (ether("5")).toString() + await ethPoolInstance.deposit(amountInWei, {from: accountA, value: amountInWei}); + const ethBalance = await web3.eth.getBalance(ethPoolInstance.address); + expect(new BN(ethBalance)).to.be.bignumber.least(amountInWei); + var totalSupply = await ethPoolInstance.totalSupply(); + expect(new BN(totalSupply)).to.be.bignumber.least(amountInWei); + }); + + it('should add profit 0.5 ETH and calculate exchange rate', async () => { + var amountInWei = new BN(ether("0.5")).toString() + await web3.eth.sendTransaction({ + from: accountA, + to: ethRateLogicInstance.address, + value: amountInWei + }); + var exchangeRateInit = await ethPoolInstance.exchangeRate() + await ethPoolInstance.setExchangeRate({from: masterAddr}); + var exchangeRateFinal = await ethPoolInstance.exchangeRate() + expect(exchangeRateInit).to.not.equal(exchangeRateFinal); + }); + + it('should deposit 5 ETH in ETH pool(accountB)', async () => { + var amountInWei = (ether("5")).toString() + await ethPoolInstance.deposit(amountInWei, {from: accountB, value: amountInWei}); + const wrapETHBalance = await ethPoolInstance.balanceOf(accountB) + expect(new BN(wrapETHBalance)).to.be.bignumber.least((ether("4")).toString()); + }); + + it('should withdraw 0.5 ETH in ETH pool', async () => { + var amountInWei = (ether("0.5")).toString() + await ethPoolInstance.withdraw(amountInWei, accounts[2], {from: accountA}); + const ethBalance = await web3.eth.getBalance(accounts[2]); + expect(new BN(ethBalance)).to.be.bignumber.least(amountInWei); + }); + + it('should withdraw total ETH in ETH pool', async () => { + var amountInWei = (ether("1000")).toString() + var checkAmt = (ether("4.5")).toString() + await ethPoolInstance.withdraw(amountInWei, accounts[3], {from: userAddress}); + const ethBalance = await web3.eth.getBalance(accounts[3]); + expect(new BN(ethBalance)).to.be.bignumber.least(checkAmt); + }); +}); + + +async function addPool(registryInstance, poolAddr, tokenAddr) { + await registryInstance.addPool(tokenAddr, poolAddr, {from: masterAddr}); + + var _poolAddr = await registryInstance.poolToken(tokenAddr); + expect(_poolAddr).to.equal(poolAddr); +} + +async function enablePool(registryInstance, poolAddr) { + await registryInstance.updatePool(poolAddr, {from: masterAddr}); + + var _isPool = await registryInstance.isPool(poolAddr); + expect(_isPool).to.equal(true); +} + +async function updateRateLogic(registryInstance, poolAddr, logicAddr) { + await registryInstance.updatePoolLogic(poolAddr, logicAddr, {from: masterAddr}); + + var _logicAddr = await registryInstance.poolLogic(poolAddr); + expect(_logicAddr).to.equal(logicAddr); +} \ No newline at end of file