This repository was archived by the owner on Mar 11, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
ATA: Extract associated-token-account-client crate from associated-token-account crate #7005
Merged
Merged
Changes from 10 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
1bc80ae
extract associated-token-address crate
kevinheavey 5bbba83
use spl_associated_token_address in spl crates
kevinheavey 24f0380
use spl-program-ids in associated-token-address crate
kevinheavey b9cfae0
update imports in repo
kevinheavey a464e47
use solana-inline-spl instead of spl-program-ids
kevinheavey 4266e4f
remove remaining references to spl-program-ids
kevinheavey f817cee
Rename associated-token-address -> associated-token-account-client
joncinque 53b5574
Use the new package everywhere
joncinque f6ac427
Remove solana-inline-spl dependency
joncinque 52fc28e
Remove dependency on ata program from token-client
joncinque e302226
Add #[doc(hidden)] attribute to internal function
joncinque 3d275ce
Remove ATA from token upgrade CLI
joncinque 50b7023
Add program module for program id
joncinque 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[package] | ||
name = "spl-associated-token-account-client" | ||
version = "1.0.0" | ||
description = "Solana Program Library Associated Token Account Client" | ||
authors = ["Solana Labs Maintainers <[email protected]>"] | ||
repository = "https://github.com/solana-labs/solana-program-library" | ||
license = "Apache-2.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
solana-program = "2.0.3" | ||
|
||
[package.metadata.docs.rs] | ||
targets = ["x86_64-unknown-linux-gnu"] |
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,69 @@ | ||
//! Address derivation functions | ||
|
||
use solana_program::pubkey::Pubkey; | ||
|
||
/// Derives the associated token account address and bump seed | ||
/// for the given wallet address, token mint and token program id | ||
pub fn get_associated_token_address_and_bump_seed( | ||
wallet_address: &Pubkey, | ||
token_mint_address: &Pubkey, | ||
program_id: &Pubkey, | ||
token_program_id: &Pubkey, | ||
) -> (Pubkey, u8) { | ||
get_associated_token_address_and_bump_seed_internal( | ||
wallet_address, | ||
token_mint_address, | ||
program_id, | ||
token_program_id, | ||
) | ||
} | ||
|
||
mod inline_spl_token { | ||
solana_program::declare_id!("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"); | ||
} | ||
|
||
/// Derives the associated token account address for the given wallet address | ||
/// and token mint | ||
pub fn get_associated_token_address( | ||
wallet_address: &Pubkey, | ||
token_mint_address: &Pubkey, | ||
) -> Pubkey { | ||
get_associated_token_address_with_program_id( | ||
wallet_address, | ||
token_mint_address, | ||
&inline_spl_token::ID, | ||
) | ||
} | ||
|
||
/// Derives the associated token account address for the given wallet address, | ||
/// token mint and token program id | ||
pub fn get_associated_token_address_with_program_id( | ||
wallet_address: &Pubkey, | ||
token_mint_address: &Pubkey, | ||
token_program_id: &Pubkey, | ||
) -> Pubkey { | ||
get_associated_token_address_and_bump_seed( | ||
wallet_address, | ||
token_mint_address, | ||
&crate::id(), | ||
token_program_id, | ||
) | ||
.0 | ||
} | ||
|
||
/// For internal use only. | ||
pub fn get_associated_token_address_and_bump_seed_internal( | ||
wallet_address: &Pubkey, | ||
token_mint_address: &Pubkey, | ||
program_id: &Pubkey, | ||
token_program_id: &Pubkey, | ||
) -> (Pubkey, u8) { | ||
Pubkey::find_program_address( | ||
&[ | ||
&wallet_address.to_bytes(), | ||
&token_program_id.to_bytes(), | ||
&token_mint_address.to_bytes(), | ||
], | ||
program_id, | ||
) | ||
} |
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,107 @@ | ||
//! Instruction creators for the program | ||
use { | ||
crate::{address::get_associated_token_address_with_program_id, id}, | ||
solana_program::{ | ||
instruction::{AccountMeta, Instruction}, | ||
pubkey::Pubkey, | ||
system_program, | ||
}, | ||
}; | ||
|
||
fn build_associated_token_account_instruction( | ||
funding_address: &Pubkey, | ||
wallet_address: &Pubkey, | ||
token_mint_address: &Pubkey, | ||
token_program_id: &Pubkey, | ||
instruction: u8, | ||
) -> Instruction { | ||
let associated_account_address = get_associated_token_address_with_program_id( | ||
wallet_address, | ||
token_mint_address, | ||
token_program_id, | ||
); | ||
// safety check, assert if not a creation instruction, which is only 0 or 1 | ||
assert!(instruction <= 1); | ||
Instruction { | ||
program_id: id(), | ||
accounts: vec![ | ||
AccountMeta::new(*funding_address, true), | ||
AccountMeta::new(associated_account_address, false), | ||
AccountMeta::new_readonly(*wallet_address, false), | ||
AccountMeta::new_readonly(*token_mint_address, false), | ||
AccountMeta::new_readonly(system_program::id(), false), | ||
AccountMeta::new_readonly(*token_program_id, false), | ||
], | ||
data: vec![instruction], | ||
} | ||
} | ||
|
||
/// Creates Create instruction | ||
pub fn create_associated_token_account( | ||
funding_address: &Pubkey, | ||
wallet_address: &Pubkey, | ||
token_mint_address: &Pubkey, | ||
token_program_id: &Pubkey, | ||
) -> Instruction { | ||
build_associated_token_account_instruction( | ||
funding_address, | ||
wallet_address, | ||
token_mint_address, | ||
token_program_id, | ||
0, // AssociatedTokenAccountInstruction::Create | ||
) | ||
} | ||
|
||
/// Creates CreateIdempotent instruction | ||
pub fn create_associated_token_account_idempotent( | ||
funding_address: &Pubkey, | ||
wallet_address: &Pubkey, | ||
token_mint_address: &Pubkey, | ||
token_program_id: &Pubkey, | ||
) -> Instruction { | ||
build_associated_token_account_instruction( | ||
funding_address, | ||
wallet_address, | ||
token_mint_address, | ||
token_program_id, | ||
1, // AssociatedTokenAccountInstruction::CreateIdempotent | ||
) | ||
} | ||
|
||
/// Creates a `RecoverNested` instruction | ||
pub fn recover_nested( | ||
wallet_address: &Pubkey, | ||
owner_token_mint_address: &Pubkey, | ||
nested_token_mint_address: &Pubkey, | ||
token_program_id: &Pubkey, | ||
) -> Instruction { | ||
let owner_associated_account_address = get_associated_token_address_with_program_id( | ||
wallet_address, | ||
owner_token_mint_address, | ||
token_program_id, | ||
); | ||
let destination_associated_account_address = get_associated_token_address_with_program_id( | ||
wallet_address, | ||
nested_token_mint_address, | ||
token_program_id, | ||
); | ||
let nested_associated_account_address = get_associated_token_address_with_program_id( | ||
&owner_associated_account_address, // ATA is wrongly used as a wallet_address | ||
nested_token_mint_address, | ||
token_program_id, | ||
); | ||
|
||
Instruction { | ||
program_id: id(), | ||
accounts: vec![ | ||
AccountMeta::new(nested_associated_account_address, false), | ||
AccountMeta::new_readonly(*nested_token_mint_address, false), | ||
AccountMeta::new(destination_associated_account_address, false), | ||
AccountMeta::new_readonly(owner_associated_account_address, false), | ||
AccountMeta::new_readonly(*owner_token_mint_address, false), | ||
AccountMeta::new(*wallet_address, true), | ||
AccountMeta::new_readonly(*token_program_id, false), | ||
], | ||
data: vec![2], // AssociatedTokenAccountInstruction::RecoverNested | ||
} | ||
} |
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,8 @@ | ||
//! Client crate for interacting with the spl-associated-token-account program | ||
#![deny(missing_docs)] | ||
#![forbid(unsafe_code)] | ||
|
||
pub mod address; | ||
pub mod instruction; | ||
|
||
solana_program::declare_id!("ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL"); |
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.