Skip to content

Commit 52c5809

Browse files
committed
solana: add counter to dummy transfer hook
to be able to test if it was executed
1 parent 424cf70 commit 52c5809

File tree

4 files changed

+136
-18
lines changed

4 files changed

+136
-18
lines changed

solana/idl/json/dummy_transfer_hook.json

+24
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@
3030
"isMut": false,
3131
"isSigner": false
3232
},
33+
{
34+
"name": "counter",
35+
"isMut": true,
36+
"isSigner": false
37+
},
3338
{
3439
"name": "systemProgram",
3540
"isMut": false,
@@ -73,6 +78,11 @@
7378
"docs": [
7479
"computes and the on-chain code correctly passes on the PDA."
7580
]
81+
},
82+
{
83+
"name": "counter",
84+
"isMut": true,
85+
"isSigner": false
7686
}
7787
],
7888
"args": [
@@ -82,5 +92,19 @@
8292
}
8393
]
8494
}
95+
],
96+
"accounts": [
97+
{
98+
"name": "Counter",
99+
"type": {
100+
"kind": "struct",
101+
"fields": [
102+
{
103+
"name": "count",
104+
"type": "u64"
105+
}
106+
]
107+
}
108+
}
85109
]
86110
}

solana/idl/ts/dummy_transfer_hook.ts

+48
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ export type DummyTransferHook = {
3030
"isMut": false,
3131
"isSigner": false
3232
},
33+
{
34+
"name": "counter",
35+
"isMut": true,
36+
"isSigner": false
37+
},
3338
{
3439
"name": "systemProgram",
3540
"isMut": false,
@@ -73,6 +78,11 @@ export type DummyTransferHook = {
7378
"docs": [
7479
"computes and the on-chain code correctly passes on the PDA."
7580
]
81+
},
82+
{
83+
"name": "counter",
84+
"isMut": true,
85+
"isSigner": false
7686
}
7787
],
7888
"args": [
@@ -82,6 +92,20 @@ export type DummyTransferHook = {
8292
}
8393
]
8494
}
95+
],
96+
"accounts": [
97+
{
98+
"name": "counter",
99+
"type": {
100+
"kind": "struct",
101+
"fields": [
102+
{
103+
"name": "count",
104+
"type": "u64"
105+
}
106+
]
107+
}
108+
}
85109
]
86110
};
87111

@@ -117,6 +141,11 @@ export const IDL: DummyTransferHook = {
117141
"isMut": false,
118142
"isSigner": false
119143
},
144+
{
145+
"name": "counter",
146+
"isMut": true,
147+
"isSigner": false
148+
},
120149
{
121150
"name": "systemProgram",
122151
"isMut": false,
@@ -160,6 +189,11 @@ export const IDL: DummyTransferHook = {
160189
"docs": [
161190
"computes and the on-chain code correctly passes on the PDA."
162191
]
192+
},
193+
{
194+
"name": "counter",
195+
"isMut": true,
196+
"isSigner": false
163197
}
164198
],
165199
"args": [
@@ -169,5 +203,19 @@ export const IDL: DummyTransferHook = {
169203
}
170204
]
171205
}
206+
],
207+
"accounts": [
208+
{
209+
"name": "counter",
210+
"type": {
211+
"kind": "struct",
212+
"fields": [
213+
{
214+
"name": "count",
215+
"type": "u64"
216+
}
217+
]
218+
}
219+
}
172220
]
173221
};

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)