Skip to content

Commit

Permalink
refactor: integration tests (#32)
Browse files Browse the repository at this point in the history
* feat: update the core package

* refactor: clean up `IntegrationBase`, add helpers

* refactor: clean up arbitration tests

* refactor: clean up dispute escalation tests

* refactor: clean up finalization tests

* refactor: clean up payments tests

* refactor: clean up response proposal tests

* refactor: clean up request creation tests

* refactor: clean up response dispute tests

* refactor: clean up root verification tests

* refactor: clean up accounting tests

* refactor: clean up bond escalation tests

* refactor: remove an unused helper

* test: additional checks for the request creation

* test: additional checks and comments for dispute tests

* style: run linter
  • Loading branch information
gas1cent authored Dec 13, 2023
1 parent bbb8aa5 commit f5d18c7
Show file tree
Hide file tree
Showing 14 changed files with 1,014 additions and 1,383 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,9 @@
"package.json": "sort-package-json"
},
"dependencies": {
"@defi-wonderland/prophet-core-contracts": "0.0.0-ad870035",
"@defi-wonderland/prophet-core-contracts": "0.0.0-49fc8fa6",
"@defi-wonderland/solidity-utils": "0.0.0-3e9c8e8b",
"@openzeppelin/contracts": "^4.9.3",
"ds-test": "https://github.com/dapphub/ds-test.git#e282159d5170298eb2455a6c05280ab5a73a4ef0",
"forge-std": "https://github.com/foundry-rs/forge-std.git#f73c73d2018eb6a111f35e4dae7b4f27401e9421",
"solmate": "https://github.com/transmissions11/solmate.git#bfc9c25865a274a7827fea5abf6e4fb64fc64e6c"
},
"devDependencies": {
Expand All @@ -52,6 +50,8 @@
"@typechain/truffle-v5": "8.0.2",
"@typechain/web3-v1": "6.0.2",
"dotenv-cli": "7.2.1",
"ds-test": "https://github.com/dapphub/ds-test.git#e282159d5170298eb2455a6c05280ab5a73a4ef0",
"forge-std": "https://github.com/foundry-rs/forge-std.git#f73c73d2018eb6a111f35e4dae7b4f27401e9421",
"fs-extra": "10.1.0",
"husky": "8.0.3",
"lint-staged": "13.2.2",
Expand Down
142 changes: 87 additions & 55 deletions solidity/test/integration/AccountingExtension.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,34 @@ import './IntegrationBase.sol';
contract Integration_AccountingExtension is IntegrationBase {
address public user = makeAddr('user');

function test_depositERC20(uint256 _initialBalance, uint256 _depositAmount) public {
vm.assume(_initialBalance >= _depositAmount);
_forBondDepositERC20(_accountingExtension, user, usdc, _depositAmount, _initialBalance);
// Check: is virtual balance updated?
assertEq(_depositAmount, _accountingExtension.balanceOf(user, usdc));
// Check: is token contract balance updated?
assertEq(_initialBalance - _depositAmount, usdc.balanceOf(user));
}
function setUp() public override {
super.setUp();

function test_withdrawERC20(uint256 _initialBalance, uint256 _depositAmount, uint256 _withdrawAmount) public {
vm.assume(_withdrawAmount <= _depositAmount);
// Deposit some USDC
_forBondDepositERC20(_accountingExtension, user, usdc, _depositAmount, _initialBalance);
// Full allowance for both tokens
vm.prank(user);
usdc.approve(address(_accountingExtension), type(uint256).max);

vm.prank(user);
_accountingExtension.withdraw(usdc, _withdrawAmount);
weth.approve(address(_accountingExtension), type(uint256).max);
}

/**
* @notice Depositing ERC20 should update the virtual balance and the token contract balance
*/
function test_depositERC20(uint256 _initialBalance, uint256 _depositAmount) public {
vm.assume(_initialBalance >= _depositAmount);
_deposit(_accountingExtension, user, usdc, _depositAmount, _initialBalance);

// Check: is virtual balance updated?
assertEq(_depositAmount - _withdrawAmount, _accountingExtension.balanceOf(user, usdc));
assertEq(_depositAmount, _accountingExtension.balanceOf(user, usdc));

// Check: is token contract balance updated?
assertEq(_initialBalance - _depositAmount + _withdrawAmount, usdc.balanceOf(user));
assertEq(_initialBalance - _depositAmount, usdc.balanceOf(user));
}

/**
* @notice Depositing more than the user's balance should revert
*/
function test_depositERC20_invalidAmount(uint256 _initialBalance, uint256 _invalidDepositAmount) public {
vm.assume(_invalidDepositAmount > _initialBalance);
deal(address(usdc), user, _initialBalance);
Expand All @@ -43,80 +48,107 @@ contract Integration_AccountingExtension is IntegrationBase {
vm.stopPrank();
}

/**
* @notice Withdrawing ERC20 should update the virtual balance and the token contract balance
*/
function test_withdrawERC20(uint256 _initialBalance, uint256 _depositAmount, uint256 _withdrawAmount) public {
vm.assume(_withdrawAmount <= _depositAmount);
_deposit(_accountingExtension, user, usdc, _depositAmount, _initialBalance);

vm.prank(user);
_accountingExtension.withdraw(usdc, _withdrawAmount);

// Check: is virtual balance updated?
assertEq(_depositAmount - _withdrawAmount, _accountingExtension.balanceOf(user, usdc));

// Check: is token contract balance updated?
assertEq(_initialBalance - _depositAmount + _withdrawAmount, usdc.balanceOf(user));
}

/**
* @notice Withdrawing more than the user's virtual balance should revert
*/
function test_withdrawERC20_insufficientFunds(
uint256 _initialBalance,
uint256 _depositAmount,
uint256 _withdrawAmount
) public {
vm.assume(_withdrawAmount > _depositAmount);
_forBondDepositERC20(_accountingExtension, user, usdc, _depositAmount, _initialBalance);
_deposit(_accountingExtension, user, usdc, _depositAmount, _initialBalance);

// Check: does it revert if trying to withdraw an amount greater than virtual balance?
vm.expectRevert(IAccountingExtension.AccountingExtension_InsufficientFunds.selector);
vm.prank(user);
_accountingExtension.withdraw(usdc, _withdrawAmount);
}

/**
* @notice Withdrawing more WETH than was deposited by the user should revert
*/
function test_withdrawETH_insufficientFunds(
uint256 _initialBalance,
uint256 _depositAmount,
uint256 _withdrawAmount
) public {
vm.assume(_withdrawAmount > _depositAmount);
_forBondDepositERC20(_accountingExtension, user, IERC20(address(weth)), _depositAmount, _initialBalance);
_deposit(_accountingExtension, user, weth, _depositAmount, _initialBalance);

// Check: does it revert if trying to withdraw an amount greater than virtual balance?
vm.expectRevert(IAccountingExtension.AccountingExtension_InsufficientFunds.selector);
vm.prank(user);
_accountingExtension.withdraw(weth, _withdrawAmount);
}

/**
* @notice Withdrawing the bonded funds should revert
*/
function test_withdrawBondedFunds(uint256 _initialBalance, uint256 _bondAmount) public {
vm.assume(_bondAmount > 0);
_forBondDepositERC20(_accountingExtension, user, usdc, _bondAmount, _initialBalance);

HttpRequestModule _requestModule = new HttpRequestModule(oracle);
BondedResponseModule _responseModule = new BondedResponseModule(oracle);
BondedDisputeModule _bondedDisputeModule = new BondedDisputeModule(oracle);

IOracle.Request memory _request = IOracle.Request({
nonce: 0,
requester: user,
requestModuleData: abi.encode(
IHttpRequestModule.RequestParameters({
url: '',
method: IHttpRequestModule.HttpMethod.GET,
body: '',
accountingExtension: _accountingExtension,
paymentToken: usdc,
paymentAmount: _bondAmount
})
),
responseModuleData: abi.encode(
IBondedResponseModule.RequestParameters({
accountingExtension: _accountingExtension,
bondToken: usdc,
bondSize: _bondAmount,
deadline: block.timestamp + BLOCK_TIME * 600,
disputeWindow: _baseDisputeWindow
})
),
disputeModuleData: abi.encode(),
resolutionModuleData: abi.encode(),
finalityModuleData: abi.encode(),
requestModule: address(_requestModule),
responseModule: address(_responseModule),
disputeModule: address(_bondedDisputeModule),
resolutionModule: address(0),
finalityModule: address(0)
});
_deposit(_accountingExtension, user, usdc, _bondAmount, _initialBalance);

mockRequest.requestModuleData = abi.encode(
IHttpRequestModule.RequestParameters({
url: _expectedUrl,
body: _expectedBody,
method: _expectedMethod,
accountingExtension: _accountingExtension,
paymentToken: usdc,
paymentAmount: _bondAmount
})
);

mockRequest.requester = user;

vm.startPrank(user);
_accountingExtension.approveModule(address(_requestModule));
oracle.createRequest(_request, _ipfsHash);
oracle.createRequest(mockRequest, _ipfsHash);

// Check: does it revert if trying to withdraw an amount that is bonded to a request?
vm.expectRevert(IAccountingExtension.AccountingExtension_InsufficientFunds.selector);
_accountingExtension.withdraw(usdc, _bondAmount);
vm.stopPrank();
}

/**
* @notice Deposits the specified amount of tokens into the accounting extension
*
* @param _accounting The accounting extension
* @param _depositor The depositor
* @param _token The token to deposit
* @param _depositAmount The amount to deposit
* @param _balanceIncrease The amount to increase the depositor's initial balance by
*/
function _deposit(
IAccountingExtension _accounting,
address _depositor,
IERC20 _token,
uint256 _depositAmount,
uint256 _balanceIncrease
) internal {
vm.assume(_balanceIncrease >= _depositAmount);
deal(address(_token), _depositor, _balanceIncrease);

vm.prank(_depositor);
_accounting.deposit(_token, _depositAmount);
}
}
Loading

0 comments on commit f5d18c7

Please sign in to comment.