From 3d2c4dce1b8fb213555e4ad14a64a41e3f756ae5 Mon Sep 17 00:00:00 2001 From: Thrilok Kumar Date: Thu, 10 Sep 2020 02:10:19 +0530 Subject: [PATCH] Updated test-cases --- contracts/logicProxy/basicCon.sol | 46 +++++++++++++++++++ contracts/tests/FlusherLogic.sol | 7 +++ contracts/tests/settleLogic.sol | 10 +++++ migrations/2_registry.js | 4 ++ test/1_registry.js | 38 ++-------------- test/2_daiPool.js | 73 +++++++++++++++++++++++++------ test/3_ethPool.js | 68 +++++++++++++++++++++++++--- 7 files changed, 192 insertions(+), 54 deletions(-) create mode 100644 contracts/logicProxy/basicCon.sol create mode 100644 contracts/tests/FlusherLogic.sol create mode 100644 contracts/tests/settleLogic.sol diff --git a/contracts/logicProxy/basicCon.sol b/contracts/logicProxy/basicCon.sol new file mode 100644 index 0000000..2b28362 --- /dev/null +++ b/contracts/logicProxy/basicCon.sol @@ -0,0 +1,46 @@ +pragma solidity ^0.6.0; + +interface TokenInterface { + function approve(address, uint) external; + function transfer(address, uint) external; + function transferFrom(address, address, uint) external; + function deposit() external payable; + function withdraw(uint) external; + function balanceOf(address) external view returns (uint); +} + +interface TokenPool { + function deposit(uint amount) external payable returns (uint); + function withdraw(uint amount, address to) external returns (uint); +} + +interface Registry { + function poolToken(address) external view returns (address); +} + +contract BasicProxy { + function getRegistryAddr() internal pure returns (address) { + return 0x53A664d8F4FF1201eA9415825a746D1652345110; + } + + function getEthAddr() internal pure returns (address) { + return 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE; + } + + function deposit(address token, uint amount) external payable { + Registry registry = Registry(getRegistryAddr()); + address tokenPoolAddr = registry.poolToken(token); + require(tokenPoolAddr != address(0), "Token-pool-not-found"); + uint bal = getEthAddr() == token ? address(this).balance : TokenInterface(token).balanceOf(address(this)); + uint _amt = amount >= bal ? bal : amount; + uint ethAmt = getEthAddr() == token ? _amt : 0; + TokenPool(tokenPoolAddr).deposit.value(ethAmt)(amount); + } + + function withdraw(address token, uint amount, address to) external { + Registry registry = Registry(getRegistryAddr()); + address tokenPoolAddr = registry.poolToken(token); + require(tokenPoolAddr != address(0), "Token-pool-not-found"); + TokenPool(tokenPoolAddr).withdraw(amount, to); + } +} \ No newline at end of file diff --git a/contracts/tests/FlusherLogic.sol b/contracts/tests/FlusherLogic.sol new file mode 100644 index 0000000..8b54c73 --- /dev/null +++ b/contracts/tests/FlusherLogic.sol @@ -0,0 +1,7 @@ +pragma solidity ^0.6.0; + +contract FlusherLogic { + function isFlusher(address addres) external returns (bool) { + return true; + } +} \ No newline at end of file diff --git a/contracts/tests/settleLogic.sol b/contracts/tests/settleLogic.sol new file mode 100644 index 0000000..774107b --- /dev/null +++ b/contracts/tests/settleLogic.sol @@ -0,0 +1,10 @@ +pragma solidity ^0.6.0; + +interface PoolInterface { + function setExchangeRate() external; +} +contract SettleLogic { + function calculateExchangeRate(address pool) external { + PoolInterface(pool).setExchangeRate(); + } +} \ No newline at end of file diff --git a/migrations/2_registry.js b/migrations/2_registry.js index 498be56..821322a 100644 --- a/migrations/2_registry.js +++ b/migrations/2_registry.js @@ -1,4 +1,8 @@ const Registry = artifacts.require("Registry"); +const FlusherLogic = artifacts.require("FlusherLogic"); +const SettleLogic = artifacts.require("SettleLogic"); module.exports = async function(deployer, networks, accounts) { await deployer.deploy(Registry, accounts[0]); //deploy registry.sol contract + await deployer.deploy(FlusherLogic, accounts[0]); //deploy flusherLogic.sol contract + await deployer.deploy(SettleLogic, accounts[0]); //deploy settleLogic.sol contract }; \ No newline at end of file diff --git a/test/1_registry.js b/test/1_registry.js index 1825221..ce9a209 100644 --- a/test/1_registry.js +++ b/test/1_registry.js @@ -48,40 +48,24 @@ contract('Registry.sol', async accounts => { await addPool(registryInstance, ethPoolInstance.address, ethAddr); }); - it('should enable ETH pool in registry', async () => { - await enablePool(registryInstance, ethPoolInstance.address); - }); - it('should remove ETH pool in registry', async () => { await removePool(registryInstance, ethAddr); }); - it('should disable ETH pool in registry', async () => { - await disablePool(registryInstance, ethPoolInstance.address); - }); - it('should add ETH pool in registry', async () => { await addPool(registryInstance, ethPoolInstance.address, ethAddr); }); - it('should enable ETH pool in registry', async () => { - await enablePool(registryInstance, ethPoolInstance.address); - }); - 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 ETH Logic contract in registry', async () => { - await updateRateLogic(registryInstance, ethPoolInstance.address, ethRateLogicInstance.address); + await updateRateLogic(registryInstance, ethPoolInstance.address, ethAddr, ethRateLogicInstance.address); }); it('should update DAI Logic contract in registry', async () => { - await updateRateLogic(registryInstance, daiPoolInstance.address, daiRateLogicInstance.address); + await updateRateLogic(registryInstance, daiPoolInstance.address, daiAddr, daiRateLogicInstance.address); }); }); @@ -100,22 +84,8 @@ async function removePool(registryInstance, tokenAddr) { } -async function enablePool(registryInstance, poolAddr) { - await registryInstance.updatePool(poolAddr, {from: masterAddr}); - - var _isPool = await registryInstance.isPool(poolAddr); - expect(_isPool).to.equal(true); -} - -async function disablePool(registryInstance, poolAddr) { - await registryInstance.updatePool(poolAddr, {from: masterAddr}); - - var _isPool = await registryInstance.isPool(poolAddr); - expect(_isPool).to.equal(false); - } - -async function updateRateLogic(registryInstance, poolAddr, logicAddr) { - await registryInstance.updatePoolLogic(poolAddr, logicAddr, {from: masterAddr}); +async function updateRateLogic(registryInstance, poolAddr, tokenAddr, logicAddr) { + await registryInstance.updatePoolLogic(tokenAddr, logicAddr, {from: masterAddr}); var _logicAddr = await registryInstance.poolLogic(poolAddr); expect(_logicAddr).to.equal(logicAddr); diff --git a/test/2_daiPool.js b/test/2_daiPool.js index b9dafa4..300b00e 100644 --- a/test/2_daiPool.js +++ b/test/2_daiPool.js @@ -7,6 +7,8 @@ const PoolETHContract = artifacts.require("PoolETH"); const DaiRateLogic = artifacts.require("DaiRateLogic"); const EthRateLogic = artifacts.require("EthRateLogic"); +const FlusherLogic = artifacts.require("FlusherLogic"); +const SettleLogic = artifacts.require("SettleLogic"); const masterAddr = "0xfCD22438AD6eD564a1C26151Df73F6B33B817B56" @@ -31,6 +33,8 @@ contract('DAI Pool', async accounts => { let ethRateLogicInstance; let daiRateLogicInstance; + let flusherLogicInstance; + let settleLogicInstance; before(async() => { registryInstance = await RegistryContract.deployed(); ethPoolInstance = await PoolETHContract.deployed(); @@ -38,6 +42,8 @@ contract('DAI Pool', async accounts => { ethRateLogicInstance = await EthRateLogic.deployed(); daiRateLogicInstance = await DaiRateLogic.deployed(); + flusherLogicInstance = await FlusherLogic.deployed(); + settleLogicInstance = await SettleLogic.deployed(); }) it('should send ether to the user address', async () => { @@ -74,12 +80,21 @@ contract('DAI Pool', async accounts => { 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, daiAddr, daiRateLogicInstance.address); }); - it('should update DAI Logic contract in registry', async () => { - await updateRateLogic(registryInstance, daiPoolInstance.address, daiRateLogicInstance.address); + it('should update Flusher Logic contract in registry for DAI POOL', async () => { + await updateFlusherLogic(registryInstance, daiPoolInstance.address, daiAddr, flusherLogicInstance.address); + }); + + it('should update Settle Logic contract in registry for DAI POOL', async () => { + await updateSettleLogic(registryInstance, daiPoolInstance.address, daiAddr, settleLogicInstance.address); + }); + + it('should update Pool Cap in registry for DAI POOL', async () => { + var amountInWei = (ether("100000000")).toString() + await updatePoolCap(registryInstance, daiPoolInstance.address, daiAddr, amountInWei); }); it('should give DAI allowance for DAI pool', async () => { @@ -105,7 +120,7 @@ contract('DAI Pool', async accounts => { .transfer(daiRateLogicInstance.address, amountInWei) .send({ from: userAddress}); var exchangeRateInit = await daiPoolInstance.exchangeRate() - await daiPoolInstance.setExchangeRate({from: masterAddr}); + await updateExchangeLogic(daiPoolInstance, settleLogicInstance.address); var exchangeRateFinal = await daiPoolInstance.exchangeRate() expect(exchangeRateInit).to.not.equal(exchangeRateFinal); }); @@ -149,16 +164,48 @@ async function addPool(registryInstance, poolAddr, 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 updatePoolCap(registryInstance, poolAddr, tokenAddr, capAmt) { + await registryInstance.updateCap(tokenAddr, capAmt, {from: masterAddr}); + + var _capAmt = await registryInstance.poolCap(poolAddr); + expect(new BN(_capAmt)).to.bignumber.equal(capAmt); } -async function updateRateLogic(registryInstance, poolAddr, logicAddr) { - await registryInstance.updatePoolLogic(poolAddr, logicAddr, {from: masterAddr}); +async function updateRateLogic(registryInstance, poolAddr, tokenAddr, logicAddr) { + await registryInstance.updatePoolLogic(tokenAddr, logicAddr, {from: masterAddr}); var _logicAddr = await registryInstance.poolLogic(poolAddr); expect(_logicAddr).to.equal(logicAddr); -} \ No newline at end of file +} + +async function updateFlusherLogic(registryInstance, poolAddr, tokenAddr, flusherLogic) { + await registryInstance.updateFlusherLogic(tokenAddr, flusherLogic, {from: masterAddr}); + + var _logicAddr = await registryInstance.flusherLogic(poolAddr); + expect(_logicAddr).to.equal(flusherLogic); +} + +async function updateSettleLogic(registryInstance, poolAddr, tokenAddr, settleLogic) { + await registryInstance.addSettleLogic(tokenAddr, settleLogic, {from: masterAddr}); + + var _isSettleLogic = await registryInstance.settleLogic(poolAddr, settleLogic); + expect(_isSettleLogic).to.equal(true); +} + +async function updateExchangeLogic(daiPoolInstance, settleLogic) { + var abi = { + "inputs": [ + { + "internalType": "address", + "name": "pool", + "type": "address" + } + ], + "name": "calculateExchangeRate", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + } + var encodeCalldata = web3.eth.abi.encodeFunctionCall(abi, [daiPoolInstance.address]); + await daiPoolInstance.settle([settleLogic], [encodeCalldata], {from: masterAddr}); +} diff --git a/test/3_ethPool.js b/test/3_ethPool.js index 34a23fd..d398bb3 100644 --- a/test/3_ethPool.js +++ b/test/3_ethPool.js @@ -4,6 +4,8 @@ const { expect } = require('chai'); const RegistryContract = artifacts.require("Registry"); const PoolETHContract = artifacts.require("PoolETH"); +const FlusherLogic = artifacts.require("FlusherLogic"); +const SettleLogic = artifacts.require("SettleLogic"); const EthRateLogic = artifacts.require("EthRateLogic"); @@ -21,11 +23,15 @@ contract('ETH Pool', async accounts => { let registryInstance; let ethRateLogicInstance; + let flusherLogicInstance; + let settleLogicInstance; before(async() => { registryInstance = await RegistryContract.deployed(); ethPoolInstance = await PoolETHContract.deployed(); ethRateLogicInstance = await EthRateLogic.deployed(); + flusherLogicInstance = await FlusherLogic.deployed(); + settleLogicInstance = await SettleLogic.deployed(); }) it('should send ether to the master address', async () => { @@ -43,12 +49,21 @@ contract('ETH Pool', async accounts => { await addPool(registryInstance, ethPoolInstance.address, ethAddr); }); - it('should enable ETH pool in registry', async () => { - await enablePool(registryInstance, ethPoolInstance.address); + it('should update ETH Logic contract in registry', async () => { + await updateRateLogic(registryInstance, ethPoolInstance.address, ethAddr, ethRateLogicInstance.address); }); - it('should update ETH Logic contract in registry', async () => { - await updateRateLogic(registryInstance, ethPoolInstance.address, ethRateLogicInstance.address); + it('should update Flusher Logic contract in registry for ETH POOL', async () => { + await updateFlusherLogic(registryInstance, ethPoolInstance.address, ethAddr, flusherLogicInstance.address); + }); + + it('should update Settle Logic contract in registry for ETH POOL', async () => { + await updateSettleLogic(registryInstance, ethPoolInstance.address, ethAddr, settleLogicInstance.address); + }); + + it('should update Pool Cap in registry for ETH POOL', async () => { + var amountInWei = (ether("100000000")).toString() + await updatePoolCap(registryInstance, ethPoolInstance.address, ethAddr, amountInWei); }); it('should deposit 5 ETH in ETH pool', async () => { @@ -68,7 +83,7 @@ contract('ETH Pool', async accounts => { value: amountInWei }); var exchangeRateInit = await ethPoolInstance.exchangeRate() - await ethPoolInstance.setExchangeRate({from: masterAddr}); + await updateExchangeLogic(ethPoolInstance, settleLogicInstance.address); var exchangeRateFinal = await ethPoolInstance.exchangeRate() expect(exchangeRateInit).to.not.equal(exchangeRateFinal); }); @@ -111,9 +126,48 @@ async function enablePool(registryInstance, poolAddr) { expect(_isPool).to.equal(true); } -async function updateRateLogic(registryInstance, poolAddr, logicAddr) { - await registryInstance.updatePoolLogic(poolAddr, logicAddr, {from: masterAddr}); +async function updateRateLogic(registryInstance, poolAddr, tokenAddr, logicAddr) { + await registryInstance.updatePoolLogic(tokenAddr, logicAddr, {from: masterAddr}); var _logicAddr = await registryInstance.poolLogic(poolAddr); expect(_logicAddr).to.equal(logicAddr); +} + +async function updatePoolCap(registryInstance, poolAddr, tokenAddr, capAmt) { + await registryInstance.updateCap(tokenAddr, capAmt, {from: masterAddr}); + + var _capAmt = await registryInstance.poolCap(poolAddr); + expect(new BN(_capAmt)).to.bignumber.equal(capAmt); +} + +async function updateFlusherLogic(registryInstance, poolAddr, tokenAddr, flusherLogic) { + await registryInstance.updateFlusherLogic(tokenAddr, flusherLogic, {from: masterAddr}); + + var _logicAddr = await registryInstance.flusherLogic(poolAddr); + expect(_logicAddr).to.equal(flusherLogic); +} + +async function updateSettleLogic(registryInstance, poolAddr, tokenAddr, settleLogic) { + await registryInstance.addSettleLogic(tokenAddr, settleLogic, {from: masterAddr}); + + var _isSettleLogic = await registryInstance.settleLogic(poolAddr, settleLogic); + expect(_isSettleLogic).to.equal(true); +} + +async function updateExchangeLogic(ethPoolInstance, settleLogic) { + var abi = { + "inputs": [ + { + "internalType": "address", + "name": "pool", + "type": "address" + } + ], + "name": "calculateExchangeRate", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + } + var encodeCalldata = web3.eth.abi.encodeFunctionCall(abi, [ethPoolInstance.address]); + await ethPoolInstance.settle([settleLogic], [encodeCalldata], {from: masterAddr}); } \ No newline at end of file