I'm looking into using the transfer hook from token-2022 to run some logic whenever my custom spl-token is transferred. I came this Github link and while it helps, I have a lot of questions. Like for instance, does the program have to only have this one instruction for the transfer hook or can I add this instruction to a program I'm already writing and pass that program ID into the CLI when I'm creating the token-2022 and it'll just "know" which instruction to use? Ideally, I'd like to avoid deploying 2 programs if I can.
1 Answer
can I add this instruction to a program I'm already writing and pass that program ID into the CLI when I'm creating the token-2022 and it'll just "know" which instruction to use
This is pretty much how it works, yeah!
Basically, there's an interface for any "transfer hook program" and it has two instructions:
InitializeExtraAccountMetaList
Set up additional required accounts for a transferExecute
Do said transfer
Anytime you send a Transfer
instruction to Token2022 and the mint being transferred has a transfer hook program, Token2022 is going to CPI to the hook program's Execute
instruction.
Since it's an interface, we can do that since the instruction layout is always the same.
A hook program can have any number of other instructions, as long as it at least has TransferHookInstruction::Execute
.