Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit 3571d08

Browse files
committed
extract associated-token-address crate
1 parent 5f651ad commit 3571d08

File tree

4 files changed

+91
-0
lines changed

4 files changed

+91
-0
lines changed

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ split-debuginfo = "unpacked"
55
members = [
66
"associated-token-account/program",
77
"associated-token-account/program-test",
8+
"associated-token-address",
89
"binary-option/program",
910
"binary-oracle-pair/program",
1011
"examples/rust/cross-program-invocation",

associated-token-address/Cargo.toml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[package]
2+
name = "spl-associated-token-address"
3+
version = "1.0.0"
4+
description = "Solana Program Library Associated Token Address"
5+
authors = ["Solana Labs Maintainers <[email protected]>"]
6+
repository = "https://github.com/solana-labs/solana-program-library"
7+
license = "Apache-2.0"
8+
edition = "2021"
9+
10+
[dependencies]
11+
solana-program = "2.0.0"
12+
13+
[package.metadata.docs.rs]
14+
targets = ["x86_64-unknown-linux-gnu"]

associated-token-address/src/lib.rs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
//! Convention for associating token accounts with a user wallet
2+
#![deny(missing_docs)]
3+
#![forbid(unsafe_code)]
4+
5+
use solana_program::pubkey::Pubkey;
6+
7+
/// Derives the associated token account address and bump seed
8+
/// for the given wallet address, token mint and token program id
9+
pub fn get_associated_token_address_and_bump_seed(
10+
wallet_address: &Pubkey,
11+
token_mint_address: &Pubkey,
12+
program_id: &Pubkey,
13+
token_program_id: &Pubkey,
14+
) -> (Pubkey, u8) {
15+
get_associated_token_address_and_bump_seed_internal(
16+
wallet_address,
17+
token_mint_address,
18+
program_id,
19+
token_program_id,
20+
)
21+
}
22+
23+
const TOKEN_PROGRAM: Pubkey = solana_program::pubkey!("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA");
24+
const ASSOCIATED_TOKEN_PROGRAM: Pubkey = solana_program::pubkey!("ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL");
25+
26+
/// Derives the associated token account address for the given wallet address
27+
/// and token mint
28+
pub fn get_associated_token_address(
29+
wallet_address: &Pubkey,
30+
token_mint_address: &Pubkey,
31+
) -> Pubkey {
32+
get_associated_token_address_with_program_id(
33+
wallet_address,
34+
token_mint_address,
35+
&TOKEN_PROGRAM,
36+
)
37+
}
38+
39+
/// Derives the associated token account address for the given wallet address,
40+
/// token mint and token program id
41+
pub fn get_associated_token_address_with_program_id(
42+
wallet_address: &Pubkey,
43+
token_mint_address: &Pubkey,
44+
token_program_id: &Pubkey,
45+
) -> Pubkey {
46+
get_associated_token_address_and_bump_seed(
47+
wallet_address,
48+
token_mint_address,
49+
&ASSOCIATED_TOKEN_PROGRAM,
50+
token_program_id,
51+
)
52+
.0
53+
}
54+
55+
fn get_associated_token_address_and_bump_seed_internal(
56+
wallet_address: &Pubkey,
57+
token_mint_address: &Pubkey,
58+
program_id: &Pubkey,
59+
token_program_id: &Pubkey,
60+
) -> (Pubkey, u8) {
61+
Pubkey::find_program_address(
62+
&[
63+
&wallet_address.to_bytes(),
64+
&token_program_id.to_bytes(),
65+
&token_mint_address.to_bytes(),
66+
],
67+
program_id,
68+
)
69+
}

0 commit comments

Comments
 (0)