Factory Contract
Deploys and registers vault contracts. Validates all parameters against the Globals contract before deployment.
Source: contracts/factory/
Source Modules
contract.rs
Constructor, set_vault_wasm_hash, create_vault, queries, inline GlobalsClient and mirrored structs
storage.rs
Data keys (globals, wasm_hash, 3-index vault registry), getters/setters, pagination
errors.rs
#[contracterror] enum (11 variants)
events.rs
VaultDeployed, WasmHashUpdated events
test.rs
3 tests
How It Works
Governor deploys the Factory with a Globals address and an initial vault WASM hash.
When a Pool Manager calls
create_vault(params, salt), the factory:Validates the manager is approved and active (via
GlobalsClient::list_pool_managers)Checks the manager is not delinquent (
is_delinquent— blocked if any vault defaulted)Checks the manager's credit limit (
outstanding + principal ≤ max_credit)Validates the lending token is whitelisted (
get_supported_assets)Validates interest rate, loan term, principal, and split ratio against
get_global_settingsChecks the manager has enough available collateral (
principal × default_collateral_ratio / 100)Locks manager collateral via
GlobalsClient::lock_collateralIncrements outstanding principal via
GlobalsClient::increment_outstandingDeploys a new vault using
e.deployer().with_current_contract(salt).deploy_v2(wasm_hash, (globals, params))Registers the vault in Globals via
GlobalsClient::register_vault(so the vault is authorised for cross-contract calls), then in three Factory indexes: all vaults, per-manager, per-borrower
The governor can update the vault WASM hash via
set_vault_wasm_hash(for future vault template upgrades).
create_vault calls back into Globals (lock_collateral, increment_outstanding, register_vault). These require the Factory to be registered in Globals via set_factory first — otherwise the calls fail. See Deployment.
Functions
Constructor
Initialise the factory with admin (governor), Globals reference, and the vault template hash.
Admin Actions (#[only_admin])
set_vault_wasm_hash(new_hash)
Update the vault WASM template
upgrade(new_wasm_hash, operator)
Upgrade the Factory (auth via stored admin)
Vault Creation
create_vault(params: VaultParams, salt)
Deploy a new vault with full validation; returns the vault Address
VaultParams (15 fields):
Queries
get_globals()
Globals contract address
get_vault_wasm_hash()
Current vault template hash
get_vault_count()
Total deployed vaults (O(1))
get_all_vaults()
First 100 vault addresses (use pagination for more)
get_vaults_page(start, limit)
A page of vault addresses
get_manager_vaults(manager)
First 100 vaults for a manager
get_manager_vaults_page(manager, start, limit)
A page of a manager's vaults
get_manager_vault_count(manager)
Number of vaults for a manager
get_borrower_vaults(borrower)
First 100 vaults for a borrower
get_borrower_vaults_page(borrower, start, limit)
A page of a borrower's vaults
get_borrower_vault_count(borrower)
Number of vaults for a borrower
Validation Checks
The Factory performs extensive validation before deploying a vault. Error codes are from the Factory error enum (contracts/factory/src/errors.rs):
Manager approved
ManagerNotApproved (1)
Manager must be active in Globals
Asset whitelisted
AssetNotSupported (2)
Token must be whitelisted in Globals
Interest rate
InvalidRate (3)
Must be within global min/max
Loan term
InvalidTerm (4)
Must be within global min/max
Principal
InvalidPrincipal (5)
Must be > 0 and within global min/max
Split ratio
InvalidSplitRatio (6)
Must be 1–99
WASM hash set
WasmHashNotSet (7)
Vault template hash must be configured
Authorised
Unauthorized (8)
Caller not authorised
Collateral
InsufficientCollateral (9)
Manager must have enough available collateral
Not delinquent
ManagerDelinquent (10)
Manager must have no uncleared defaults
Credit limit
CreditLimitExceeded (11)
outstanding + principal ≤ max_credit
Events
VaultDeployed
vault, manager, borrower, principal, loan_term, interest_rate
Vault created
WasmHashUpdated
old_hash, new_hash
Template updated
Last updated