spl-token-wrap-cli 2.0.0

Solana Program Library Token Wrap command-line utility
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
use {
    crate::{
        common::{
            get_account_owner, get_mint_for_token_account, parse_presigner, parse_pubkey,
            parse_token_program, process_transaction,
        },
        config::Config,
        output::{format_output, println_display},
        CommandResult, Error,
    },
    clap::{value_parser, ArgMatches, Args},
    serde_derive::{Deserialize, Serialize},
    serde_with::{serde_as, DisplayFromStr},
    solana_clap_v3_utils::{
        input_parsers::signer::{SignerSource, SignerSourceParserBuilder},
        keypair::{signer_from_source_with_config, SignerFromPathConfig},
    },
    solana_cli_output::{
        display::writeln_name_value, return_signers_data, CliSignOnlyData, QuietDisplay,
        ReturnSignersConfig, VerboseDisplay,
    },
    solana_hash::Hash,
    solana_presigner::Presigner,
    solana_pubkey::Pubkey,
    solana_remote_wallet::remote_wallet::RemoteWalletManager,
    solana_signature::Signature,
    solana_signer::Signer,
    solana_transaction::Transaction,
    spl_token_wrap::{
        get_escrow_address, get_wrapped_mint_address, get_wrapped_mint_authority,
        instruction::unwrap,
    },
    std::{
        fmt::{Display, Formatter},
        rc::Rc,
        sync::Arc,
    },
};

#[derive(Clone, Debug, Args)]
pub struct UnwrapArgs {
    /// The address of the wrapped token account to unwrap from
    #[clap(value_parser = parse_pubkey)]
    pub wrapped_token_account: Pubkey,

    /// The address of the token account to receive the unwrapped tokens
    #[clap(value_parser = parse_pubkey)]
    pub unwrapped_token_recipient: Pubkey,

    /// The amount of tokens to unwrap
    #[clap(value_parser)]
    pub amount: u64,

    /// Signer source of transfer authority (to burn wrapped tokens)
    /// if different from fee payer
    #[clap(
        long,
        value_parser = SignerSourceParserBuilder::default().allow_all().build()
    )]
    pub transfer_authority: Option<SignerSource>,

    /// The address of the unwrapped mint, queried if not provided
    #[clap(long, value_parser = parse_pubkey)]
    pub unwrapped_mint: Option<Pubkey>,

    /// The address of the token program for the wrapped mint,
    /// queried if not provided.
    #[clap(long, value_parser = parse_token_program)]
    pub wrapped_token_program: Option<Pubkey>,

    /// The address of the token program for the unwrapped mint,
    /// queried if not provided.
    #[clap(long, value_parser = parse_token_program)]
    pub unwrapped_token_program: Option<Pubkey>,

    /// Member signer of a multisig account.
    /// Use this argument multiple times for each signer.
    #[clap(
        long,
        multiple = true,
        value_parser = SignerSourceParserBuilder::default().allow_all().build(),
        requires = "blockhash"
    )]
    pub multisig_signer: Option<Vec<SignerSource>>,

    #[clap(long, value_parser = value_parser!(Hash))]
    pub blockhash: Option<Hash>,

    /// Signatures to add to transaction.
    /// Often the `PUBKEY=SIGNATURE` output from a multisig --sign-only signer.
    #[clap(
        long,
        multiple = true,
        value_parser = parse_presigner,
        requires = "blockhash"
    )]
    pub signer: Option<Vec<Presigner>>,

    /// Do not broadcast signed transaction, just sign
    #[clap(long)]
    pub sign_only: bool,
}

#[serde_as]
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct UnwrapOutput {
    #[serde_as(as = "DisplayFromStr")]
    pub unwrapped_mint_address: Pubkey,

    #[serde_as(as = "DisplayFromStr")]
    pub unwrapped_token_program: Pubkey,

    #[serde_as(as = "DisplayFromStr")]
    pub wrapped_mint_address: Pubkey,

    #[serde_as(as = "DisplayFromStr")]
    pub wrapped_token_account: Pubkey,

    #[serde_as(as = "DisplayFromStr")]
    pub escrow_account: Pubkey,

    #[serde_as(as = "DisplayFromStr")]
    pub recipient_token_account: Pubkey,

    pub amount: u64,

    pub signatures: Vec<Signature>,

    pub sign_only_data: Option<CliSignOnlyData>,
}

impl Display for UnwrapOutput {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        writeln_name_value(
            f,
            "Unwrapped token program:",
            &self.unwrapped_token_program.to_string(),
        )?;
        writeln_name_value(
            f,
            "Unwrapped mint address:",
            &self.unwrapped_mint_address.to_string(),
        )?;
        writeln_name_value(
            f,
            "Wrapped mint address:",
            &self.wrapped_mint_address.to_string(),
        )?;
        writeln_name_value(
            f,
            "Wrapped token account:",
            &self.wrapped_token_account.to_string(),
        )?;
        writeln_name_value(f, "Escrow account:", &self.escrow_account.to_string())?;
        writeln_name_value(
            f,
            "Recipient unwrapped token account:",
            &self.recipient_token_account.to_string(),
        )?;
        writeln_name_value(f, "Amount unwrapped:", &self.amount.to_string())?;

        if let Some(data) = &self.sign_only_data {
            writeln!(f, "{}", data)?;
        } else {
            writeln!(f, "Signers:")?;
            for signature in &self.signatures {
                writeln!(f, "  {signature}")?;
            }
        }

        Ok(())
    }
}

impl QuietDisplay for UnwrapOutput {
    fn write_str(&self, _: &mut dyn std::fmt::Write) -> std::fmt::Result {
        Ok(())
    }
}
impl VerboseDisplay for UnwrapOutput {}

pub async fn command_unwrap(
    config: &Config,
    args: UnwrapArgs,
    matches: &ArgMatches,
    wallet_manager: &mut Option<Rc<RemoteWalletManager>>,
) -> CommandResult {
    let ResolvedAddrs {
        wrapped_token_program,
        unwrapped_mint_address,
        wrapped_mint_address,
        wrapped_mint_authority_address,
        unwrapped_token_program,
        escrow_account,
        transfer_authority_signer,
    } = resolve_addresses(config, &args, matches, wallet_manager).await?;

    let mut multisig_signers: Vec<Arc<dyn Signer>> = vec![];
    if let Some(sources) = &args.multisig_signer {
        for source in sources {
            let signer = signer_from_source_with_config(
                matches,
                source,
                "multisig_signer",
                wallet_manager,
                &SignerFromPathConfig {
                    allow_null_signer: true,
                },
            )
            .map_err(|e| e.to_string())?;
            multisig_signers.push(Arc::from(signer));
        }
    }

    let multisig_pubkeys = multisig_signers
        .iter()
        .map(|s| s.pubkey())
        .collect::<Vec<Pubkey>>();

    let instruction = unwrap(
        &spl_token_wrap::id(),
        &escrow_account,
        &args.unwrapped_token_recipient,
        &wrapped_mint_authority_address,
        &unwrapped_mint_address,
        &wrapped_token_program,
        &unwrapped_token_program,
        &args.wrapped_token_account,
        &wrapped_mint_address,
        &transfer_authority_signer.pubkey(),
        &multisig_pubkeys.iter().collect::<Vec<&Pubkey>>(),
        args.amount,
    );

    let blockhash = if let Some(hash) = args.blockhash {
        hash
    } else {
        config.rpc_client.get_latest_blockhash().await?
    };

    let payer = config.fee_payer()?;

    // Payer will always be a signer
    let mut signers = vec![payer.clone()];

    // In the case that a transfer_authority is passed (otherwise defaults to
    // payer), it needs to be added to signers if it isn't a multisig.
    if payer.pubkey() != transfer_authority_signer.pubkey() && multisig_signers.is_empty() {
        signers.push(transfer_authority_signer);
    }

    for signer in &multisig_signers {
        signers.push(signer.clone());
    }

    // Pre-signed transactions can be passed as --signer `PUBKEY=SIGNATURE`
    if let Some(pre_signers) = &args.signer {
        for signer in pre_signers {
            signers.push(Arc::from(signer));
        }
    }

    let mut transaction = Transaction::new_with_payer(&[instruction], Some(&payer.pubkey()));
    transaction.partial_sign(&signers, blockhash);

    if !args.sign_only {
        process_transaction(config, transaction.clone()).await?;
    }

    let sign_only_data = args.sign_only.then(|| {
        return_signers_data(
            &transaction,
            &ReturnSignersConfig {
                dump_transaction_message: true,
            },
        )
    });

    let output = UnwrapOutput {
        unwrapped_token_program,
        unwrapped_mint_address,
        wrapped_mint_address,
        escrow_account,
        wrapped_token_account: args.wrapped_token_account,
        recipient_token_account: args.unwrapped_token_recipient,
        amount: args.amount,
        signatures: transaction.signatures,
        sign_only_data,
    };

    Ok(format_output(config, output))
}

struct ResolvedAddrs {
    wrapped_token_program: Pubkey,
    unwrapped_mint_address: Pubkey,
    wrapped_mint_address: Pubkey,
    wrapped_mint_authority_address: Pubkey,
    unwrapped_token_program: Pubkey,
    escrow_account: Pubkey,
    transfer_authority_signer: Arc<dyn Signer>,
}

// Validates optional fields passed, or if not passed queries for them
async fn resolve_addresses(
    config: &Config,
    args: &UnwrapArgs,
    matches: &ArgMatches,
    wallet_manager: &mut Option<Rc<RemoteWalletManager>>,
) -> Result<ResolvedAddrs, Error> {
    let payer = config.fee_payer()?;

    // Validate `wrapped_token_program` governs `wrapped_token_account`
    let queried_wrapped_token_program =
        get_account_owner(&config.rpc_client, &args.wrapped_token_account).await?;
    let wrapped_token_program = if let Some(program_id) = args.wrapped_token_program {
        if program_id != queried_wrapped_token_program {
            return Err(format!(
                "Provided wrapped token program ID {program_id} does not match actual owner \
                 {queried_wrapped_token_program} of account {}",
                args.wrapped_token_account
            )
            .into());
        }
        program_id
    } else {
        queried_wrapped_token_program
    };

    // Validate `unwrapped_token_recipient` account matches the `unwrapped_mint`
    let queried_mint =
        get_mint_for_token_account(&config.rpc_client, &args.unwrapped_token_recipient).await?;
    let unwrapped_mint_address = if let Some(mint) = args.unwrapped_mint {
        if mint != queried_mint {
            return Err(format!(
                "Provided unwrapped mint {mint} does not match actual mint {queried_mint} of \
                 recipient account {}",
                args.unwrapped_token_recipient
            )
            .into());
        }
        mint
    } else {
        queried_mint
    };

    // Validate `unwrapped_mint_address` matches the `unwrapped_token_program`
    let queried_unwrapped_token_program =
        get_account_owner(&config.rpc_client, &unwrapped_mint_address).await?;
    let unwrapped_token_program = if let Some(program_id) = args.unwrapped_token_program {
        if program_id != queried_unwrapped_token_program {
            return Err(format!(
                "Provided unwrapped token program ID {program_id} does not match actual owner \
                 {queried_unwrapped_token_program} of unwrapped mint {unwrapped_mint_address}",
            )
            .into());
        }
        program_id
    } else {
        queried_unwrapped_token_program
    };

    let wrapped_mint_address =
        get_wrapped_mint_address(&unwrapped_mint_address, &wrapped_token_program);
    let wrapped_mint_authority_address = get_wrapped_mint_authority(&wrapped_mint_address);

    let escrow_account = get_escrow_address(
        &unwrapped_mint_address,
        &unwrapped_token_program,
        &wrapped_token_program,
    );

    if !config.dry_run {
        println_display(
            config,
            format!(
                "Unwrapping {} tokens from mint {wrapped_mint_address} to {unwrapped_mint_address}",
                args.amount,
            ),
        );
    }

    let transfer_authority_signer = if let Some(authority_source) = &args.transfer_authority {
        let signer = signer_from_source_with_config(
            matches,
            authority_source,
            "transfer_authority",
            wallet_manager,
            &SignerFromPathConfig {
                allow_null_signer: true,
            },
        )
        .map_err(|e| e.to_string())?;
        Arc::from(signer)
    } else {
        payer.clone() // Default to payer
    };

    Ok(ResolvedAddrs {
        wrapped_token_program,
        unwrapped_mint_address,
        wrapped_mint_address,
        wrapped_mint_authority_address,
        unwrapped_token_program,
        escrow_account,
        transfer_authority_signer,
    })
}