Skip to main content

AllocationMechanismFactory

Git Source

Title: Allocation Mechanism Factory

Author: Golem Foundation

Factory for deploying voting/allocation mechanisms with shared implementation

Follows Yearn V3 proxy pattern: lightweight proxies + shared logic ARCHITECTURE:

  • One TokenizedAllocationMechanism implementation (shared)
  • Multiple mechanism proxies (QuadraticVotingMechanism, etc.)
  • Each proxy delegates to shared implementation
  • Custom behavior via hooks in proxy contracts DEPLOYMENT:
  • CREATE2 for deterministic addresses
  • Parameters hashed for uniqueness
  • Duplicate prevention via address check
  • Registry tracking of all deployments

Notes:

  • security-contact: [email protected]

  • security: CREATE2 salt includes all parameters to prevent collisions

  • security: Duplicate deployments revert to prevent parameter confusion

State Variables

tokenizedAllocationImplementation

Shared TokenizedAllocationMechanism implementation address

All deployed mechanisms delegate to this for common logic

address public immutable tokenizedAllocationImplementation

deployedMechanisms

Array of all deployed mechanism addresses

Used for enumeration and tracking

address[] public deployedMechanisms

isMechanism

Mapping to quickly check if address is a deployed mechanism

Set to true when mechanism is deployed

mapping(address => bool) public isMechanism

Functions

constructor

Deploys the factory and shared TokenizedAllocationMechanism implementation

Implementation is deployed once and shared by all proxy mechanisms This saves significant gas on subsequent deployments (~300k → ~50k gas)

constructor() ;

predictMechanismAddress

Predict the deployment address of a QuadraticVotingMechanism

Uses CREATE2 address computation with same salt generation as deployment Address prediction is deterministic and guaranteed to match deployment

function predictMechanismAddress(
AllocationConfig memory _config,
uint256 _alphaNumerator,
uint256 _alphaDenominator,
address deployer
) public view returns (address predicted);

Parameters

NameTypeDescription
_configAllocationConfigConfiguration struct containing all mechanism parameters
_alphaNumeratoruint256Alpha numerator for ProperQF weighting (dimensionless ratio, 0 to _alphaDenominator)
_alphaDenominatoruint256Alpha denominator for ProperQF weighting (dimensionless ratio)
deployeraddressAddress that will deploy the mechanism (used in salt)

Returns

NameTypeDescription
predictedaddressDeterministic CREATE2 address where mechanism will be deployed

deployQuadraticVotingMechanism

Deploy a new QuadraticVotingMechanism with deterministic address

Uses CREATE2 for deterministic deployment address Reverts if mechanism with same parameters already exists DEPLOYMENT STEPS:

  1. Set msg.sender as mechanism owner
  2. Generate deterministic salt from all parameters
  3. Compute expected address via CREATE2
  4. Check for existing deployment (revert if exists)
  5. Deploy via CREATE2
  6. Track in registry
  7. Emit deployment event

Notes:

  • security: Caller becomes mechanism owner with admin privileges

  • security: CREATE2 ensures same parameters always deploy to same address

function deployQuadraticVotingMechanism(
AllocationConfig memory _config,
uint256 _alphaNumerator,
uint256 _alphaDenominator
) external returns (address mechanism);

Parameters

NameTypeDescription
_configAllocationConfigConfiguration struct containing mechanism parameters
_alphaNumeratoruint256Alpha numerator for ProperQF weighting (dimensionless ratio, 0 to _alphaDenominator)
_alphaDenominatoruint256Alpha denominator for ProperQF weighting (dimensionless ratio)

Returns

NameTypeDescription
mechanismaddressAddress of the deployed QuadraticVotingMechanism contract

predictOctantQFMechanismAddress

Predict the deployment address of an OctantQFMechanism

Uses CREATE2 address computation with same salt generation as deployment Address prediction is deterministic and guaranteed to match deployment

function predictOctantQFMechanismAddress(
AllocationConfig memory _config,
uint256 _alphaNumerator,
uint256 _alphaDenominator,
IAddressSet _allowset,
IAddressSet _blockset,
AccessMode _accessMode,
address deployer
) public view returns (address predicted);

Parameters

NameTypeDescription
_configAllocationConfigConfiguration struct containing all mechanism parameters
_alphaNumeratoruint256Alpha numerator for ProperQF weighting
_alphaDenominatoruint256Alpha denominator for ProperQF weighting
_allowsetIAddressSetAllowset contract used in ALLOWSET mode
_blocksetIAddressSetBlockset contract used in BLOCKSET mode
_accessModeAccessModeInitial access mode (NONE, ALLOWSET, BLOCKSET)
deployeraddressAddress that will deploy the mechanism (used in salt)

Returns

NameTypeDescription
predictedaddressDeterministic CREATE2 address where mechanism will be deployed

deployOctantQFMechanism

Deploy a new OctantQFMechanism with deterministic address

Uses CREATE2 for deterministic deployment address Reverts if mechanism with same parameters already exists DEPLOYMENT STEPS:

  1. Set msg.sender as mechanism owner
  2. Generate deterministic salt from all parameters
  3. Compute expected address via CREATE2
  4. Check for existing deployment (revert if exists)
  5. Deploy via CREATE2
  6. Track in registry
  7. Emit deployment event

Notes:

  • security: Caller becomes mechanism owner with admin privileges

  • security: CREATE2 ensures same parameters always deploy to same address

function deployOctantQFMechanism(
AllocationConfig memory _config,
uint256 _alphaNumerator,
uint256 _alphaDenominator,
IAddressSet _allowset,
IAddressSet _blockset,
AccessMode _accessMode
) external returns (address mechanism);

Parameters

NameTypeDescription
_configAllocationConfigConfiguration struct containing mechanism parameters
_alphaNumeratoruint256Alpha numerator for ProperQF weighting
_alphaDenominatoruint256Alpha denominator for ProperQF weighting
_allowsetIAddressSetAllowset contract used in ALLOWSET mode (address(0) if unused)
_blocksetIAddressSetBlockset contract used in BLOCKSET mode (address(0) if unused)
_accessModeAccessModeInitial access mode (NONE, ALLOWSET, or BLOCKSET)

Returns

NameTypeDescription
mechanismaddressAddress of the deployed OctantQFMechanism contract

_octantQFSalt

function _octantQFSalt(
AllocationConfig memory _config,
uint256 _alphaNumerator,
uint256 _alphaDenominator,
IAddressSet _allowset,
IAddressSet _blockset,
AccessMode _accessMode,
address deployer
) internal view returns (bytes32);

_octantQFBytecode

function _octantQFBytecode(
AllocationConfig memory _config,
uint256 _alphaNumerator,
uint256 _alphaDenominator,
IAddressSet _allowset,
IAddressSet _blockset,
AccessMode _accessMode
) internal view returns (bytes memory);

getDeployedCount

Get the total number of deployed mechanisms

Used for enumeration and pagination

function getDeployedCount() external view returns (uint256 count);

Returns

NameTypeDescription
countuint256Number of mechanisms deployed by this factory

getAllDeployedMechanisms

Get all deployed mechanism addresses

May be expensive for large arrays; consider pagination via getDeployedMechanism()

function getAllDeployedMechanisms() external view returns (address[] memory mechanisms);

Returns

NameTypeDescription
mechanismsaddress[]Array of all deployed mechanism addresses

getDeployedMechanism

Get a specific deployed mechanism by array index

Reverts if index out of bounds

Use with getDeployedCount() for pagination

function getDeployedMechanism(uint256 index) external view returns (address mechanism);

Parameters

NameTypeDescription
indexuint256Zero-based index in the deployedMechanisms array

Returns

NameTypeDescription
mechanismaddressAddress of the mechanism at given index

Events

AllocationMechanismDeployed

Emitted when a new allocation mechanism is deployed

event AllocationMechanismDeployed(
address indexed mechanism, address indexed asset, string name, string symbol, address indexed deployer
);

Parameters

NameTypeDescription
mechanismaddressAddress of the deployed mechanism contract
assetaddressAddress of the ERC20 token used for voting
namestringHuman-readable name of the mechanism
symbolstringToken symbol for the mechanism shares
deployeraddressAddress that deployed the mechanism (becomes owner)

Errors

MechanismAlreadyExists

error MechanismAlreadyExists(address existingMechanism);