Skip to content

Fix MixedCampaign bugs, add deadlines, and lock price sources per campaign - #2

Open
Yalhash wants to merge 3 commits into
mainfrom
fix-mixed-campaign
Open

Yalhash wants to merge 3 commits into
mainfrom
fix-mixed-campaign

Conversation

@Yalhash

@Yalhash Yalhash commented Nov 14, 2026

Copy link
Copy Markdown
Owner

Summary

Fixes the MixedCampaign bugs from the status review, adds deadlines and safer payouts to every campaign type, and replaces the Uniswap v4 spot price with a Chainlink / Uniswap v3 TWAP oracle whose price sources are locked per campaign: nobody, including the admin, can change how an existing campaign is valued.

main currently has the Foundry setup but the original contracts, so this PR contains three commits:

  1. Fix MixedCampaign bugs, add deadlines and a Chainlink/TWAP oracle
  2. Pull-based payouts to any address, multisig-ready admin
  3. Lock each mixed campaign's price sources at creation (latest change)

Bugs fixed

Bug Fix
Refunds paid ETH balances out as tokens; token donors could never be refunded ETH and token donations tracked and refunded separately, in the asset given
Token→ETH conversion inverted and truncated to 0 (e.g. USDC) Price math rewritten with FullMath; decimals handled
Spot price (slot0) movable by a flash loan to force settlement IPriceOracle backed by Chainlink (TOKEN/ETH, or TOKEN/USD via ETH/USD, with staleness checks) and a Uniswap v3 TWAP fallback (≥ 30 min window)
Pool registry accepted any PoolId (non-existent or wrong pair priced at 0) Every source is validated when its oracle is deployed: fresh positive feed, pool pairs the token with WETH, enough TWAP history

Price sources locked per campaign (this change)

  • ChainlinkTwapOracle now prices one token, and every setting is fixed in its constructor. It has no owner and no setters.
  • New PriceSourceRegistry (Ownable2Step, owned by the admin Safe) deploys these oracles with setSource(token, chainlink, twap) and records which one new campaigns use. removeSource(token) stops new mixed campaigns for that token.
  • BlockFund reads the token's oracle from the registry when it creates a mixed campaign, and the campaign stores it immutably. Changing or removing a registry entry only affects campaigns created afterwards.
  • BlockFund's registry address is fixed at deployment, and the factory no longer has an owner or any admin functions. The registry is the only admin surface.
  • Trade-off: if Chainlink retires a campaign's feed it counts as stale and the TWAP is used. If neither source works, the campaign can't settle and refunds open after the deadline.

Campaign lifecycle (all campaign types)

  • Shared CampaignBase with an Active / Settled / Refunding state and a deadline (at most 365 days).
  • Owner can settle early once the goal is reached; anyone can settle after the deadline.
  • Goal missed by the deadline, or the owner aborts: refunds open. The first outcome is final, so a later price swing can't flip it.
  • An oracle outage can't lock donors in: refunds still open after the deadline.
  • Pull-based payouts: settleCampaign() only records the outcome; the owner collects with claimFunds(to) and donors refund with withdrawDonation(to), both to any address. An owner contract without receive(), or an address blacklisted by a token like USDC, can no longer lock funds.
  • SafeERC20 (USDT-style tokens), fee-on-transfer accounting, call instead of transfer, ReentrancyGuard, custom errors, indexed events with an asset field, CampaignType.MIXED.

Interface changes

  • Campaign creation takes a deadline (Unix timestamp).
  • withdrawDonation(address to) replaces the old per-contract variants. New claimFunds(address to) for owners.
  • Mixed campaign: donateEth() / donateToken(amount).
  • BlockFund(registry) constructor; createMixedCampaign takes the token, and the oracle comes from the registry.
  • Deploy script deploys PriceSourceRegistry (owned by OWNER, which must be a contract such as a Safe except on local anvil) and BlockFund.

Testing

  • CI: forge build with no warnings; 93 tests pass, 2 fork tests skipped (no RPC in CI).
  • New tests cover each bug, the deadline flow, claiming and refunding to other addresses, ETH-rejecting and blacklisted recipients, reentrancy, two-step ownership transfer to a multisig contract, and changing or removing a token's registry entry after a campaign exists (its valuation stays the same and it can't be pushed over its goal).
  • Not yet run: the mainnet fork tests in test/fork/ (real Chainlink feeds and the USDC/WETH v3 pool, including a same-block 20,000 WETH swap that must not move the TWAP). Run with MAINNET_RPC_URL=… forge test --match-path "test/fork/*" -vvv.

Known limitations / follow-ups

  • Contracts are unaudited; don't deploy with real funds yet.
  • L2 deployments would need Chainlink sequencer-uptime checks.
  • A campaign owner contract that can't make calls at all can't claim (only its own settled funds are affected).
  • Mixed campaigns accept ETH plus one token; multi-asset campaigns and Permit2 are future work.

Generated by Claude Code

Yalhash and others added 3 commits November 10, 2026 18:39
Bug fixes:
- Refunds return each donor's own assets (ETH and token tracked and
  refunded separately); previously ETH balances were paid out as tokens
- Token value now comes from an IPriceOracle instead of the Uniswap v4
  spot price: ChainlinkTwapOracle uses Chainlink (TOKEN/ETH or TOKEN/USD
  via ETH/USD) with a Uniswap v3 TWAP fallback, both flash-loan resistant
- Price math rewritten with FullMath (correct direction, no truncation
  to 0 for low-decimal tokens, no overflow)
- Price sources are validated when configured (fresh feed, pool pairs
  token with WETH, enough TWAP history, minimum 30 min window)

Design changes:
- CampaignBase shared by all campaign types: Active/Settled/Refunding
  state, deadline (max 365 days), owner can settle early once the goal
  is reached, anyone can settle after the deadline, refunds open
  automatically if the goal is missed; first outcome is final
- Oracle outages can't lock donors in: refunds still open after deadline
- SafeERC20, fee-on-transfer accounting, call instead of transfer,
  ReentrancyGuard, custom errors, indexed events with asset field
- BlockFund uses Ownable, holds the oracle, emits CampaignType.MIXED

Tests: unit tests for every fix plus optional mainnet fork tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0178pC5ovfwig5P4H2dMDwWQ
Campaigns:
- settleCampaign() now only records the outcome; the owner collects
  with claimFunds(to) at any address they choose
- withdrawDonation(to) refunds a donor's assets to any address
- A recipient that can't receive ETH (contract without receive) or a
  token-blacklisted address can no longer lock funds in a settled
  campaign: it claims or withdraws elsewhere
- New FundsClaimed event; DonationRefunded includes the recipient

Admin:
- BlockFund and ChainlinkTwapOracle use Ownable2Step, so ownership only
  moves once the new owner (e.g. a Safe) accepts it
- Deploy script requires OWNER to be a contract (multisig) on any chain
  except local anvil
- README: creating the admin Safe, configuring the oracle through the
  Safe Transaction Builder, changing the admin

Tests cover claiming/withdrawing to other addresses, ETH-rejecting and
blacklisted recipients, and two-step transfer to a multisig contract.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0178pC5ovfwig5P4H2dMDwWQ
The admin could previously change price feeds/pools on the shared
oracle, which also changed how running campaigns were valued (e.g.
pushing one over its goal). Now:

- ChainlinkTwapOracle prices a single token and is immutable: every
  setting is fixed and validated in the constructor, with no owner and
  no setters
- New PriceSourceRegistry (Ownable2Step, owned by the admin multisig)
  deploys these oracles via setSource(token, chainlink, twap) and
  records which one new campaigns use; removeSource stops new campaigns
- BlockFund reads the token's oracle from the registry when creating a
  mixed campaign; the campaign stores it immutably. The registry address
  is fixed in BlockFund, which no longer has an owner or admin functions
- A retired feed counts as stale (TWAP fallback); with no working source
  a campaign can't settle and refunds open after the deadline

Tests include changing and removing a token's source after a campaign
exists and checking its valuation is unchanged. Deploy script and README
updated for the registry.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0178pC5ovfwig5P4H2dMDwWQ
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant