Skip to content

Commit

Permalink
Pay out oracles when closing the aggregator
Browse files Browse the repository at this point in the history
  • Loading branch information
archseer committed Mar 15, 2022
1 parent 0cdda9e commit 2748e73
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
8 changes: 8 additions & 0 deletions contracts/programs/ocr2/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ pub struct Close<'info> {
#[account(mut)]
pub receiver: SystemAccount<'info>,
pub authority: Signer<'info>,

#[account(mut, address = state.load()?.config.token_vault)]
pub token_vault: Account<'info, TokenAccount>,
/// CHECK: This is a PDA
#[account(seeds = [b"vault", state.key().as_ref()], bump = state.load()?.vault_nonce)]
pub vault_authority: AccountInfo<'info>,

pub token_program: Program<'info, Token>,
}

#[derive(Accounts)]
Expand Down
11 changes: 10 additions & 1 deletion contracts/programs/ocr2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,16 @@ pub mod ocr2 {
}

#[access_control(owner(&ctx.accounts.state, &ctx.accounts.authority))]
pub fn close(ctx: Context<Close>) -> Result<()> {
pub fn close<'info>(ctx: Context<'_, '_, '_, 'info, Close<'info>>) -> Result<()> {
// Pay out any remaining balances
pay_oracles_impl(
ctx.accounts.state.clone(),
ctx.accounts.token_program.to_account_info(),
ctx.accounts.token_vault.to_account_info(),
ctx.accounts.vault_authority.clone(),
ctx.remaining_accounts,
)?;

// NOTE: Close is handled by anchor on exit due to the `close` attribute
Ok(())
}
Expand Down
13 changes: 12 additions & 1 deletion contracts/tests/ocr2.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ describe("ocr2", async () => {
tokenVault,
mintAuthority.publicKey,
[mintAuthority],
1000000000
100000000000000
);

// TODO: listen for SetConfig event
Expand Down Expand Up @@ -949,12 +949,23 @@ describe("ocr2", async () => {
await provider.connection.getAccountInfo(provider.wallet.publicKey)
).lamports;

// fetch payees
let account = await program.account.state.fetch(state.publicKey);
let currentOracles = account.oracles.xs.slice(0, account.oracles.len);
let payees = currentOracles.map((oracle) => {
return { pubkey: oracle.payee, isWritable: true, isSigner: false };
});

await program.rpc.close({
accounts: {
state: state.publicKey,
receiver: provider.wallet.publicKey,
authority: owner.publicKey,
tokenVault: tokenVault,
vaultAuthority: vaultAuthority,
tokenProgram: TOKEN_PROGRAM_ID,
},
remainingAccounts: payees,
});

let afterBalance = (
Expand Down

0 comments on commit 2748e73

Please sign in to comment.