Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 3 additions & 1 deletion foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ out = "artifacts"
libs = ["lib"]
via_ir = true
optimizer = true
optimizer_runs = 200
optimizer_runs = 1
solc_version = "0.8.22"
bytecode_hash = "none"
cbor_metadata = false

remappings = [
"@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/",
Expand Down
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);
}
}

4 changes: 4 additions & 0 deletions src/CampaignInfo.sol
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,10 @@ contract CampaignInfo is
address tempTreasury;
for (uint256 i = 0; i < length; i++) {
tempTreasury = s_platformTreasuryAddress[tempPlatforms[i]];
// Skip cancelled treasuries
if (ICampaignTreasury(tempTreasury).cancelled()) {
continue;
}
// Try to call getExpectedAmount - will only work for payment treasuries
try ICampaignPaymentTreasury(tempTreasury).getExpectedAmount() returns (uint256 expectedAmount) {
amount += expectedAmount;
Expand Down
262 changes: 262 additions & 0 deletions src/treasuries/GoalBasedPaymentTreasury.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,262 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.22;

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

/**
* @title GoalBasedPaymentTreasury
* @notice A payment treasury with goal-based success conditions.
*/
contract GoalBasedPaymentTreasury is BasePaymentTreasury {
error GoalBasedPaymentTreasuryInvalidTimestamp();

/**
* @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);
}

function _revertIfCurrentTimeIsNotGreater(uint256 inputTime) private view {
if (block.timestamp <= inputTime) {
revert GoalBasedPaymentTreasuryInvalidTimestamp();
}
}

function _revertIfCurrentTimeIsNotWithinRange(uint256 initialTime, uint256 finalTime) private view {
uint256 currentTime = block.timestamp;
if (currentTime < initialTime || currentTime > finalTime) {
revert GoalBasedPaymentTreasuryInvalidTimestamp();
}
}

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

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

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

/**
* @dev Internal function to check if current time is greater than deadline.
*/
function _checkAfterDeadline() private 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() private view {
_checkAfterLaunch();
uint256 deadline = INFO.getDeadline();
if (block.timestamp >= deadline) {
uint256 raised = INFO.getTotalRaisedAmount();
uint256 progress = block.timestamp > deadline + INFO.getBufferTime()
? raised
: raised + INFO.getTotalExpectedAmount();
if (progress >= INFO.getGoalAmount()) {
revert PaymentTreasurySuccessConditionNotFulfilled();
}
}
}

/**
* @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
{
_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
{
_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 PaymentTreasurySuccessConditionNotFulfilled();
}
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 onlyPlatformAdminOrCampaignOwner {
_cancel(message);
}

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

6 changes: 4 additions & 2 deletions src/treasuries/TimeConstrainedPaymentTreasury.sol
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,18 @@ contract TimeConstrainedPaymentTreasury is BasePaymentTreasury, TimestampChecker

/**
* @inheritdoc ICampaignPaymentTreasury
* @dev Refunds remain available even when treasury is cancelled.
*/
function claimRefund(bytes32 paymentId, address refundAddress) public override whenNotPaused whenNotCancelled {
function claimRefund(bytes32 paymentId, address refundAddress) public override whenNotPaused {
_checkTimeIsGreater();
super.claimRefund(paymentId, refundAddress);
}

/**
* @inheritdoc ICampaignPaymentTreasury
* @dev Refunds remain available even when treasury is cancelled.
*/
function claimRefund(bytes32 paymentId) public override whenNotPaused whenNotCancelled {
function claimRefund(bytes32 paymentId) public override whenNotPaused {
_checkTimeIsGreater();
super.claimRefund(paymentId);
}
Expand Down
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