Skip to content

Commit adeb669

Browse files
fix: correctly initialize user pending basket (#116)
1 parent 5334b4c commit adeb669

File tree

3 files changed

+39
-10
lines changed

3 files changed

+39
-10
lines changed

programs/folio/src/instructions/user/redeem_folio/burn_folio_token.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,14 @@ pub fn handler<'info>(
120120
raw_shares: u64,
121121
minimum_out_for_token_amounts: Vec<MinimumOutForTokenAmount>,
122122
) -> Result<()> {
123+
let is_user_pending_basket_intialized = UserPendingBasket::process_init_if_needed(
124+
&mut ctx.accounts.user_pending_basket,
125+
ctx.bumps.user_pending_basket,
126+
&ctx.accounts.user.key(),
127+
&ctx.accounts.folio.key(),
128+
&vec![],
129+
false,
130+
)?;
123131
let current_time = Clock::get()?.unix_timestamp;
124132

125133
{
@@ -135,7 +143,11 @@ pub fn handler<'info>(
135143
.get_fee_details(&ctx.accounts.folio_fee_config)?;
136144

137145
{
138-
let token_amounts_user = &mut ctx.accounts.user_pending_basket.load_mut()?;
146+
let token_amounts_user = if is_user_pending_basket_intialized {
147+
&mut ctx.accounts.user_pending_basket.load_init()?
148+
} else {
149+
&mut ctx.accounts.user_pending_basket.load_mut()?
150+
};
139151
let folio = &mut ctx.accounts.folio.load_mut()?;
140152
let folio_basket = &mut ctx.accounts.folio_basket.load_mut()?;
141153

programs/folio/src/utils/accounts/user_pending_basket.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ impl UserPendingBasket {
2424
/// * `folio` - The folio the UserPendingBasket account belongs to.
2525
/// * `added_token_amounts` - The token amounts to add to the UserPendingBasket account.
2626
/// * `can_add_new_mints` - Whether we can add new mints to the UserPendingBasket account.
27+
///
28+
/// # Returns
29+
/// * True if discriminator is initialized.
2730
#[cfg(not(tarpaulin_include))]
2831
pub fn process_init_if_needed(
2932
account_loader_user_pending_basket: &mut AccountLoader<UserPendingBasket>,
@@ -32,7 +35,7 @@ impl UserPendingBasket {
3235
folio: &Pubkey,
3336
added_token_amounts: &Vec<TokenAmount>,
3437
can_add_new_mints: bool,
35-
) -> Result<()> {
38+
) -> Result<bool> {
3639
let account_info_user_pending_basket = account_loader_user_pending_basket.to_account_info();
3740

3841
let data = account_info_user_pending_basket.try_borrow_mut_data()?;
@@ -58,6 +61,7 @@ impl UserPendingBasket {
5861
can_add_new_mints,
5962
PendingBasketType::MintProcess,
6063
)?;
64+
Ok(true)
6165
} else {
6266
let user_pending_basket = &mut account_loader_user_pending_basket.load_mut()?;
6367

@@ -68,9 +72,8 @@ impl UserPendingBasket {
6872
can_add_new_mints,
6973
PendingBasketType::MintProcess,
7074
)?;
75+
Ok(false)
7176
}
72-
73-
Ok(())
7477
}
7578

7679
/// Add token amounts to the pending basket of the user. If can add new mints it mean it won't error out if the mint is not in the basket yet.

tests-ts/bankrun/tests/tests-folio-redeeming.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ describe("Bankrun - Folio redeeming", () => {
265265
},
266266

267267
{
268-
desc: "(users burns max amount of shares, even when his alreadyIncludedTokens is empty)",
268+
desc: "(users burns max amount of shares, even when his alreadyIncludedTokens is empty, and user pending basket is not initialized)",
269269
expectedError: null,
270270
folioBasketTokens: [
271271
new FolioTokenAmount(MINTS[0].publicKey, new BN(1_000).mul(D9)),
@@ -608,6 +608,12 @@ describe("Bankrun - Folio redeeming", () => {
608608
userKeypair.publicKey,
609609
alreadyIncludedTokens
610610
);
611+
} else {
612+
const userPendingBasket = getUserPendingBasketPDA(
613+
folioPDA,
614+
userKeypair.publicKey
615+
);
616+
await closeAccount(context, userPendingBasket);
611617
}
612618

613619
await travelFutureSlot(context);
@@ -626,11 +632,19 @@ describe("Bankrun - Folio redeeming", () => {
626632
)
627633
).basket.tokenAmounts;
628634

629-
userPendingBasketBefore = (
630-
await programFolio.account.userPendingBasket.fetch(
631-
getUserPendingBasketPDA(folioPDA, userKeypair.publicKey)
632-
)
633-
).basket.tokenAmounts;
635+
if (!user_pending_basket_not_initalized) {
636+
userPendingBasketBefore = (
637+
await programFolio.account.userPendingBasket.fetch(
638+
getUserPendingBasketPDA(folioPDA, userKeypair.publicKey)
639+
)
640+
).basket.tokenAmounts;
641+
} else {
642+
userPendingBasketBefore = Array(110).fill({
643+
mint: PublicKey.default,
644+
amountForMinting: new BN(0),
645+
amountForRedeeming: new BN(0),
646+
});
647+
}
634648

635649
try {
636650
txnResult = await burnFolioToken<true>(

0 commit comments

Comments
 (0)