-
Notifications
You must be signed in to change notification settings - Fork 0
feat: configure configurable deposit cap #156
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
Open
Changes from all commits
Commits
Show all changes
2 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
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
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
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
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 |
|---|---|---|
|
|
@@ -25,4 +25,5 @@ pub(crate) enum DataKey { | |
| WindDownAcc, | ||
| WindDownOwed, | ||
| WindDownPosition(Address), | ||
| DepositCap, | ||
| } | ||
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 |
|---|---|---|
|
|
@@ -27,10 +27,11 @@ use state::FIRST_EPOCH; | |
|
|
||
| pub use error::VaultError; | ||
| pub use event::{ | ||
| CustodianSet, Deployed, DepositClaimed, DepositRequested, EpochClosed, EpochFulfilled, Funded, | ||
| NoticeSet, RedeemClaimed, RedeemRequested, UpgradeCancelled, UpgradeDelayProposed, | ||
| UpgradeDelaySet, UpgradeProposed, Upgraded, WindDownActivated, WindDownClaimed, | ||
| WindDownDelaySet, WindDownProposalCancelled, WindDownProposed, WindDownRoundFinalized, | ||
| CustodianSet, Deployed, DepositCapUpdated, DepositClaimed, DepositRequested, EpochClosed, | ||
| EpochFulfilled, Funded, NoticeSet, RedeemClaimed, RedeemRequested, UpgradeCancelled, | ||
| UpgradeDelayProposed, UpgradeDelaySet, UpgradeProposed, Upgraded, WindDownActivated, | ||
| WindDownClaimed, WindDownDelaySet, WindDownProposalCancelled, WindDownProposed, | ||
| WindDownRoundFinalized, | ||
| }; | ||
| pub use pricing::{DirectUnitPricing, PricingScheme}; | ||
| pub use roles::VaultRoles; | ||
|
|
@@ -164,6 +165,27 @@ impl AsyncVault { | |
| .unwrap_or_else(|| panic_with_error!(e, VaultError::AmountTooLarge)) | ||
| } | ||
|
|
||
| pub fn deposit_cap(e: &Env) -> Option<i128> { | ||
| state::deposit_cap(e) | ||
| } | ||
|
|
||
| #[only_admin] | ||
| pub fn set_deposit_cap(e: &Env, cap: Option<i128>, caller: Address) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. New governance parameter, but §8.8's Configuration row still lists only bounds, freshness, timelock and wind-down delay. Worth adding the cap there? |
||
| caller.require_auth(); | ||
| if let Some(c) = cap { | ||
| if c < 0 { | ||
| panic_with_error!(e, VaultError::InvalidAmount); | ||
| } | ||
| } | ||
| let old_cap = state::deposit_cap(e); | ||
| state::set_deposit_cap(e, cap); | ||
| DepositCapUpdated { | ||
| old_cap, | ||
| new_cap: cap, | ||
| } | ||
| .publish(e); | ||
| } | ||
|
|
||
| #[only_admin] | ||
| pub fn set_custodian(e: &Env, custodian: Address, caller: Address) { | ||
| caller.require_auth(); | ||
|
|
||
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
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,188 @@ | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn deposit_cap_defaults_to_none_unbounded() { | ||
| let f = setup(); | ||
| assert_eq!(f.vault.deposit_cap(), None); | ||
|
|
||
| let investor = f.investor(50_000); | ||
| let epoch = f.vault.request_deposit(&investor, &50_000); | ||
| assert_eq!(f.vault.get_epoch(&epoch).unwrap().total_deposited, 50_000); | ||
| } | ||
|
|
||
| #[test] | ||
| fn governance_sets_updates_and_clears_cap() { | ||
| let f = setup(); | ||
|
|
||
| // Set cap | ||
| f.vault.set_deposit_cap(&Some(1_000), &f.admin); | ||
| assert_eq!(f.vault.deposit_cap(), Some(1_000)); | ||
|
|
||
| // Update cap | ||
| f.vault.set_deposit_cap(&Some(2_000), &f.admin); | ||
| assert_eq!(f.vault.deposit_cap(), Some(2_000)); | ||
|
|
||
| // Clear cap | ||
| f.vault.set_deposit_cap(&None, &f.admin); | ||
| assert_eq!(f.vault.deposit_cap(), None); | ||
| } | ||
|
|
||
| #[test] | ||
| fn negative_cap_rejected() { | ||
| let f = setup(); | ||
| refused( | ||
| f.vault.try_set_deposit_cap(&Some(-1), &f.admin), | ||
| VaultError::InvalidAmount, | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn unauthorized_caller_rejected() { | ||
| let f = setup(); | ||
| let rando = Address::generate(&f.e); | ||
|
|
||
| f.e.set_auths(&[]); | ||
| assert!(f.vault.try_set_deposit_cap(&Some(1_000), &rando).is_err()); | ||
| assert!(f | ||
| .vault | ||
| .try_set_deposit_cap(&Some(1_000), &f.manager) | ||
| .is_err()); | ||
| assert!(f | ||
| .vault | ||
| .try_set_deposit_cap(&Some(1_000), &f.guardian) | ||
| .is_err()); | ||
| assert!(f | ||
| .vault | ||
| .try_set_deposit_cap(&Some(1_000), &f.treasury) | ||
| .is_err()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn boundary_value_enforcement() { | ||
| let f = setup(); | ||
| f.vault.set_deposit_cap(&Some(1_000), &f.admin); | ||
|
|
||
| let investor1 = f.investor(2_000); | ||
| let investor2 = f.investor(2_000); | ||
|
|
||
| // Deposit exceeding cap from empty vault fails | ||
| refused( | ||
| f.vault.try_request_deposit(&investor1, &1_001), | ||
| VaultError::DepositCapExceeded, | ||
| ); | ||
|
|
||
| // Deposit exactly equal to cap succeeds | ||
| let epoch = f.vault.request_deposit(&investor1, &1_000); | ||
| assert_eq!(f.vault.get_epoch(&epoch).unwrap().total_deposited, 1_000); | ||
|
|
||
| // Any subsequent deposit fails | ||
| refused( | ||
| f.vault.try_request_deposit(&investor2, &1), | ||
| VaultError::DepositCapExceeded, | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn cancellation_frees_capacity() { | ||
| let f = setup(); | ||
| f.vault.set_deposit_cap(&Some(1_000), &f.admin); | ||
|
|
||
| let investor1 = f.investor(1_000); | ||
| let investor2 = f.investor(1_000); | ||
|
|
||
| let epoch_id = f.vault.request_deposit(&investor1, &1_000); | ||
|
|
||
| // Blocked while request is pending | ||
| refused( | ||
| f.vault.try_request_deposit(&investor2, &500), | ||
| VaultError::DepositCapExceeded, | ||
| ); | ||
|
|
||
| // Cancel refund restores capacity | ||
| f.vault.cancel_deposit(&investor1, &epoch_id); | ||
| assert_eq!(f.vault.get_epoch(&epoch_id).unwrap().total_deposited, 0); | ||
|
|
||
| // Now investor2 can deposit | ||
| f.vault.request_deposit(&investor2, &500); | ||
| assert_eq!(f.vault.get_epoch(&epoch_id).unwrap().total_deposited, 500); | ||
| } | ||
|
|
||
| #[test] | ||
| fn deployed_capital_and_multi_epoch_accounting() { | ||
| let f = setup(); | ||
| f.vault.set_deposit_cap(&Some(1_000), &f.admin); | ||
|
|
||
| let investor1 = f.investor(1_000); | ||
| let investor2 = f.investor(1_000); | ||
|
|
||
| // Epoch 1 deposit | ||
| f.vault.request_deposit(&investor1, &1_000); | ||
| f.vault.close_epoch(&f.manager); | ||
| f.attest(wad(1)); | ||
| f.vault.fulfill_epoch(&1); | ||
|
|
||
| // Deploy 800 to custodian | ||
| f.vault.set_custodian(&f.custodian, &f.admin); | ||
| f.vault.deploy_to_custodian(&f.treasury, &800); | ||
| assert_eq!(f.vault.net_deployed(), 800); | ||
|
|
||
| // In Epoch 2, deposit of 1 still breaches cap because deployed capital is counted | ||
| refused( | ||
| f.vault.try_request_deposit(&investor2, &1), | ||
| VaultError::DepositCapExceeded, | ||
| ); | ||
|
|
||
| // Governance raises cap to 1_500 | ||
| f.vault.set_deposit_cap(&Some(1_500), &f.admin); | ||
|
|
||
| // Now investor2 can deposit 500 | ||
| let epoch2 = f.vault.request_deposit(&investor2, &500); | ||
| assert_eq!(f.vault.get_epoch(&epoch2).unwrap().total_deposited, 500); | ||
| } | ||
|
|
||
| #[test] | ||
| fn redemptions_free_capacity() { | ||
| let f = setup(); | ||
| f.vault.set_deposit_cap(&Some(1_000), &f.admin); | ||
|
|
||
| let investor1 = f.investor(1_000); | ||
| let investor2 = f.investor(1_000); | ||
|
|
||
| // Epoch 1: investor1 deposits 1_000 and claims 1_000 shares | ||
| f.vault.request_deposit(&investor1, &1_000); | ||
| f.vault.close_epoch(&f.manager); | ||
| f.attest(wad(1)); | ||
| f.vault.fulfill_epoch(&1); | ||
| f.vault.claim_deposit(&investor1, &1); | ||
|
|
||
| // In Epoch 2, deposited capital is 1_000, so new deposit of 500 fails | ||
| refused( | ||
| f.vault.try_request_deposit(&investor2, &500), | ||
| VaultError::DepositCapExceeded, | ||
| ); | ||
|
|
||
| // Investor 1 requests redemption of 500 shares | ||
| f.vault.request_redeem(&investor1, &500); | ||
| f.vault.close_epoch(&f.manager); | ||
| f.attest(wad(1)); | ||
| f.vault.fulfill_epoch(&2); | ||
|
|
||
| // Redemption is now priced and committed (500 committed to exit) | ||
| assert_eq!(f.vault.committed(), 500); | ||
|
|
||
| // In Epoch 3, headroom has opened up by 500, so investor2 can deposit 500 | ||
| let epoch3 = f.vault.request_deposit(&investor2, &500); | ||
| assert_eq!(f.vault.get_epoch(&epoch3).unwrap().total_deposited, 500); | ||
| } | ||
|
|
||
| #[test] | ||
| fn zero_cap_blocks_all_deposits() { | ||
| let f = setup(); | ||
| f.vault.set_deposit_cap(&Some(0), &f.admin); | ||
|
|
||
| let investor = f.investor(100); | ||
| refused( | ||
| f.vault.try_request_deposit(&investor, &1), | ||
| VaultError::DepositCapExceeded, | ||
| ); | ||
| } |
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
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.
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.
The PR body names
total_economic_assets(), which is not in the diff. Leftover from before the metric was simplified?