Counterparti is running on the Base Sepolia test network. Prize pools use test USDC with no real-world value. Learn more
Counterparti

03 / 10

Smart Contracts

CompetitionEscrow and CompetitionEscrowFactory — architecture, state machine, and key functions.

Overview

Two contracts, both in contracts/src/:

  • CompetitionEscrowFactory — singleton, deploys one escrow per competition using EIP-1167 minimal proxy (clone) pattern
  • CompetitionEscrow — holds USDC for one competition; manages funding, locking, winner payout, and refunds

All funds are USDC on Base (mainnet: 8453, Sepolia testnet: 84532).


CompetitionEscrowFactory

Deployed once. The factory is Ownable — the platform controls fee settings and can upgrade the implementation for future competitions (existing escrows are unaffected).

Key functions

| Function | Who | Description | |---|---|---| | createEscrow(competitionId, token, prizeShareBps, submissionDeadline, expirationTimestamp) | Organizer | Deploys a deterministic clone. Caller becomes organizer on the escrow. | | predictEscrow(organizer, competitionId) | Anyone | Returns the address a future escrow will be deployed to, before deployment. | | setFee(newBps) | Owner | Update platform fee for future escrows (max 10%). Existing escrows keep their fee snapshot. | | setFeeRecipient(addr) | Owner | Update where fees flow. | | setImplementation(addr) | Owner | Upgrade the implementation for future clones. |

Salt derivation

Escrow addresses are deterministic:

salt = keccak256(abi.encodePacked(organizer, competitionId))
escrow = CREATE2(implementation, salt)

This means the escrow address is predictable before deployment — the frontend can display it to the organizer before they fund it.


CompetitionEscrow

Each competition gets its own escrow clone. No proxy admin, no upgradeability — immutable after initialize().

State machine

Funding → Locked → Resolved
                 ↘ Cancelled
  • Funding — escrow is deployed and accepting deposits
  • Locked — optional; organizer has called lock() to stop new contributions
  • Resolved — announceWinners() called; prize amounts assigned; fee taken; winners can claim
  • Cancelled — organizer cancelled an empty escrow, the platform cancelled, or expirationTimestamp passed; contributors can refund

Key functions

| Function | Who | Description | |---|---|---| | fund(amount) | Anyone | Deposit USDC. Updates totalContributed and contributions[msg.sender]. | | lock() | Organizer | Optional. Move from Funding → Locked; stops new contributions. | | announceWinners(winners[]) | Organizer | Move from Funding or Locked → Resolved. winners array must match prizeShareBps length. Computes prize amounts from pool, deducts platform fee (sent immediately to feeRecipient), assigns prizeOf[winner]. | | claimPrize() | Winner | Pull prize. Reverts with NothingToClaim if not a winner. | | cancel() | Organizer / Platform / Anyone | Organizer: only while the pool is empty. Platform (feeRecipient): any time before resolution. Anyone: after expirationTimestamp. | | claimRefund() | Contributor | Pull contribution back after cancellation. | | emergencyWithdraw(to) | Platform | Sweep the balance to to and cancel. Not allowed once resolved. |

Prize calculation

netPool = totalContributed * (10_000 - feeBps) / 10_000
prizeOf[winners[i]] = netPool * prizeShareBps[i] / 10_000

prizeShareBps is set at initialization and sums to 10_000. Example for a three-tier competition:

[6000, 3000, 1000]  → 60% / 30% / 10%

Because shares are basis-point percentages of the final pool, open-pool competitions automatically scale all prizes as contributors add funds.

Security properties

  • Pull payments only — no push transfers to winners or contributors
  • Reentrancy guard on all state-changing functions
  • Fee capped at 10% (MAX_FEE_BPS = 1000)
  • Expiration rescue — if organizer disappears, expirationTimestamp unlocks refunds for everyone
  • Fee snapshot — platform fee is locked at escrow creation; factory owner changes don't affect existing escrows

Deployment

The implementation and factory are compiled and deployed once per network with Foundry (contracts/script/Deploy.s.sol, configured via contracts/.env). Use a multisig as the factory owner and fee recipient on mainnet.

Per-competition escrows are deployed by the organizer's own wallet from the organizer dashboard (DeployEscrowButton), so the organizer pays the gas and becomes the escrow's on-chain organizer. The app never holds a deployer key.

  1. getEscrowDeployParams returns the factory, token, competitionId = keccak256(slug), prize shares and timestamps for the stored competition.
  2. The organizer's wallet calls createEscrow(...).
  3. recordEscrowDeployment fetches the receipt, decodes EscrowCreated from the factory, checks the competition id, token, shares and that the organizer address is one of the user's Privy-linked wallets, then saves escrow_address and chain_id.
  4. If step 3 never ran (tab closed), recoverEscrowDeployment finds the escrow at its deterministic address and verifies it the same way.

To predict an escrow address before deployment:

const predicted = await publicClient.readContract({
  address: FACTORY_ADDRESS,
  abi: factoryAbi,
  functionName: "predictEscrow",
  args: [organizerAddress, competitionIdHash],
});

Addresses

Set via environment variables:

NEXT_PUBLIC_CHAIN=base-sepolia                 # which network the app uses: base-sepolia | base
NEXT_PUBLIC_FACTORY_ADDRESS_BASE=0x...         # Base mainnet
NEXT_PUBLIC_FACTORY_ADDRESS_BASE_SEPOLIA=0x... # Base Sepolia