solana-program-library/associated-token-account/program/src/lib.rs

90 lines
2.8 KiB
Rust
Raw Permalink Normal View History

2020-11-03 11:22:07 -08:00
//! Convention for associating token accounts with a user wallet
2020-11-01 21:15:11 -08:00
#![deny(missing_docs)]
#![forbid(unsafe_code)]
mod entrypoint;
pub mod processor;
2020-11-03 11:22:07 -08:00
// Export current SDK types for downstream users building with a different SDK version
2020-11-01 21:15:11 -08:00
pub use solana_program;
use solana_program::{
instruction::{AccountMeta, Instruction},
program_pack::Pack,
pubkey::Pubkey,
sysvar,
};
2020-11-05 12:58:25 -08:00
solana_program::declare_id!("ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL");
2020-11-01 21:15:11 -08:00
pub(crate) fn get_associated_token_address_and_bump_seed(
wallet_address: &Pubkey,
spl_token_mint_address: &Pubkey,
program_id: &Pubkey,
) -> (Pubkey, u8) {
get_associated_token_address_and_bump_seed_internal(
wallet_address,
spl_token_mint_address,
2020-11-01 21:15:11 -08:00
program_id,
&spl_token::id(),
2020-11-01 21:15:11 -08:00
)
}
2020-11-03 11:22:07 -08:00
/// Derives the associated token account address for the given wallet address and token mint
2020-11-01 21:15:11 -08:00
pub fn get_associated_token_address(
wallet_address: &Pubkey,
spl_token_mint_address: &Pubkey,
) -> Pubkey {
get_associated_token_address_and_bump_seed(&wallet_address, &spl_token_mint_address, &id()).0
}
fn get_associated_token_address_and_bump_seed_internal(
wallet_address: &Pubkey,
spl_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(),
&spl_token_mint_address.to_bytes(),
],
program_id,
)
}
2020-11-03 11:22:07 -08:00
/// Create an associated token account for the given wallet address and token mint
2020-11-01 21:15:11 -08:00
///
/// Accounts expected by this instruction:
///
/// 0. `[writeable,signer]` Funding account (must be a system account)
2020-11-03 11:22:07 -08:00
/// 1. `[writeable]` Associated token account address to be created
2020-11-01 21:15:11 -08:00
/// 2. `[]` Wallet address for the new associated token account
2020-11-03 14:52:58 -08:00
/// 3. `[]` The token mint for the new associated token account
2020-11-01 21:15:11 -08:00
/// 4. `[]` System program
2020-12-05 18:15:09 -08:00
/// 5. `[]` SPL Token program
/// 6. `[]` Rent sysvar
2020-11-01 21:15:11 -08:00
///
pub fn create_associated_token_account(
funding_address: &Pubkey,
wallet_address: &Pubkey,
spl_token_mint_address: &Pubkey,
) -> Instruction {
let associated_account_address =
get_associated_token_address(wallet_address, spl_token_mint_address);
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(*spl_token_mint_address, false),
AccountMeta::new_readonly(solana_program::system_program::id(), false),
AccountMeta::new_readonly(spl_token::id(), false),
AccountMeta::new_readonly(sysvar::rent::id(), false),
],
data: vec![],
}
}