Skip to content

Commit

Permalink
test: Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bingen committed Mar 5, 2024
1 parent 8938ed0 commit 5d710e0
Show file tree
Hide file tree
Showing 28 changed files with 1,308 additions and 1,539 deletions.
7 changes: 7 additions & 0 deletions contracts/src/test/TestContracts/BaseTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ contract BaseTest is Test {
address public D;
address public E;
address public F;
address public G;

uint256 public constant MAX_UINT256 = type(uint256).max;
uint256 public constant SECONDS_IN_1_YEAR = 31536000; // 60*60*24*365
Expand Down Expand Up @@ -93,6 +94,12 @@ contract BaseTest is Test {
vm.stopPrank();
}

function checkRecoveryMode(bool _enabled) public {
uint256 price = priceFeed.getPrice();
bool recoveryMode = troveManager.checkRecoveryMode(price);
assertEq(recoveryMode, _enabled);
}

function logContractAddresses() public view {
console.log("ActivePool addr: ", address(activePool));
console.log("BorrowerOps addr: ", address(borrowerOperations));
Expand Down
48 changes: 26 additions & 22 deletions contracts/src/test/TestContracts/DevTestSetup.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import "../../DefaultPool.sol";
import "../../GasPool.sol";
import "../../HintHelpers.sol";
import "../../MultiTroveGetter.sol";
import "./PriceFeedTestnet.sol";
import "../../SortedTroves.sol";
import "../../StabilityPool.sol";
import "../../TroveManager.sol";
Expand All @@ -23,7 +22,23 @@ import "./BaseTest.sol";

contract DevTestSetup is BaseTest {

IPriceFeedTestnet priceFeed;
IERC20 WETH;

function giveAndApproveETH(address _account, uint256 _amount) public {
// Give some ETH to test accounts
deal(address(WETH), _account, _amount);

// Check accounts are funded
assertEq(WETH.balanceOf(_account), _amount);

// Approve ETH to BorrowerOperations
vm.startPrank(_account);
WETH.approve(address(borrowerOperations), _amount);
vm.stopPrank();

// Check approvals
assertEq(WETH.allowance(_account, address(borrowerOperations)), _amount);
}

function setUp() public virtual {
// Start tests at a non-zero timestamp
Expand All @@ -32,27 +47,10 @@ contract DevTestSetup is BaseTest {
accounts = new Accounts();
createAccounts();

(A, B, C, D, E, F) =
(accountsList[0], accountsList[1], accountsList[2], accountsList[3], accountsList[4], accountsList[5]);
(A, B, C, D, E, F, G) =
(accountsList[0], accountsList[1], accountsList[2], accountsList[3], accountsList[4], accountsList[5], accountsList[6]);

// Give some StETH to test accounts
uint256 initialETHAmount = 10_000e18;
deal(A, initialETHAmount);
deal(B, initialETHAmount);
deal(C, initialETHAmount);
deal(D, initialETHAmount);
deal(E, initialETHAmount);
deal(F, initialETHAmount);

// Check accounts are funded
assertEq(A.balance, initialETHAmount);
assertEq(B.balance, initialETHAmount);
assertEq(C.balance, initialETHAmount);
assertEq(D.balance, initialETHAmount);
assertEq(E.balance, initialETHAmount);
assertEq(F.balance, initialETHAmount);

IERC20 WETH = new ERC20("Wrapped ETH", "WETH");
WETH = new ERC20("Wrapped ETH", "WETH");

// TODO: optimize deployment order & constructor args & connector functions

Expand Down Expand Up @@ -128,5 +126,11 @@ contract DevTestSetup is BaseTest {
address(troveManager),
address(activePool)
);

// Give some ETH to test accounts, and approve it to BorrowerOperations
uint256 initialETHAmount = 10_000e18;
for(uint256 i = 0; i < 6; i++) { // A to F
giveAndApproveETH(accountsList[i], initialETHAmount);
}
}
}
18 changes: 17 additions & 1 deletion contracts/src/test/TestContracts/NonPayable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,38 @@

pragma solidity 0.8.18;

//import "../Dependencies/console.sol";
import "openzeppelin-contracts/contracts/token/ERC20/utils/SafeERC20.sol";
// import "forge-std/console.sol";


contract NonPayable {
using SafeERC20 for IERC20;

bool isPayable;
IERC20 public ETH;

function setETH(IERC20 _eth) external {
ETH = _eth;
}

function setPayable(bool _isPayable) external {
isPayable = _isPayable;
}

function forward(address _dest, bytes calldata _data) external payable {
//console.logBytes(_data);
(bool success, bytes memory returnData) = _dest.call{ value: msg.value }(_data);
//console.log(msg.value, "msg.value");
//console.log(success, "success");
//console.logBytes(returnData);
require(success, string(returnData));
}

function receiveETH(uint256 _amount) external {
// Pull ETH tokens from sender
ETH.safeTransferFrom(msg.sender, address(this), _amount);
}

receive() external payable {
require(isPayable);
}
Expand Down
19 changes: 19 additions & 0 deletions contracts/src/test/basicOps.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,25 @@ import "./TestContracts/DevTestSetup.sol";

contract BasicOps is DevTestSetup {

function testOpenTroveFailsWithoutAllowance() public {
priceFeed.setPrice(2000e18);

vm.startPrank(G);
vm.expectRevert("ERC20: insufficient allowance");
borrowerOperations.openTrove(1e18, 2e18, 2000e18, ZERO_ADDRESS, ZERO_ADDRESS, 0);
vm.stopPrank();
}

function testOpenTroveFailsWithoutBalance() public {
priceFeed.setPrice(2000e18);

vm.startPrank(G);
WETH.approve(address(borrowerOperations), 2e18);
vm.expectRevert("ERC20: transfer amount exceeds balance");
borrowerOperations.openTrove(1e18, 2e18, 2000e18, ZERO_ADDRESS, ZERO_ADDRESS, 0);
vm.stopPrank();
}

function testOpenTrove() public {
priceFeed.setPrice(2000e18);
uint256 trovesCount = troveManager.getTroveOwnersCount();
Expand Down
29 changes: 29 additions & 0 deletions contracts/src/test/borrowerOperations.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
pragma solidity ^0.8.18;

import "./TestContracts/DevTestSetup.sol";


contract BorrowerOperationsTest is DevTestSetup {
// closeTrove(): reverts when trove is the only one in the system", async () =>
function testCloseLastTroveReverts() public {
priceFeed.setPrice(2000e18);
openTroveNoHints100pctMaxFee(A, 100 ether, 100000e18, 1e17);

// Artificially mint to Alice so she has enough to close her trove
deal(address(boldToken), A, 100200e18);

// Check she has more Bold than her trove debt
uint256 aliceBal = boldToken.balanceOf(A);
(uint256 aliceDebt,,,) = troveManager.getEntireDebtAndColl(A);
assertGe(aliceBal, aliceDebt, "Not enough balance");

// check Recovery Mode
checkRecoveryMode(false);

// Alice attempts to close her trove
vm.startPrank(A);
vm.expectRevert("TroveManager: Only one trove in the system");
borrowerOperations.closeTrove();
vm.stopPrank();
}
}
37 changes: 15 additions & 22 deletions contracts/test/AccessControlTest.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const deploymentHelper = require("../utils/deploymentHelpers.js");
const testHelpers = require("../utils/testHelpers.js");
const { fundAccounts } = require("../utils/fundAccounts.js");
const TroveManagerTester = artifacts.require("TroveManagerTester");

const th = testHelpers.TestHelper;
Expand Down Expand Up @@ -53,7 +54,10 @@ contract(

await deploymentHelper.connectCoreContracts(coreContracts);

for (account of accounts.slice(0, 10)) {

await fundAccounts(accounts.slice(0, 10), coreContracts.WETH);

for (const account of accounts.slice(0, 10)) {
await th.openTrove(coreContracts, {
extraBoldAmount: toBN(dec(20000, 18)),
ICR: toBN(dec(2, 18)),
Expand All @@ -68,6 +72,7 @@ contract(
try {
const tx1 = await borrowerOperations.moveETHGainToTrove(
bob,
1,
{ from: bob }
);
} catch (err) {
Expand Down Expand Up @@ -239,15 +244,11 @@ contract(
}
});

// fallback (payment)
it("fallback(): reverts when called by an account that is not Borrower Operations nor Default Pool", async () => {
// receiveETH (payment)
it("receiveETH(): reverts when called by an account that is not Borrower Operations nor Default Pool", async () => {
// Attempt call from alice
try {
const txAlice = await web3.eth.sendTransaction({
from: alice,
to: activePool.address,
value: 100,
});
await activePool.receiveETH(100, { from: alice });
} catch (err) {
assert.include(err.message, "revert");
assert.include(
Expand Down Expand Up @@ -298,15 +299,11 @@ contract(
}
});

// fallback (payment)
it("fallback(): reverts when called by an account that is not the Active Pool", async () => {
// receiveETH (payment)
it("receiveETH(): reverts when called by an account that is not the Active Pool", async () => {
// Attempt call from alice
try {
const txAlice = await web3.eth.sendTransaction({
from: alice,
to: defaultPool.address,
value: 100,
});
await defaultPool.receiveETH(100, { from: alice });
} catch (err) {
assert.include(err.message, "revert");
assert.include(
Expand Down Expand Up @@ -334,15 +331,11 @@ contract(

// --- onlyActivePool ---

// fallback (payment)
it("fallback(): reverts when called by an account that is not the Active Pool", async () => {
// receiveETH (payment)
it("receiveETH(): reverts when called by an account that is not the Active Pool", async () => {
// Attempt call from alice
try {
const txAlice = await web3.eth.sendTransaction({
from: alice,
to: stabilityPool.address,
value: 100,
});
await stabilityPool.receiveETH(100, { from: alice });
} catch (err) {
assert.include(err.message, "revert");
assert.include(
Expand Down
Loading

0 comments on commit 5d710e0

Please sign in to comment.