Skip to content

Commit 14a2a9d

Browse files
committed
fix: validate new mint decimals and supply on migration
1 parent eb4e792 commit 14a2a9d

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

programs/earn/src/instructions/admin/initialize.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ cfg_if::cfg_if!(
2727
if #[cfg(feature = "migrate")] {
2828
declare_program!(old_earn);
2929
use old_earn::{accounts::Global as OldGlobal, ID as OLD_EARN_PROGRAM_ID};
30+
31+
use crate::utils::conversion::{get_scaled_ui_config, principal_to_amount_up};
3032
}
3133
);
3234

@@ -61,6 +63,13 @@ pub struct Initialize<'info> {
6163
)]
6264
pub m_mint: InterfaceAccount<'info, Mint>,
6365

66+
#[cfg(feature = "migrate")]
67+
#[account(
68+
address = old_global_account.mint @ EarnError::InvalidMint,
69+
mint::decimals = m_mint.decimals
70+
)]
71+
pub old_m_mint: InterfaceAccount<'info, Mint>,
72+
6473
/// CHECK: This account is validated by its seeds
6574
#[account(
6675
seeds = [TOKEN_AUTHORITY_SEED],
@@ -184,6 +193,14 @@ impl Initialize<'_> {
184193
ctx.accounts.old_global_account.index, // index
185194
ctx.accounts.old_global_account.timestamp as i64, // timestamp
186195
)?;
196+
197+
// Check that the supply of the new mint (adjusted for the multiplier) is not greater than the supply of the old m mint
198+
let scaled_ui_config = get_scaled_ui_config(&ctx.accounts.m_mint)?;
199+
let new_supply_amount = principal_to_amount_up(ctx.accounts.m_mint.supply, scaled_ui_config.new_multiplier.into())?;
200+
201+
if new_supply_amount > ctx.accounts.old_m_mint.supply {
202+
return err!(EarnError::InvalidMint);
203+
}
187204
} else {
188205
update_multiplier(
189206
&mut ctx.accounts.m_mint, // mint

tests/unit/earn.test.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -849,7 +849,7 @@ class EarnTest<V extends Variant = Variant.New> {
849849
case Variant.New:
850850
await this.earn.methods
851851
.initialize(initialIndex!)
852-
.accounts({
852+
.accountsPartial({
853853
admin: this.admin.publicKey,
854854
mMint: this.mMint.publicKey,
855855
})
@@ -862,6 +862,7 @@ class EarnTest<V extends Variant = Variant.New> {
862862
.accounts({
863863
admin: this.admin.publicKey,
864864
mMint: this.mMint.publicKey,
865+
oldMMint: this.oldMMint!.publicKey,
865866
})
866867
.signers([this.admin])
867868
.rpc();
@@ -979,6 +980,7 @@ for (const variant of VARIANTS) {
979980
.accounts({
980981
admin: $.admin.publicKey,
981982
mMint: $.mMint.publicKey,
983+
oldMMint: $.oldMMint!.publicKey,
982984
})
983985
.signers([$.admin])
984986
.rpc(),
@@ -987,7 +989,7 @@ for (const variant of VARIANTS) {
987989
await $.expectSystemError(
988990
$.earn.methods
989991
.initialize(initialIndex)
990-
.accounts({
992+
.accountsPartial({
991993
admin: $.admin.publicKey,
992994
mMint: $.mMint.publicKey,
993995
})
@@ -1013,6 +1015,7 @@ for (const variant of VARIANTS) {
10131015
admin: $.admin.publicKey,
10141016
mMint: $.mMint.publicKey,
10151017
globalAccount: wrongGlobalAccount,
1018+
oldMMint: $.oldMMint!.publicKey,
10161019
})
10171020
.signers([$.admin])
10181021
.rpc(),
@@ -1046,6 +1049,7 @@ for (const variant of VARIANTS) {
10461049
.accountsPartial({
10471050
admin: $.admin.publicKey,
10481051
mMint: wrongMint.publicKey,
1052+
oldMMint: $.oldMMint!.publicKey,
10491053
tokenProgram: TOKEN_PROGRAM_ID,
10501054
})
10511055
.signers([$.admin])
@@ -1083,6 +1087,7 @@ for (const variant of VARIANTS) {
10831087
.accountsPartial({
10841088
admin: $.admin.publicKey,
10851089
mMint: $.mMint.publicKey,
1090+
oldMMint: $.oldMMint!.publicKey,
10861091
portalTokenAuthority: wrongPortalAuthority,
10871092
})
10881093
.signers([$.admin])
@@ -1119,6 +1124,7 @@ for (const variant of VARIANTS) {
11191124
.accountsPartial({
11201125
admin: $.admin.publicKey,
11211126
mMint: $.mMint.publicKey,
1127+
oldMMint: $.oldMMint!.publicKey,
11221128
extSwapGlobal: wrongExtSwapGlobal,
11231129
})
11241130
.signers([$.admin])
@@ -1153,6 +1159,7 @@ for (const variant of VARIANTS) {
11531159
.accountsPartial({
11541160
admin: $.admin.publicKey,
11551161
mMint: wrongMint.publicKey,
1162+
oldMMint: $.oldMMint!.publicKey,
11561163
})
11571164
.signers([$.admin])
11581165
.rpc(),
@@ -1187,6 +1194,7 @@ for (const variant of VARIANTS) {
11871194
.accountsPartial({
11881195
admin: $.admin.publicKey,
11891196
mMint: wrongMint.publicKey,
1197+
oldMMint: $.oldMMint!.publicKey,
11901198
})
11911199
.signers([$.admin])
11921200
.rpc(),
@@ -1220,6 +1228,7 @@ for (const variant of VARIANTS) {
12201228
.accountsPartial({
12211229
admin: $.admin.publicKey,
12221230
mMint: mint.publicKey,
1231+
oldMMint: $.oldMMint!.publicKey,
12231232
})
12241233
.signers([$.admin])
12251234
.rpc(),
@@ -1258,6 +1267,10 @@ for (const variant of VARIANTS) {
12581267
// migrate test cases
12591268
// [X] given the old global account does not match the seed + program ID
12601269
// [X] it reverts with a constraint seed error
1270+
// [ ] given the new m mint doesn't have the same decimals as the old one
1271+
// [ ] it reverts with a mint decimals error
1272+
// [ ] given the effective supply of the new mint is greater than than the supply of the old mint
1273+
// [ ] it reverts with an invalid mint error
12611274
// [X] given all the accounts are correct
12621275
// [X] the global account is created
12631276
// [X] the admin is set to the signer
@@ -1289,6 +1302,7 @@ for (const variant of VARIANTS) {
12891302
.accountsPartial({
12901303
admin: $.admin.publicKey,
12911304
mMint: $.mMint.publicKey,
1305+
oldMMint: $.oldMMint!.publicKey,
12921306
oldGlobalAccount: wrongGlobalAccount,
12931307
})
12941308
.signers([$.admin])
@@ -1310,14 +1324,15 @@ for (const variant of VARIANTS) {
13101324
.accounts({
13111325
admin: $.admin.publicKey,
13121326
mMint: $.mMint.publicKey,
1327+
oldMMint: $.oldMMint!.publicKey,
13131328
})
13141329
.signers([$.admin])
13151330
.rpc();
13161331
} else {
13171332
// Create and send the transaction
13181333
await $.earn.methods
13191334
.initialize(initialIndex)
1320-
.accounts({
1335+
.accountsPartial({
13211336
admin: $.admin.publicKey,
13221337
mMint: $.mMint.publicKey,
13231338
})

0 commit comments

Comments
 (0)