Locks an asset. ### Accounts: 0. `[writable]` asset 1. `[signer]` signer
(program_id: &Pubkey, ctx: Context<Lock>)
| 19 | /// 0. `[writable]` asset |
| 20 | /// 1. `[signer]` signer |
| 21 | pub fn process_lock(program_id: &Pubkey, ctx: Context<Lock>) -> ProgramResult { |
| 22 | // account validation |
| 23 | |
| 24 | require!( |
| 25 | ctx.accounts.signer.is_signer(), |
| 26 | ProgramError::MissingRequiredSignature, |
| 27 | "signer" |
| 28 | ); |
| 29 | |
| 30 | require!( |
| 31 | ctx.accounts.asset.owner() == program_id, |
| 32 | ProgramError::IllegalOwner, |
| 33 | "asset" |
| 34 | ); |
| 35 | |
| 36 | let mut data = ctx.accounts.asset.try_borrow_mut_data()?; |
| 37 | |
| 38 | require!( |
| 39 | data.len() >= Asset::LEN && data[0] == Discriminator::Asset.into(), |
| 40 | AssetError::Uninitialized, |
| 41 | "asset" |
| 42 | ); |
| 43 | |
| 44 | // locks the asset |
| 45 | |
| 46 | let (asset, extensions) = data.split_at_mut(Asset::LEN); |
| 47 | let asset = Asset::load_mut(asset); |
| 48 | let manager = Extension::get::<Manager>(extensions).map(|s| s.delegate); |
| 49 | |
| 50 | // Validate whether signer is the owner or a lock delegate. |
| 51 | // |
| 52 | // if the asset has a delegate, the signer must be the delegate or the manager |
| 53 | // delegate (if there is one) |
| 54 | if asset.delegate.value().is_some() { |
| 55 | assert_delegate( |
| 56 | &[asset.delegate.value(), manager], |
| 57 | ctx.accounts.signer.key(), |
| 58 | DelegateRole::Lock, |
| 59 | )?; |
| 60 | } |
| 61 | // otherwise, if the signer is not the owner, the signer must be the |
| 62 | // manager delegate |
| 63 | else if asset.owner != *ctx.accounts.signer.key() { |
| 64 | assert_delegate(&[manager], ctx.accounts.signer.key(), DelegateRole::Lock)?; |
| 65 | } |
| 66 | |
| 67 | asset.state = State::Locked; |
| 68 | |
| 69 | Ok(()) |
| 70 | } |
no test coverage detected