Skip to content

Commit

Permalink
Merge pull request #21 from metaDAOproject/permissionless_settle
Browse files Browse the repository at this point in the history
Permissionless settle funds
  • Loading branch information
Henry-E authored Mar 26, 2024
2 parents 12fff44 + f233caa commit 7283b75
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 16 deletions.
61 changes: 61 additions & 0 deletions programs/openbook-twap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,35 @@ pub struct PruneOrders<'info> {
pub openbook_program: Program<'info, OpenbookV2>,
}

#[derive(Accounts)]
pub struct SettleFundsExpired<'info> {
#[account(mut)]
pub twap_market: Account<'info, TWAPMarket>,
/// CHECK: verified in CPI
#[account(mut)]
pub open_orders_account: UncheckedAccount<'info>,
#[account(mut)]
pub market: AccountLoader<'info, Market>,
/// CHECK: verified in CPI
pub market_authority: UncheckedAccount<'info>,
/// CHECK: verified in CPI
#[account(mut)]
pub market_base_vault: UncheckedAccount<'info>,
/// CHECK: verified in CPI
#[account(mut)]
pub market_quote_vault: UncheckedAccount<'info>,
/// CHECK: verified in CPI
#[account(mut)]
pub user_base_account: UncheckedAccount<'info>,
/// CHECK: verified in CPI
#[account(mut)]
pub user_quote_account: UncheckedAccount<'info>,
/// CHECK: verified in CPI
pub token_program: UncheckedAccount<'info>,
pub openbook_program: Program<'info, OpenbookV2>,
pub system_program: Program<'info, System>,
}

#[derive(Accounts)]
pub struct CloseMarket<'info> {
/// CHECK: This is a permissionless function but could be made to require the close_market_rent_receiver's signature
Expand Down Expand Up @@ -629,6 +658,38 @@ pub mod openbook_twap {
Ok(())
}

pub fn settle_funds_expired(ctx: Context<SettleFundsExpired>) -> Result<()> {
let market_key = ctx.accounts.market.key();

let seeds =
TWAPMarket::get_twap_market_seeds(&market_key, &ctx.accounts.twap_market.pda_bump);
let signer_seeds = &[&seeds[..]];

openbook_v2::cpi::settle_funds_expired(
CpiContext::new_with_signer(
ctx.accounts.openbook_program.to_account_info(),
openbook_v2::cpi::accounts::SettleFundsExpired {
close_market_admin: ctx.accounts.twap_market.to_account_info(),
owner: ctx.accounts.twap_market.to_account_info(), // Placeholder for the owner signer account
penalty_payer: ctx.accounts.twap_market.to_account_info(), // Placeholder for the penalty payer signer account
open_orders_account: ctx.accounts.open_orders_account.to_account_info(),
market: ctx.accounts.market.to_account_info(),
market_authority: ctx.accounts.market_authority.to_account_info(),
market_base_vault: ctx.accounts.market_base_vault.to_account_info(),
market_quote_vault: ctx.accounts.market_quote_vault.to_account_info(),
user_base_account: ctx.accounts.user_base_account.to_account_info(),
user_quote_account: ctx.accounts.user_quote_account.to_account_info(),
referrer_account: None,
token_program: ctx.accounts.token_program.to_account_info(),
system_program: ctx.accounts.system_program.to_account_info(),
},
signer_seeds,
),
)?;

Ok(())
}

pub fn close_market<'info>(ctx: Context<CloseMarket>) -> Result<()> {
let market_key = ctx.accounts.market.key();

Expand Down
31 changes: 15 additions & 16 deletions tests/openbook-twap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,22 +423,21 @@ describe("openbook-twap", () => {
})
.rpc();

let [settleFundsIx, settleFundsSigners] = await openbook.settleFundsIx(
oos[i],
await openbook.deserializeOpenOrderAccount(oos[i]),
market,
await openbook.deserializeMarketAccount(market),
metaAccount,
usdcAccount,
null,
provider.publicKey
);

let settleTx = new anchor.web3.Transaction().add(settleFundsIx);
[settleTx.recentBlockhash] = await banksClient.getLatestBlockhash();
settleTx.feePayer = provider.publicKey;

await provider.sendAndConfirm(settleTx, settleFundsSigners);
await openbookTwap.methods
.settleFundsExpired()
.accounts({
twapMarket,
openOrdersAccount: oos[i],
market,
marketAuthority: storedMarket.marketAuthority,
marketBaseVault: storedMarket.marketBaseVault,
marketQuoteVault: storedMarket.marketQuoteVault,
userBaseAccount: metaAccount,
userQuoteAccount: usdcAccount,
tokenProgram: TOKEN_PROGRAM_ID,
openbookProgram: OPENBOOK_PROGRAM_ID,
})
.rpc();
}
// Fetch the current balance in lamports
const balanceBefore = await banksClient.getBalance(
Expand Down

0 comments on commit 7283b75

Please sign in to comment.