forked from regolith-labs/ore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupgrade.rs
More file actions
72 lines (65 loc) · 2.17 KB
/
upgrade.rs
File metadata and controls
72 lines (65 loc) · 2.17 KB
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
use ore_api::{consts::*, error::OreError, instruction::StakeArgs, loaders::*};
use ore_utils::spl::mint_to_signed;
use solana_program::{
account_info::AccountInfo, entrypoint::ProgramResult, program_error::ProgramError,
program_pack::Pack,
};
use spl_token::state::Mint;
/// Upgrade allows a user to migrate a v1 token to a v2 token at a 1:1 exchange rate.
pub fn process_upgrade<'a, 'info>(
accounts: &'a [AccountInfo<'info>],
data: &[u8],
) -> ProgramResult {
// Parse args
let args = StakeArgs::try_from_bytes(data)?;
let amount = u64::from_le_bytes(args.amount);
// Load accounts
let [signer, beneficiary_info, mint_info, mint_v1_info, sender_info, treasury_info, token_program] =
accounts
else {
return Err(ProgramError::NotEnoughAccountKeys);
};
load_signer(signer)?;
load_token_account(beneficiary_info, Some(&signer.key), &MINT_ADDRESS, true)?;
load_mint(mint_info, MINT_ADDRESS, true)?;
load_mint(mint_v1_info, MINT_V1_ADDRESS, true)?;
load_token_account(sender_info, Some(signer.key), &MINT_V1_ADDRESS, true)?;
load_program(token_program, spl_token::id())?;
// Burn v1 tokens
solana_program::program::invoke(
&spl_token::instruction::burn(
&spl_token::id(),
sender_info.key,
mint_v1_info.key,
signer.key,
&[signer.key],
amount,
)?,
&[
token_program.clone(),
sender_info.clone(),
mint_v1_info.clone(),
signer.clone(),
],
)?;
// Account for decimals change.
// v1 token has 9 decimals. v2 token has 11.
let amount_to_mint = amount.saturating_mul(100);
// Cap at max supply.
let mint_data = mint_info.data.borrow();
let mint = Mint::unpack(&mint_data)?;
if mint.supply.saturating_add(amount_to_mint).gt(&MAX_SUPPLY) {
return Err(OreError::MaxSupply.into());
}
// Mint to the beneficiary account
drop(mint_data);
mint_to_signed(
mint_info,
beneficiary_info,
treasury_info,
token_program,
amount_to_mint,
&[&[TREASURY, &[TREASURY_BUMP]]],
)?;
Ok(())
}