Skip to content
This repository has been archived by the owner on Jan 9, 2024. It is now read-only.

Commit

Permalink
Slither findings (#206)
Browse files Browse the repository at this point in the history
* Function ScaledPool._threshold_price(uint256,uint256,uint256) (src/base/ScaledPool.sol#388-391) is not in mixedCase
Reference: https://github.com/crytic/slither/wiki/Detector-Documentation#conformance-to-solidity-naming-conventions

* Fix slither reentrancy warnings
Reference: https://github.com/crytic/slither/wiki/Detector-Documentation#reentrancy-vulnerabilities-3

* Change rate calculation, perform multiply before divide:
ScaledPool._pendingInterestFactor(uint256) (src/base/ScaledPool.sol#386-389) performs a multiplication on the result of a division:
	-rate = (interestRate / SECONDS_PER_YEAR) * elapsed_ (src/base/ScaledPool.sol#387)
Reference: https://github.com/crytic/slither/wiki/Detector-Documentation#divide-before-multiply
Fix tests

* Fix
Variable ScaledPool.RATE_DECREASE_COEFFICIENT (src/base/ScaledPool.sol#29) is too similar to ScaledPool.RATE_INCREASE_COEFFICIENT (src/base/ScaledPool.sol#28)
Variable IERC20Pool.borrowerInfo(address).pendingDebt_ (src/erc20/interfaces/IERC20Pool.sol#189) is too similar to ERC20Pool.borrowerInfo(address).pending_debt (src/erc20/ERC20Pool.sol#286)
Reference: https://github.com/crytic/slither/wiki/Detector-Documentation#variable-names-are-too-similar

IERC20Pool.borrowers(address).inflatorSnapshot (src/erc20/interfaces/IERC20Pool.sol#73) shadows:
	- IScaledPool.inflatorSnapshot() (src/base/interfaces/IScaledPool.sol#94) (function)
Reference: https://github.com/crytic/slither/wiki/Detector-Documentation#local-variable-shadowing

* Remove slither timestamp detector as we're not using it for randomness but storing and using for calculating interest rates
See https://github.com/crytic/slither/wiki/Detector-Documentation#block-timestamp

* Fix Reentrancy in ERC20PoolFactory.deployPool(address,address,uint256) (src/erc20/ERC20PoolFactory.sol#25-36):
	External calls:
	- pool.initialize(interestRate_) (src/erc20/ERC20PoolFactory.sol#31)
	State variables written after the call(s):
	- deployedPools[ERC20_NON_SUBSET_HASH][collateral_][quote_] = pool_ (src/erc20/ERC20PoolFactory.sol#34)
Reference: https://github.com/crytic/slither/wiki/Detector-Documentation#reentrancy-vulnerabilities-1

FenwickTree Fix FenwickTree._findSum(uint256).ss (src/base/FenwickTree.sol#135) is a local variable never initialized
FenwickTree._add(uint256,uint256).ii (src/base/FenwickTree.sol#65) is a local variable never initialized
FenwickTree._prefixSum(uint256).ii (src/base/FenwickTree.sol#108) is a local variable never initialized
FenwickTree._mult(uint256,uint256).sum (src/base/FenwickTree.sol#38) is a local variable never initialized
FenwickTree._remove(uint256,uint256).ii (src/base/FenwickTree.sol#87) is a local variable never initialized
Reference: https://github.com/crytic/slither/wiki/Detector-Documentation#uninitialized-local-variables

FenwickTree improvements - minimize calculations of indexes, LSB

before commit
╭──────────────────────────────────────────────────────────┬─────────────────┬───────┬────────┬────────┬─────────╮
│ src/_test/FenwickTree.t.sol:FenwickTreeInstance contract ┆                 ┆       ┆        ┆        ┆         │
╞══════════════════════════════════════════════════════════╪═════════════════╪═══════╪════════╪════════╪═════════╡
│ Deployment Cost                                          ┆ Deployment Size ┆       ┆        ┆        ┆         │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
│ 516149                                                   ┆ 2610            ┆       ┆        ┆        ┆         │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
│ Function Name                                            ┆ min             ┆ avg   ┆ median ┆ max    ┆ # calls │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
│ add                                                      ┆ 13051           ┆ 97343 ┆ 37808  ┆ 300708 ┆ 4       │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
│ findSum                                                  ┆ 14183           ┆ 21813 ┆ 14714  ┆ 68961  ┆ 8       │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
│ mult                                                     ┆ 58722           ┆ 69520 ┆ 69520  ┆ 80318  ┆ 2       │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
│ prefixSum                                                ┆ 1592            ┆ 11216 ┆ 11934  ┆ 15783  ┆ 8       │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
│ remove                                                   ┆ 13018           ┆ 13018 ┆ 13018  ┆ 13018  ┆ 1       │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
│ treeSum                                                  ┆ 341             ┆ 341   ┆ 341    ┆ 341    ┆ 1       │
╰──────────────────────────────────────────────────────────┴─────────────────┴───────┴────────┴────────┴─────────╯

after commit
╭──────────────────────────────────────────────────────────┬─────────────────┬───────┬────────┬────────┬─────────╮
│ src/_test/FenwickTree.t.sol:FenwickTreeInstance contract ┆                 ┆       ┆        ┆        ┆         │
╞══════════════════════════════════════════════════════════╪═════════════════╪═══════╪════════╪════════╪═════════╡
│ Deployment Cost                                          ┆ Deployment Size ┆       ┆        ┆        ┆         │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
│ 576206                                                   ┆ 2910            ┆       ┆        ┆        ┆         │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
│ Function Name                                            ┆ min             ┆ avg   ┆ median ┆ max    ┆ # calls │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
│ add                                                      ┆ 12579           ┆ 96771 ┆ 37202  ┆ 300102 ┆ 4       │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
│ findSum                                                  ┆ 13434           ┆ 18162 ┆ 13965  ┆ 68212  ┆ 16      │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
│ mult                                                     ┆ 56841           ┆ 67577 ┆ 67577  ┆ 78314  ┆ 2       │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
│ prefixSum                                                ┆ 1427            ┆ 10366 ┆ 10997  ┆ 14730  ┆ 8       │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
│ remove                                                   ┆ 12613           ┆ 12613 ┆ 12613  ┆ 12613  ┆ 1       │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
│ treeSum                                                  ┆ 341             ┆ 341   ┆ 341    ┆ 341    ┆ 2       │
╰──────────────────────────────────────────────────────────┴─────────────────┴───────┴────────┴────────┴─────────╯

* Fix Variable ScaledPool._poolInitializations (src/base/ScaledPool.sol#69) is not in mixedCase
Reference: https://github.com/crytic/slither/wiki/Detector-Documentation#conformance-to-solidity-naming-conventions

* Disable inline as we're not using strict equality to determine if an account has enough Ether or tokens

FenwickTree._mult(uint256,uint256) (src/base/FenwickTree.sol#33-61) uses a dangerous strict equality:
	- (lsbJ < _lsb(i_)) || (i_ == 0 && j <= SIZE) (src/base/FenwickTree.sol#53)
Reference: https://github.com/crytic/slither/wiki/Detector-Documentation#dangerous-strict-equalities
  • Loading branch information
grandizzy authored Aug 23, 2022
1 parent b3bcdcc commit 14f003c
Show file tree
Hide file tree
Showing 14 changed files with 183 additions and 154 deletions.
2 changes: 1 addition & 1 deletion slither.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
"@prb-math=lib/prb-math/",
"@base64-sol=lib/base64/"
],
"detectors_to_exclude": "solc-version"
"detectors_to_exclude": "solc-version,timestamp"
}
66 changes: 33 additions & 33 deletions src/_test/ERC20Pool/ERC20ScaledPoolBorrow.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ contract ERC20ScaledBorrowTest is DSTestPlus {

// get a 21_000 DAI loan
vm.expectEmit(true, true, false, true);
emit Transfer(address(_pool), address(_borrower), 21_000 * 1e18);
vm.expectEmit(true, true, false, true);
emit Borrow(address(_borrower), 2_981.007422784467321543 * 1e18, 21_000 * 1e18);
vm.expectEmit(true, true, false, true);
emit Transfer(address(_pool), address(_borrower), 21_000 * 1e18);
_pool.borrow(21_000 * 1e18, 3000, address(0), address(0));

assertEq(_pool.htp(), 210.201923076923077020 * 1e18);
Expand Down Expand Up @@ -134,9 +134,9 @@ contract ERC20ScaledBorrowTest is DSTestPlus {

// borrow 19_000 DAI
vm.expectEmit(true, true, false, true);
emit Transfer(address(_pool), address(_borrower), 19_000 * 1e18);
vm.expectEmit(true, true, false, true);
emit Borrow(address(_borrower), 2_966.176540084047110076 * 1e18, 19_000 * 1e18);
vm.expectEmit(true, true, false, true);
emit Transfer(address(_pool), address(_borrower), 19_000 * 1e18);
_pool.borrow(19_000 * 1e18, 3500, address(0), address(0));

assertEq(_pool.htp(), 400.384615384615384800 * 1e18);
Expand All @@ -153,9 +153,9 @@ contract ERC20ScaledBorrowTest is DSTestPlus {

// repay partial
vm.expectEmit(true, true, false, true);
emit Transfer(address(_borrower), address(_pool), 10_000 * 1e18);
vm.expectEmit(true, true, false, true);
emit Repay(address(_borrower), 2_966.176540084047110076 * 1e18, 10_000 * 1e18);
vm.expectEmit(true, true, false, true);
emit Transfer(address(_borrower), address(_pool), 10_000 * 1e18);
_pool.repay(10_000 * 1e18, address(0), address(0));

assertEq(_pool.htp(), 300.384615384615384800 * 1e18);
Expand All @@ -172,9 +172,9 @@ contract ERC20ScaledBorrowTest is DSTestPlus {
// repay entire loan
deal(address(_quote), _borrower, _quote.balanceOf(_borrower) + 40 * 1e18);
vm.expectEmit(true, true, false, true);
emit Transfer(address(_borrower), address(_pool), 30_038.461538461538480000 * 1e18);
vm.expectEmit(true, true, false, true);
emit Repay(address(_borrower), BucketMath.MAX_PRICE, 30_038.461538461538480000 * 1e18);
vm.expectEmit(true, true, false, true);
emit Transfer(address(_borrower), address(_pool), 30_038.461538461538480000 * 1e18);
_pool.repay(30_040 * 1e18, address(0), address(0));

assertEq(_pool.htp(), 0);
Expand Down Expand Up @@ -213,53 +213,53 @@ contract ERC20ScaledBorrowTest is DSTestPlus {
assertEq(_pool.borrowerDebt(), 21_020.192307692307702000 * 1e18);
(uint256 debt, uint256 pendingDebt, uint256 col, uint256 inflator) = _pool.borrowerInfo(address(_borrower));
assertEq(debt, 21_020.192307692307702000 * 1e18);
assertEq(pendingDebt, 21_051.890446233188505554 * 1e18);
assertEq(pendingDebt, 21_051.890446235135648008 * 1e18);
assertEq(col, 50 * 1e18);
assertEq(inflator, 1 * 1e18);

skip(864000);
_pool.pledgeCollateral(10 * 1e18, address(0), address(0));
assertEq(_pool.borrowerDebt(), 21_083.636385097313216749 * 1e18);
assertEq(_pool.borrowerDebt(), 21_083.636385101213387311 * 1e18);
(debt, pendingDebt, col, inflator) = _pool.borrowerInfo(address(_borrower));
assertEq(debt, 21_083.636385097313216749 * 1e18);
assertEq(pendingDebt, 21_083.636385097313216749 * 1e18);
assertEq(debt, 21_083.636385101213387311 * 1e18);
assertEq(pendingDebt, 21_083.636385101213387311 * 1e18);
assertEq(col, 60 * 1e18);
assertEq(inflator, 1.003018244385032969 * 1e18);
assertEq(inflator, 1.003018244385218513 * 1e18);

skip(864000);
_pool.pullCollateral(10 * 1e18, address(0), address(0));
assertEq(_pool.borrowerDebt(), 21_118.612213256345042351 * 1e18);
assertEq(_pool.borrowerDebt(), 21_118.612213260575675180 * 1e18);
(debt, pendingDebt, col, inflator) = _pool.borrowerInfo(address(_borrower));
assertEq(debt, 21_118.612213256345042351 * 1e18);
assertEq(pendingDebt, 21_118.612213256345042351 * 1e18);
assertEq(debt, 21_118.612213260575675180 * 1e18);
assertEq(pendingDebt, 21_118.612213260575675180 * 1e18);
assertEq(col, 50 * 1e18);
assertEq(inflator, 1.004682160092703849 * 1e18);
assertEq(inflator, 1.004682160092905114 * 1e18);

skip(864000);
_pool.borrow(0, 3000, address(0), address(0));
assertEq(_pool.borrowerDebt(), 21_157.152642997118010824 * 1e18);
assertEq(_pool.borrowerDebt(), 21_157.152643010853298669 * 1e18);
(debt, pendingDebt, col, inflator) = _pool.borrowerInfo(address(_borrower));
assertEq(debt, 21_157.152642997118010824 * 1e18);
assertEq(pendingDebt, 21_157.152642997118010824 * 1e18);
assertEq(debt, 21_157.152643010853298669 * 1e18);
assertEq(pendingDebt, 21_157.152643010853298669 * 1e18);
assertEq(col, 50 * 1e18);
assertEq(inflator, 1.006515655675266581 * 1e18);
assertEq(inflator, 1.006515655675920014 * 1e18);

skip(864000);
_pool.repay(0, address(0), address(0));
assertEq(_pool.borrowerDebt(), 21_199.628356880380570924 * 1e18);
assertEq(_pool.borrowerDebt(), 21_199.628356897284446170 * 1e18);
(debt, pendingDebt, col, inflator) = _pool.borrowerInfo(address(_borrower));
assertEq(debt, 21_199.628356880380570924 * 1e18);
assertEq(pendingDebt, 21_199.628356880380570924 * 1e18);
assertEq(debt, 21_199.628356897284446170 * 1e18);
assertEq(pendingDebt, 21_199.628356897284446170 * 1e18);
assertEq(col, 50 * 1e18);
assertEq(inflator, 1.008536365726892447 * 1e18);
assertEq(inflator, 1.008536365727696620 * 1e18);

skip(864000);
assertEq(_pool.borrowerDebt(), 21_199.628356880380570924 * 1e18);
assertEq(_pool.borrowerDebt(), 21_199.628356897284446170 * 1e18);
(debt, pendingDebt, col, inflator) = _pool.borrowerInfo(address(_borrower));
assertEq(debt, 21_199.628356880380570924 * 1e18);
assertEq(pendingDebt, 21_246.450141911768550258 * 1e18);
assertEq(debt, 21_199.628356897284446170 * 1e18);
assertEq(pendingDebt, 21_246.450141935843879765 * 1e18);
assertEq(col, 50 * 1e18);
assertEq(inflator, 1.008536365726892447 * 1e18);
assertEq(inflator, 1.008536365727696620 * 1e18);
}

/**
Expand Down Expand Up @@ -308,9 +308,9 @@ contract ERC20ScaledBorrowTest is DSTestPlus {

// should be able to borrow if properly specified
vm.expectEmit(true, true, false, true);
emit Transfer(address(_pool), address(_borrower2), 10 * 1e18);
vm.expectEmit(true, true, false, true);
emit Borrow(address(_borrower2), 2_995.912459898389633881 * 1e18, 10 * 1e18);
vm.expectEmit(true, true, false, true);
emit Transfer(address(_pool), address(_borrower2), 10 * 1e18);
_pool.borrow(10 * 1e18, 3000, address(0), address(0));
}

Expand Down Expand Up @@ -357,9 +357,9 @@ contract ERC20ScaledBorrowTest is DSTestPlus {

// should be able to repay loan if properly specified
vm.expectEmit(true, true, false, true);
emit Transfer(address(_borrower), address(_pool), 0.0001 * 1e18);
vm.expectEmit(true, true, false, true);
emit Repay(address(_borrower), _pool.lup(), 0.0001 * 1e18);
vm.expectEmit(true, true, false, true);
emit Transfer(address(_borrower), address(_pool), 0.0001 * 1e18);
_pool.repay(0.0001 * 1e18, address(_borrower2), address(_borrower2));
}

Expand Down
26 changes: 13 additions & 13 deletions src/_test/ERC20Pool/ERC20ScaledPoolCollateral.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ contract ERC20ScaledCollateralTest is DSTestPlus {

// get a 21_000 Quote loan
vm.expectEmit(true, true, false, true);
emit Transfer(address(_pool), address(_borrower), 21_000 * 1e18);
vm.expectEmit(true, true, false, true);
emit Borrow(address(_borrower), 2_981.007422784467321543 * 1e18, 21_000 * 1e18);
vm.expectEmit(true, true, false, true);
emit Transfer(address(_pool), address(_borrower), 21_000 * 1e18);
_pool.borrow(21_000 * 1e18, 3000, address(0), address(0));

// check pool state
Expand Down Expand Up @@ -147,15 +147,15 @@ contract ERC20ScaledCollateralTest is DSTestPlus {
_pool.pullCollateral(unencumberedCollateral, address(0), address(0));

// check pool state
assertEq(_pool.htp(), 2_989.185764499773229142 * 1e18);
assertEq(_pool.htp(), 2_989.185764500745498129 * 1e18);
assertEq(_pool.lup(), 2_981.007422784467321543 * 1e18);

assertEq(_pool.poolSize(), 30_025.933063898944800000 * 1e18);
assertEq(_pool.borrowerDebt(), 21_049.006823135579696033 * 1e18);
assertEq(_pool.poolSize(), 30_025.933063902025680000 * 1e18);
assertEq(_pool.borrowerDebt(), 21_049.006823139002918431 * 1e18);
assertEq(_pool.lenderDebt(), 21_000 * 1e18);
assertEq(_pool.pledgedCollateral(), _pool.encumberedCollateral(_pool.borrowerDebt(), _pool.lup()));

assertEq(_pool.encumberedCollateral(_pool.borrowerDebt(), _pool.lup()), 7.061038044472344858 * 1e18);
assertEq(_pool.encumberedCollateral(_pool.borrowerDebt(), _pool.lup()), 7.061038044473493202 * 1e18);
assertEq(_pool.encumberedCollateral(_pool.lenderDebt(), _pool.lup()), 7.044598359431304627 * 1e18);

// check borrower state
Expand All @@ -166,7 +166,7 @@ contract ERC20ScaledCollateralTest is DSTestPlus {
_pool.encumberedCollateral(_pool.borrowerDebt(), _pool.lup()),
_pool.encumberedCollateral(borrowerDebt, _pool.lup())
);
assertEq(_collateral.balanceOf(address(_borrower)), 142.938961955527655142 * 1e18);
assertEq(_collateral.balanceOf(address(_borrower)), 142.938961955526506798 * 1e18);

assertEq(_pool.borrowerCollateralization(borrowerDebt, borrowerCollateral, _pool.lup()), _pool.poolCollateralization());
}
Expand Down Expand Up @@ -210,9 +210,9 @@ contract ERC20ScaledCollateralTest is DSTestPlus {
// actor deposits collateral into a bucket
uint256 collateralToDeposit = 4 * 1e18;
vm.expectEmit(true, true, false, true);
emit Transfer(address(_bidder), address(_pool), collateralToDeposit);
vm.expectEmit(true, true, false, true);
emit AddCollateral(address(_bidder), priceAtTestIndex, collateralToDeposit);
vm.expectEmit(true, true, false, true);
emit Transfer(address(_bidder), address(_pool), collateralToDeposit);
_pool.addCollateral(collateralToDeposit, testIndex);

// check bucket state
Expand All @@ -229,18 +229,18 @@ contract ERC20ScaledCollateralTest is DSTestPlus {
// actor withdraws some of their collateral
uint256 collateralToWithdraw = 1.53 * 1e18;
vm.expectEmit(true, true, true, true);
emit Transfer(address(_pool), address(_bidder), collateralToWithdraw);
vm.expectEmit(true, true, true, true);
emit RemoveCollateral(address(_bidder), priceAtTestIndex, collateralToWithdraw);
vm.expectEmit(true, true, true, true);
emit Transfer(address(_pool), address(_bidder), collateralToWithdraw);
uint256 lpRedeemed = _pool.removeCollateral(collateralToWithdraw, testIndex);
assertEq(lpRedeemed, 4_606.664793962758783502850000000 * 1e27);

// actor withdraws remainder of their _collateral
collateralToWithdraw = 2.47 * 1e18;
vm.expectEmit(true, true, true, true);
emit Transfer(address(_pool), address(_bidder), collateralToWithdraw);
vm.expectEmit(true, true, true, true);
emit RemoveCollateral(address(_bidder), priceAtTestIndex, collateralToWithdraw);
vm.expectEmit(true, true, true, true);
emit Transfer(address(_pool), address(_bidder), collateralToWithdraw);
uint256 collateralRemoved;
(collateralRemoved, lpRedeemed) = _pool.removeAllCollateral(testIndex);
assertEq(collateralRemoved, collateralToWithdraw);
Expand Down
8 changes: 4 additions & 4 deletions src/_test/ERC20Pool/ERC20ScaledPoolInterestRate.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,15 @@ contract ERC20ScaledInterestRateTest is DSTestPlus {
assertEq(_pool.htp(), 0);
assertEq(_pool.lup(), BucketMath.MAX_PRICE);

assertEq(_pool.poolSize(), 110_162.490615980593600000 * 1e18);
assertEq(_pool.poolSize(), 110_162.490615984432250000 * 1e18);
assertEq(_pool.borrowerDebt(), 0);
assertEq(_pool.lenderDebt(), 0);

(uint256 debt, uint256 pendingDebt, uint256 col, uint256 inflator) = _pool.borrowerInfo(address(_borrower));
assertEq(debt, 0);
assertEq(pendingDebt, 0);
assertEq(col, 100 * 1e18);
assertEq(inflator, 1.001507985182860621 * 1e18);
assertEq(inflator, 1.001507985182953253 * 1e18);

assertEq(_pool.interestRate(), 0.055 * 1e18); // FIXME here it should decrease
assertEq(_pool.interestRateUpdate(), 864000);
Expand All @@ -122,12 +122,12 @@ contract ERC20ScaledInterestRateTest is DSTestPlus {
_pool.pledgeCollateral(50 * 1e18, address(0), address(0));
_pool.borrow(15_000 * 1e18, 4300, address(0), address(0));
assertEq(_pool.inflatorSnapshot(), 1.0 * 1e18);
assertEq(_pool.pendingInflator(), 1.000005707778845707 * 1e18);
assertEq(_pool.pendingInflator(), 1.000005707778846384 * 1e18);
vm.warp(block.timestamp+3600);

// ensure pendingInflator increases as time passes
assertEq(_pool.inflatorSnapshot(), 1.0 * 1e18);
assertEq(_pool.pendingInflator(), 1.000011415590270154 * 1e18);
assertEq(_pool.pendingInflator(), 1.000011415590271509 * 1e18);
}

// TODO: add test related to pool utilization changes
Expand Down
32 changes: 16 additions & 16 deletions src/_test/ERC20Pool/ERC20ScaledPoolPrecision.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ contract ERC20ScaledPoolPrecisionTest is DSTestPlus {
_pool.addQuoteToken(50_000 * _quotePoolPrecision, 2549);
_pool.addQuoteToken(50_000 * _quotePoolPrecision, 2550);
vm.expectEmit(true, true, false, true);
emit Transfer(address(_lender), address(_pool), 50_000 * _quotePrecision);
vm.expectEmit(true, true, false, true);
emit AddQuoteToken(address(_lender), _pool.indexToPrice(2551), 50_000 * _quotePoolPrecision, BucketMath.MAX_PRICE);
vm.expectEmit(true, true, false, true);
emit Transfer(address(_lender), address(_pool), 50_000 * _quotePrecision);
_pool.addQuoteToken(50_000 * _quotePoolPrecision, 2551);

// check balances
Expand All @@ -102,9 +102,9 @@ contract ERC20ScaledPoolPrecisionTest is DSTestPlus {

// lender removes some quote token from highest priced bucket
vm.expectEmit(true, true, false, true);
emit Transfer(address(_pool), address(_lender), 25_000 * _quotePrecision);
vm.expectEmit(true, true, false, true);
emit RemoveQuoteToken(address(_lender), _pool.indexToPrice(2549), 25_000 * _quotePoolPrecision, BucketMath.MAX_PRICE);
vm.expectEmit(true, true, false, true);
emit Transfer(address(_pool), address(_lender), 25_000 * _quotePrecision);
_pool.removeQuoteToken(25_000 * _quotePoolPrecision, 2549);

// check balances
Expand Down Expand Up @@ -142,9 +142,9 @@ contract ERC20ScaledPoolPrecisionTest is DSTestPlus {
// borrowers adds collateral
changePrank(_borrower);
vm.expectEmit(true, true, false, true);
emit Transfer(address(_borrower), address(_pool), 50 * _collateralPrecision);
vm.expectEmit(true, true, false, true);
emit PledgeCollateral(address(_borrower), 50 * _collateralPoolPrecision);
vm.expectEmit(true, true, false, true);
emit Transfer(address(_borrower), address(_pool), 50 * _collateralPrecision);
_pool.pledgeCollateral(50 * _collateralPoolPrecision, address(0), address(0));

// check balances
Expand All @@ -164,9 +164,9 @@ contract ERC20ScaledPoolPrecisionTest is DSTestPlus {

// borrower borrows
vm.expectEmit(true, true, false, true);
emit Transfer(address(_pool), address(_borrower), 10_000 * _quotePrecision);
vm.expectEmit(true, true, false, true);
emit Borrow(address(_borrower), _pool.indexToPrice(2549), 10_000 * _quotePoolPrecision);
vm.expectEmit(true, true, false, true);
emit Transfer(address(_pool), address(_borrower), 10_000 * _quotePrecision);
_pool.borrow(10_000 * _quotePoolPrecision, 3000, address(0), address(0));

// check balances
Expand All @@ -191,9 +191,9 @@ contract ERC20ScaledPoolPrecisionTest is DSTestPlus {

// borrower repays half of loan
vm.expectEmit(true, true, false, true);
emit Transfer(address(_borrower), address(_pool), 5_000 * _quotePrecision);
vm.expectEmit(true, true, false, true);
emit Repay(address(_borrower), _pool.indexToPrice(2549), 5_000 * _quotePoolPrecision);
vm.expectEmit(true, true, false, true);
emit Transfer(address(_borrower), address(_pool), 5_000 * _quotePrecision);
_pool.repay(5_000 * _quotePoolPrecision, address(0), address(0));

// check balances
Expand All @@ -218,9 +218,9 @@ contract ERC20ScaledPoolPrecisionTest is DSTestPlus {
// remove all of the remaining unencumbered collateral
uint256 unencumberedCollateral = col - _pool.encumberedCollateral(debt, _pool.lup());
vm.expectEmit(true, true, false, true);
emit Transfer(address(_pool), address(_borrower), unencumberedCollateral / _pool.collateralScale());
vm.expectEmit(true, true, false, true);
emit PullCollateral(address(_borrower), unencumberedCollateral);
vm.expectEmit(true, true, false, true);
emit Transfer(address(_pool), address(_borrower), unencumberedCollateral / _pool.collateralScale());
_pool.pullCollateral(unencumberedCollateral, address(0), address(0));

// FIXME: check balances
Expand Down Expand Up @@ -260,11 +260,11 @@ contract ERC20ScaledPoolPrecisionTest is DSTestPlus {
uint256 collateralRequired = Maths.wdiv(quoteToPurchase, _pool.indexToPrice(2549));
uint256 adjustedCollateralReq = collateralRequired / _pool.collateralScale();
vm.expectEmit(true, true, false, true);
emit Purchase(address(_bidder), _pool.indexToPrice(2549), quoteToPurchase, collateralRequired);
vm.expectEmit(true, true, false, true);
emit Transfer(address(_bidder), address(_pool), adjustedCollateralReq);
vm.expectEmit(true, true, false, true);
emit Transfer(address(_pool), address(_bidder), 500 * _quotePrecision);
vm.expectEmit(true, true, false, true);
emit Purchase(address(_bidder), _pool.indexToPrice(2549), quoteToPurchase, collateralRequired);
// _bidder.purchaseQuote(_pool, quoteToPurchase, 2549);

// check bucket state
Expand All @@ -291,9 +291,9 @@ contract ERC20ScaledPoolPrecisionTest is DSTestPlus {
changePrank(_lender);
uint256 lpRedemption = Maths.wrdivr(Maths.wmul(availableCollateral, _pool.indexToPrice(2549)), _pool.exchangeRate(2549));
vm.expectEmit(true, true, true, true);
emit Transfer(address(_pool), address(_lender), adjustedCollateralReq);
vm.expectEmit(true, true, true, true);
emit ClaimCollateral(address(_lender), _pool.indexToPrice(2549), availableCollateral, lpRedemption);
vm.expectEmit(true, true, true, true);
emit Transfer(address(_pool), address(_lender), adjustedCollateralReq);
// _lender.claimCollateral(_pool, availableCollateral, 2549);

// check bucket state
Expand Down
Loading

0 comments on commit 14f003c

Please sign in to comment.