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

[token-cli] Add support for transfer-hook account resolution for transfers with the transfer-fee extension. #7171

Merged

Conversation

tonton-sol
Copy link
Contributor

@tonton-sol tonton-sol commented Aug 20, 2024

Per #7059

Adds a new offchain helper offchain::create_transfer_checked_with_fee_instruction_with_extra_metas.
Adds a new onchain helper onchain::invoke_transfer_checked_with_fee

Adds logic to transfer_with_fee in token/client/src/token.rs to add the extra-account-metas using the offchain helper if the token uses the transfer-hook extension.

@mergify mergify bot added the community Community contribution label Aug 20, 2024
@tonton-sol
Copy link
Contributor Author

What tests if any needed to be added?

@tonton-sol tonton-sol changed the title Add support for transfer-hook account resolution for transfers the transfer-fee extension. [token-cli] Add support for transfer-hook account resolution for transfers the transfer-fee extension. Aug 20, 2024
Copy link
Contributor

@buffalojoec buffalojoec left a comment

Choose a reason for hiding this comment

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

Nice!! The changes look great to me.

For the tests, you'll want to test the helpers by adding a few cases to the Token-2022 tests under token/program-2022-test in the transfer_hook test module.

The idea is to basically make copies of success_transfer and success_transfers_using_onchain_helper, but add the transfer fee config to the mint. The former is going to test with the transfer hook example program, which should still work fine for this case, since it's really just going to test that the proper extra metas are loaded and passed to the example program in token/transfer-hook/example. The latter is using a mocked-up processor in the test module (see this line here).

Then maybe add a block in the Token CLI's transfer_hook tests to ensure we can parse a transfer instruction with a fee and all the extra transfer hook accounts as arguments.

If you have any questions, let me know, and thanks!

token/program-2022/src/onchain.rs Outdated Show resolved Hide resolved
@tonton-sol
Copy link
Contributor Author

@buffalojoec I am working in token/program-2022-test/transfer_hook.rs and I noticed that rust-analyser/cargo is not working in this directory. Is this a known thing?

@tonton-sol
Copy link
Contributor Author

And actually, does it make sense to add the combination test here in token/program-2022-test? Shouldn't there then be tests for every arbitrary combination of extensions?

@tonton-sol tonton-sol changed the title [token-cli] Add support for transfer-hook account resolution for transfers the transfer-fee extension. [token-cli] Add support for transfer-hook account resolution for transfers with the transfer-fee extension. Aug 21, 2024
@buffalojoec
Copy link
Contributor

buffalojoec commented Aug 21, 2024

@buffalojoec I am working in token/program-2022-test/transfer_hook.rs and I noticed that rust-analyser/cargo is not working in this directory. Is this a known thing?

Yeah, you need to add test-sbf to Rust Analyzer's default features. Even after doing that, I don't think it ever really worked for me. Sometimes I just comment out the #![cfg(feature = "test-sbf")] at the top, handle all of my compile errors, then add it back.

And actually, does it make sense to add the combination test here in token/program-2022-test? Shouldn't there then be tests for every arbitrary combination of extensions?

I didn't want to dump that whole workload on you. 😅

@tonton-sol
Copy link
Contributor Author

Looking at the rest of the tests, I actually think its okay to have these mixed extension tests in token/program-2022-test/transfer_hook.rs because of the special treatment needed for the extra metas. So I will go ahead and add tests for the w/ fee conditions.

Also there are no tests here for spl_token_2022::offchain, only spl_transfer_hook_interface. Is this by design? Would it even make sense to test the higher level helper functions here?

@tonton-sol
Copy link
Contributor Author

tonton-sol commented Aug 22, 2024

Then maybe add a block in the Token CLI's transfer_hook tests to ensure we can parse a transfer instruction with a fee and all the extra transfer hook accounts as arguments.

I am having trouble trying to add the arguments for the expected fee to the transfer test. I have confirmed that I can successfully create the token with both the transfer-hook and transfer-fee extensions in the test, so this is not a problem. Could this be caused by --sign-only and doing process_command() in offline mode?

I assume it's a catch-22 because we need the associated token account to be created meaning it cannot be done in offline mode. If it is online mode, the transfer-hook program would need to be deployed and initialized. Is this correct?

@buffalojoec
Copy link
Contributor

buffalojoec commented Aug 22, 2024

Then maybe add a block in the Token CLI's transfer_hook tests to ensure we can parse a transfer instruction with a fee and all the extra transfer hook accounts as arguments.

I am having trouble trying to add the arguments for the expected fee to the transfer test. I have confirmed that I can successfully create the token with both the transfer-hook and transfer-fee extensions in the test, so this is not a problem. Could this be caused by --sign-only and doing process_command() in offline mode?

I assume it's a catch-22 because we need the associated token account to be created meaning it cannot be done in offline mode. If it is online mode, the transfer-hook program would need to be deployed and initialized. Is this correct?

Yeah that's correct. The test I linked is sending the command to create the token, then disable the transfer hook (set program to None), but the middle bit where made the permalink is doing --sign-only so that it doesn't actually invoke the client but it's testing the parsing of the arguments and validation against the mint's extensions.

Maybe duplicate that test and set the token up with a fee config, similar to this test here, and then add the fee argument to the parsing test block? That way you can also set up the token account required.

@tonton-sol
Copy link
Contributor Author

Okay I figured it out! In sign-only + offline mode you need to explicitly pass the --program-id or else it will default to Tokenkeg. This is a problem for the transfer-fee extension but not the transfer-hook extension because transfer_checked() checks if it is either Tokenkeg or Token2022 while transfer_checked_with_fee() checks if it is Token2022. I guess this is just a weird quirk of testing this way.

@tonton-sol
Copy link
Contributor Author

I think this is good to go!

Copy link
Contributor

@buffalojoec buffalojoec left a comment

Choose a reason for hiding this comment

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

Nice! This is super close, just a nit and comment about the one test, then we can ship.

token/program-2022-test/tests/transfer_hook.rs Outdated Show resolved Hide resolved
token/program-2022-test/tests/transfer_hook.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@buffalojoec buffalojoec left a comment

Choose a reason for hiding this comment

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

Nice work!!

@github-actions github-actions bot added the stale [bot only] Added to stale content; will be closed soon label Sep 10, 2024
@tonton-sol
Copy link
Contributor Author

Will this auto close and not be added? 😅

@buffalojoec
Copy link
Contributor

Will this auto close and not be added? 😅

Whoops, sorry! I thought you could merge after I approved.

@buffalojoec buffalojoec merged commit 9842992 into solana-labs:master Sep 12, 2024
35 checks passed
@tonton-sol
Copy link
Contributor Author

I'd need to be employed to do that right? 😉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
community Community contribution stale [bot only] Added to stale content; will be closed soon
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants