spl_token_wrap/
lib.rs

1//! Token Wrap program
2#![deny(missing_docs)]
3#![forbid(unsafe_code)]
4
5mod entrypoint;
6pub mod error;
7pub mod instruction;
8pub mod metadata;
9pub mod metaplex;
10pub mod mint_customizer;
11pub mod processor;
12pub mod state;
13
14use {
15    solana_pubkey::Pubkey,
16    spl_associated_token_account_client::address::get_associated_token_address_with_program_id,
17};
18
19solana_pubkey::declare_id!("TwRapQCDhWkZRrDaHfZGuHxkZ91gHDRkyuzNqeU5MgR");
20
21const WRAPPED_MINT_SEED: &[u8] = br"mint";
22
23pub(crate) fn get_wrapped_mint_address_with_seed(
24    unwrapped_mint: &Pubkey,
25    wrapped_token_program_id: &Pubkey,
26) -> (Pubkey, u8) {
27    Pubkey::find_program_address(
28        &get_wrapped_mint_seeds(unwrapped_mint, wrapped_token_program_id),
29        &id(),
30    )
31}
32
33pub(crate) fn get_wrapped_mint_seeds<'a>(
34    unwrapped_mint: &'a Pubkey,
35    wrapped_token_program_id: &'a Pubkey,
36) -> [&'a [u8]; 3] {
37    [
38        WRAPPED_MINT_SEED,
39        unwrapped_mint.as_ref(),
40        wrapped_token_program_id.as_ref(),
41    ]
42}
43
44pub(crate) fn get_wrapped_mint_signer_seeds<'a>(
45    unwrapped_mint: &'a Pubkey,
46    wrapped_token_program_id: &'a Pubkey,
47    bump_seed: &'a [u8],
48) -> [&'a [u8]; 4] {
49    [
50        WRAPPED_MINT_SEED,
51        unwrapped_mint.as_ref(),
52        wrapped_token_program_id.as_ref(),
53        bump_seed,
54    ]
55}
56
57/// Derive the SPL Token wrapped mint address associated with an unwrapped mint
58pub fn get_wrapped_mint_address(
59    unwrapped_mint: &Pubkey,
60    wrapped_token_program_id: &Pubkey,
61) -> Pubkey {
62    get_wrapped_mint_address_with_seed(unwrapped_mint, wrapped_token_program_id).0
63}
64
65const WRAPPED_MINT_AUTHORITY_SEED: &[u8] = br"authority";
66
67pub(crate) fn get_wrapped_mint_authority_seeds(wrapped_mint: &Pubkey) -> [&[u8]; 2] {
68    [WRAPPED_MINT_AUTHORITY_SEED, wrapped_mint.as_ref()]
69}
70
71pub(crate) fn get_wrapped_mint_authority_signer_seeds<'a>(
72    wrapped_mint: &'a Pubkey,
73    bump_seed: &'a [u8],
74) -> [&'a [u8]; 3] {
75    [
76        WRAPPED_MINT_AUTHORITY_SEED,
77        wrapped_mint.as_ref(),
78        bump_seed,
79    ]
80}
81
82pub(crate) fn get_wrapped_mint_authority_with_seed(wrapped_mint: &Pubkey) -> (Pubkey, u8) {
83    Pubkey::find_program_address(&get_wrapped_mint_authority_seeds(wrapped_mint), &id())
84}
85
86/// Derive the SPL Token wrapped mint authority address
87pub fn get_wrapped_mint_authority(wrapped_mint: &Pubkey) -> Pubkey {
88    get_wrapped_mint_authority_with_seed(wrapped_mint).0
89}
90
91const WRAPPED_MINT_BACKPOINTER_SEED: &[u8] = br"backpointer";
92
93pub(crate) fn get_wrapped_mint_backpointer_address_seeds(wrapped_mint: &Pubkey) -> [&[u8]; 2] {
94    [WRAPPED_MINT_BACKPOINTER_SEED, wrapped_mint.as_ref()]
95}
96
97pub(crate) fn get_wrapped_mint_backpointer_address_signer_seeds<'a>(
98    wrapped_mint: &'a Pubkey,
99    bump_seed: &'a [u8],
100) -> [&'a [u8]; 3] {
101    [
102        WRAPPED_MINT_BACKPOINTER_SEED,
103        wrapped_mint.as_ref(),
104        bump_seed,
105    ]
106}
107
108pub(crate) fn get_wrapped_mint_backpointer_address_with_seed(
109    wrapped_mint: &Pubkey,
110) -> (Pubkey, u8) {
111    Pubkey::find_program_address(
112        &get_wrapped_mint_backpointer_address_seeds(wrapped_mint),
113        &id(),
114    )
115}
116
117/// Derive the SPL Token wrapped mint backpointer address
118pub fn get_wrapped_mint_backpointer_address(wrapped_mint: &Pubkey) -> Pubkey {
119    get_wrapped_mint_backpointer_address_with_seed(wrapped_mint).0
120}
121
122/// Derive the escrow `ATA` that backs a given wrapped mint.
123pub fn get_escrow_address(
124    unwrapped_mint: &Pubkey,
125    unwrapped_token_program_id: &Pubkey,
126    wrapped_token_program_id: &Pubkey,
127) -> Pubkey {
128    let wrapped_mint = get_wrapped_mint_address(unwrapped_mint, wrapped_token_program_id);
129    let mint_authority = get_wrapped_mint_authority(&wrapped_mint);
130
131    get_associated_token_address_with_program_id(
132        &mint_authority,
133        unwrapped_mint,
134        unwrapped_token_program_id,
135    )
136}