-
Notifications
You must be signed in to change notification settings - Fork 4
feat(stablecoin): add deposit_collateral instruction #345
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
gravityblast
wants to merge
3
commits into
feat/stablecoin-open-position-rebuild
Choose a base branch
from
feat/stablecoin-deposit-collateral
base: feat/stablecoin-open-position-rebuild
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 all commits
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
Some comments aren't visible on the classic Files Changed page.
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
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,159 @@ | ||
| use lee_core::{ | ||
| account::{Account, AccountWithMetadata, Data}, | ||
| program::{AccountPostState, ChainedCall, ProgramId}, | ||
| }; | ||
| use stablecoin_core::{ | ||
| compute_protocol_parameters_pda, verify_position_and_get_seed, Position, ProtocolParameters, | ||
| }; | ||
| use token_core::TokenHolding; | ||
|
|
||
| /// Deposit `amount` additional collateral tokens into an existing `position`'s vault. | ||
| /// | ||
| /// Increases `Position.collateral_amount` by `amount` and emits a single chained | ||
| /// `Token::Transfer` from the user's authorized holding into the vault. No PDA | ||
| /// seed is attached: the sender is the user's own holding, which the transaction's | ||
| /// witness set authorizes. | ||
| /// | ||
| /// Deliberately allowed while the protocol is frozen, and deliberately skips the | ||
| /// §6.2 collateralization check — a deposit can only improve the position, so | ||
| /// spec §7 keeps this path open in emergencies. | ||
| /// | ||
| /// # Panics | ||
| /// - `owner` or `user_collateral_holding` is not authorized. | ||
| /// - `position` is uninitialized, not owned by `stablecoin_program_id`, does not decode as a | ||
| /// [`Position`], or sits at an address that does not match | ||
| /// `compute_position_pda(stablecoin_program_id, owner, Position.position_nonce)`. | ||
| /// - `Position.owner_account_id` does not match `owner`. | ||
| /// - `vault` does not match `Position.vault_account_id`, is uninitialized, does not decode as a | ||
| /// [`TokenHolding`], or holds a token other than the protocol's collateral definition. | ||
| /// - `protocol_parameters` is uninitialized, not owned by `stablecoin_program_id`, or does not | ||
| /// decode. | ||
| /// - `user_collateral_holding` is owned by a different Token Program than the vault, or its | ||
| /// `definition_id` is not `ProtocolParameters.collateral_definition_id`. | ||
| /// - `Position.collateral_amount + amount` overflows. | ||
| pub fn deposit_collateral( | ||
| owner: AccountWithMetadata, | ||
| position: AccountWithMetadata, | ||
| vault: AccountWithMetadata, | ||
| user_collateral_holding: AccountWithMetadata, | ||
| protocol_parameters: AccountWithMetadata, | ||
| stablecoin_program_id: ProgramId, | ||
| amount: u128, | ||
| ) -> (Vec<AccountPostState>, Vec<ChainedCall>) { | ||
| assert!(owner.is_authorized, "Owner authorization is missing"); | ||
| assert!( | ||
| user_collateral_holding.is_authorized, | ||
| "User collateral holding authorization is missing" | ||
| ); | ||
|
|
||
| assert_ne!( | ||
| position.account, | ||
| Account::default(), | ||
| "Position account must be initialized" | ||
| ); | ||
| assert_eq!( | ||
| position.account.program_owner, stablecoin_program_id, | ||
| "Position is not owned by this stablecoin program" | ||
| ); | ||
| let position_data = Position::try_from(&position.account.data) | ||
| .expect("Position account must hold valid Position state"); | ||
| // Binds the position to this owner. The seed is unused downstream — the | ||
| // position was already PDA-claimed by `open_position`. | ||
| let _position_seed = verify_position_and_get_seed( | ||
| &position, | ||
| &owner, | ||
| position_data.position_nonce, | ||
| stablecoin_program_id, | ||
| ); | ||
| assert_eq!( | ||
| position_data.owner_account_id, owner.account_id, | ||
| "Position owner_account_id does not match the owner account" | ||
| ); | ||
| assert_eq!( | ||
| position_data.vault_account_id, vault.account_id, | ||
| "Position vault_account_id does not match the vault account" | ||
| ); | ||
|
|
||
| assert_ne!( | ||
| protocol_parameters.account, | ||
| Account::default(), | ||
| "ProtocolParameters account must be initialized" | ||
| ); | ||
| assert_eq!( | ||
| protocol_parameters.account.program_owner, stablecoin_program_id, | ||
| "ProtocolParameters account must be owned by the stablecoin program" | ||
| ); | ||
| // Pin the address: ownership plus a successful decode would otherwise let | ||
| // any stablecoin-owned account that decodes as ProtocolParameters stand in | ||
| // for the global config and redirect the collateral-definition binding. | ||
| assert_eq!( | ||
| protocol_parameters.account_id, | ||
| compute_protocol_parameters_pda(stablecoin_program_id), | ||
| "ProtocolParameters account ID does not match expected PDA derivation" | ||
| ); | ||
| let parameters = ProtocolParameters::try_from(&protocol_parameters.account.data) | ||
| .expect("ProtocolParameters must decode"); | ||
| // `is_frozen` is deliberately not read: a deposit only improves the | ||
| // position's collateralization, so spec §7 keeps it available when frozen. | ||
|
|
||
| // Validate the vault before trusting its `program_owner` to route the chained | ||
| // call. Without this the transfer would be aimed at whatever program owns the | ||
| // account and left to fail downstream, and a vault holding some other token | ||
| // would silently mis-bank the deposit. | ||
| assert_ne!( | ||
| vault.account, | ||
| Account::default(), | ||
| "Vault account must be initialized" | ||
| ); | ||
| let vault_holding = TokenHolding::try_from(&vault.account.data) | ||
| .expect("Vault account must hold a valid TokenHolding"); | ||
| assert_eq!( | ||
| vault_holding.definition_id(), | ||
| parameters.collateral_definition_id, | ||
| "Vault holding does not match the protocol's collateral definition" | ||
| ); | ||
|
|
||
| let token_program_id = vault.account.program_owner; | ||
| assert_eq!( | ||
| user_collateral_holding.account.program_owner, token_program_id, | ||
| "User collateral holding must be owned by the same Token Program as the vault" | ||
| ); | ||
| let user_holding = TokenHolding::try_from(&user_collateral_holding.account.data) | ||
| .expect("User holding must be a valid TokenHolding"); | ||
| assert_eq!( | ||
| user_holding.definition_id(), | ||
| parameters.collateral_definition_id, | ||
| "User collateral holding does not match the protocol's collateral definition" | ||
| ); | ||
|
|
||
| let new_collateral = position_data | ||
| .collateral_amount | ||
| .checked_add(amount) | ||
| .expect("Position collateral_amount overflow"); | ||
|
|
||
| let mut position_post = position.account.clone(); | ||
| position_post.data = Data::from(&Position { | ||
| collateral_amount: new_collateral, | ||
| ..position_data | ||
| }); | ||
|
|
||
| let post_states = vec![ | ||
| AccountPostState::new(owner.account), | ||
| AccountPostState::new(position_post), | ||
| AccountPostState::new(vault.account.clone()), | ||
| AccountPostState::new(user_collateral_holding.account.clone()), | ||
| AccountPostState::new(protocol_parameters.account), | ||
| ]; | ||
|
|
||
| // No PDA seed: the sender is the user's own holding, authorized by the | ||
| // transaction's witness set. The receiving vault needs no authorization. | ||
| let transfer_call = ChainedCall::new( | ||
| token_program_id, | ||
| vec![user_collateral_holding, vault], | ||
| &token_core::Instruction::Transfer { | ||
| amount_to_transfer: amount, | ||
| }, | ||
| ); | ||
|
|
||
| (post_states, vec![transfer_call]) | ||
| } | ||
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.
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.
This update ignores
vault_holding's balance, so a direct token donation leaves the accounting permanently desynchronized.Token::Transferrequires only the sender's authorization. Starting from position500/ vault500, another holder can transfer1to the vault; depositing100then writes position600while the chained transfer writes vault601. Withdrawing the recorded600leaves position0/ vault1, and no further withdrawal can recover it.Please reconcile from the fungible vault balance before adding
amount(or provide an equivalent recovery path) and cover transfer -> deposit.