Skip to content

Commit

Permalink
remove close escrow instruction
Browse files Browse the repository at this point in the history
  • Loading branch information
docongminh committed Jan 14, 2025
1 parent dc31f66 commit dda602f
Show file tree
Hide file tree
Showing 9 changed files with 10 additions and 995 deletions.
3 changes: 3 additions & 0 deletions programs/locker/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,7 @@ pub enum LockerError {

#[msg("Invalid merkle proof")]
InvalidMerkleProof,

#[msg("Escrow is not cancelled")]
EscrowNotCancelled,
}
38 changes: 5 additions & 33 deletions programs/locker/src/instructions/v3/close_claim_status.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use crate::*;
use anchor_lang::system_program;
use util::{account_info_ref_lifetime_shortener, close};

/// Accounts for [locker::close_claim_status].
#[derive(Accounts)]
Expand All @@ -10,12 +8,13 @@ pub struct CloseClaimStatus<'info> {
#[account(
mut,
has_one = recipient,
has_one = escrow
has_one = escrow,
close = rent_receiver
)]
pub claim_status: Account<'info, ClaimStatus>,

/// CHECK: this account use to verify escrow cancelled or closed
pub escrow: UncheckedAccount<'info>,
#[account(constraint = escrow.load()?.cancelled_at > 0 @ LockerError::EscrowNotCancelled)]
pub escrow: AccountLoader<'info, VestingEscrowV3>,

/// CHECKED: The system account will receive the rent
#[account(mut)]
Expand All @@ -27,34 +26,7 @@ pub struct CloseClaimStatus<'info> {
}

pub fn handle_close_claim_status<'c: 'info, 'info>(
ctx: Context<'_, '_, 'c, 'info, CloseClaimStatus<'info>>,
_ctx: Context<'_, '_, 'c, 'info, CloseClaimStatus<'info>>,
) -> Result<()> {
// only allow close claim status if escrow is cancel
let mut is_close =
ctx.accounts.escrow.owner == &system_program::ID && ctx.accounts.escrow.data_len() == 0;

if !is_close {
let escrow_info = &ctx.accounts.escrow.to_account_info();

let escrow: AccountLoader<VestingEscrowV3> = AccountLoader::try_from_unchecked(
&ctx.program_id,
account_info_ref_lifetime_shortener(&escrow_info),
)?;

is_close = escrow.load()?.cancelled_at > 0;
}

if is_close {
close(
ctx.accounts.claim_status.to_account_info(),
ctx.accounts.rent_receiver.to_account_info(),
)?;
}

emit_cpi!(EventCloseClaimStatus {
escrow: ctx.accounts.escrow.key(),
recipient: ctx.accounts.recipient.key(),
rent_receiver: ctx.accounts.rent_receiver.key()
});
Ok(())
}
137 changes: 0 additions & 137 deletions programs/locker/src/instructions/v3/close_vesting_escrow.rs

This file was deleted.

2 changes: 0 additions & 2 deletions programs/locker/src/instructions/v3/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
pub mod create_vesting_escrow;
pub mod claim;
pub mod create_vesting_escrow_metadata;
pub mod close_vesting_escrow;
pub mod cancel_vesting_escrow;
pub mod close_claim_status;

pub use create_vesting_escrow::*;
pub use claim::*;
pub use create_vesting_escrow_metadata::*;
pub use close_vesting_escrow::*;
pub use cancel_vesting_escrow::*;
pub use close_claim_status::*;
20 changes: 2 additions & 18 deletions programs/locker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ pub mod locker {
}

/// Close claim status account
/// Only allow close if escrow cancelled or closed
/// Only allow close if escrow cancelled
/// Rent fee will be transferred to recipient
/// # Arguments
///
Expand All @@ -210,23 +210,7 @@ pub mod locker {
) -> Result<()> {
handle_close_claim_status(ctx)
}

/// Close vesting escrow V3
/// - Close vesting escrow and escrow ATA and escrow metadata if all recipients already claimed all tokens
/// - Rent receiver must be escrow's creator
/// This instruction supports both splToken and token2022
/// # Arguments
///
/// * ctx - The accounts needed by instruction.
/// * remaining_accounts_info: additional accounts needed by instruction
///
pub fn close_vesting_escrow_v3<'c: 'info, 'info>(
ctx: Context<'_, '_, 'c, 'info, CloseVestingEscrowV3<'info>>,
remaining_accounts_info: Option<RemainingAccountsInfo>,
) -> Result<()> {
handle_close_vesting_escrow_v3(ctx, remaining_accounts_info)
}


/// Cancel a vesting escrow v3
/// - The rest of token will be transferred to the creator
/// This instruction supports both splToken and token2022
Expand Down
7 changes: 0 additions & 7 deletions programs/locker/src/util/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,3 @@ pub fn close<'info>(info: AccountInfo<'info>, sol_destination: AccountInfo<'info
pub fn is_closed(info: &AccountInfo) -> bool {
info.owner == &System::id() && info.data_is_empty()
}

/// This is safe because it shortens lifetimes 'info: 'o and 'a: 'o to that of 'o
pub fn account_info_ref_lifetime_shortener<'info: 'a + 'o, 'a: 'o, 'o>(
account_info: &'a AccountInfo<'info>,
) -> &'o AccountInfo<'o> {
unsafe { core::mem::transmute(account_info) }
}
78 changes: 0 additions & 78 deletions tests/locker_utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1114,84 +1114,6 @@ export async function closeVestingEscrow(params: CloseVestingEscrowParams) {
}
}

export async function closeVestingEscrowV3(params: CloseVestingEscrowParams) {
let { isAssertion, escrow, creator } = params;
const program = createLockerProgram(new Wallet(creator));
let [escrowMetadata] = deriveEscrowMetadata(escrow, program.programId);
let escrowState = await program.account.vestingEscrowV3.fetch(escrow);

let tokenProgram =
escrowState.tokenProgramFlag == 0
? TOKEN_PROGRAM_ID
: TOKEN_2022_PROGRAM_ID;
const escrowToken = getAssociatedTokenAddressSync(
escrowState.tokenMint,
escrow,
true,
tokenProgram,
ASSOCIATED_TOKEN_PROGRAM_ID
);

const creatorToken = getAssociatedTokenAddressSync(
escrowState.tokenMint,
creator.publicKey,
true,
tokenProgram,
ASSOCIATED_TOKEN_PROGRAM_ID
);

let remainingAccountsInfo = null;
let remainingAccounts: AccountMeta[] = [];
if (escrowState.tokenProgramFlag == 1) {
let cancelTransferHookAccounts =
await TokenExtensionUtil.getExtraAccountMetasForTransferHook(
program.provider.connection,
escrowState.tokenMint,
escrowToken,
creatorToken,
escrow,
tokenProgram
);

[remainingAccountsInfo, remainingAccounts] = new RemainingAccountsBuilder()
.addSlice(
RemainingAccountsType.TransferHookEscrow,
cancelTransferHookAccounts
)
.build();
}

await program.methods
.closeVestingEscrowV3(remainingAccountsInfo)
.accounts({
escrow,
escrowToken,
creatorToken,
memoProgram: MEMO_PROGRAM,
escrowMetadata,
tokenMint: escrowState.tokenMint,
tokenProgram,
creator: creator.publicKey,
})
.signers([creator])
.remainingAccounts(remainingAccounts)
.rpc();

if (isAssertion) {
let escrowStateAfter = await program.account.vestingEscrow.fetchNullable(
escrow
);
expect(escrowStateAfter).eq(null);
let escrowMetadataState =
await program.account.vestingEscrowMetadata.fetchNullable(escrow);
expect(escrowMetadataState).eq(null);
let escrowTokenState = await program.provider.connection.getAccountInfo(
escrowToken
);
expect(escrowTokenState).eq(null);
}
}

export interface CloseClaimStatusParams {
isAssertion: boolean;
recipient: web3.Keypair;
Expand Down
Loading

0 comments on commit dda602f

Please sign in to comment.