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: rearrange core contracts addresses #32

Merged
merged 10 commits into from
Mar 27, 2024
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"@metamask/eth-sig-util": "^7.0.0",
"@nomicfoundation/hardhat-chai-matchers": "^2.0.2",
"@nomicfoundation/hardhat-ethers": "^3.0.4",
"@nomicfoundation/hardhat-network-helpers": "^1.0.9",
"@nomicfoundation/hardhat-network-helpers": "^1.0.10",
"@nomicfoundation/hardhat-toolbox": "^3.0.0",
"@nomicfoundation/hardhat-verify": "^1.0.0",
"@openzeppelin/contracts": "4.9.3",
Expand All @@ -39,6 +39,7 @@
"@typescript-eslint/parser": "^7.3.1",
"chai": "^4.2.0",
"chain": "^0.4.0",
"dotenv": "^16.4.1",
"eslint": "^8.57.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-prettier": "^5.0.1",
Expand All @@ -53,8 +54,7 @@
"solidity-coverage": "^0.8.0",
"ts-node": ">=8.0.0",
"tslint-config-prettier": "^1.18.0",
"typescript": "5.3.3",
"dotenv": "^16.4.1"
"typescript": "5.3.3"
},
"dependencies": {
"hardhat": "^2.17.4",
Expand Down
12 changes: 11 additions & 1 deletion src/contracts/Bridge.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ contract Bridge is Pausable, Context {
uint8 public immutable _domainID;

IFeeHandler public _feeHandler;

IAccessControlSegregator public _accessControl;
address public _executorAddress;
address public _routerAddress;


// resourceID => handler address
mapping(bytes32 => address) public _resourceIDToHandlerAddress;
Expand Down Expand Up @@ -140,4 +142,12 @@ contract Bridge is Pausable, Context {
IERCHandler handler = IERCHandler(handlerAddress);
handler.withdraw(data);
}

function adminChangeRouterAddress(address routerAddress) external onlyAllowed {
_routerAddress = routerAddress;
}

function adminChangeExecutorAddress(address executorAddress) external onlyAllowed {
_executorAddress = executorAddress;
}
}
9 changes: 3 additions & 6 deletions src/contracts/TestContracts.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pragma solidity 0.8.11;
import "@openzeppelin/contracts/token/ERC20/presets/ERC20PresetMinterPauser.sol";
import "./handlers/ERCHandlerHelpers.sol";
import "./interfaces/IERC20Plus.sol";
import "./interfaces/IBridge.sol";

contract NoArgument {
event NoArgumentCalled();
Expand Down Expand Up @@ -58,13 +59,9 @@ abstract contract HandlerRevert is ERCHandlerHelpers {
uint256 private _totalAmount;

constructor(
address bridgeAddress,
address routerAddress,
address executorAddress
IBridge bridgeAddress
) ERCHandlerHelpers(
bridgeAddress,
routerAddress,
executorAddress
bridgeAddress
) {}

function executeProposal(bytes32, bytes calldata) external view {
Expand Down
48 changes: 40 additions & 8 deletions src/contracts/handlers/ERC20Handler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,41 @@ import "../ERC20Safe.sol";
*/
contract ERC20Handler is IHandler, ERCHandlerHelpers, ERC20Safe {
/**
@param bridgeAddress Contract address of previously deployed Bridge.
@param bridge Contract address of previously deployed Bridge.
*/
constructor(
address bridgeAddress,
address routerAddress,
address executorAddress
IBridge bridge
) ERCHandlerHelpers(
bridgeAddress,
routerAddress,
executorAddress
bridge
) {}

modifier onlyBridge() {
_onlyBridge();
_;
}

modifier onlyRouter() {
_onlyRouter();
_;
}

modifier onlyExecutor() {
_onlyExecutor();
_;
}

function _onlyBridge() private {
if (msg.sender != address(_bridge)) revert SenderNotBridgeContract();
}

function _onlyExecutor() private {
if (msg.sender != _bridge._executorAddress()) revert SenderNotExecutorContract();
}

function _onlyRouter() private {
if (msg.sender != _bridge._routerAddress()) revert SenderNotRouterContract();
}

/**
@notice A deposit is initiated by making a deposit in the Bridge contract.
@param resourceID ResourceID used to find address of token to be used for deposit.
Expand Down Expand Up @@ -114,7 +137,7 @@ contract ERC20Handler is IHandler, ERCHandlerHelpers, ERC20Safe {
recipient address bytes 32 - 64
amount uint256 bytes 64 - 96
*/
function withdraw(bytes memory data) external override onlyBridge {
function withdraw(bytes memory data) external onlyBridge {
address tokenAddress;
address recipient;
uint256 amount;
Expand Down Expand Up @@ -142,4 +165,13 @@ contract ERC20Handler is IHandler, ERCHandlerHelpers, ERC20Safe {
_setDecimals(contractAddress, externalTokenDecimals);
}
}

/**
@notice First verifies {contractAddress} is whitelisted, then sets
{_tokenContractAddressToTokenProperties[contractAddress].isBurnable} to true.
@param contractAddress Address of contract to be used when making or executing deposits.
*/
function setBurnable(address contractAddress) external onlyBridge {
_setBurnable(contractAddress);
}
}
55 changes: 7 additions & 48 deletions src/contracts/handlers/ERCHandlerHelpers.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
pragma solidity 0.8.11;

import "../interfaces/IERCHandler.sol";
import "../interfaces/IBridge.sol";


/**
@title Function used across handler contracts.
@author ChainSafe Systems.
@notice This contract is intended to be used with the Bridge contract.
*/
abstract contract ERCHandlerHelpers is IERCHandler {
address public immutable _bridgeAddress;
address public immutable _routerAddress;
address public immutable _executorAddress;
contract ERCHandlerHelpers {
IBridge public _bridge;

uint8 public constant DEFAULT_DECIMALS = 18;

Expand Down Expand Up @@ -40,52 +40,11 @@ abstract contract ERCHandlerHelpers is IERCHandler {
// token contract address => ERCTokenContractProperties
mapping(address => ERCTokenContractProperties) public _tokenContractAddressToTokenProperties;

modifier onlyBridge() {
_onlyBridge();
_;
}

modifier onlyRouter() {
_onlyRouter();
_;
}

modifier onlyExecutor() {
_onlyExecutor();
_;
}

function _onlyBridge() private view {
if (msg.sender != _bridgeAddress) revert SenderNotBridgeContract();
}

function _onlyExecutor() private view {
if (msg.sender != _executorAddress) revert SenderNotExecutorContract();
}

function _onlyRouter() private view {
if (msg.sender != _routerAddress) revert SenderNotRouterContract();
}


/**
@param bridgeAddress Contract address of previously deployed Bridge.
@param routerAddress Contract address of previously deployed Router.
@param executorAddress Contract address of previously deployed Executor.
*/
constructor(address bridgeAddress, address routerAddress, address executorAddress) {
_bridgeAddress = bridgeAddress;
_routerAddress = routerAddress;
_executorAddress = executorAddress;
}

/**
@notice First verifies {contractAddress} is whitelisted, then sets
{_tokenContractAddressToTokenProperties[contractAddress].isBurnable} to true.
@param contractAddress Address of contract to be used when making or executing deposits.
@param bridge Contract address of previously deployed Bridge.
*/
function setBurnable(address contractAddress) external override onlyBridge {
_setBurnable(contractAddress);
constructor(IBridge bridge) {
_bridge = bridge;
}

function _setResource(bytes32 resourceID, address contractAddress) internal {
Expand Down
13 changes: 7 additions & 6 deletions src/contracts/handlers/FeeHandlerRouter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
pragma solidity 0.8.11;

import "../interfaces/IFeeHandler.sol";
import "../interfaces/IBridge.sol";
import "@openzeppelin/contracts/access/AccessControl.sol";

/**
Expand All @@ -11,7 +12,7 @@ import "@openzeppelin/contracts/access/AccessControl.sol";
@notice This contract is intended to be used with the Bridge contract.
*/
contract FeeHandlerRouter is IFeeHandler, AccessControl {
address public immutable _routerAddress;
IBridge public immutable _bridge;

// domainID => resourceID => securityModel => feeHandlerAddress
mapping(uint8 => mapping(bytes32 => mapping(uint8 => IFeeHandler))) public
Expand All @@ -32,8 +33,8 @@ contract FeeHandlerRouter is IFeeHandler, AccessControl {
_;
}

function _onlyRouter() private view {
if (msg.sender != _routerAddress) revert SenderNotRouterContract();
function _onlyRouter() private {
if (msg.sender != _bridge._routerAddress()) revert SenderNotRouterContract();
}

modifier onlyAdmin() {
Expand All @@ -46,10 +47,10 @@ contract FeeHandlerRouter is IFeeHandler, AccessControl {
}

/**
@param routerAddress Contract address of previously deployed Router.
@param bridge Contract address of previously deployed Bridge.
*/
constructor(address routerAddress) {
_routerAddress = routerAddress;
constructor(IBridge bridge) {
_bridge = bridge;
_setupRole(DEFAULT_ADMIN_ROLE, msg.sender);
}

Expand Down
18 changes: 8 additions & 10 deletions src/contracts/handlers/PermissionlessGenericHandler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

pragma solidity 0.8.11;
import "../interfaces/IHandler.sol";
import "../interfaces/IBridge.sol";

/**
@title Handles generic deposits and deposit executions.
Expand All @@ -12,8 +13,7 @@
contract PermissionlessGenericHandler is IHandler {
uint256 public constant MAX_FEE = 1000000;

address public immutable _bridgeAddress;
address public immutable _executorAddress;
IBridge public immutable _bridge;

modifier onlyBridge() {
_onlyBridge();
Expand All @@ -27,25 +27,23 @@
error InvalidExecutioDataDepositor(address executioDataDepositor);

function _onlyBridge() private view {
if (msg.sender != _bridgeAddress) revert SenderNotBridgeContract();
if (msg.sender != address(_bridge)) revert SenderNotBridgeContract();
}

modifier onlyExecutor() {
_onlyExecutor();
_;
}

function _onlyExecutor() private view {
if (msg.sender != _executorAddress) revert SenderNotExecutorContract();
function _onlyExecutor() private {
if (msg.sender != _bridge._executorAddress()) revert SenderNotExecutorContract();
}

/**
@param bridgeAddress Contract address of previously deployed Bridge.
@param executorAddress Contract address of previously deployed Executor.
@param bridge Contract address of previously deployed Bridge.
*/
constructor(address bridgeAddress, address executorAddress) {
_bridgeAddress = bridgeAddress;
_executorAddress = executorAddress;
constructor(IBridge bridge) {
_bridge = bridge;
}

/**
Expand Down Expand Up @@ -118,7 +116,7 @@
After this, the target contract will get the following:
executeFuncSignature(address executionDataDepositor, uint256[] uintArray, address addr)
*/
function deposit(bytes32 resourceID, address depositor, bytes calldata data) external pure returns (bytes memory) {

Check warning on line 119 in src/contracts/handlers/PermissionlessGenericHandler.sol

View workflow job for this annotation

GitHub Actions / Lint Solidity

Variable "resourceID" is unused
if (data.length < 76) revert IncorrectDataLength(data.length); // 32 + 2 + 1 + 1 + 20 + 20

uint256 maxFee;
Expand Down
20 changes: 9 additions & 11 deletions src/contracts/handlers/fee/BasicFeeHandler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
pragma solidity 0.8.11;

import "../../interfaces/IFeeHandler.sol";
import "../../interfaces/IBridge.sol";
import "@openzeppelin/contracts/access/AccessControl.sol";

import "../FeeHandlerRouter.sol";
Expand All @@ -13,9 +14,8 @@
@notice This contract is intended to be used with the Bridge contract.
*/
contract BasicFeeHandler is IFeeHandler, AccessControl {
address public immutable _bridgeAddress;
IBridge public immutable _bridge;
address public immutable _feeHandlerRouterAddress;
address public immutable _routerAddress;
// domainID => resourceID => securityModel => fee
mapping (uint8 => mapping(bytes32 => mapping(uint8 => uint256))) public _domainResourceIDSecurityModelToFee;

Expand Down Expand Up @@ -44,25 +44,23 @@
_;
}

function _onlyRouterOrFeeRouter() private view {
function _onlyRouterOrFeeRouter() private {
if (msg.sender != _feeHandlerRouterAddress &&
msg.sender != _routerAddress
msg.sender != _bridge._routerAddress()
) revert SenderNotBridgeOrRouter();
}

/**
@param bridgeAddress Contract address of previously deployed Bridge.
@param bridge Contract address of previously deployed Bridge.
@param feeHandlerRouterAddress Contract address of previously deployed FeeHandlerRouter.
*/
constructor(address bridgeAddress, address feeHandlerRouterAddress, address routerAddress) {
if (bridgeAddress == address(0) ||
feeHandlerRouterAddress == address(0) ||
routerAddress == address(0)
constructor(IBridge bridge, address feeHandlerRouterAddress) {
if (address(bridge) == address(0) ||
feeHandlerRouterAddress == address(0)
) revert ZeroAddressProvided();

_bridgeAddress = bridgeAddress;
_bridge = bridge;
_feeHandlerRouterAddress = feeHandlerRouterAddress;
_routerAddress = routerAddress;
_setupRole(DEFAULT_ADMIN_ROLE, msg.sender);
}

Expand Down Expand Up @@ -95,8 +93,8 @@
uint8 destinationDomainID,
bytes32 resourceID,
uint8 securityModel,
bytes calldata depositData,

Check warning on line 96 in src/contracts/handlers/fee/BasicFeeHandler.sol

View workflow job for this annotation

GitHub Actions / Lint Solidity

Variable "depositData" is unused
bytes calldata feeData

Check warning on line 97 in src/contracts/handlers/fee/BasicFeeHandler.sol

View workflow job for this annotation

GitHub Actions / Lint Solidity

Variable "feeData" is unused
) external virtual payable onlyRouterOrFeeRouter {
uint256 currentFee = _domainResourceIDSecurityModelToFee[destinationDomainID][resourceID][securityModel];
if (msg.value != currentFee) revert IncorrectFeeSupplied(msg.value);
Expand All @@ -115,13 +113,13 @@
@return Returns the fee amount.
*/
function calculateFee(
address sender,

Check warning on line 116 in src/contracts/handlers/fee/BasicFeeHandler.sol

View workflow job for this annotation

GitHub Actions / Lint Solidity

Variable "sender" is unused
uint8 fromDomainID,

Check warning on line 117 in src/contracts/handlers/fee/BasicFeeHandler.sol

View workflow job for this annotation

GitHub Actions / Lint Solidity

Variable "fromDomainID" is unused
uint8 destinationDomainID,
bytes32 resourceID,
uint8 securityModel,
bytes calldata depositData,

Check warning on line 121 in src/contracts/handlers/fee/BasicFeeHandler.sol

View workflow job for this annotation

GitHub Actions / Lint Solidity

Variable "depositData" is unused
bytes calldata feeData

Check warning on line 122 in src/contracts/handlers/fee/BasicFeeHandler.sol

View workflow job for this annotation

GitHub Actions / Lint Solidity

Variable "feeData" is unused
) virtual external view returns(uint256, address) {
return (_domainResourceIDSecurityModelToFee[destinationDomainID][resourceID][securityModel], address(0));
}
Expand Down
Loading
Loading