Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

solana: support tokens with transfer hooks #404

Merged
merged 8 commits into from
Jun 12, 2024
Merged

solana: support tokens with transfer hooks #404

merged 8 commits into from
Jun 12, 2024

Conversation

kcsongor
Copy link
Contributor

@kcsongor kcsongor commented Apr 12, 2024

This PR adds support for token2022 tokens with transfer hooks. For more details on transfer hooks, see https://solana.com/developers/guides/token-extensions/transfer-hook.

At a high level, a transfer hook is a program that is called (by the token program) each time a transfer (of a token of a specific mint) is made. The key difficulty is that the transfer hook program may require additional accounts, which need to be propagated through the instruction.

This PR modifies the on-chain code to call the appropriate helper function that propagates the necessary accounts, and also the off-chain code (ts sdk) that derives these account addresses based on the on-chain schema standard that token2022 introduces for transfer hooks. The rust tests (cargo test-sbf) still test against the legacy spl token, while in this PR I modified the typescript tests to test against a token with a transfer hook. This way, both paths are tested appropriately.

There is a dummy transfer hook in the test that bumps a counter on each transfer, and has a dummy account seeded by the owner field of the sender token account, which is just used to test that the account information is properly propagated.

Adding this was the motivation for #391, because the on-chain helper code is simply completely unusable (broken) in earlier sdk versions.

ABI changes

Breaking changes

This change is breaking, so it warrants a major ABI version upgrade (bumped the version to 2.0.0). This is because we now have to perform an extra transfer when burning and minting tokens in order to trigger any transfer hooks. This extra transfer in turn requires some additional accounts -- refer to the documentation in the transfer_burn and release_inbound_mint functions for more details.

New features

Also added an extra set of instructions to programmatically create/manage address lookup tables. These were necessary to add extra transaction size headroom for transfer hooks to be triggered. Without a lookup table, a transfer hook could use up to 2 custom accounts, but now there is space for 15+. See the documentation in lut.rs for more details.

Copy link
Collaborator

@johnsaigle johnsaigle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No opposition to the changes so far. Looks like this needs a bit of work to get past CI

@kcsongor
Copy link
Contributor Author

Hm, interesting. anchor test passes for me locally, I can't reproduce the CI failure here (which seems to be deterministic) :(

@kcsongor kcsongor force-pushed the solana/token22 branch 2 times, most recently from 0028237 to 943e5bd Compare April 16, 2024 08:43
@johnsaigle
Copy link
Collaborator

@kcsongor I'm getting the same issue as CI when I run make anchor-test locally.

Maybe check your local CLI tool versions? Here are mine:

avm 0.29.0
solana-cli 1.18.10 (src:50b3f337; feat:3469865029, client:SolanaLabs)

@kcsongor
Copy link
Contributor Author

avm 0.29.0
solana-cli 1.18.10 (src:50b3f337; feat:3469865029, client:SolanaLabs)
anchor-cli 0.30.0

for me

@johnsaigle
Copy link
Collaborator

I have anchor-cli 0.29.0. Maybe you could try downgrading?

@kcsongor
Copy link
Contributor Author

kcsongor commented Apr 17, 2024

Downgraded to 0.29.0, it still works. But I managed to reproduce this on my x86 ubuntu machine (it's working on arm64/macOS). What arch/OS are you running on?

@johnsaigle
Copy link
Collaborator

johnsaigle commented Apr 17, 2024

I'm using Darwin arm64. Strange that we're getting different results.

@johnsaigle
Copy link
Collaborator

I haven't done an in-depth review yet but I've assigned myself and will return to it later today or this week.

Quick note: Are the changes to the Solidity files meant to be here or are they the result of the rebase? I'm unclear how they're related to the changes for the Solana code.

@kcsongor
Copy link
Contributor Author

Ah that's right -- the base branch of this PR is #399. I rebased this one, but not that one yet, so github shows a larger diff. Will fix that by rebasing #399 also.

@kcsongor kcsongor force-pushed the solana/ata-specify-token-program branch from 10c2d3e to b312d0d Compare April 17, 2024 18:00
@kcsongor kcsongor force-pushed the solana/token22 branch 2 times, most recently from 604e25a to 29fb437 Compare April 17, 2024 18:01
@kcsongor
Copy link
Contributor Author

Done, the diff shows up correctly now -- thanks for pointing it out!

@nik-suri nik-suri linked an issue Apr 17, 2024 that may be closed by this pull request
Copy link
Collaborator

@johnsaigle johnsaigle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few questions about the API. Will approve after we discuss.

johnsaigle
johnsaigle previously approved these changes Apr 18, 2024
@kcsongor
Copy link
Contributor Author

kcsongor commented Apr 19, 2024

Added a new commit which makes some non-trivial changes. tldr we now perform a transfer before burning on the way out, and a transfer after minting on the way in. This is done in order to trigger the transfer hooks of the underlying token, which minting and burning don't trigger.

CI fails because now there are too many accounts in the transfer transaction (since it does an approve, then a transfer, then a release outbound via wormhole. The transfer hook adds 3 extra accounts, which just about tips it over the limit. I'll have to add a lookup table to the sdk).

@kcsongor kcsongor force-pushed the solana/token22 branch 2 times, most recently from cfeeccb to a3ccab3 Compare April 19, 2024 17:05
@kcsongor kcsongor force-pushed the solana/ata-specify-token-program branch from b312d0d to 63c3743 Compare April 25, 2024 16:20
Copy link
Collaborator

@johnsaigle johnsaigle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some notes, mostly to confirm that removing some of the checks should be safe. I like the Anchor constraint changes for the Mode checks and the comment additions quite a lot.

johnsaigle
johnsaigle previously approved these changes Jun 6, 2024
Copy link
Collaborator

@johnsaigle johnsaigle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good on my side from a security perspective. Csongor and I discussed some ideas on Slack and I'm comfortable with the state of the PR.

@johnsaigle johnsaigle removed their assignment Jun 7, 2024
Copy link
Collaborator

@nonergodic nonergodic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no additional changes since my last review from what I can tell

the only thing that stuck out to me when taking a second glance is that the ntt quoter version also got bumped to version 2.0 even though for the ntt quoter itself nothing actually changed, which might be slightly confusing ("What's the difference between ntt-quoter 1.x and 2.0?" "No difference.") though ofc not bumping it is maybe also bad ("Is this the right version of the quoter for that version of NTT?").

@kcsongor kcsongor merged commit bd1f7ef into main Jun 12, 2024
6 checks passed
@kcsongor kcsongor deleted the solana/token22 branch June 12, 2024 18:21
@tonyjin tonyjin removed the on hold label Jun 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
abi Changes Contract ABI solana Change to Solana programs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support token2022 standard on Solana
7 participants