From 1b61edc6ffe89d82a2918c942b7704dd1b0a6e3e Mon Sep 17 00:00:00 2001 From: andyk Date: Wed, 16 Sep 2020 11:10:25 +0300 Subject: [PATCH] add tests on liquiditySwap validation logic --- deployed-contracts.json | 2 +- helpers/types.ts | 1 + test/collateral-swap.spec.ts | 73 +++++++++++++++++++++++++++++++++++- 3 files changed, 73 insertions(+), 3 deletions(-) diff --git a/deployed-contracts.json b/deployed-contracts.json index e29e363d..00c4ef4f 100644 --- a/deployed-contracts.json +++ b/deployed-contracts.json @@ -1,7 +1,7 @@ { "MintableERC20": { "buidlerevm": { - "address": "0x2D8553F9ddA85A9B3259F6Bf26911364B85556F5", + "address": "0x58F132FBB86E21545A4Bace3C19f1C05d86d7A22", "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" }, "localhost": { diff --git a/helpers/types.ts b/helpers/types.ts index 966180d4..bad17f9b 100644 --- a/helpers/types.ts +++ b/helpers/types.ts @@ -67,6 +67,7 @@ export enum ProtocolErrors { NO_VARIABLE_RATE_LOAN_IN_RESERVE = '18', // 'User does not have a variable rate loan in progress on this reserve' UNDERLYING_BALANCE_NOT_GREATER_THAN_0 = '19', // 'The underlying balance needs to be greater than 0' DEPOSIT_ALREADY_IN_USE = '20', // 'User deposit is already being used as collateral' + INVALID_EQUAL_ASSETS_TO_SWAP = '56', // User can't use same reserve as destination of liquidity swap // require error messages - LendingPool NOT_ENOUGH_STABLE_BORROW_BALANCE = '21', // 'User does not have any stable rate loan for this reserve' diff --git a/test/collateral-swap.spec.ts b/test/collateral-swap.spec.ts index cbae27f5..7e692659 100644 --- a/test/collateral-swap.spec.ts +++ b/test/collateral-swap.spec.ts @@ -13,12 +13,63 @@ const {expect} = require('chai'); makeSuite('LendingPool SwapDeposit function', (testEnv: TestEnv) => { let _mockSwapAdapter = {} as MockSwapAdapter; - const {HEALTH_FACTOR_LOWER_THAN_LIQUIDATION_THRESHOLD} = ProtocolErrors; + const { + HEALTH_FACTOR_LOWER_THAN_LIQUIDATION_THRESHOLD, + NO_UNFREEZED_RESERVE, + NO_ACTIVE_RESERVE, + INVALID_EQUAL_ASSETS_TO_SWAP, + } = ProtocolErrors; before(async () => { _mockSwapAdapter = await getMockSwapAdapter(); }); + it('Should not allow to swap if from equal to', async () => { + const {pool, weth} = testEnv; + + await expect( + pool.swapLiquidity( + _mockSwapAdapter.address, + weth.address, + weth.address, + '1'.toString(), + '0x10' + ) + ).to.be.revertedWith(INVALID_EQUAL_ASSETS_TO_SWAP); + }); + + it('Should not allow to swap if from or to reserves are not active', async () => { + const {pool, weth, dai, configurator} = testEnv; + + await configurator.deactivateReserve(weth.address); + + await expect( + pool.swapLiquidity( + _mockSwapAdapter.address, + weth.address, + dai.address, + '1'.toString(), + '0x10' + ) + ).to.be.revertedWith(NO_ACTIVE_RESERVE); + await configurator.activateReserve(weth.address); + + await configurator.deactivateReserve(dai.address); + + await expect( + pool.swapLiquidity( + _mockSwapAdapter.address, + weth.address, + dai.address, + '1'.toString(), + '0x10' + ) + ).to.be.revertedWith(NO_ACTIVE_RESERVE); + + //cleanup state + await configurator.activateReserve(dai.address); + }); + it('Deposits WETH into the reserve', async () => { const {pool, weth, users} = testEnv; const amountToDeposit = ethers.utils.parseEther('1'); @@ -151,7 +202,7 @@ makeSuite('LendingPool SwapDeposit function', (testEnv: TestEnv) => { }); it('Should set usage as collateral to false if no leftovers after swap', async () => { - const {pool, weth, dai, aEth, users} = testEnv; + const {pool, weth, dai, users} = testEnv; const userAddress = await pool.signer.getAddress(); // add more liquidity to allow user 0 to swap everything he has @@ -195,4 +246,22 @@ makeSuite('LendingPool SwapDeposit function', (testEnv: TestEnv) => { 'usageAsCollateralEnabled are not set to false' ); }); + it('Should not allow to swap if to reserve are freezed', async () => { + const {pool, weth, dai, configurator} = testEnv; + + await configurator.freezeReserve(dai.address); + + await expect( + pool.swapLiquidity( + _mockSwapAdapter.address, + weth.address, + dai.address, + '1'.toString(), + '0x10' + ) + ).to.be.revertedWith(NO_UNFREEZED_RESERVE); + + //cleanup state + await configurator.unfreezeReserve(dai.address); + }); });