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

ATA: Extract associated-token-account-client crate from associated-token-account crate #7005

Merged
merged 13 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ split-debuginfo = "unpacked"

[workspace]
members = [
"associated-token-account/client",
"associated-token-account/program",
"associated-token-account/program-test",
"binary-option/program",
Expand Down
14 changes: 14 additions & 0 deletions associated-token-account/client/Cargo.toml
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 <maintainers@solanalabs.com>"]
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"]
70 changes: 70 additions & 0 deletions associated-token-account/client/src/address.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//! 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.
kevinheavey marked this conversation as resolved.
Show resolved Hide resolved
#[doc(hidden)]
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,
)
}
107 changes: 107 additions & 0 deletions associated-token-account/client/src/instruction.rs
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
}
}
8 changes: 8 additions & 0 deletions associated-token-account/client/src/lib.rs
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");
1 change: 1 addition & 0 deletions associated-token-account/program-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ solana-program = "2.0.3"
solana-program-test = "2.0.3"
solana-sdk = "2.0.3"
spl-associated-token-account = { version = "4.0.0", path = "../program", features = ["no-entrypoint"] }
spl-associated-token-account-client = { version = "1.0.0", path = "../client" }
spl-token = { version = "6.0", path = "../../token/program", features = ["no-entrypoint"] }
spl-token-2022 = { version = "4.0.0", path = "../../token/program-2022", features = ["no-entrypoint"] }
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ use {
},
spl_associated_token_account::{
error::AssociatedTokenAccountError,
get_associated_token_address_with_program_id,
instruction::{
create_associated_token_account, create_associated_token_account_idempotent,
},
},
spl_associated_token_account_client::address::get_associated_token_address_with_program_id,
spl_token_2022::{
extension::ExtensionType,
instruction::initialize_account,
Expand Down
5 changes: 2 additions & 3 deletions associated-token-account/program-test/tests/extended_mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ use {
signer::keypair::Keypair,
transaction::{Transaction, TransactionError},
},
spl_associated_token_account::{
get_associated_token_address_with_program_id, instruction::create_associated_token_account,
},
spl_associated_token_account::instruction::create_associated_token_account,
spl_associated_token_account_client::address::get_associated_token_address_with_program_id,
spl_token_2022::{
error::TokenError,
extension::{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ use {
signature::Signer,
transaction::{Transaction, TransactionError},
},
spl_associated_token_account::{
get_associated_token_address_with_program_id, instruction::create_associated_token_account,
},
spl_associated_token_account::instruction::create_associated_token_account,
spl_associated_token_account_client::address::get_associated_token_address_with_program_id,
spl_token_2022::{extension::ExtensionType, state::Account},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ use {
signer::keypair::Keypair,
transaction::{Transaction, TransactionError},
},
spl_associated_token_account::{get_associated_token_address_with_program_id, instruction},
spl_associated_token_account::instruction,
spl_associated_token_account_client::address::get_associated_token_address_with_program_id,
spl_token_2022::{
extension::{ExtensionType, StateWithExtensionsOwned},
state::{Account, Mint},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ use {
solana_program::pubkey::Pubkey,
solana_program_test::*,
solana_sdk::{program_pack::Pack, signature::Signer, transaction::Transaction},
spl_associated_token_account::{
get_associated_token_address, instruction::create_associated_token_account,
},
spl_associated_token_account::instruction::create_associated_token_account,
spl_associated_token_account_client::address::get_associated_token_address,
spl_token::state::Account,
};

Expand Down
2 changes: 1 addition & 1 deletion associated-token-account/program/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ no-entrypoint = []
test-sbf = []

[dependencies]
assert_matches = "1.5.0"
borsh = "1.5.1"
num-derive = "0.4"
num-traits = "0.2"
solana-program = "2.0.3"
spl-associated-token-account-client = { version = "1.0.0", path = "../client" }
buffalojoec marked this conversation as resolved.
Show resolved Hide resolved
spl-token = { version = "6.0", path = "../../token/program", features = [
"no-entrypoint",
] }
Expand Down
Loading
Loading