Skip to content

Commit

Permalink
Merge pull request #135 from defi-wonderland/dev
Browse files Browse the repository at this point in the history
chore: BalCoW v1.0.0 release
  • Loading branch information
wei3erHase authored Jul 26, 2024
2 parents 8a60ea2 + 0fe7750 commit 4289bb8
Show file tree
Hide file tree
Showing 100 changed files with 5,689 additions and 4,515 deletions.
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
MAINNET_RPC=
MAINNET_DEPLOYER_PK=

GNOSIS_RPC=
GNOSIS_DEPLOYER_PK=

SEPOLIA_RPC=
SEPOLIA_DEPLOYER_PK=

Expand Down
1 change: 1 addition & 0 deletions .forge-snapshots/exitPool.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
174743
1 change: 1 addition & 0 deletions .forge-snapshots/joinPool.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
138985
1 change: 1 addition & 0 deletions .forge-snapshots/newBCoWFactory.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
4899289
1 change: 1 addition & 0 deletions .forge-snapshots/newBCoWPool.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
4042925
2 changes: 1 addition & 1 deletion .forge-snapshots/newBFactory.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3528278
4140477
2 changes: 1 addition & 1 deletion .forge-snapshots/newBPool.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3315258
3486610
2 changes: 1 addition & 1 deletion .forge-snapshots/settlementCoWSwap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
186763
215793
2 changes: 1 addition & 1 deletion .forge-snapshots/settlementCoWSwapInverse.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
196603
225641
2 changes: 1 addition & 1 deletion .forge-snapshots/swapExactAmountIn.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
81507
104920
2 changes: 1 addition & 1 deletion .forge-snapshots/swapExactAmountInInverse.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
91166
114589
32 changes: 22 additions & 10 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@ jobs:
- uses: actions/checkout@v3

- name: Install Foundry
uses: foundry-rs/foundry-toolchain@v1
uses: foundry-rs/foundry-toolchain@v1.2.0
with:
version: nightly

- name: Use Node.js
uses: actions/setup-node@v3
with:
node-version: 18.x
node-version: 20.x
cache: 'yarn'

- name: Install dependencies
run: yarn --frozen-lockfile --network-concurrency 1

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

- name: Run tests
Expand All @@ -45,52 +45,64 @@ jobs:
- uses: actions/checkout@v3

- name: Install Foundry
uses: foundry-rs/foundry-toolchain@v1
uses: foundry-rs/foundry-toolchain@v1.2.0
with:
version: nightly

- name: Use Node.js
uses: actions/setup-node@v3
with:
node-version: 18.x
node-version: 20.x
cache: 'yarn'

- name: Install dependencies
run: yarn --frozen-lockfile --network-concurrency 1

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

- name: Run tests
run: yarn test:integration

- name: Ensure gas snapshots were updated
run: git diff --exit-code -- .forge-snapshots/

lint:
name: Static Analysis
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0

- uses: wagoid/commitlint-github-action@v5

- name: Install Foundry
uses: foundry-rs/foundry-toolchain@v1
uses: foundry-rs/foundry-toolchain@v1.2.0
with:
version: nightly

- name: Use Node.js
uses: actions/setup-node@v3
with:
node-version: 18.x
node-version: 20.x
cache: 'yarn'

- name: Install bulloak
uses: baptiste0928/cargo-install@v3
with:
crate: bulloak

- name: Install dependencies
run: yarn --frozen-lockfile --network-concurrency 1

- name: Run forge-fmt && solhint
run: yarn lint:check

- name: Run bulloak check
run: yarn lint:bulloak

- name: Ensure auto-generated smocks are up to date
run: yarn smock && git diff --exit-code -- test/smock

- name: Run natspec-smells
run: yarn lint:natspec 2>&1 >/dev/null | grep 'No issues found'
3 changes: 2 additions & 1 deletion .solhint.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
"avoid-low-level-calls": "off",
"constructor-syntax": "warn",
"func-visibility": ["warn", { "ignoreConstructors": true }],
"no-empty-blocks": "error",
"no-inline-assembly": "off",
"ordering": "warn",
"private-vars-leading-underscore": ["warn", { "strict": false }],
"private-vars-leading-underscore": ["warn", { "strict": true }],
"quotes": "off",
"one-contract-per-file": "warn",
"style-guide-casing": ["warn", { "ignoreEvents": true } ]
Expand Down
24 changes: 20 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ yarn build # build artifacts to `out/`
yarn test # run the tests
```

## Changes on BPool from (Balancer V1)[https://github.com/balancer/balancer-core]
## Changes on BPool from [Balancer V1](https://github.com/balancer/balancer-core)
- Migrated to Foundry project structure
- Implementation of interfaces with Natspec documentation
- Replaced `require(cond, 'STRING')` for `if(!cond) revert CustomError()`
Expand All @@ -37,21 +37,37 @@ yarn test # run the tests
- Immutably stores CoW Protocol's `SolutionSettler` and `VaultRelayer` addresses at deployment
- Immutably stores Cow Protocol's a Domain Separator at deployment (to avoid replay attacks)
- Immutably stores Cow Protocol's `GPv2Order.appData` to be allowed to swap
- Gives infinite ERC20 approval to the CoW Protocol's `VaultRelayer` contract
- Gives infinite ERC20 approval to the CoW Protocol's `VaultRelayer` contract at finalization time.
- Implements IERC1271 `isValidSignature` method to allow for validating intentions of swaps
- Implements a `commit` method to avoid multiple swaps from conflicting with each other.
- This is stored in the same transient storage slot as reentrancy locks in order to prevent calls to swap/join functions within a settlement execution or vice versa.
- It's an error to override a commitment since that could be used to clear reentrancy locks. Commitments can only be cleared by ending a transaction.
- Validates the `GPv2Order` requirements before allowing the swap

## Features on BCoWFactory
- Added a `logBCoWPool` to log the finalization of BCoWPool contracts, to be called by a child pool
- Added a `logBCoWPool` to log the finalization of BCoWPool contracts, to be called by a child pool.

## Creating a Pool
- Create a new pool by calling `IBFactory.newBPool()`
- Create a new pool by calling the corresponding pool factory:
- `IBFactory.newBPool()` for regular Balancer `BPool`s
- `IBCoWFactory.newBPool()` for Balancer `BCoWPool`s, compatible with CoW Protocol
- Give ERC20 allowance to the pool by calling `IERC20.approve(pool, amount)`
- Bind tokens one by one by calling `IBPool.bind(token, amount, weight)`
- The amount represents the initial balance of the token in the pool (pulled from the caller's balance)
- The weight represents the intended distribution of value between the tokens in the pool
- Modify the pool's swap fee by calling `IBPool.setSwapFee(fee)`
- Finalize the pool by calling `IBPool.finalize()`

# Deployments
Ethereum Mainnet:
- BCoWFactory: [0x5AC134DAC7070eFeE8b1C5e3fD0B353922ceD843](https://etherscan.io/address/0x5AC134DAC7070eFeE8b1C5e3fD0B353922ceD843)
- BCoWHelper: [0x703Bd8115E6F21a37BB5Df97f78614ca72Ad7624](https://etherscan.io/address/0x703Bd8115E6F21a37BB5Df97f78614ca72Ad7624)

Ethereum Sepolia:
- BCoWFactory: [0xf3916A8567DdC51a60208B35AC542F5226f46773](https://sepolia.etherscan.io/address/0xf3916A8567DdC51a60208B35AC542F5226f46773)
- BCoWHelper: [0x55DDf396886C85e443E0B5A8E42CAA3939E4Cf50](https://sepolia.etherscan.io/address/0x55DDf396886C85e443E0B5A8E42CAA3939E4Cf50)
- BCoWPool: [0x60048091401F27117C3DFb8136c1ec550D949B12](https://sepolia.etherscan.io/address/0x60048091401F27117C3DFb8136c1ec550D949B12)

Gnosis Mainnet:
- BCoWFactory: [0xaD0447be7BDC80cf2e6DA20B13599E5dc859b667](https://gnosisscan.io/address/0xaD0447be7BDC80cf2e6DA20B13599E5dc859b667)
- BCoWHelper: [0x21Ac2E4115429EcE4b5FE79409fCC48EB6315Ccc](https://gnosisscan.io/address/0x21Ac2E4115429EcE4b5FE79409fCC48EB6315Ccc)
10 changes: 8 additions & 2 deletions foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@ sort_imports = true
[profile.default]
solc_version = '0.8.25'
libs = ["node_modules", "lib"]
optimizer_runs = 50 # TODO: increase for production and add via-ir
ffi = true
optimizer_runs = 500
evm_version = 'cancun'
fs_permissions = [{ access = "read-write", path = ".forge-snapshots/"}]
# 2018: function can be view, so far only caused by mocks
# 2394: solc insists on reporting on every transient storage use
# 5574, 3860: bytecode size limit, so far only caused by test contracts
ignored_error_codes = [2018, 2394, 5574, 3860]
deny_warnings = true

[profile.optimized]
via_ir = true
Expand All @@ -33,8 +37,10 @@ max_test_rejects = 2_500_000

[rpc_endpoints]
mainnet = "${MAINNET_RPC}"
gnosis = "${GNOSIS_RPC}"
sepolia = "${SEPOLIA_RPC}"

[etherscan]
mainnet = { key = "${ETHERSCAN_API_KEY}", chain = "mainnet" }
gnosis = { key = "${ETHERSCAN_API_KEY}", chain = "gnosis" }
sepolia = { key = "${ETHERSCAN_API_KEY}", chain = "sepolia" }
38 changes: 23 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,34 +1,41 @@
{
"name": "balancer-core",
"version": "0.0.7",
"name": "balancer-cow-amm",
"version": "1.0.0",
"private": true,
"description": "Balancer Core Contracts and ABI",
"homepage": "https://github.com/balancer-labs/balancer-core#readme",
"description": "Balancer CoW AMM",
"homepage": "https://github.com/balancer/cow-amm#readme",
"bugs": {
"url": "https://github.com/balancer-labs/balancer-core/issues"
"url": "https://github.com/balancer/cow-amm/issues"
},
"repository": {
"type": "git",
"url": "git+https://github.com/balancer-labs/balancer-core.git"
"url": "git+https://github.com/balancer/cow-amm.git"
},
"license": "GPL-3.0-only",
"license": "GPL-3.0-or-later",
"scripts": {
"build": "forge build",
"build:optimized": "FOUNDRY_PROFILE=optimized forge build",
"coverage": "forge coverage --match-contract Unit",
"deploy:bcowfactory:mainnet": "bash -c 'source .env && forge script DeployBCoWFactory -vvvvv --rpc-url $MAINNET_RPC --broadcast --chain mainnet --private-key $MAINNET_DEPLOYER_PK --verify --etherscan-api-key $ETHERSCAN_API_KEY'",
"deploy:bcowfactory:testnet": "bash -c 'source .env && forge script DeployBCoWFactory -vvvvv --rpc-url $SEPOLIA_RPC --broadcast --chain sepolia --private-key $SEPOLIA_DEPLOYER_PK --verify --etherscan-api-key $ETHERSCAN_API_KEY'",
"deploy:bfactory:mainnet": "bash -c 'source .env && forge script DeployBFactory -vvvvv --rpc-url $MAINNET_RPC --broadcast --chain mainnet --private-key $MAINNET_DEPLOYER_PK --verify --etherscan-api-key $ETHERSCAN_API_KEY'",
"deploy:bfactory:testnet": "bash -c 'source .env && forge script DeployBFactory -vvvvv --rpc-url $SEPOLIA_RPC --broadcast --chain sepolia --private-key $SEPOLIA_DEPLOYER_PK --verify --etherscan-api-key $ETHERSCAN_API_KEY'",
"coverage": "forge coverage --match-path 'test/unit/**'",
"deploy:bcowfactory:gnosis": "forge script DeployBCoWFactory -vvvvv --rpc-url $GNOSIS_RPC --broadcast --chain gnosis --private-key $GNOSIS_DEPLOYER_PK --verify",
"deploy:bcowfactory:mainnet": "forge script DeployBCoWFactory -vvvvv --rpc-url $MAINNET_RPC --broadcast --chain mainnet --private-key $MAINNET_DEPLOYER_PK --verify",
"deploy:bcowfactory:testnet": "forge script DeployBCoWFactory -vvvvv --rpc-url $SEPOLIA_RPC --broadcast --chain sepolia --private-key $SEPOLIA_DEPLOYER_PK --verify",
"deploy:bfactory:gnosis": "forge script DeployBFactory -vvvvv --rpc-url $GNOSIS_RPC --broadcast --chain gnosis --private-key $GNOSIS_DEPLOYER_PK --verify",
"deploy:bfactory:mainnet": "forge script DeployBFactory -vvvvv --rpc-url $MAINNET_RPC --broadcast --chain mainnet --private-key $MAINNET_DEPLOYER_PK --verify",
"deploy:bfactory:testnet": "forge script DeployBFactory -vvvvv --rpc-url $SEPOLIA_RPC --broadcast --chain sepolia --private-key $SEPOLIA_DEPLOYER_PK --verify",
"lint:bulloak": "find test/unit -name '*.tree' | xargs bulloak check",
"lint:check": "solhint 'src/**/*.sol' 'test/**/*.sol' 'script/**/*.sol' && forge fmt --check",
"lint:fix": "solhint --fix 'src/**/*.sol' 'test/**/*.sol' 'script/**/*.sol' && sort-package-json && forge fmt",
"lint:natspec": "npx @defi-wonderland/natspec-smells --config natspec-smells.config.js",
"prepare": "husky install",
"script:gnosis": "forge script MainnetScript -vvvvv --rpc-url $GNOSIS_RPC --broadcast --chain gnosis --private-key $GNOSIS_DEPLOYER_PK --verify",
"script:mainnet": "forge script MainnetScript -vvvvv --rpc-url $MAINNET_RPC --broadcast --chain mainnet --private-key $MAINNET_DEPLOYER_PK --verify",
"script:testnet": "forge script TestnetScript -vvvvv --rpc-url $SEPOLIA_RPC --broadcast --chain sepolia --private-key $SEPOLIA_DEPLOYER_PK --verify",
"smock": "smock-foundry --contracts src/contracts",
"test": "forge test -vvv",
"test:integration": "forge test --match-contract Integration -vvv --isolate",
"test": "yarn test:integration && yarn test:unit",
"test:integration": "forge test --ffi --match-path 'test/integration/**' -vvv --isolate",
"test:local": "FOUNDRY_FUZZ_RUNS=100 forge test -vvv",
"test:unit": "forge test --match-contract Unit -vvv",
"test:scaffold": "bulloak check --fix test/unit/*.tree && forge fmt",
"test:unit": "forge test --match-path 'test/unit/**' -vvv",
"test:unit:deep": "FOUNDRY_FUZZ_RUNS=5000 yarn test:unit"
},
"lint-staged": {
Expand All @@ -40,6 +47,7 @@
"@cowprotocol/contracts": "github:cowprotocol/contracts.git#a10f40788a",
"@openzeppelin/contracts": "5.0.2",
"composable-cow": "github:cowprotocol/composable-cow.git#24d556b",
"cow-amm": "github:cowprotocol/cow-amm.git#6566128",
"solmate": "github:transmissions11/solmate#c892309"
},
"devDependencies": {
Expand Down
3 changes: 3 additions & 0 deletions remappings.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ solmate/=node_modules/solmate/src
@cowprotocol/=node_modules/@cowprotocol/contracts/src/contracts
cowprotocol/=node_modules/@cowprotocol/contracts/src/
@composable-cow/=node_modules/composable-cow/
@cow-amm/=node_modules/cow-amm/src
lib/openzeppelin/=node_modules/@openzeppelin

contracts/=src/contracts
interfaces/=src/interfaces
libraries/=src/libraries
6 changes: 6 additions & 0 deletions script/.solhint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"rules": {
"one-contract-per-file": "off",
"custom-errors": "off"
}
}
43 changes: 43 additions & 0 deletions script/Deploy.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.25;

import {BCoWFactory} from 'contracts/BCoWFactory.sol';
import {BCoWHelper} from 'contracts/BCoWHelper.sol';
import {BFactory} from 'contracts/BFactory.sol';
import {IBFactory} from 'interfaces/IBFactory.sol';

import {Script} from 'forge-std/Script.sol';
import {Params} from 'script/Params.s.sol';

/// @notice This base script is shared across `yarn script:{b|bcow}factory:{mainnet|testnet}`
abstract contract DeployBaseFactory is Script, Params {
constructor() Params(block.chainid) {}

function run() public {
vm.startBroadcast();
IBFactory bFactory = _deployFactory();
bFactory.setBDao(_bFactoryDeploymentParams.bDao);
vm.stopBroadcast();
}

function _deployFactory() internal virtual returns (IBFactory);
}

/// @notice This script will be executed by `yarn script:bfactory:{mainnet|testnet}`
contract DeployBFactory is DeployBaseFactory {
function _deployFactory() internal override returns (IBFactory bFactory) {
bFactory = new BFactory();
}
}

/// @notice This script will be executed by `yarn script:bcowfactory:{mainnet|testnet}`
contract DeployBCoWFactory is DeployBaseFactory {
function _deployFactory() internal override returns (IBFactory bFactory) {
bFactory = new BCoWFactory({
solutionSettler: _bCoWFactoryDeploymentParams.settlement,
appData: _bCoWFactoryDeploymentParams.appData
});

new BCoWHelper(address(bFactory));
}
}
17 changes: 0 additions & 17 deletions script/DeployBCoWFactory.s.sol

This file was deleted.

Loading

0 comments on commit 4289bb8

Please sign in to comment.