Skip to content

Commit 4b55b84

Browse files
committed
solana: add counter to dummy transfer hook
to be able to test if it was executed
1 parent 29fb437 commit 4b55b84

File tree

2 files changed

+64
-18
lines changed

2 files changed

+64
-18
lines changed

solana/programs/dummy-transfer-hook/src/lib.rs

+50-18
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub const DESTINATION_TOKEN_ACCOUNT_INDEX: u8 = 2;
1717
pub const AUTHORITY_ACCOUNT_INDEX: u8 = 3;
1818

1919
/// Number of extra accounts in the ExtraAccountMetaList account
20-
pub const EXTRA_ACCOUNTS_LEN: u8 = 1;
20+
pub const EXTRA_ACCOUNTS_LEN: u8 = 2;
2121

2222
#[program]
2323
pub mod dummy_transfer_hook {
@@ -31,21 +31,30 @@ pub mod dummy_transfer_hook {
3131
pub fn initialize_extra_account_meta_list(
3232
ctx: Context<InitializeExtraAccountMetaList>,
3333
) -> Result<()> {
34-
let account_metas = vec![ExtraAccountMeta::new_with_seeds(
35-
&[
36-
Seed::Literal {
37-
bytes: "dummy_account".as_bytes().to_vec(),
38-
},
39-
// owner field of the sender token account
40-
Seed::AccountData {
41-
account_index: SENDER_TOKEN_ACCOUNT_INDEX,
42-
data_index: 32,
43-
length: 32,
44-
},
45-
],
46-
false, // is_signer
47-
false, // is_writable
48-
)?];
34+
let account_metas = vec![
35+
ExtraAccountMeta::new_with_seeds(
36+
&[
37+
Seed::Literal {
38+
bytes: "dummy_account".as_bytes().to_vec(),
39+
},
40+
// owner field of the sender token account
41+
Seed::AccountData {
42+
account_index: SENDER_TOKEN_ACCOUNT_INDEX,
43+
data_index: 32,
44+
length: 32,
45+
},
46+
],
47+
false, // is_signer
48+
false, // is_writable
49+
)?,
50+
ExtraAccountMeta::new_with_seeds(
51+
&[Seed::Literal {
52+
bytes: "counter".as_bytes().to_vec(),
53+
}],
54+
false, // is_signer
55+
true, // is_writable
56+
)?,
57+
];
4958

5059
assert_eq!(EXTRA_ACCOUNTS_LEN as usize, account_metas.len());
5160

@@ -58,8 +67,8 @@ pub mod dummy_transfer_hook {
5867
Ok(())
5968
}
6069

61-
pub fn transfer_hook(_ctx: Context<TransferHook>, _amount: u64) -> Result<()> {
62-
// NOTE: for now, the account constraints implement all the restrictions.
70+
pub fn transfer_hook(ctx: Context<TransferHook>, _amount: u64) -> Result<()> {
71+
ctx.accounts.counter.count += 1;
6372
Ok(())
6473
}
6574

@@ -87,6 +96,12 @@ pub mod dummy_transfer_hook {
8796
}
8897
}
8998

99+
#[account]
100+
#[derive(InitSpace)]
101+
pub struct Counter {
102+
pub count: u64,
103+
}
104+
90105
#[derive(Accounts)]
91106
pub struct InitializeExtraAccountMetaList<'info> {
92107
#[account(mut)]
@@ -104,6 +119,16 @@ pub struct InitializeExtraAccountMetaList<'info> {
104119
pub mint: InterfaceAccount<'info, Mint>,
105120
pub token_program: Interface<'info, TokenInterface>,
106121
pub associated_token_program: Program<'info, AssociatedToken>,
122+
123+
#[account(
124+
init,
125+
payer = payer,
126+
space = 8 + Counter::INIT_SPACE,
127+
seeds = [b"counter"],
128+
bump
129+
)]
130+
pub counter: Account<'info, Counter>,
131+
107132
pub system_program: Program<'info, System>,
108133
}
109134

@@ -136,4 +161,11 @@ pub struct TransferHook<'info> {
136161
/// CHECK: dummy account. It just tests that the off-chain code correctly
137162
/// computes and the on-chain code correctly passes on the PDA.
138163
pub dummy_account: AccountInfo<'info>,
164+
165+
#[account(
166+
mut,
167+
seeds = [b"counter"],
168+
bump
169+
)]
170+
pub counter: Account<'info, Counter>,
139171
}

solana/tests/example-native-token-transfer.ts

+14
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,16 @@ describe("example-native-token-transfers", () => {
6161
dummyTransferHook.programId
6262
);
6363

64+
const [counterPDA] = PublicKey.findProgramAddressSync(
65+
[Buffer.from("counter")],
66+
dummyTransferHook.programId
67+
);
68+
69+
async function counterValue(): Promise<anchor.BN> {
70+
const counter = await dummyTransferHook.account.counter.fetch(counterPDA);
71+
return counter.count
72+
}
73+
6474
it("Initialize mint", async () => {
6575
const extensions = [spl.ExtensionType.TransferHook];
6676
const mintLen = spl.getMintLen(extensions);
@@ -128,6 +138,7 @@ describe("example-native-token-transfers", () => {
128138
.accountsStrict({
129139
payer: payer.publicKey,
130140
mint: mint.publicKey,
141+
counter: counterPDA,
131142
extraAccountMetaList: extraAccountMetaListPDA,
132143
tokenProgram: spl.TOKEN_2022_PROGRAM_ID,
133144
associatedTokenProgram: spl.ASSOCIATED_TOKEN_PROGRAM_ID,
@@ -243,6 +254,7 @@ describe("example-native-token-transfers", () => {
243254

244255
// TODO: assert other stuff in the message
245256
// console.log(nttManagerMessage);
257+
expect((await counterValue()).toString()).to.be.eq("1")
246258
});
247259

248260
it("Can receive tokens", async () => {
@@ -300,6 +312,8 @@ describe("example-native-token-transfers", () => {
300312
});
301313

302314
expect(released).to.equal(true);
315+
316+
expect((await counterValue()).toString()).to.be.eq("2")
303317
});
304318
});
305319

0 commit comments

Comments
 (0)