Skip to content

Commit

Permalink
upgrade to Anchor v0.26.0 (position bundles related codes)
Browse files Browse the repository at this point in the history
- ProgramResult to Result<()>
- add /// CHECK comments
- remove space attribute on Mint account
- change create_metadata_accounts_v2 to v3
- update testcases
  - Change in error code detected first
  - Change in account closing method
    coral-xyz/anchor#2169
  • Loading branch information
yugure-orca committed Mar 13, 2023
1 parent a32da9a commit 36ea6fe
Show file tree
Hide file tree
Showing 13 changed files with 116 additions and 108 deletions.
Expand Up @@ -30,14 +30,15 @@ pub struct CloseBundledPosition<'info> {

pub position_bundle_authority: Signer<'info>,

/// CHECK: safe, for receiving rent only
#[account(mut)]
pub receiver: UncheckedAccount<'info>,
}

pub fn handler(
ctx: Context<CloseBundledPosition>,
bundle_index: u16,
) -> ProgramResult {
) -> Result<()> {
let position_bundle = &mut ctx.accounts.position_bundle;

// Allow delegation
Expand Down
Expand Up @@ -22,14 +22,15 @@ pub struct DeletePositionBundle<'info> {

pub position_bundle_owner: Signer<'info>,

/// CHECK: safe, for receiving rent only
#[account(mut)]
pub receiver: UncheckedAccount<'info>,

#[account(address = token::ID)]
pub token_program: Program<'info, Token>,
}

pub fn handler(ctx: Context<DeletePositionBundle>) -> ProgramResult {
pub fn handler(ctx: Context<DeletePositionBundle>) -> Result<()> {
let position_bundle = &ctx.accounts.position_bundle;

if !position_bundle.is_deletable() {
Expand Down
Expand Up @@ -16,7 +16,6 @@ pub struct InitializePositionBundle<'info> {

#[account(init,
payer = funder,
space = Mint::LEN,
mint::authority = funder, // will be removed in the transaction
mint::decimals = 0,
)]
Expand All @@ -29,6 +28,7 @@ pub struct InitializePositionBundle<'info> {
)]
pub position_bundle_token_account: Box<Account<'info, TokenAccount>>,

/// CHECK: safe, the account that will be the owner of the position bundle can be arbitrary
pub position_bundle_owner: UncheckedAccount<'info>,

#[account(mut)]
Expand All @@ -43,7 +43,7 @@ pub struct InitializePositionBundle<'info> {

pub fn handler(
ctx: Context<InitializePositionBundle>,
) -> ProgramResult {
) -> Result<()> {
let position_bundle_mint = &ctx.accounts.position_bundle_mint;
let position_bundle = &mut ctx.accounts.position_bundle;

Expand Down
Expand Up @@ -17,7 +17,6 @@ pub struct InitializePositionBundleWithMetadata<'info> {

#[account(init,
payer = funder,
space = Mint::LEN,
mint::authority = funder, // will be removed in the transaction
mint::decimals = 0,
)]
Expand All @@ -35,6 +34,7 @@ pub struct InitializePositionBundleWithMetadata<'info> {
)]
pub position_bundle_token_account: Box<Account<'info, TokenAccount>>,

/// CHECK: safe, the account that will be the owner of the position bundle can be arbitrary
pub position_bundle_owner: UncheckedAccount<'info>,

#[account(mut)]
Expand All @@ -57,7 +57,7 @@ pub struct InitializePositionBundleWithMetadata<'info> {

pub fn handler(
ctx: Context<InitializePositionBundleWithMetadata>,
) -> ProgramResult {
) -> Result<()> {
let position_bundle_mint = &ctx.accounts.position_bundle_mint;
let position_bundle = &mut ctx.accounts.position_bundle;

Expand Down
Expand Up @@ -43,7 +43,7 @@ pub fn handler(
bundle_index: u16,
tick_lower_index: i32,
tick_upper_index: i32,
) -> ProgramResult {
) -> Result<()> {
let whirlpool = &ctx.accounts.whirlpool;
let position_bundle = &mut ctx.accounts.position_bundle;
let position = &mut ctx.accounts.bundled_position;
Expand Down
10 changes: 5 additions & 5 deletions programs/whirlpool/src/lib.rs
Expand Up @@ -537,7 +537,7 @@ pub mod whirlpool {
/// A unique token will be minted to represent the position bundle in the users wallet.
pub fn initialize_position_bundle(
ctx: Context<InitializePositionBundle>,
) -> ProgramResult {
) -> Result<()> {
return instructions::initialize_position_bundle::handler(ctx);
}

Expand All @@ -546,7 +546,7 @@ pub mod whirlpool {
/// Additional Metaplex metadata is appended to identify the token.
pub fn initialize_position_bundle_with_metadata(
ctx: Context<InitializePositionBundleWithMetadata>,
) -> ProgramResult {
) -> Result<()> {
return instructions::initialize_position_bundle_with_metadata::handler(ctx);
}

Expand All @@ -559,7 +559,7 @@ pub mod whirlpool {
/// - `PositionBundleNotDeletable` - The provided position bundle has open positions.
pub fn delete_position_bundle(
ctx: Context<DeletePositionBundle>,
) -> ProgramResult {
) -> Result<()> {
return instructions::delete_position_bundle::handler(ctx);
}

Expand All @@ -584,7 +584,7 @@ pub mod whirlpool {
bundle_index: u16,
tick_lower_index: i32,
tick_upper_index: i32,
) -> ProgramResult {
) -> Result<()> {
return instructions::open_bundled_position::handler(
ctx,
bundle_index,
Expand All @@ -607,7 +607,7 @@ pub mod whirlpool {
pub fn close_bundled_position(
ctx: Context<CloseBundledPosition>,
bundle_index: u16,
) -> ProgramResult {
) -> Result<()> {
return instructions::close_bundled_position::handler(ctx, bundle_index);
}
}
14 changes: 7 additions & 7 deletions programs/whirlpool/src/state/position_bundle.rs
Expand Up @@ -18,7 +18,7 @@ impl PositionBundle {
pub fn initialize(
&mut self,
position_bundle_mint: Pubkey,
) -> Result<(), ErrorCode> {
) -> Result<()> {
self.position_bundle_mint = position_bundle_mint;
// position_bitmap is initialized using Default trait
Ok(())
Expand All @@ -38,24 +38,24 @@ impl PositionBundle {
pub fn open_bundled_position(
&mut self,
bundle_index: u16,
) -> Result<(), ErrorCode> {
) -> Result<()> {
self.update_bitmap(bundle_index, true)
}

pub fn close_bundled_position(
&mut self,
bundle_index: u16,
) -> Result<(), ErrorCode> {
) -> Result<()> {
self.update_bitmap(bundle_index, false)
}

fn update_bitmap(
&mut self,
bundle_index: u16,
open: bool,
) -> Result<(), ErrorCode> {
) -> Result<()> {
if !PositionBundle::is_valid_bundle_index(bundle_index) {
return Err(ErrorCode::InvalidBundleIndex);
return Err(ErrorCode::InvalidBundleIndex.into());
}

let bitmap_index = bundle_index / 8;
Expand All @@ -69,12 +69,12 @@ impl PositionBundle {
if open && opened {
// UNREACHABLE
// Anchor should reject with AccountDiscriminatorAlreadySet
return Err(ErrorCode::BundledPositionAlreadyOpened);
return Err(ErrorCode::BundledPositionAlreadyOpened.into());
}
if !open && !opened {
// UNREACHABLE
// Anchor should reject with AccountNotInitialized
return Err(ErrorCode::BundledPositionAlreadyClosed);
return Err(ErrorCode::BundledPositionAlreadyClosed.into());
}

let updated_bitmap = bitmap ^ mask;
Expand Down
21 changes: 13 additions & 8 deletions programs/whirlpool/src/util/token.rs
Expand Up @@ -223,7 +223,7 @@ pub fn mint_position_bundle_token_and_remove_authority<'info>(
position_bundle_mint: &Account<'info, Mint>,
position_bundle_token_account: &Account<'info, TokenAccount>,
token_program: &Program<'info, Token>,
) -> ProgramResult {
) -> Result<()> {
mint_position_bundle_token(
funder,
position_bundle_mint,
Expand All @@ -247,7 +247,7 @@ pub fn mint_position_bundle_token_with_metadata_and_remove_authority<'info>(
token_program: &Program<'info, Token>,
system_program: &Program<'info, System>,
rent: &Sysvar<'info, Rent>,
) -> ProgramResult {
) -> Result<()> {
mint_position_bundle_token(
funder,
position_bundle_mint,
Expand All @@ -266,7 +266,7 @@ pub fn mint_position_bundle_token_with_metadata_and_remove_authority<'info>(
nft_name += &mint_address[mint_address.len()-4..];

invoke(
&create_metadata_accounts_v2(
&create_metadata_accounts_v3(
metadata_program.key(),
position_bundle_metadata.key(),
position_bundle_mint.key(),
Expand All @@ -282,6 +282,7 @@ pub fn mint_position_bundle_token_with_metadata_and_remove_authority<'info>(
true,
None,
None,
None,
),
&[
position_bundle_metadata.to_account_info(),
Expand All @@ -306,7 +307,7 @@ fn mint_position_bundle_token<'info>(
position_bundle_mint: &Account<'info, Mint>,
position_bundle_token_account: &Account<'info, TokenAccount>,
token_program: &Program<'info, Token>,
) -> ProgramResult {
) -> Result<()> {
invoke(
&mint_to(
token_program.key,
Expand All @@ -322,14 +323,16 @@ fn mint_position_bundle_token<'info>(
funder.to_account_info(),
token_program.to_account_info(),
],
)
)?;

Ok(())
}

fn remove_position_bundle_token_mint_authority<'info>(
funder: &Signer<'info>,
position_bundle_mint: &Account<'info, Mint>,
token_program: &Program<'info, Token>,
) -> ProgramResult {
) -> Result<()> {
invoke(
&set_authority(
token_program.key,
Expand All @@ -344,7 +347,9 @@ fn remove_position_bundle_token_mint_authority<'info>(
funder.to_account_info(),
token_program.to_account_info(),
],
)
)?;

Ok(())
}

pub fn burn_and_close_position_bundle_token<'info>(
Expand All @@ -353,7 +358,7 @@ pub fn burn_and_close_position_bundle_token<'info>(
position_bundle_mint: &Account<'info, Mint>,
position_bundle_token_account: &Account<'info, TokenAccount>,
token_program: &Program<'info, Token>,
) -> ProgramResult {
) -> Result<()> {
// use same logic
burn_and_close_user_position_token(
position_bundle_authority,
Expand Down
2 changes: 1 addition & 1 deletion programs/whirlpool/src/util/util.rs
Expand Up @@ -11,7 +11,7 @@ use crate::errors::ErrorCode;
pub fn verify_position_bundle_authority<'info>(
position_bundle_token_account: &TokenAccount,
position_bundle_authority: &Signer<'info>,
) -> Result<(), ProgramError> {
) -> Result<()> {
// use same logic
verify_position_authority(
position_bundle_token_account,
Expand Down
4 changes: 2 additions & 2 deletions sdk/tests/integration/initialize_position_bundle.test.ts
Expand Up @@ -200,7 +200,7 @@ describe("initialize_position_bundle", () => {

await assert.rejects(
tx.buildAndExecute(),
/Cross-program invocation with unauthorized signer or writable account/ // cannot init PDA account
/0x7d6/ // ConstraintSeeds
);
});

Expand Down Expand Up @@ -248,7 +248,7 @@ describe("initialize_position_bundle", () => {

await assert.rejects(
tx.buildAndExecute(),
/invalid program argument/
/0xbc7/ // AccountSysvarMismatch
);
});

Expand Down
Expand Up @@ -233,7 +233,7 @@ describe("initialize_position_bundle_with_metadata", () => {

await assert.rejects(
tx.buildAndExecute(),
/Cross-program invocation with unauthorized signer or writable account/ // cannot init PDA account
/0x7d6/ // ConstraintSeeds
);
});

Expand Down Expand Up @@ -305,7 +305,7 @@ describe("initialize_position_bundle_with_metadata", () => {

await assert.rejects(
tx.buildAndExecute(),
/invalid program argument/
/0xbc7/ // AccountSysvarMismatch
);
});

Expand Down

0 comments on commit 36ea6fe

Please sign in to comment.