Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
510 changes: 510 additions & 0 deletions script/DeployAllAndSetupGoalBasedPaymentTreasury.s.sol

Large diffs are not rendered by default.

33 changes: 33 additions & 0 deletions script/DeployGoalBasedPaymentTreasuryImplementation.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.22;

import {Script} from "forge-std/Script.sol";
import {console2} from "forge-std/console2.sol";
import {GoalBasedPaymentTreasury} from "src/treasuries/GoalBasedPaymentTreasury.sol";

contract DeployGoalBasedPaymentTreasuryImplementation is Script {
function deploy() public returns (address) {
console2.log("Deploying GoalBasedPaymentTreasuryImplementation...");
GoalBasedPaymentTreasury goalBasedPaymentTreasuryImplementation = new GoalBasedPaymentTreasury();
console2.log("GoalBasedPaymentTreasuryImplementation deployed at:", address(goalBasedPaymentTreasuryImplementation));
return address(goalBasedPaymentTreasuryImplementation);
}

function run() external {
uint256 deployerKey = vm.envUint("PRIVATE_KEY");
bool simulate = vm.envOr("SIMULATE", false);

if (!simulate) {
vm.startBroadcast(deployerKey);
}

address implementationAddress = deploy();

if (!simulate) {
vm.stopBroadcast();
}

console2.log("GOAL_BASED_PAYMENT_TREASURY_IMPLEMENTATION_ADDRESS", implementationAddress);
}
}

268 changes: 268 additions & 0 deletions src/treasuries/GoalBasedPaymentTreasury.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,268 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.22;

import {BasePaymentTreasury} from "../utils/BasePaymentTreasury.sol";
import {ICampaignPaymentTreasury} from "../interfaces/ICampaignPaymentTreasury.sol";
import {TimestampChecker} from "../utils/TimestampChecker.sol";

/**
* @title GoalBasedPaymentTreasury
* @notice A payment treasury with goal-based success conditions.
*/
contract GoalBasedPaymentTreasury is BasePaymentTreasury, TimestampChecker {

/**
* @dev Emitted when the goal has not been met for operations requiring success.
*/
error GoalBasedPaymentTreasuryGoalNotMet();

/**
* @dev Emitted when attempting to refund after goal has been met.
*/
error GoalBasedPaymentTreasuryNotRefundable();

/**
* @dev Emitted when an unauthorized action is attempted.
*/
error GoalBasedPaymentTreasuryUnauthorized();

/**
* @notice Initializes the GoalBasedPaymentTreasury contract.
* @param _platformHash The platform hash identifier.
* @param _infoAddress The address of the CampaignInfo contract.
* @param _trustedForwarder The address of the trusted forwarder for meta-transactions.
*/
function initialize(bytes32 _platformHash, address _infoAddress, address _trustedForwarder) external initializer {
__BaseContract_init(_platformHash, _infoAddress, _trustedForwarder);
}

/**
* @dev Internal function to check if current time is within launchTime → deadline range.
*/
function _checkCreateTimeRange() internal view {
_revertIfCurrentTimeIsNotWithinRange(INFO.getLaunchTime(), INFO.getDeadline());
}

/**
* @dev Internal function to check if current time is within launchTime → deadline + buffer range.
*/
function _checkConfirmTimeRange() internal view {
_revertIfCurrentTimeIsNotWithinRange(INFO.getLaunchTime(), INFO.getDeadline() + INFO.getBufferTime());
}

/**
* @dev Internal function to check if current time is greater than launchTime.
*/
function _checkAfterLaunch() internal view {
_revertIfCurrentTimeIsNotGreater(INFO.getLaunchTime());
}

/**
* @dev Internal function to check if current time is greater than deadline.
*/
function _checkAfterDeadline() internal view {
_revertIfCurrentTimeIsNotGreater(INFO.getDeadline());
}

/**
* @dev Internal function to check optimistic lock for refunds.
* Blocks refunds if the campaign is projected to succeed (Confirmed + Pending >= Goal).
*/
function _checkRefundAllowed() internal view {
if (block.timestamp >= INFO.getDeadline()) {
uint256 progress = block.timestamp > INFO.getDeadline() + INFO.getBufferTime()
? getRaisedAmount()
: getRaisedAmount() + getExpectedAmount();
if (progress >= INFO.getGoalAmount()) {
revert GoalBasedPaymentTreasuryNotRefundable();
}
}
}

/**
* @inheritdoc ICampaignPaymentTreasury
* @dev Create operations are only allowed during launchTime → deadline.
*/
function createPayment(
bytes32 paymentId,
bytes32 buyerId,
bytes32 itemId,
address paymentToken,
uint256 amount,
uint256 expiration,
ICampaignPaymentTreasury.LineItem[] calldata lineItems,
ICampaignPaymentTreasury.ExternalFees[] calldata externalFees
) public override whenNotPaused whenNotCancelled {
_checkCreateTimeRange();
super.createPayment(paymentId, buyerId, itemId, paymentToken, amount, expiration, lineItems, externalFees);
}

/**
* @inheritdoc ICampaignPaymentTreasury
* @dev Create operations are only allowed during launchTime → deadline.
*/
function createPaymentBatch(
bytes32[] calldata paymentIds,
bytes32[] calldata buyerIds,
bytes32[] calldata itemIds,
address[] calldata paymentTokens,
uint256[] calldata amounts,
uint256[] calldata expirations,
ICampaignPaymentTreasury.LineItem[][] calldata lineItemsArray,
ICampaignPaymentTreasury.ExternalFees[][] calldata externalFeesArray
) public override whenNotPaused whenNotCancelled {
_checkCreateTimeRange();
super.createPaymentBatch(
paymentIds, buyerIds, itemIds, paymentTokens, amounts, expirations, lineItemsArray, externalFeesArray
);
}

/**
* @inheritdoc ICampaignPaymentTreasury
* @dev Create operations are only allowed during launchTime → deadline.
*/
function processCryptoPayment(
bytes32 paymentId,
bytes32 itemId,
address buyerAddress,
address paymentToken,
uint256 amount,
ICampaignPaymentTreasury.LineItem[] calldata lineItems,
ICampaignPaymentTreasury.ExternalFees[] calldata externalFees
) public override whenNotPaused whenNotCancelled {
_checkCreateTimeRange();
super.processCryptoPayment(paymentId, itemId, buyerAddress, paymentToken, amount, lineItems, externalFees);
}

/**
* @inheritdoc ICampaignPaymentTreasury
* @dev Confirm operations are allowed during launchTime → deadline + buffer.
*/
function confirmPayment(bytes32 paymentId, address buyerAddress)
public
override
whenNotPaused
whenNotCancelled
{
_checkConfirmTimeRange();
super.confirmPayment(paymentId, buyerAddress);
}

/**
* @inheritdoc ICampaignPaymentTreasury
* @dev Confirm operations are allowed during launchTime → deadline + buffer.
*/
function confirmPaymentBatch(bytes32[] calldata paymentIds, address[] calldata buyerAddresses)
public
override
whenNotPaused
whenNotCancelled
{
_checkConfirmTimeRange();
super.confirmPaymentBatch(paymentIds, buyerAddresses);
}

/**
* @inheritdoc ICampaignPaymentTreasury
* @dev Cancel operations are allowed during launchTime → deadline + buffer.
*/
function cancelPayment(bytes32 paymentId)
public
override
whenNotPaused
whenNotCancelled
{
_checkConfirmTimeRange();
super.cancelPayment(paymentId);
}

/**
* @inheritdoc ICampaignPaymentTreasury
* @dev Refunds are allowed after launchTime, but blocked after deadline if goal is met.
*/
function claimRefund(bytes32 paymentId, address refundAddress)
public
override
whenNotPaused
whenNotCancelled

@lucian-calian lucian-calian Feb 3, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't the whenNotCancelled be removed? If a campaign is cancelled, the backers would not be able to reclaim funds. Same for TimeConstrainedPaymentTreasury.
See discussion: https://discord.com/channels/1150443607313092750/1434891866746392756/1464204507717046283

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're correct. Let me remove the constraint and redeploy the treasury implementation contract. Thanks for bringing it up.

{
_checkAfterLaunch();
_checkRefundAllowed();
super.claimRefund(paymentId, refundAddress);
}

/**
* @inheritdoc ICampaignPaymentTreasury
* @dev Refunds are allowed after launchTime, but blocked after deadline if goal is met.
*/
function claimRefund(bytes32 paymentId)
public
override
whenNotPaused
whenNotCancelled
{
_checkAfterLaunch();
_checkRefundAllowed();
super.claimRefund(paymentId);
}

/**
* @inheritdoc ICampaignPaymentTreasury
* @dev Claiming expired funds is allowed after launchTime.
*/
function claimExpiredFunds() public override whenNotPaused {
_checkAfterLaunch();
super.claimExpiredFunds();
}

/**
* @inheritdoc ICampaignPaymentTreasury
* @dev Fee disbursement is only allowed after deadline and requires goal to be met.
*/
function disburseFees() public override whenNotPaused {
_checkAfterDeadline();
if (!_checkSuccessCondition()) {
revert GoalBasedPaymentTreasuryGoalNotMet();
}
super.disburseFees();
}

/**
* @inheritdoc BasePaymentTreasury
* @dev Claiming non-goal line items is allowed after launchTime.
*/
function claimNonGoalLineItems(address token) public override whenNotPaused {
_checkAfterLaunch();
super.claimNonGoalLineItems(token);
}

/**
* @inheritdoc ICampaignPaymentTreasury
* @dev Withdrawal is only allowed after deadline.
* Success condition is checked in the base implementation.
*/
function withdraw() public override whenNotPaused whenNotCancelled {
_checkAfterDeadline();
super.withdraw();
}

/**
* @inheritdoc BasePaymentTreasury
* @dev This function is overridden to allow the platform admin and the campaign owner to cancel a treasury.
*/
function cancelTreasury(bytes32 message) public override {
if (_msgSender() != INFO.getPlatformAdminAddress(PLATFORM_HASH) && _msgSender() != INFO.owner()) {
revert GoalBasedPaymentTreasuryUnauthorized();
}
_cancel(message);
}

/**
* @inheritdoc BasePaymentTreasury
* @dev Success condition: confirmed raised amount >= goal amount.
*/
function _checkSuccessCondition() internal view virtual override returns (bool) {
return getRaisedAmount() >= INFO.getGoalAmount();
}
}

2 changes: 1 addition & 1 deletion src/utils/BasePaymentTreasury.sol
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ abstract contract BasePaymentTreasury is
/**
* @inheritdoc ICampaignPaymentTreasury
*/
function getExpectedAmount() external view returns (uint256) {
function getExpectedAmount() public view returns (uint256) {
address[] memory acceptedTokens = INFO.getAcceptedTokens();
uint256 totalNormalized = 0;

Expand Down
Loading