From 7eef64d8cd94865211d0c7b6559949782ed8d05c Mon Sep 17 00:00:00 2001 From: aparnakr Date: Mon, 17 Aug 2020 16:52:53 -0700 Subject: [PATCH 1/2] fix the ability to get all vault owners --- contracts/OptionsContract.sol | 9 ++++++++- contracts/echidna/EchidnaOptionsContract.sol | 4 ++++ test/optionsContract.test.ts | 9 +++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/contracts/OptionsContract.sol b/contracts/OptionsContract.sol index 4d931f7..981492a 100644 --- a/contracts/OptionsContract.sol +++ b/contracts/OptionsContract.sol @@ -37,7 +37,7 @@ contract OptionsContract is Ownable, ERC20 { mapping(address => Vault) internal vaults; - address payable[] public vaultOwners; + address payable[] private vaultOwners; // 10 is 0.01 i.e. 1% incentive. Number public liquidationIncentive = Number(10, -3); @@ -226,6 +226,13 @@ contract OptionsContract is Ownable, ERC20 { return vaultOwners.length; } + /** + * @notice This function returns all the vault owners + */ + function getVaultOwners() external view returns (address payable[] memory) { + return vaultOwners; + } + /** * @notice Can only be called by owner. Used to update the fees, minCollateralizationRatio, etc * @param _liquidationIncentive The incentive paid to liquidator. 10 is 0.01 i.e. 1% incentive. diff --git a/contracts/echidna/EchidnaOptionsContract.sol b/contracts/echidna/EchidnaOptionsContract.sol index 09346a6..d6563ed 100644 --- a/contracts/echidna/EchidnaOptionsContract.sol +++ b/contracts/echidna/EchidnaOptionsContract.sol @@ -76,4 +76,8 @@ contract EchidnaOptionsContract is TestOptionsContract { { return minCollateralizationRatio.value >= 10; } + + function echidna_vault_balances_sum() public view returns (bool) { + return minCollateralizationRatio.value >= 10; + } } diff --git a/test/optionsContract.test.ts b/test/optionsContract.test.ts index 6460eed..3a0093e 100644 --- a/test/optionsContract.test.ts +++ b/test/optionsContract.test.ts @@ -362,6 +362,10 @@ contract('OptionsContract', accounts => { }); describe('#openVault()', () => { + it('there should be no vault owners before opening any vaults', async () => { + const vaultOwners = await optionsContracts[0].getVaultOwners(); + expect(vaultOwners.toString()).to.equal(''); + }); it('should open first vault correctly', async () => { const result = await optionsContracts[0].openVault({ from: creatorAddress @@ -398,6 +402,11 @@ contract('OptionsContract', accounts => { expect(vault['1'].toString()).to.equal('0'); expect(vault['2'].toString()).to.equal('0'); expect(vault['3']).to.equal(true); + + // test getVaultOwners + const vaultOwners = await optionsContracts[0].getVaultOwners(); + expect(vaultOwners[0].toString()).to.equal(creatorAddress); + expect(vaultOwners[1].toString()).to.equal(firstOwnerAddress); }); }); From 077e56b04807d617dfe2b2f922903556135e5422 Mon Sep 17 00:00:00 2001 From: aparnakr Date: Mon, 17 Aug 2020 18:55:36 -0700 Subject: [PATCH 2/2] some better invariants, not sure why they don't fail --- contracts/echidna/EchidnaOptionsContract.sol | 52 ++++++++++++++++++- .../echidna/contracts/TestOptionsContract.sol | 3 +- 2 files changed, 52 insertions(+), 3 deletions(-) diff --git a/contracts/echidna/EchidnaOptionsContract.sol b/contracts/echidna/EchidnaOptionsContract.sol index d6563ed..80772b9 100644 --- a/contracts/echidna/EchidnaOptionsContract.sol +++ b/contracts/echidna/EchidnaOptionsContract.sol @@ -77,7 +77,55 @@ contract EchidnaOptionsContract is TestOptionsContract { return minCollateralizationRatio.value >= 10; } - function echidna_vault_balances_sum() public view returns (bool) { - return minCollateralizationRatio.value >= 10; + function echidna_vault_collateral_sum() public view returns (bool) { + uint256 collateralSum = 0; + if (vaultOwners.length > 0) { + for (uint256 i = 0; i < vaultOwners.length; i++) { + Vault memory vault = vaults[vaultOwners[i]]; + collateralSum = collateralSum.add(vault.collateral); + } + } + + uint256 collateralBalance = 0; + if (isETH(collateral)) { + collateralBalance = address(this).balance; + } else { + collateralBalance = collateral.balanceOf(address(this)); + } + return collateralBalance == collateralSum; + } + + function echidna_vault_oTokens_sum() public view returns (bool) { + uint256 oTokensSum = 0; + if (vaultOwners.length > 0) { + for (uint256 i = 0; i < vaultOwners.length; i++) { + Vault memory vault = vaults[vaultOwners[i]]; + oTokensSum = oTokensSum.add(vault.oTokensIssued); + } + } + + return oTokensSum == totalSupply(); + } + + function echidna_vault_underlying_sum() public view returns (bool) { + uint256 underlyingSum = 0; + if (vaultOwners.length > 0) { + for (uint256 i = 0; i < vaultOwners.length; i++) { + Vault memory vault = vaults[vaultOwners[i]]; + underlyingSum = underlyingSum.add(vault.underlying); + } + } + + uint256 underlyingBalance = 0; + if (isETH(underlying)) { + underlyingBalance = address(this).balance; + } else { + underlyingBalance = underlying.balanceOf(address(this)); + } + return underlyingBalance == underlyingSum; + } + + function echidna_test_should_fail() public view returns (bool) { + return collateral.balanceOf(address(this)) == 0; } } diff --git a/contracts/echidna/contracts/TestOptionsContract.sol b/contracts/echidna/contracts/TestOptionsContract.sol index c8065d0..a7db7c7 100644 --- a/contracts/echidna/contracts/TestOptionsContract.sol +++ b/contracts/echidna/contracts/TestOptionsContract.sol @@ -127,7 +127,8 @@ contract TestOptionsContract is Ownable, ERC20 { IERC20 _collateral = IERC20(usdc); int32 _collExp = -6; - IERC20 _underlying = IERC20(address(new ERC20())); + // IERC20 _underlying = IERC20(address(new ERC20())); + IERC20 _underlying = IERC20(address(0)); int32 _underlyingExp = -18; int32 _oTokenExchangeExp = -6; uint256 _strikePrice = 25;