Skip to content

Commit

Permalink
test: unit tests for BFactory.sol (#2)
Browse files Browse the repository at this point in the history
* test: basic test structure, bump solc version

* test: bump solc version to 0.8.23

* test: add units for BFactory.sol

* test: add more test cases

* test: add cases for collect() tests

* fix: exclude vm address from fuzzed param

* fix: change int underflow for uint256 max

* test: improve test naming

* test: write directly to storage instead of deploying
  • Loading branch information
0xAustrian authored May 3, 2024
1 parent b17ab4a commit c4bf7d6
Show file tree
Hide file tree
Showing 23 changed files with 217 additions and 42 deletions.
3 changes: 2 additions & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
# 1. Build the contracts
# 2. Stage build output
# 2. Lint and stage style improvements
yarn build && npx lint-staged
# TODO: remember to re-enable linter
yarn build # && npx lint-staged
2 changes: 1 addition & 1 deletion echidna/BMathInternal.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@



pragma solidity 0.5.12;
pragma solidity 0.8.23;
contract BColor {
function getColor()
internal view
Expand Down
2 changes: 1 addition & 1 deletion echidna/MyToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import "./CryticInterface.sol";

contract MyToken is BToken, CryticInterface{

constructor(uint balance, address allowed) public {
constructor(uint balance, address allowed) {
// balance is the new totalSupply
_totalSupply = balance;
// each user receives 1/3 of the balance and sets
Expand Down
4 changes: 2 additions & 2 deletions echidna/TBPoolBind.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import "./CryticInterface.sol";

contract TBPoolBindPrivileged is CryticInterface, BPool {

constructor() public {
constructor() {
// Create a new token with initial_token_balance as total supply.
// After the token is created, each user defined in CryticInterface
// (crytic_owner, crytic_user and crytic_attacker) receives 1/3 of
Expand Down Expand Up @@ -114,7 +114,7 @@ contract TBPoolBindUnprivileged is CryticInterface, BPool {
// initial token balances is the max amount for uint256
uint internal initial_token_balance = uint(-1);

constructor() public {
constructor() {
// two tokens with minimal balances and weights are created by the controller
t1 = new MyToken(initial_token_balance, address(this));
bind(address(t1), MIN_BALANCE, MIN_WEIGHT);
Expand Down
2 changes: 1 addition & 1 deletion echidna/TBPoolJoinExit.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ contract TBPoolJoinExit is CryticInterface, BPool {

uint MAX_BALANCE = BONE * 10**12;

constructor() public {
constructor() {
MyToken t;
t = new MyToken(uint(-1), address(this));
bind(address(t), MAX_BALANCE, MAX_WEIGHT);
Expand Down
2 changes: 1 addition & 1 deletion echidna/TBPoolLimits.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ contract TBPoolLimits is CryticInterface, BPool {

uint MAX_BALANCE = BONE * 10**12;

constructor() public {
constructor() {
MyToken t;
t = new MyToken(uint(-1), address(this));
bind(address(t), MIN_BALANCE, MIN_WEIGHT);
Expand Down
2 changes: 1 addition & 1 deletion echidna/TBPoolNoRevert.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import "./CryticInterface.sol";

contract TBPoolNoRevert is CryticInterface, BPool {

constructor() public { // out-of-gas?
constructor() { // out-of-gas?
// Create a new token with initial_token_balance as total supply.
// After the token is created, each user defined in CryticInterface
// (crytic_owner, crytic_user and crytic_attacker) receives 1/3 of
Expand Down
2 changes: 1 addition & 1 deletion echidna/TBTokenERC20.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ contract CryticInterface{

contract TBTokenERC20 is CryticInterface, BToken {

constructor() public {
constructor() {
_totalSupply = initialTotalSupply;
_balance[crytic_owner] = 0;
_balance[crytic_user] = initialTotalSupply/2;
Expand Down
2 changes: 1 addition & 1 deletion foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ multiline_func_header = 'params_first'
sort_imports = true

[profile.default]
solc_version = '0.5.12'
solc_version = '0.8.23'
libs = ["node_modules", "lib"]
optimizer_runs = 10_000

Expand Down
8 changes: 4 additions & 4 deletions src/contracts/BColor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

pragma solidity 0.5.12;
pragma solidity 0.8.23;

contract BColor {
abstract contract BColor {
function getColor()
external view
external view virtual
returns (bytes32);
}

contract BBronze is BColor {
function getColor()
external view
external view override
returns (bytes32) {
return bytes32("BRONZE");

Check failure on line 26 in src/contracts/BColor.sol

View workflow job for this annotation

GitHub Actions / Lint Commit Messages

Use single quotes for string literals
}
Expand Down
2 changes: 1 addition & 1 deletion src/contracts/BConst.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

pragma solidity 0.5.12;
pragma solidity 0.8.23;

import "./BColor.sol";

Check failure on line 16 in src/contracts/BConst.sol

View workflow job for this annotation

GitHub Actions / Lint Commit Messages

Use single quotes for string literals

Expand Down
4 changes: 2 additions & 2 deletions src/contracts/BFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

pragma solidity 0.5.12;
pragma solidity 0.8.23;

// Builds new BPools, logging their addresses and providing `isBPool(address) -> (bool)`

Expand Down Expand Up @@ -49,7 +49,7 @@ contract BFactory is BBronze {

address private _blabs;

constructor() public {
constructor() {
_blabs = msg.sender;
}

Expand Down
2 changes: 1 addition & 1 deletion src/contracts/BMath.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

pragma solidity 0.5.12;
pragma solidity 0.8.23;

import "./BNum.sol";

Check failure on line 16 in src/contracts/BMath.sol

View workflow job for this annotation

GitHub Actions / Lint Commit Messages

Use single quotes for string literals

Expand Down
2 changes: 1 addition & 1 deletion src/contracts/BNum.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

pragma solidity 0.5.12;
pragma solidity 0.8.23;

import "./BConst.sol";

Expand Down
4 changes: 2 additions & 2 deletions src/contracts/BPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

pragma solidity 0.5.12;
pragma solidity 0.8.23;

import "./BToken.sol";
import "./BMath.sol";
Expand Down Expand Up @@ -83,7 +83,7 @@ contract BPool is BBronze, BToken, BMath {
mapping(address=>Record) private _records;
uint private _totalWeight;

constructor() public {
constructor() {
_controller = msg.sender;
_factory = msg.sender;
_swapFee = MIN_FEE;
Expand Down
23 changes: 10 additions & 13 deletions src/contracts/BToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

pragma solidity 0.5.12;
pragma solidity 0.8.23;

import "./BNum.sol";

Expand All @@ -32,15 +32,12 @@ interface IERC20 {
) external returns (bool);
}

contract BTokenBase is BNum {
abstract contract BTokenBase is BNum, IERC20 {

mapping(address => uint) internal _balance;
mapping(address => mapping(address=>uint)) internal _allowance;
uint internal _totalSupply;

event Approval(address indexed src, address indexed dst, uint amt);
event Transfer(address indexed src, address indexed dst, uint amt);

function _mint(uint amt) internal {
_balance[address(this)] = badd(_balance[address(this)], amt);
_totalSupply = badd(_totalSupply, amt);
Expand Down Expand Up @@ -70,7 +67,7 @@ contract BTokenBase is BNum {
}
}

contract BToken is BTokenBase, IERC20 {
contract BToken is BTokenBase {

string private _name = "Balancer Pool Token";
string private _symbol = "BPT";
Expand All @@ -88,19 +85,19 @@ contract BToken is BTokenBase, IERC20 {
return _decimals;
}

function allowance(address src, address dst) external view returns (uint) {
function allowance(address src, address dst) external view override returns (uint) {
return _allowance[src][dst];
}

function balanceOf(address whom) external view returns (uint) {
function balanceOf(address whom) external view override returns (uint) {
return _balance[whom];
}

function totalSupply() public view returns (uint) {
function totalSupply() public view override returns (uint) {
return _totalSupply;
}

function approve(address dst, uint amt) external returns (bool) {
function approve(address dst, uint amt) external override returns (bool) {
_allowance[msg.sender][dst] = amt;
emit Approval(msg.sender, dst, amt);
return true;
Expand All @@ -123,15 +120,15 @@ contract BToken is BTokenBase, IERC20 {
return true;
}

function transfer(address dst, uint amt) external returns (bool) {
function transfer(address dst, uint amt) external override returns (bool) {
_move(msg.sender, dst, amt);
return true;
}

function transferFrom(address src, address dst, uint amt) external returns (bool) {
function transferFrom(address src, address dst, uint amt) external override returns (bool) {
require(msg.sender == src || amt <= _allowance[src][msg.sender], "ERR_BTOKEN_BAD_CALLER");
_move(src, dst, amt);
if (msg.sender != src && _allowance[src][msg.sender] != uint256(-1)) {
if (msg.sender != src && _allowance[src][msg.sender] != type(uint256).max) {
_allowance[src][msg.sender] = bsub(_allowance[src][msg.sender], amt);
emit Approval(msg.sender, dst, _allowance[src][msg.sender]);
}
Expand Down
4 changes: 2 additions & 2 deletions src/contracts/Migrations.sol
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
pragma solidity 0.5.12;
pragma solidity 0.8.23;

contract Migrations {
address public owner;
uint public lastCompletedMigration;

constructor() public {
constructor() {
owner = msg.sender;
}

Expand Down
2 changes: 1 addition & 1 deletion src/contracts/test/TMath.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

pragma solidity 0.5.12;
pragma solidity 0.8.23;

import "../BMath.sol";
import "../BNum.sol";
Expand Down
4 changes: 2 additions & 2 deletions src/contracts/test/TToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

pragma solidity 0.5.12;
pragma solidity 0.8.23;

// Test Token

Expand Down Expand Up @@ -127,7 +127,7 @@ contract TToken {
function transferFrom(address src, address dst, uint amt) external returns (bool) {
require(msg.sender == src || amt <= _allowance[src][msg.sender], "ERR_BTOKEN_BAD_CALLER");
_move(src, dst, amt);
if (msg.sender != src && _allowance[src][msg.sender] != uint256(-1)) {
if (msg.sender != src && _allowance[src][msg.sender] != type(uint256).max) {
_allowance[src][msg.sender] = sub(_allowance[src][msg.sender], amt);
emit Approval(msg.sender, dst, _allowance[src][msg.sender]);
}
Expand Down
2 changes: 1 addition & 1 deletion src/contracts/test/echidna/TBPoolJoinExitPool.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import "../../BNum.sol";

pragma solidity 0.5.12;
pragma solidity 0.8.23;

// This test is similar to TBPoolJoin but with an exit fee
contract TBPoolJoinExit is BNum {
Expand Down
2 changes: 1 addition & 1 deletion src/contracts/test/echidna/TBPoolJoinExitPoolNoFee.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import "../../BNum.sol";

pragma solidity 0.5.12;
pragma solidity 0.8.23;

// This test is similar to TBPoolJoinExit but with no exit fee
contract TBPoolJoinExitNoFee is BNum {
Expand Down
2 changes: 1 addition & 1 deletion src/contracts/test/echidna/TBPoolJoinPool.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import "../../BNum.sol";

pragma solidity 0.5.12;
pragma solidity 0.8.23;

contract TBPoolJoinPool is BNum {

Expand Down
Loading

0 comments on commit c4bf7d6

Please sign in to comment.