Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adding smock package #10

Merged
merged 9 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ jobs:
- name: Install dependencies
run: yarn --frozen-lockfile --network-concurrency 1

- name: Create mock files with smock
run: yarn smock

- name: Precompile using 0.8.14 and via-ir=false
run: yarn build

Expand Down Expand Up @@ -58,6 +61,9 @@ jobs:
- name: Install dependencies
run: yarn --frozen-lockfile --network-concurrency 1

- name: Create mock files with smock
run: yarn smock

- name: Precompile using 0.8.14 and via-ir=false
run: yarn build

Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ broadcast/*/*/*

# Out dir
out

# Smock dir
test/smock
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"lint:sol-logic": "solhint -c .solhint.json 'src/**/*.sol' 'script/**/*.sol'",
"lint:sol-tests": "solhint -c .solhint.tests.json 'test/**/*.sol'",
"prepare": "husky install",
"smock": "smock-foundry --contracts src/contracts",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should add this when building and running the tests

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mhhh, i've added it on the CI, but i believe smock contracts are sth that are not gonna suffer much changes, and if it does all you get is a compilation error on a smock contract, and you'll know you must run yarn smock, but adding this routine to the yarn build seems like suboptimal time-wise, wdyt?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no worries, I was suggesting it so you can run yarn test:unit without having to run yarn smock before

"test": "forge test -vvv",
"test:integration": "forge test --match-contract Integration -vvv",
"test:unit": "forge test --match-contract Unit -vvv",
Expand All @@ -42,6 +43,7 @@
"@commitlint/cli": "19.3.0",
"@commitlint/config-conventional": "19.2.2",
"@defi-wonderland/natspec-smells": "1.1.1",
"@defi-wonderland/smock-foundry": "1.5.0",
"forge-std": "github:foundry-rs/forge-std#5475f85",
"husky": ">=8",
"lint-staged": ">=10",
Expand Down
4 changes: 2 additions & 2 deletions src/contracts/BFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@

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

import './BPool.sol';

Check warning on line 18 in src/contracts/BFactory.sol

View workflow job for this annotation

GitHub Actions / Lint Commit Messages

global import of path ./BPool.sol is not allowed. Specify names to import individually or bind all exports of the module into a name (import "path" as Name)

contract BFactory is BBronze {
event LOG_NEW_POOL(address indexed caller, address indexed pool);

Check warning on line 21 in src/contracts/BFactory.sol

View workflow job for this annotation

GitHub Actions / Lint Commit Messages

event name must be in CapWords

event LOG_BLABS(address indexed caller, address indexed blabs);

Check warning on line 23 in src/contracts/BFactory.sol

View workflow job for this annotation

GitHub Actions / Lint Commit Messages

event name must be in CapWords

mapping(address => bool) private _isBPool;
mapping(address => bool) internal _isBPool;

Check warning on line 25 in src/contracts/BFactory.sol

View workflow job for this annotation

GitHub Actions / Lint Commit Messages

Function order is incorrect, state variable declaration can not go after event definition (line 23)

function isBPool(address b) external view returns (bool) {
return _isBPool[b];
Expand All @@ -36,7 +36,7 @@
return bpool;
}

address private _blabs;
address internal _blabs;

constructor() {
_blabs = msg.sender;
Expand All @@ -47,15 +47,15 @@
}

function setBLabs(address b) external {
require(msg.sender == _blabs, 'ERR_NOT_BLABS');

Check warning on line 50 in src/contracts/BFactory.sol

View workflow job for this annotation

GitHub Actions / Lint Commit Messages

Use Custom Errors instead of require statements
emit LOG_BLABS(msg.sender, b);
_blabs = b;
}

function collect(BPool pool) external {
require(msg.sender == _blabs, 'ERR_NOT_BLABS');

Check warning on line 56 in src/contracts/BFactory.sol

View workflow job for this annotation

GitHub Actions / Lint Commit Messages

Use Custom Errors instead of require statements
uint256 collected = IERC20(pool).balanceOf(address(this));
bool xfer = pool.transfer(_blabs, collected);
require(xfer, 'ERR_ERC20_FAILED');

Check warning on line 59 in src/contracts/BFactory.sol

View workflow job for this annotation

GitHub Actions / Lint Commit Messages

Use Custom Errors instead of require statements
}
}
20 changes: 10 additions & 10 deletions src/contracts/BPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import './BToken.sol';
contract BPool is BBronze, BToken, BMath {
struct Record {
bool bound; // is token bound to pool
uint256 index; // private
uint256 index; // internal
uint256 denorm; // denormalized weight
uint256 balance;
}
Expand Down Expand Up @@ -55,20 +55,20 @@ contract BPool is BBronze, BToken, BMath {
_;
}

bool private _mutex;
bool internal _mutex;

address private _factory; // BFactory address to push token exitFee to
address private _controller; // has CONTROL role
bool private _publicSwap; // true if PUBLIC can call SWAP functions
address internal _factory; // BFactory address to push token exitFee to
address internal _controller; // has CONTROL role
bool internal _publicSwap; // true if PUBLIC can call SWAP functions

// `setSwapFee` and `finalize` require CONTROL
// `finalize` sets `PUBLIC can SWAP`, `PUBLIC can JOIN`
uint256 private _swapFee;
bool private _finalized;
uint256 internal _swapFee;
bool internal _finalized;

address[] private _tokens;
mapping(address => Record) private _records;
uint256 private _totalWeight;
address[] internal _tokens;
mapping(address => Record) internal _records;
uint256 internal _totalWeight;

constructor() {
_controller = msg.sender;
Expand Down
6 changes: 3 additions & 3 deletions src/contracts/BToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ abstract contract BTokenBase is BNum, IERC20 {
}

contract BToken is BTokenBase {
string private _name = 'Balancer Pool Token';
string private _symbol = 'BPT';
uint8 private _decimals = 18;
string internal _name = 'Balancer Pool Token';
string internal _symbol = 'BPT';
uint8 internal _decimals = 18;

function name() public view returns (string memory) {
return _name;
Expand Down
23 changes: 0 additions & 23 deletions src/contracts/Migrations.sol

This file was deleted.

39 changes: 20 additions & 19 deletions test/unit/BPool.t.sol
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.23;

import {BConst} from 'contracts/BConst.sol';
import {BPool} from 'contracts/BPool.sol';
import {MockBPool} from 'test/smock/MockBPool.sol';

import {BConst} from 'contracts/BConst.sol';
import {IERC20} from 'contracts/BToken.sol';
import {Test} from 'forge-std/Test.sol';
import {LibString} from 'solmate/utils/LibString.sol';
Expand All @@ -18,11 +20,11 @@ abstract contract BasePoolTest is Test, BConst, Utils {
uint256 internal constant _RECORD_MAPPING_SLOT_NUMBER = 10;
uint256 internal constant _TOKENS_ARRAY_SLOT_NUMBER = 9;

BPool public bPool;
MockBPool public bPool;
address[TOKENS_AMOUNT] public tokens;

function setUp() public {
bPool = new BPool();
bPool = new MockBPool();

// Create fake tokens
for (uint256 i = 0; i < tokens.length; i++) {
Expand All @@ -48,31 +50,23 @@ abstract contract BasePoolTest is Test, BConst, Utils {
}

function _setTokens(address[] memory _tokens) internal {
_writeArrayLengthToStorage(address(bPool), _TOKENS_ARRAY_SLOT_NUMBER, _tokens.length); // write length
for (uint256 i = 0; i < _tokens.length; i++) {
_writeAddressArrayItemToStorage(address(bPool), _TOKENS_ARRAY_SLOT_NUMBER, i, _tokens[i]); // write token
}
}

function _setRecordBound(address _token) internal {
_writeStructPropertyAtAddressMapping(address(bPool), _RECORD_MAPPING_SLOT_NUMBER, _token, 0, 1); // bound (1 == true)
bPool.set__tokens(_tokens);
}

function _setRecordBalance(address _token, uint256 _balance) internal {
_writeStructPropertyAtAddressMapping(address(bPool), _RECORD_MAPPING_SLOT_NUMBER, _token, 3, _balance); // balance
function _setRecord(address _token, BPool.Record memory _record) internal {
bPool.set__records(_token, _record);
}

function _setPublicSwap(bool _isPublicSwap) internal {
// TODO: make it depend on the bool value
_writeUintToStorage(address(bPool), 6, 0x0000000000000000000000010000000000000000000000000000000000000000);
bPool.set__publicSwap(_isPublicSwap);
}

function _setFinalize(bool _isFinalized) internal {
// TODO: make it depend on the bool value
_writeUintToStorage(address(bPool), 8, 1);
bPool.set__finalized(_isFinalized);
}

function _setTotalSupply(uint256 _totalSupply) internal {
// NOTE: not in smock as it uses ERC20.totalSupply()
_writeUintToStorage(address(bPool), 2, _totalSupply);
}
}
Expand Down Expand Up @@ -340,8 +334,15 @@ contract BPool_Unit_JoinPool is BasePoolTest {

// Set balances
for (uint256 i = 0; i < tokens.length; i++) {
_setRecordBound(tokens[i]);
_setRecordBalance(tokens[i], _fuzz.balance[i]);
_setRecord(
tokens[i],
BPool.Record({
bound: true,
index: 0, // NOTE: irrelevant for this method
denorm: 0, // NOTE: irrelevant for this method
balance: _fuzz.balance[i]
})
);
}

// Set public swap
Expand Down
75 changes: 73 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,16 @@
solc-typed-ast "18.1.2"
yargs "17.7.2"

"@defi-wonderland/[email protected]":
version "1.5.0"
resolved "https://registry.yarnpkg.com/@defi-wonderland/smock-foundry/-/smock-foundry-1.5.0.tgz#e0f91cb40a1805644fbe1bcec8bbb6556d72253c"
integrity sha512-GjMc8Tgg+jBzV0zp00WTSlExptthocrnE3FY58Zm4Li+jWwrphKRflGRf4F65f1t976SNIW63mleWJViFa4mRA==
dependencies:
fast-glob "3.3.2"
handlebars "4.7.7"
solc-typed-ast "18.1.3"
yargs "17.7.2"

"@noble/[email protected]", "@noble/curves@~1.3.0":
version "1.3.0"
resolved "https://registry.yarnpkg.com/@noble/curves/-/curves-1.3.0.tgz#01be46da4fd195822dab821e72f71bf4aeec635e"
Expand Down Expand Up @@ -371,7 +381,7 @@ available-typed-arrays@^1.0.7:
dependencies:
possible-typed-array-names "^1.0.0"

axios@^1.6.7:
axios@^1.6.7, axios@^1.6.8:
version "1.6.8"
resolved "https://registry.yarnpkg.com/axios/-/axios-1.6.8.tgz#66d294951f5d988a00e87a0ffb955316a619ea66"
integrity sha512-v/ZHtJDU39mDpyBoFVkETcd/uNdxrWRrg3bKpOKzXFA6Bvqopts6ALSMU3y6ijYxbw2B+wPrIv46egTzJXCLGQ==
Expand Down Expand Up @@ -952,6 +962,18 @@ graceful-fs@^4.2.0:
resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.2.11.tgz#4183e4e8bf08bb6e05bbb2f7d2e0c8f712ca40e3"
integrity sha512-RbJ5/jmFcNNCcDV5o9eTnBLJ/HszWV0P73bc+Ff4nS/rJj+YaS6IGyiOL0VoBYX+l1Wrl3k63h/KrH+nhJ0XvQ==

[email protected]:
version "4.7.7"
resolved "https://registry.yarnpkg.com/handlebars/-/handlebars-4.7.7.tgz#9ce33416aad02dbd6c8fafa8240d5d98004945a1"
integrity sha512-aAcXm5OAfE/8IXkcZvCepKU3VzW1/39Fb5ZuqMtgI/hT8X2YgoMvBY5dLhq/cpOvw7Lk1nK/UF71aLG/ZnVYRA==
dependencies:
minimist "^1.2.5"
neo-async "^2.6.0"
source-map "^0.6.1"
wordwrap "^1.0.0"
optionalDependencies:
uglify-js "^3.1.4"

has-flag@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/has-flag/-/has-flag-3.0.0.tgz#b5d454dc2199ae225699f3467e5a07f3b955bafd"
Expand Down Expand Up @@ -1387,7 +1409,7 @@ minimatch@^5.0.1:
dependencies:
brace-expansion "^2.0.1"

minimist@^1.2.8:
minimist@^1.2.5, minimist@^1.2.8:
version "1.2.8"
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.8.tgz#c1a464e7693302e082a075cee0c057741ac4772c"
integrity sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==
Expand All @@ -1397,6 +1419,11 @@ [email protected]:
resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.2.tgz#d09d1f357b443f493382a8eb3ccd183872ae6009"
integrity sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==

neo-async@^2.6.0:
version "2.6.2"
resolved "https://registry.yarnpkg.com/neo-async/-/neo-async-2.6.2.tgz#b4aafb93e3aeb2d8174ca53cf163ab7d7308305f"
integrity sha512-Yd3UES5mWCSqR+qNT93S3UoYUkqAZ9lLg8a7g9rimsWmYGK8cVToA4/sF3RrshdyV3sAGMXVUmpMYOw+dLpOuw==

npm-run-path@^5.1.0:
version "5.3.0"
resolved "https://registry.yarnpkg.com/npm-run-path/-/npm-run-path-5.3.0.tgz#e23353d0ebb9317f174e93417e4a4d82d0249e9f"
Expand Down Expand Up @@ -1676,6 +1703,22 @@ [email protected]:
src-location "^1.1.0"
web3-eth-abi "^4.2.0"

[email protected]:
version "18.1.3"
resolved "https://registry.yarnpkg.com/solc-typed-ast/-/solc-typed-ast-18.1.3.tgz#243cc0c5a4f701445ac10341224bf8c18a6ed252"
integrity sha512-11iBtavJJtkzrmQdlAiZ7sz3C3WOQ8MaN7+r4b9C6B/3ORqg4oTUW5/ANyGyus5ppXDXzPyT90BYCfP73df3HA==
dependencies:
axios "^1.6.8"
commander "^12.0.0"
decimal.js "^10.4.3"
findup-sync "^5.0.0"
fs-extra "^11.2.0"
jsel "^1.1.6"
semver "^7.6.0"
solc "0.8.25"
src-location "^1.1.0"
web3-eth-abi "^4.2.0"

[email protected]:
version "0.8.24"
resolved "https://registry.yarnpkg.com/solc/-/solc-0.8.24.tgz#6e5693d28208d00a20ff2bdabc1dec85a5329bbb"
Expand All @@ -1689,6 +1732,19 @@ [email protected]:
semver "^5.5.0"
tmp "0.0.33"

[email protected]:
version "0.8.25"
resolved "https://registry.yarnpkg.com/solc/-/solc-0.8.25.tgz#393f3101617388fb4ba2a58c5b03ab02678e375c"
integrity sha512-7P0TF8gPeudl1Ko3RGkyY6XVCxe2SdD/qQhtns1vl3yAbK/PDifKDLHGtx1t7mX3LgR7ojV7Fg/Kc6Q9D2T8UQ==
dependencies:
command-exists "^1.2.8"
commander "^8.1.0"
follow-redirects "^1.12.1"
js-sha3 "0.8.0"
memorystream "^0.3.1"
semver "^5.5.0"
tmp "0.0.33"

"solhint@github:solhint-community/solhint-community#v4.0.0-rc01":
version "4.0.0-rc01"
resolved "https://codeload.github.com/solhint-community/solhint-community/tar.gz/b04088d3d2bb6ceb8c9bf77783e71195d19fb653"
Expand Down Expand Up @@ -1736,6 +1792,11 @@ [email protected]:
semver "^7.6.0"
sort-object-keys "^1.1.3"

source-map@^0.6.1:
version "0.6.1"
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.6.1.tgz#74722af32e9614e9c287a8d0bbde48b5e2f1a263"
integrity sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==

split2@^4.0.0:
version "4.2.0"
resolved "https://registry.yarnpkg.com/split2/-/split2-4.2.0.tgz#c9c5920904d148bab0b9f67145f245a86aadbfa4"
Expand Down Expand Up @@ -1858,6 +1919,11 @@ to-regex-range@^5.0.1:
dependencies:
is-number "^7.0.0"

uglify-js@^3.1.4:
version "3.17.4"
resolved "https://registry.yarnpkg.com/uglify-js/-/uglify-js-3.17.4.tgz#61678cf5fa3f5b7eb789bb345df29afb8257c22c"
integrity sha512-T9q82TJI9e/C1TAxYvfb16xO120tMVFZrGA3f9/P4424DNu6ypK103y0GPFVa17yotwSyZW5iYXgjYHkGrJW/g==

unicorn-magic@^0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/unicorn-magic/-/unicorn-magic-0.1.0.tgz#1bb9a51c823aaf9d73a8bfcd3d1a23dde94b0ce4"
Expand Down Expand Up @@ -1956,6 +2022,11 @@ which@^2.0.1:
dependencies:
isexe "^2.0.0"

wordwrap@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/wordwrap/-/wordwrap-1.0.0.tgz#27584810891456a4171c8d0226441ade90cbcaeb"
integrity sha512-gvVzJFlPycKc5dZN4yPkP8w7Dc37BtP1yczEneOb4uq34pXZcvrtRTmWV8W+Ume+XCxKgbjM+nevkyFPMybd4Q==

wrap-ansi@^7.0.0:
version "7.0.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
Expand Down
Loading