-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add an implementation contract that can protect it's function unleass proxied. * Added a multicall file. * Added a contract that can prevent re-entrancy * feat(utils): more utils * test(utils): most utils coverage * style(utils): prettier * test(utils): more utils coverage * refactor(Implementation): dependencies * style(solidity): prettier * refactor(utils): naming convention and tests * refactor(Paused): param name * Update test/utils/ReentrancyGuard.js * move setup to IImplementation --------- Co-authored-by: Kiryl Yermakou <[email protected]> Co-authored-by: Milap Sheth <[email protected]>
- Loading branch information
1 parent
9ff8488
commit b94ea74
Showing
25 changed files
with
651 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -124,3 +124,4 @@ dist | |
# Build | ||
artifacts | ||
/interfaces | ||
temp-arguments.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.0; | ||
|
||
import { IContractIdentifier } from './IContractIdentifier.sol'; | ||
|
||
interface IImplementation is IContractIdentifier { | ||
error NotProxy(); | ||
|
||
function setup(bytes calldata data) external; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.0; | ||
|
||
/** | ||
* @title IMulticall | ||
* @notice This contract is a multi-functional smart contract which allows for multiple | ||
* contract calls in a single transaction. | ||
*/ | ||
interface IMulticall { | ||
error MulticallFailed(); | ||
|
||
/** | ||
* @notice Performs multiple delegate calls and returns the results of all calls as an array | ||
* @dev This function requires that the contract has sufficient balance for the delegate calls. | ||
* If any of the calls fail, the function will revert with the failure message. | ||
* @param data An array of encoded function calls | ||
* @return results An bytes array with the return data of each function call | ||
*/ | ||
function multicall(bytes[] calldata data) external payable returns (bytes[] memory results); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.0; | ||
|
||
/** | ||
* @title Pausable | ||
* @notice This contract provides a mechanism to halt the execution of specific functions | ||
* if a pause condition is activated. | ||
*/ | ||
interface IPausable { | ||
event Paused(address indexed account); | ||
event Unpaused(address indexed account); | ||
|
||
error Pause(); | ||
error NotPaused(); | ||
|
||
/** | ||
* @notice Check if the contract is paused | ||
* @return paused A boolean representing the pause status. True if paused, false otherwise. | ||
*/ | ||
function paused() external view returns (bool); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.0; | ||
|
||
/** | ||
* @title ReentrancyGuard | ||
* @notice This contract provides a mechanism to halt the execution of specific functions | ||
* if a pause condition is activated. | ||
*/ | ||
interface IReentrancyGuard { | ||
error ReentrantCall(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.0; | ||
|
||
/** | ||
* @title AddressBytesUtils | ||
* @dev This library provides utility functions to convert between `address` and `bytes`. | ||
*/ | ||
library AddressBytes { | ||
error InvalidBytesLength(bytes bytesAddress); | ||
|
||
/** | ||
* @dev Converts a bytes address to an address type. | ||
* @param bytesAddress The bytes representation of an address | ||
* @return addr The converted address | ||
*/ | ||
function toAddress(bytes memory bytesAddress) internal pure returns (address addr) { | ||
if (bytesAddress.length != 20) revert InvalidBytesLength(bytesAddress); | ||
|
||
assembly { | ||
addr := mload(add(bytesAddress, 20)) | ||
} | ||
} | ||
|
||
/** | ||
* @dev Converts an address to bytes. | ||
* @param addr The address to be converted | ||
* @return bytesAddress The bytes representation of the address | ||
*/ | ||
function toBytes(address addr) internal pure returns (bytes memory bytesAddress) { | ||
bytesAddress = new bytes(20); | ||
// we can test if using a single 32 byte variable that is the address with the length together and using one mstore would be slightly cheaper. | ||
assembly { | ||
mstore(add(bytesAddress, 20), addr) | ||
mstore(bytesAddress, 20) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.0; | ||
|
||
import { AddressBytes } from '../../libs/AddressBytes.sol'; | ||
|
||
contract TestAddressBytes { | ||
using AddressBytes for address; | ||
using AddressBytes for bytes; | ||
|
||
function toAddress(bytes memory bytesAddress) external pure returns (address addr) { | ||
return bytesAddress.toAddress(); | ||
} | ||
|
||
function toBytes(address addr) external pure returns (bytes memory bytesAddress) { | ||
return addr.toBytes(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.0; | ||
|
||
import { Implementation } from '../../upgradable/Implementation.sol'; | ||
|
||
contract TestImplementation is Implementation { | ||
uint256 public val; | ||
|
||
function setup(bytes calldata params) external override onlyProxy { | ||
val = abi.decode(params, (uint256)); | ||
} | ||
|
||
function contractId() external pure override returns (bytes32) { | ||
return keccak256('TestImplementation'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.0; | ||
|
||
import { Multicall } from '../../utils/Multicall.sol'; | ||
|
||
contract TestMulticall is Multicall { | ||
uint256 public nonce; | ||
bytes[] public lastMulticallReturns; | ||
event Function1Called(uint256 nonce_); | ||
event Function2Called(uint256 nonce_); | ||
|
||
function function1() external returns (uint256) { | ||
uint256 nonce_ = nonce++; | ||
emit Function1Called(nonce_); | ||
return nonce_; | ||
} | ||
|
||
function function2() external returns (uint256) { | ||
uint256 nonce_ = nonce++; | ||
emit Function2Called(nonce_); | ||
return nonce_; | ||
} | ||
|
||
function multicallTest(bytes[] calldata data) external { | ||
lastMulticallReturns = multicall(data); | ||
} | ||
|
||
function getLastMulticallReturns() external view returns (bytes[] memory r) { | ||
return lastMulticallReturns; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.0; | ||
|
||
import { Pausable } from '../../utils/Pausable.sol'; | ||
|
||
contract TestPausable is Pausable { | ||
event TestEvent(); | ||
|
||
function pause() external { | ||
_pause(); | ||
} | ||
|
||
function unpause() external { | ||
_unpause(); | ||
} | ||
|
||
function testPaused() external whenNotPaused { | ||
emit TestEvent(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.0; | ||
|
||
import { ReentrancyGuard } from '../../utils/ReentrancyGuard.sol'; | ||
|
||
contract TestReentrancyGuard is ReentrancyGuard { | ||
uint256 public value; | ||
|
||
constructor() { | ||
require(ENTERED_SLOT == uint256(keccak256('ReentrancyGuard:entered')) - 1, 'invalid constant'); | ||
} | ||
|
||
function testFunction() external noReEntrancy { | ||
value = 1; | ||
this.callback(); | ||
value = 2; | ||
} | ||
|
||
function callback() external noReEntrancy {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.0; | ||
|
||
import { IImplementation } from '../interfaces/IImplementation.sol'; | ||
|
||
/** | ||
* @title Implementation | ||
* @notice This contract serves as a base for other contracts and enforces a proxy-first access restriction. | ||
* @dev Derived contracts must implement the setup function. | ||
*/ | ||
abstract contract Implementation is IImplementation { | ||
address private immutable implementationAddress; | ||
|
||
/** | ||
* @dev Contract constructor that sets the implementation address to the address of this contract. | ||
*/ | ||
constructor() { | ||
implementationAddress = address(this); | ||
} | ||
|
||
/** | ||
* @dev Modifier to require the caller to be the proxy contract. | ||
* Reverts if the caller is the current contract (i.e., the implementation contract itself). | ||
*/ | ||
modifier onlyProxy() { | ||
if (implementationAddress == address(this)) revert NotProxy(); | ||
_; | ||
} | ||
|
||
/** | ||
* @notice Initializes contract parameters. | ||
* This function is intended to be overridden by derived contracts. | ||
* The overriding function must have the onlyProxy modifier. | ||
* @param params The parameters to be used for initialization | ||
*/ | ||
function setup(bytes calldata params) external virtual; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.