forked from samuelvanderwaal/metaboss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathburn.rs
108 lines (91 loc) · 2.75 KB
/
burn.rs
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
use anyhow::Result;
use mpl_token_metadata::{
id,
instruction::update_metadata_accounts,
state::{Data, Metadata},
};
use retry::{delay::Exponential, retry};
use solana_client::rpc_client::RpcClient;
use solana_sdk::{
pubkey::Pubkey,
signature::Signature,
signer::{keypair::Keypair, Signer},
transaction::Transaction,
};
use spl_associated_token_account::get_associated_token_address;
use spl_token;
use std::str::FromStr;
use crate::decode::decode;
use crate::derive::derive_metadata_pda;
use crate::parse::parse_keypair;
pub fn burn_one(client: &RpcClient, keypair: String, mint_address: String) -> Result<()> {
let mint_pubkey = Pubkey::from_str(&mint_address)?;
let keypair = parse_keypair(&keypair)?;
let owner_pubkey = keypair.pubkey();
let sig = burn(client, &keypair, &owner_pubkey, &mint_pubkey, 1)?;
println!("TxId: {}", sig);
Ok(())
}
pub fn burn(
client: &RpcClient,
signer: &Keypair,
owner_pubkey: &Pubkey,
mint_pubkey: &Pubkey,
amount: u64,
) -> Result<Signature> {
let assoc = get_associated_token_address(&owner_pubkey, &mint_pubkey);
let spl_token_program_id = spl_token::id();
let burn_ix = spl_token::instruction::burn(
&spl_token_program_id,
&assoc,
mint_pubkey,
&signer.pubkey(),
&[&signer.pubkey()],
amount,
)?;
let close_associated_token_account = spl_token::instruction::close_account(
&spl_token_program_id,
&assoc,
&signer.pubkey(),
&signer.pubkey(),
&[&signer.pubkey()],
)?;
let mut instructions = vec![burn_ix, close_associated_token_account];
let metadata: Metadata = decode(client, &mint_pubkey.to_string())?;
if signer.pubkey() == metadata.update_authority {
let metadata_pubkey = derive_metadata_pda(mint_pubkey);
let data = default_data();
let clear_metadata_account = update_metadata_accounts(
id(),
metadata_pubkey,
signer.pubkey(),
None,
Some(data),
None,
);
instructions.push(clear_metadata_account);
}
let recent_blockhash = client.get_latest_blockhash()?;
let tx = Transaction::new_signed_with_payer(
&instructions,
Some(&signer.pubkey()),
&[signer],
recent_blockhash,
);
// Send tx with retries.
let res = retry(
Exponential::from_millis_with_factor(250, 2.0).take(3),
|| client.send_and_confirm_transaction(&tx),
);
let sig = res?;
Ok(sig)
}
fn default_data() -> Data {
Data {
name: String::default(),
symbol: String::default(),
uri: String::default(),
seller_fee_basis_points: u16::default(),
creators: None,
}
}