-
Notifications
You must be signed in to change notification settings - Fork 0
Add goal based PaymentTreasury model #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AdnanHKx
wants to merge
3
commits into
2.0-rc
Choose a base branch
from
feat/goal-payment-treasury
base: 2.0-rc
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,285 @@ | ||
| // 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(); | ||
|
|
||
| /** | ||
| * @dev Constructor for the GoalBasedPaymentTreasury contract. | ||
| */ | ||
| constructor() {} | ||
|
|
||
| /** | ||
| * @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); | ||
| } | ||
|
|
||
| /** | ||
| * @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 | ||
| currentTimeIsWithinRange(INFO.getLaunchTime(), INFO.getDeadline()) | ||
| whenNotPaused | ||
| whenNotCancelled | ||
| { | ||
| 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 | ||
| currentTimeIsWithinRange(INFO.getLaunchTime(), INFO.getDeadline()) | ||
| whenNotPaused | ||
| whenNotCancelled | ||
| { | ||
| 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 | ||
| currentTimeIsWithinRange(INFO.getLaunchTime(), INFO.getDeadline()) | ||
| whenNotPaused | ||
| whenNotCancelled | ||
| { | ||
| 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 | ||
| currentTimeIsWithinRange(INFO.getLaunchTime(), INFO.getDeadline() + INFO.getBufferTime()) | ||
| whenNotPaused | ||
| whenNotCancelled | ||
| { | ||
| 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 | ||
| currentTimeIsWithinRange(INFO.getLaunchTime(), INFO.getDeadline() + INFO.getBufferTime()) | ||
| whenNotPaused | ||
| whenNotCancelled | ||
| { | ||
| super.confirmPaymentBatch(paymentIds, buyerAddresses); | ||
| } | ||
|
|
||
| /** | ||
| * @inheritdoc ICampaignPaymentTreasury | ||
| * @dev Cancel operations are allowed during launchTime → deadline + buffer. | ||
| */ | ||
| function cancelPayment(bytes32 paymentId) | ||
| public | ||
| override | ||
| currentTimeIsWithinRange(INFO.getLaunchTime(), INFO.getDeadline() + INFO.getBufferTime()) | ||
| whenNotPaused | ||
| whenNotCancelled | ||
| { | ||
| 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 | ||
| currentTimeIsGreater(INFO.getLaunchTime()) | ||
| whenNotPaused | ||
| whenNotCancelled | ||
| { | ||
| // Optimistic Lock: Block refunds if the campaign is projected to succeed (Confirmed + Pending >= Goal). | ||
| // This prevents a "bank run" during the settlement buffer while pending payments are confirmed. | ||
| // If pending payments fail and the total drops below the goal after buffer has elapsed, refunds will unlock. | ||
| if (block.timestamp >= INFO.getDeadline() && getGoalProgress() >= INFO.getGoalAmount()) { | ||
| revert GoalBasedPaymentTreasuryNotRefundable(); | ||
| } | ||
| 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 | ||
| currentTimeIsGreater(INFO.getLaunchTime()) | ||
| whenNotPaused | ||
| whenNotCancelled | ||
| { | ||
| // Optimistic Lock: Block refunds if the campaign is projected to succeed (Confirmed + Pending >= Goal). | ||
| // This prevents a "bank run" during the settlement buffer while pending payments are confirmed. | ||
| // If pending payments fail and the total drops below the goal after buffer has elapsed, refunds will unlock. | ||
| if (block.timestamp >= INFO.getDeadline() && getGoalProgress() >= INFO.getGoalAmount()) { | ||
| revert GoalBasedPaymentTreasuryNotRefundable(); | ||
| } | ||
| super.claimRefund(paymentId); | ||
| } | ||
|
|
||
| /** | ||
| * @inheritdoc ICampaignPaymentTreasury | ||
| * @dev Claiming expired funds is allowed after launchTime. | ||
| */ | ||
| function claimExpiredFunds() | ||
| public | ||
| override | ||
| currentTimeIsGreater(INFO.getLaunchTime()) | ||
| whenNotPaused | ||
| { | ||
| super.claimExpiredFunds(); | ||
| } | ||
|
|
||
| /** | ||
| * @inheritdoc ICampaignPaymentTreasury | ||
| * @dev Fee disbursement is only allowed after deadline and requires goal to be met. | ||
| */ | ||
| function disburseFees() | ||
| public | ||
| override | ||
| currentTimeIsGreater(INFO.getDeadline()) | ||
| whenNotPaused | ||
| { | ||
| if (!_checkSuccessCondition()) { | ||
| revert GoalBasedPaymentTreasuryGoalNotMet(); | ||
| } | ||
| super.disburseFees(); | ||
| } | ||
|
|
||
| /** | ||
| * @inheritdoc BasePaymentTreasury | ||
| * @dev Claiming non-goal line items is allowed after launchTime. | ||
| */ | ||
| function claimNonGoalLineItems(address token) | ||
| public | ||
| override | ||
| currentTimeIsGreater(INFO.getLaunchTime()) | ||
| whenNotPaused | ||
| { | ||
| super.claimNonGoalLineItems(token); | ||
| } | ||
|
|
||
| /** | ||
| * @inheritdoc ICampaignPaymentTreasury | ||
| * @dev Withdrawal is only allowed after deadline. | ||
| * Success condition is checked in the base implementation. | ||
| */ | ||
| function withdraw() | ||
| public | ||
| override | ||
| currentTimeIsGreater(INFO.getDeadline()) | ||
| whenNotPaused | ||
| whenNotCancelled | ||
| { | ||
| 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); | ||
| } | ||
|
|
||
| /** | ||
| * @notice Returns goal progress - time-aware for consistency. | ||
| * @return The current goal progress amount (normalized to 18 decimals). | ||
| */ | ||
| function getGoalProgress() public view returns (uint256) { | ||
| if (block.timestamp > INFO.getDeadline() + INFO.getBufferTime()) { | ||
| // After settlement period: only confirmed matters | ||
| return getRaisedAmount(); | ||
| } | ||
| // During campaign/buffer: optimistic view (pending + confirmed) | ||
| return getRaisedAmount() + getExpectedAmount(); | ||
|
mahabubAlahi marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| /** | ||
| * @inheritdoc BasePaymentTreasury | ||
| * @dev Success condition: confirmed raised amount >= goal amount. | ||
| */ | ||
| function _checkSuccessCondition() internal view virtual override returns (bool) { | ||
| return getRaisedAmount() >= INFO.getGoalAmount(); | ||
| } | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't the
whenNotCancelledbe 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
There was a problem hiding this comment.
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.