Verified contractExactBUSL-1.1
FluidContractFactory
Address0x4ec7b668baf70d4a4b0fc7941a7708a07b6d45be
- Balance
0 ETH
- Transactions
286
- Compiler
solc 0.8.21
- Same code
Only this contract
Details
- Compiler
- solc 0.8.21+commit.d9974bed
- Language
- solidity
- Optimization
- Yes, 10,000,000 runs
- EVM version
- paris
- License
- BUSL-1.1
- Match
- Exactcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 20,656,228
- Creation transaction
- 0x8f16cc808e…c22647207a
3 files
- contracts/deployer/main.solmain3.96 KB
- contracts/libraries/addressCalcs.sol1.75 KB
- solmate/src/auth/Owned.sol1.48 KB
contracts/deployer/main.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.21;
import { Owned } from "solmate/src/auth/Owned.sol";
import { AddressCalcs } from "../libraries/addressCalcs.sol";
/// @title FluidContractFactory
/// @notice A contract that allows deployers to deploy any contract by passing the contract data in bytes
/// @dev The main objective of this contract is to avoid storing contract addresses in our protocols which requires 160 bits of storage
/// Instead, we can just store the nonce & deployment of this address to calculate the address realtime using "AddressCalcs" library
contract FluidContractFactory is Owned {
/// @notice Thrown when an invalid operation is attempted
error FluidContractFactory__InvalidOperation();
/// @notice Emitted when a new contract is deployed
/// @param addr The address of the deployed contract
/// @param nonce The nonce used for deployment
event LogContractDeployed(address indexed addr, uint256 indexed nonce);
/// @notice Emitted when a deployer's count is updated
/// @param deployer The address of the deployer
/// @param count The new count for the deployer
event LogUpdateDeployer(address indexed deployer, uint16 indexed count);
/// @notice Mapping to store the deployment count for each deployer
mapping(address => uint16) public deployer;
/// @notice total number of contracts deployed
uint256 public totalContracts;
/// @notice Constructor to initialize the contract
/// @param owner_ The address of the contract owner
constructor(address owner_) Owned(owner_) {}
/// @notice Updates the allowed deployments count for a specific deployer
/// @param deployer_ The address of the deployer
/// @param count_ The new count for the deployer
/// @dev Only callable by the contract owner
function updateDeployer(address deployer_, uint16 count_) public onlyOwner {
deployer[deployer_] = count_;
emit LogUpdateDeployer(deployer_, count_);
}
/// @notice Deploys a new contract
/// @param contractCode_ The bytecode of the contract to deploy
/// @return contractAddress_ The address of the deployed contract
/// @dev Decrements the deployer's allowed deployments count if not the owner
function deployContract(bytes calldata contractCode_) external returns (address contractAddress_) {
if (msg.sender != owner) {
// if deployer count is 0 then it'll underflow and hence solidity will throw error
deployer[msg.sender] -= 1;
}
uint256 nonce_ = ++totalContracts;
// compute contract address for nonce.
contractAddress_ = getContractAddress(nonce_);
if (contractAddress_ != _deploy(contractCode_)) {
revert FluidContractFactory__InvalidOperation();
}
emit LogContractDeployed(contractAddress_, nonce_);
}
/// @notice Calculates the address of a contract for a given nonce
/// @param nonce_ The nonce to use for address calculation
/// @return contractAddress_ The calculated contract address
function getContractAddress(uint256 nonce_) public view returns (address contractAddress_) {
return AddressCalcs.addressCalc(address(this), nonce_);
}
/// @notice Internal function to deploy a contract
/// @param bytecode_ The bytecode of the contract to deploy
/// @return address_ The address of the deployed contract
/// @dev Uses inline assembly for efficient deployment
function _deploy(bytes memory bytecode_) internal returns (address address_) {
if (bytecode_.length == 0) {
revert FluidContractFactory__InvalidOperation();
}
/// @solidity memory-safe-assembly
assembly {
address_ := create(0, add(bytecode_, 0x20), mload(bytecode_))
}
if (address_ == address(0)) {
revert FluidContractFactory__InvalidOperation();
}
}
}
contracts/libraries/addressCalcs.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.21;
/// @notice implements calculation of address for contracts deployed through CREATE.
/// Accepts contract deployed from which address & nonce
library AddressCalcs {
/// @notice Computes the address of a contract based
/// @param deployedFrom_ Address from which the contract was deployed
/// @param nonce_ Nonce at which the contract was deployed
/// @return contract_ Address of deployed contract
function addressCalc(address deployedFrom_, uint nonce_) internal pure returns (address contract_) {
// @dev based on https://ethereum.stackexchange.com/a/61413
// nonce of smart contract always starts with 1. so, with nonce 0 there won't be any deployment
// hence, nonce of vault deployment starts with 1.
bytes memory data;
if (nonce_ == 0x00) {
return address(0);
} else if (nonce_ <= 0x7f) {
data = abi.encodePacked(bytes1(0xd6), bytes1(0x94), deployedFrom_, uint8(nonce_));
} else if (nonce_ <= 0xff) {
data = abi.encodePacked(bytes1(0xd7), bytes1(0x94), deployedFrom_, bytes1(0x81), uint8(nonce_));
} else if (nonce_ <= 0xffff) {
data = abi.encodePacked(bytes1(0xd8), bytes1(0x94), deployedFrom_, bytes1(0x82), uint16(nonce_));
} else if (nonce_ <= 0xffffff) {
data = abi.encodePacked(bytes1(0xd9), bytes1(0x94), deployedFrom_, bytes1(0x83), uint24(nonce_));
} else {
data = abi.encodePacked(bytes1(0xda), bytes1(0x94), deployedFrom_, bytes1(0x84), uint32(nonce_));
}
return address(uint160(uint256(keccak256(data))));
}
}solmate/src/auth/Owned.sol
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity >=0.8.0;
/// @notice Simple single owner authorization mixin.
/// @author Solmate (https://github.com/transmissions11/solmate/blob/main/src/auth/Owned.sol)
abstract contract Owned {
/*//////////////////////////////////////////////////////////////
EVENTS
//////////////////////////////////////////////////////////////*/
event OwnershipTransferred(address indexed user, address indexed newOwner);
/*//////////////////////////////////////////////////////////////
OWNERSHIP STORAGE
//////////////////////////////////////////////////////////////*/
address public owner;
modifier onlyOwner() virtual {
require(msg.sender == owner, "UNAUTHORIZED");
_;
}
/*//////////////////////////////////////////////////////////////
CONSTRUCTOR
//////////////////////////////////////////////////////////////*/
constructor(address _owner) {
owner = _owner;
emit OwnershipTransferred(address(0), _owner);
}
/*//////////////////////////////////////////////////////////////
OWNERSHIP LOGIC
//////////////////////////////////////////////////////////////*/
function transferOwnership(address newOwner) public virtual onlyOwner {
owner = newOwner;
emit OwnershipTransferred(msg.sender, newOwner);
}
}
Compiler settingsShow
{
"metadata": {
"bytecodeHash": "ipfs",
"useLiteralContent": true
},
"libraries": {},
"optimizer": {
"runs": 10000000,
"enabled": true
},
"evmVersion": "paris",
"remappings": []
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.