forked from regolith-labs/ore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upgrade.rs
67 lines (60 loc) · 1.81 KB
/
upgrade.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
use ore_api::prelude::*;
use steel::*;
/// Upgrade allows a user to migrate a v1 token to a v2 token at a 1:1 exchange rate.
pub fn process_upgrade(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult {
// Parse args
let args = Upgrade::try_from_bytes(data)?;
let amount = u64::from_le_bytes(args.amount);
// Load accounts
let [signer_info, beneficiary_info, mint_info, mint_v1_info, sender_info, treasury_info, token_program] =
accounts
else {
return Err(ProgramError::NotEnoughAccountKeys);
};
signer_info.is_signer()?;
beneficiary_info
.is_writable()?
.as_token_account()?
.assert(|t| t.owner == *signer_info.key)?
.assert(|t| t.mint == MINT_ADDRESS)?;
let mint = mint_info
.is_writable()?
.has_address(&MINT_ADDRESS)?
.as_mint()?;
mint_v1_info
.is_writable()?
.has_address(&MINT_V1_ADDRESS)?
.as_mint()?;
sender_info
.is_writable()?
.as_token_account()?
.assert(|t| t.owner == *signer_info.key)?
.assert(|t| t.mint == MINT_V1_ADDRESS)?;
treasury_info.is_treasury()?;
token_program.is_program(&spl_token::ID)?;
// Burn v1 tokens
burn(
sender_info,
mint_v1_info,
signer_info,
token_program,
amount,
)?;
// 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.
if mint.supply.saturating_add(amount_to_mint).gt(&MAX_SUPPLY) {
return Err(OreError::MaxSupply.into());
}
// Mint to the beneficiary account
mint_to_signed(
mint_info,
beneficiary_info,
treasury_info,
token_program,
amount_to_mint,
&[TREASURY],
)?;
Ok(())
}