-
Notifications
You must be signed in to change notification settings - Fork 87
refactor: light token client #2197
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
Open
ananas-block
wants to merge
4
commits into
main
Choose a base branch
from
jorrit/refactor-light-token-client
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+3,435
−641
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
da718b8
refactor: light token client to a clean minimal impl, move existing a…
ananas-block 5a40574
fix: wrap unwrap for any spl program, add check in approve
ananas-block e73e501
fix: remove light-test-utils from forester, fix actions
ananas-block d07db07
chore: remove new ata named functions
ananas-block File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
forester-utils/src/instructions/compress_and_close_mint.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| use borsh::BorshDeserialize; | ||
| use light_client::{ | ||
| indexer::Indexer, | ||
| rpc::{Rpc, RpcError}, | ||
| }; | ||
| use light_compressed_account::instruction_data::traits::LightInstructionData; | ||
| use light_compressed_token_sdk::compressed_token::{ | ||
| create_compressed_mint::find_mint_address, mint_action::MintActionMetaConfig, | ||
| }; | ||
| use light_compressible::config::CompressibleConfig; | ||
| use light_token_interface::{ | ||
| instructions::mint_action::{ | ||
| CompressAndCloseMintAction, MintActionCompressedInstructionData, MintWithContext, | ||
| }, | ||
| state::Mint, | ||
| LIGHT_TOKEN_PROGRAM_ID, | ||
| }; | ||
| use solana_instruction::Instruction; | ||
| use solana_pubkey::Pubkey; | ||
|
|
||
| /// Creates a CompressAndCloseMint instruction by fetching required data from RPC/indexer. | ||
| /// | ||
| /// This is permissionless - anyone can call when the mint is compressible (rent expired). | ||
| /// | ||
| /// # Parameters | ||
| /// - `rpc`: RPC client that also implements Indexer | ||
| /// - `payer`: Account paying for transaction fees | ||
| /// - `compressed_mint_address`: The 32-byte compressed address of the mint | ||
| /// - `mint_seed`: The seed pubkey used to derive the mint PDA | ||
| /// - `idempotent`: If true, succeed silently when Mint doesn't exist | ||
| pub async fn create_compress_and_close_mint_instruction<R: Rpc + Indexer>( | ||
| rpc: &mut R, | ||
| payer: Pubkey, | ||
| compressed_mint_address: [u8; 32], | ||
| mint_seed: Pubkey, | ||
| idempotent: bool, | ||
| ) -> Result<Instruction, RpcError> { | ||
| // Derive the mint PDA from mint_seed | ||
| let (mint_pda, _bump) = find_mint_address(&mint_seed); | ||
|
|
||
| // Get the compressed mint account | ||
| let compressed_mint_account = rpc | ||
| .get_compressed_account(compressed_mint_address, None) | ||
| .await? | ||
| .value | ||
| .ok_or_else(|| RpcError::AccountDoesNotExist(format!("{:?}", compressed_mint_address)))?; | ||
|
|
||
| // Try to deserialize the compressed mint - may be None if Mint is already decompressed | ||
| let compressed_mint: Option<Mint> = compressed_mint_account | ||
| .data | ||
| .as_ref() | ||
| .and_then(|d| BorshDeserialize::deserialize(&mut d.data.as_slice()).ok()); | ||
|
|
||
| // Get validity proof for the compressed mint | ||
| let rpc_proof_result = rpc | ||
| .get_validity_proof(vec![compressed_mint_account.hash], vec![], None) | ||
| .await? | ||
| .value; | ||
|
|
||
| // Build MintWithContext | ||
| let compressed_mint_inputs = MintWithContext { | ||
| prove_by_index: rpc_proof_result.accounts[0].root_index.proof_by_index(), | ||
| leaf_index: compressed_mint_account.leaf_index, | ||
| root_index: rpc_proof_result.accounts[0] | ||
| .root_index | ||
| .root_index() | ||
| .unwrap_or_default(), | ||
| address: compressed_mint_address, | ||
| mint: compressed_mint.map(|m| m.try_into().unwrap()), | ||
| }; | ||
|
|
||
| // Build instruction data with CompressAndCloseMint action | ||
| let instruction_data = MintActionCompressedInstructionData::new( | ||
| compressed_mint_inputs, | ||
| rpc_proof_result.proof.into(), | ||
| ) | ||
| .with_compress_and_close_mint(CompressAndCloseMintAction { | ||
| idempotent: if idempotent { 1 } else { 0 }, | ||
| }); | ||
|
|
||
| // Get CompressibleConfig for rent_sponsor | ||
| let config_address = CompressibleConfig::light_token_v1_config_pda(); | ||
| let compressible_config: CompressibleConfig = rpc | ||
| .get_anchor_account(&config_address) | ||
| .await? | ||
| .ok_or_else(|| { | ||
| RpcError::CustomError(format!( | ||
| "CompressibleConfig not found at {}", | ||
| config_address | ||
| )) | ||
| })?; | ||
|
|
||
| // Build account metas configuration | ||
| let state_tree_info = rpc_proof_result.accounts[0].tree_info; | ||
| let config = MintActionMetaConfig::new( | ||
| payer, | ||
| payer, // authority - permissionless, using payer | ||
| state_tree_info.tree, | ||
| state_tree_info.queue, | ||
| state_tree_info.queue, | ||
| ) | ||
| .with_compressible_mint(mint_pda, config_address, compressible_config.rent_sponsor); | ||
|
|
||
| // Get account metas | ||
| let account_metas = config.to_account_metas(); | ||
|
|
||
| // Serialize instruction data | ||
| let data = instruction_data | ||
| .data() | ||
| .map_err(|e| RpcError::CustomError(format!("Failed to serialize instruction: {:?}", e)))?; | ||
|
|
||
| // Build final instruction | ||
| Ok(Instruction { | ||
| program_id: LIGHT_TOKEN_PROGRAM_ID.into(), | ||
| accounts: account_metas, | ||
| data, | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,7 +21,7 @@ use light_registry::{ | |
| utils::{get_forester_pda, get_protocol_config_pda_address}, | ||
| ForesterConfig, | ||
| }; | ||
| use light_token_client::actions::{ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. renaming to latest |
||
| use light_test_utils::actions::legacy::{ | ||
| create_compressible_token_account, CreateCompressibleTokenAccountInputs, | ||
| }; | ||
| use light_token_interface::state::TokenDataVersion; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧹 Nitpick | 🔵 Trivial
Add mint context to instruction-build errors.
Including seed/compressed address in both error paths will make triage much faster when a build fails.
♻️ Proposed tweak
Also applies to: 218-230
🤖 Prompt for AI Agents