-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
176 additions
and
12 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,68 @@ | ||
use crate::states::*; | ||
use crate::util::get_recent_epoch; | ||
use anchor_lang::prelude::*; | ||
use anchor_spl::token::spl_token::instruction::AuthorityType; | ||
use anchor_spl::token::{set_authority, SetAuthority, Token}; | ||
use anchor_spl::token_interface::TokenAccount; | ||
|
||
pub const LOCK_POSITION_SEED: &str = "locked_position"; | ||
#[derive(Accounts)] | ||
pub struct LockPosition<'info> { | ||
/// The position owner or delegated authority | ||
#[account(mut)] | ||
pub nft_owner: Signer<'info>, | ||
|
||
/// The token account for the tokenized position | ||
#[account( | ||
constraint = nft_account.mint == personal_position.nft_mint, | ||
token::token_program = token_program, | ||
)] | ||
pub nft_account: Box<InterfaceAccount<'info, TokenAccount>>, | ||
|
||
/// Decrease liquidity for this position | ||
#[account()] | ||
pub personal_position: Box<Account<'info, PersonalPositionState>>, | ||
|
||
#[account( | ||
init, | ||
seeds = [ | ||
LOCKED_POSITION_SEED.as_bytes(), | ||
personal_position.key().as_ref(), | ||
], | ||
bump, | ||
payer = nft_owner, | ||
space = LockedPositionState::LEN | ||
)] | ||
pub locked_position: Box<Account<'info, LockedPositionState>>, | ||
|
||
/// SPL program to transfer out tokens | ||
pub token_program: Program<'info, Token>, | ||
|
||
/// Program to create the position manager state account | ||
pub system_program: Program<'info, System>, | ||
} | ||
|
||
pub fn lock_position<'a, 'b, 'c: 'info, 'info>( | ||
ctx: Context<'a, 'b, 'c, 'info, LockPosition<'info>>, | ||
) -> Result<()> { | ||
require_gt!(ctx.accounts.personal_position.liquidity, 0); | ||
ctx.accounts.locked_position.initialize( | ||
ctx.bumps.locked_position, | ||
ctx.accounts.nft_owner.key(), | ||
ctx.accounts.personal_position.pool_id, | ||
ctx.accounts.personal_position.key(), | ||
ctx.accounts.nft_account.key(), | ||
get_recent_epoch()?, | ||
); | ||
|
||
let cpi_context = CpiContext::new( | ||
ctx.accounts.token_program.to_account_info(), | ||
SetAuthority { | ||
current_authority: ctx.accounts.nft_owner.to_account_info(), | ||
account_or_mint: ctx.accounts.nft_account.to_account_info(), | ||
}, | ||
); | ||
set_authority(cpi_context, AuthorityType::AccountOwner, Some(crate::id()))?; | ||
|
||
Ok(()) | ||
} |
This file contains 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 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 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 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,58 @@ | ||
use anchor_lang::prelude::*; | ||
pub const LOCKED_POSITION_SEED: &str = "locked_position"; | ||
|
||
#[account] | ||
#[derive(Default, Debug)] | ||
pub struct LockedPositionState { | ||
/// Bump to identify PDA | ||
pub bump: [u8; 1], | ||
/// Record owner | ||
pub owner: Pubkey, | ||
/// The ID of the pool with which this record is connected | ||
pub pool_id: Pubkey, | ||
/// The ID of the position with which this record is connected | ||
pub position_id: Pubkey, | ||
/// NFT Account | ||
pub nft_account: Pubkey, | ||
/// account update recent epoch | ||
pub recent_epoch: u64, | ||
/// Unused bytes for future upgrades. | ||
pub padding: [u64; 8], | ||
} | ||
|
||
impl LockedPositionState { | ||
pub const LEN: usize = 8 + 1 + 32 + 32 + 32 + 32 + 8 + 8 * 8; | ||
|
||
pub fn key(position_id: Pubkey) -> Pubkey { | ||
Pubkey::find_program_address( | ||
&[LOCKED_POSITION_SEED.as_bytes(), position_id.as_ref()], | ||
&crate::id(), | ||
) | ||
.0 | ||
} | ||
|
||
pub fn initialize( | ||
&mut self, | ||
bump: u8, | ||
owner: Pubkey, | ||
pool_id: Pubkey, | ||
position_id: Pubkey, | ||
nft_account: Pubkey, | ||
recent_epoch:u64, | ||
) { | ||
self.bump = [bump]; | ||
self.owner = owner; | ||
self.pool_id = pool_id; | ||
self.position_id = position_id; | ||
self.nft_account = nft_account; | ||
self.recent_epoch = recent_epoch; | ||
self.padding = [0; 8]; | ||
} | ||
|
||
pub fn check(&self, owner: Pubkey, position_id: Pubkey, nft_account: Pubkey) -> Result<()> { | ||
require_keys_eq!(self.owner, owner); | ||
require_keys_eq!(self.position_id, position_id); | ||
require_keys_eq!(self.nft_account, nft_account); | ||
Ok(()) | ||
} | ||
} |
This file contains 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 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 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