Skip to content

Commit cd72458

Browse files
refactor(tokens): convert impl blocks to free handle_* functions
All tokens examples (escrow, create-token, transfer-tokens, nft-minter, nft-operations, spl-token-minter, token-swap, token-fundraiser, external-delegate-token-master, pda-mint-authority) refactored. For escrow: disambiguated handle_withdraw_tokens_and_close using module-qualified paths (take:: and refund::) since both Take and Refund had methods with the same name.
1 parent 33edeee commit cd72458

53 files changed

Lines changed: 2679 additions & 2839 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

tokens/create-token/quasar/src/lib.rs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ mod quasar_create_token {
2020
/// Create a new token mint (account init handled by Quasar's `#[account(init)]`).
2121
#[instruction(discriminator = 0)]
2222
pub fn create_token(ctx: Ctx<CreateToken>, _decimals: u8) -> Result<(), ProgramError> {
23-
ctx.accounts.create_token()
23+
handle_create_token(&mut ctx.accounts)
2424
}
2525

2626
/// Mint tokens to the creator's token account.
2727
#[instruction(discriminator = 1)]
2828
pub fn mint_tokens(ctx: Ctx<MintTokens>, amount: u64) -> Result<(), ProgramError> {
29-
ctx.accounts.mint_tokens(amount)
29+
handle_mint_tokens(&mut ctx.accounts, amount)
3030
}
3131
}
3232

@@ -43,12 +43,10 @@ pub struct CreateToken<'info> {
4343
pub system_program: &'info Program<System>,
4444
}
4545

46-
impl CreateToken<'_> {
47-
#[inline(always)]
48-
pub fn create_token(&self) -> Result<(), ProgramError> {
49-
// Mint account is created and initialised by Quasar's account init.
50-
Ok(())
51-
}
46+
#[inline(always)]
47+
pub fn handle_create_token(accounts: &CreateToken) -> Result<(), ProgramError> {
48+
// Mint account is created and initialised by Quasar's account init.
49+
Ok(())
5250
}
5351

5452
/// Accounts for minting tokens to an existing token account.
@@ -63,11 +61,9 @@ pub struct MintTokens<'info> {
6361
pub token_program: &'info Program<Token>,
6462
}
6563

66-
impl MintTokens<'_> {
67-
#[inline(always)]
68-
pub fn mint_tokens(&mut self, amount: u64) -> Result<(), ProgramError> {
69-
self.token_program
70-
.mint_to(self.mint, self.token_account, self.authority, amount)
71-
.invoke()
72-
}
64+
#[inline(always)]
65+
pub fn handle_mint_tokens(accounts: &mut MintTokens, amount: u64) -> Result<(), ProgramError> {
66+
accounts.token_program
67+
.mint_to(accounts.mint, accounts.token_account, accounts.authority, amount)
68+
.invoke()
7369
}

tokens/escrow/quasar/src/instructions/make.rs

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,22 @@ pub struct Make<'info> {
2323
pub system_program: &'info Program<System>,
2424
}
2525

26-
impl Make<'_> {
27-
#[inline(always)]
28-
pub fn make_escrow(&mut self, receive: u64, bumps: &MakeBumps) -> Result<(), ProgramError> {
29-
self.escrow.set_inner(
30-
*self.maker.address(),
31-
*self.mint_a.address(),
32-
*self.mint_b.address(),
33-
*self.maker_ta_b.address(),
34-
receive,
35-
bumps.escrow,
36-
);
37-
Ok(())
38-
}
26+
#[inline(always)]
27+
pub fn handle_make_escrow(accounts: &mut Make, receive: u64, bumps: &MakeBumps) -> Result<(), ProgramError> {
28+
accounts.escrow.set_inner(
29+
*accounts.maker.address(),
30+
*accounts.mint_a.address(),
31+
*accounts.mint_b.address(),
32+
*accounts.maker_ta_b.address(),
33+
receive,
34+
bumps.escrow,
35+
);
36+
Ok(())
37+
}
3938

40-
#[inline(always)]
41-
pub fn deposit_tokens(&mut self, amount: u64) -> Result<(), ProgramError> {
42-
self.token_program
43-
.transfer(self.maker_ta_a, self.vault_ta_a, self.maker, amount)
44-
.invoke()
45-
}
39+
#[inline(always)]
40+
pub fn handle_deposit_tokens(accounts: &mut Make, amount: u64) -> Result<(), ProgramError> {
41+
accounts.token_program
42+
.transfer(accounts.maker_ta_a, accounts.vault_ta_a, accounts.maker, amount)
43+
.invoke()
4644
}

tokens/escrow/quasar/src/instructions/refund.rs

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,29 +26,27 @@ pub struct Refund<'info> {
2626
pub system_program: &'info Program<System>,
2727
}
2828

29-
impl Refund<'_> {
30-
#[inline(always)]
31-
pub fn withdraw_tokens_and_close(&mut self, bumps: &RefundBumps) -> Result<(), ProgramError> {
32-
let maker_key = self.escrow.maker;
33-
let bump = [bumps.escrow];
34-
let seeds: &[Seed] = &[
35-
Seed::from(b"escrow" as &[u8]),
36-
Seed::from(maker_key.as_ref()),
37-
Seed::from(&bump as &[u8]),
38-
];
29+
#[inline(always)]
30+
pub fn handle_withdraw_tokens_and_close(accounts: &mut Refund, bumps: &RefundBumps) -> Result<(), ProgramError> {
31+
let maker_key = accounts.escrow.maker;
32+
let bump = [bumps.escrow];
33+
let seeds: &[Seed] = &[
34+
Seed::from(b"escrow" as &[u8]),
35+
Seed::from(maker_key.as_ref()),
36+
Seed::from(&bump as &[u8]),
37+
];
3938

40-
self.token_program
41-
.transfer(
42-
self.vault_ta_a,
43-
self.maker_ta_a,
44-
self.escrow,
45-
self.vault_ta_a.amount(),
46-
)
47-
.invoke_signed(seeds)?;
39+
accounts.token_program
40+
.transfer(
41+
accounts.vault_ta_a,
42+
accounts.maker_ta_a,
43+
accounts.escrow,
44+
accounts.vault_ta_a.amount(),
45+
)
46+
.invoke_signed(seeds)?;
4847

49-
self.token_program
50-
.close_account(self.vault_ta_a, self.maker, self.escrow)
51-
.invoke_signed(seeds)?;
52-
Ok(())
53-
}
48+
accounts.token_program
49+
.close_account(accounts.vault_ta_a, accounts.maker, accounts.escrow)
50+
.invoke_signed(seeds)?;
51+
Ok(())
5452
}

tokens/escrow/quasar/src/instructions/take.rs

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -35,41 +35,39 @@ pub struct Take<'info> {
3535
pub system_program: &'info Program<System>,
3636
}
3737

38-
impl Take<'_> {
39-
#[inline(always)]
40-
pub fn transfer_tokens(&mut self) -> Result<(), ProgramError> {
41-
self.token_program
42-
.transfer(
43-
self.taker_ta_b,
44-
self.maker_ta_b,
45-
self.taker,
46-
self.escrow.receive,
47-
)
48-
.invoke()
49-
}
38+
#[inline(always)]
39+
pub fn handle_transfer_tokens(accounts: &mut Take) -> Result<(), ProgramError> {
40+
accounts.token_program
41+
.transfer(
42+
accounts.taker_ta_b,
43+
accounts.maker_ta_b,
44+
accounts.taker,
45+
accounts.escrow.receive,
46+
)
47+
.invoke()
48+
}
5049

51-
#[inline(always)]
52-
pub fn withdraw_tokens_and_close(&mut self, bumps: &TakeBumps) -> Result<(), ProgramError> {
53-
let maker_key = self.escrow.maker;
54-
let bump = [bumps.escrow];
55-
let seeds: &[Seed] = &[
56-
Seed::from(b"escrow" as &[u8]),
57-
Seed::from(maker_key.as_ref()),
58-
Seed::from(&bump as &[u8]),
59-
];
50+
#[inline(always)]
51+
pub fn handle_withdraw_tokens_and_close(accounts: &mut Take, bumps: &TakeBumps) -> Result<(), ProgramError> {
52+
let maker_key = accounts.escrow.maker;
53+
let bump = [bumps.escrow];
54+
let seeds: &[Seed] = &[
55+
Seed::from(b"escrow" as &[u8]),
56+
Seed::from(maker_key.as_ref()),
57+
Seed::from(&bump as &[u8]),
58+
];
6059

61-
self.token_program
62-
.transfer(
63-
self.vault_ta_a,
64-
self.taker_ta_a,
65-
self.escrow,
66-
self.vault_ta_a.amount(),
67-
)
68-
.invoke_signed(seeds)?;
60+
accounts.token_program
61+
.transfer(
62+
accounts.vault_ta_a,
63+
accounts.taker_ta_a,
64+
accounts.escrow,
65+
accounts.vault_ta_a.amount(),
66+
)
67+
.invoke_signed(seeds)?;
6968

70-
self.token_program
71-
.close_account(self.vault_ta_a, self.taker, self.escrow)
72-
.invoke_signed(seeds)?;
73-
Ok(())
74-
}
69+
accounts.token_program
70+
.close_account(accounts.vault_ta_a, accounts.taker, accounts.escrow)
71+
.invoke_signed(seeds)?;
72+
Ok(())
7573
}

tokens/escrow/quasar/src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,18 @@ mod quasar_escrow {
1919

2020
#[instruction(discriminator = 0)]
2121
pub fn make(ctx: Ctx<Make>, deposit: u64, receive: u64) -> Result<(), ProgramError> {
22-
ctx.accounts.make_escrow(receive, &ctx.bumps)?;
23-
ctx.accounts.deposit_tokens(deposit)
22+
instructions::handle_make_escrow(&mut ctx.accounts, receive, &ctx.bumps)?;
23+
instructions::handle_deposit_tokens(&mut ctx.accounts, deposit)
2424
}
2525

2626
#[instruction(discriminator = 1)]
2727
pub fn take(ctx: Ctx<Take>) -> Result<(), ProgramError> {
28-
ctx.accounts.transfer_tokens()?;
29-
ctx.accounts.withdraw_tokens_and_close(&ctx.bumps)
28+
instructions::handle_transfer_tokens(&mut ctx.accounts)?;
29+
instructions::take::handle_withdraw_tokens_and_close(&mut ctx.accounts, &ctx.bumps)
3030
}
3131

3232
#[instruction(discriminator = 2)]
3333
pub fn refund(ctx: Ctx<Refund>) -> Result<(), ProgramError> {
34-
ctx.accounts.withdraw_tokens_and_close(&ctx.bumps)
34+
instructions::refund::handle_withdraw_tokens_and_close(&mut ctx.accounts, &ctx.bumps)
3535
}
3636
}

0 commit comments

Comments
 (0)