Skip to content

Commit

Permalink
Permissioned crank integration test (#203)
Browse files Browse the repository at this point in the history
  • Loading branch information
armaniferrante authored Dec 6, 2021
1 parent 57b9281 commit f1a6be3
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 2 deletions.
2 changes: 1 addition & 1 deletion dex/tests/permissioned/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"@project-serum/anchor": "^0.18.2",
"@project-serum/anchor-cli": "^0.18.2",
"@project-serum/common": "^0.0.1-beta.3",
"@project-serum/serum": "^0.13.55",
"@project-serum/serum": "^0.13.61",
"@solana/spl-token": "^0.1.6",
"mocha": "^9.0.3"
}
Expand Down
37 changes: 37 additions & 0 deletions dex/tests/permissioned/programs/permissioned-markets/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,28 @@ impl MarketMiddleware for Identity {
Ok(())
}

/// Accounts:
///
/// 0. Authorization token (revoked).
/// ..
fn consume_events_permissioned(&self, ctx: &mut Context, _limit: &mut u16) -> ProgramResult {
verify_revoked_and_strip_auth(ctx)?;

let market_idx = ctx.accounts.len() - 3;
let auth_idx = ctx.accounts.len() - 1;

// Sign with the consume_events authority.
let market = &ctx.accounts[market_idx];
ctx.seeds.push(consume_events_authority! {
program = ctx.program_id,
dex_program = ctx.dex_program_id,
market = market.key
});

ctx.accounts[auth_idx] = Self::prepare_pda(&ctx.accounts[auth_idx]);
Ok(())
}

/// Accounts:
///
/// 0. Authorization token.
Expand Down Expand Up @@ -227,6 +249,21 @@ macro_rules! prune_authority {
};
}

#[macro_export]
macro_rules! consume_events_authority {
(
program = $program:expr,
dex_program = $dex_program:expr,
market = $market:expr
) => {
prune_authority!(
program = $program,
dex_program = $dex_program,
market = $market
)
};
}

// Error.

#[error]
Expand Down
2 changes: 1 addition & 1 deletion dex/tests/permissioned/tests/permissioned-markets.js
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ async function crankEventQueue(provider, marketProxy) {
while (eq.length > 0) {
const tx = new Transaction();
tx.add(
marketProxy.market.makeConsumeEventsInstruction([eq[0].openOrders], 1)
marketProxy.instruction.consumeEventsPermissioned([eq[0].openOrders], 1)
);
await provider.send(tx);
eq = await marketProxy.market.loadEventQueue(provider.connection);
Expand Down
5 changes: 5 additions & 0 deletions dex/tests/permissioned/tests/utils/market-lister.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ async function list({
DEX_PID,
proxyProgramId
),
crankAuthority: await Identity.consumeEventsAuthority(
market.publicKey,
DEX_PID,
proxyProgramId
),
})
);

Expand Down
13 changes: 13 additions & 0 deletions dex/tests/permissioned/tests/utils/market-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const {
OpenOrdersPda,
Logger,
ReferralFees,
PermissionedCrank,
MarketProxyBuilder,
} = require("@project-serum/serum");

Expand Down Expand Up @@ -71,6 +72,14 @@ class Identity {
...ix.keys,
];
}
consumeEventsPermissioned(ix) {
ix.keys = [
{ pubkey: SYSVAR_CLOCK_PUBKEY, isWritable: false, isSigner: false },
...ix.keys,
];
// PDA: so ensure the signer is false.
ix.keys[ix.keys.length-1].isSigner = false;
}
static async pruneAuthority(market, dexProgramId, proxyProgramId) {
const [addr] = await PublicKey.findProgramAddress(
[
Expand All @@ -82,6 +91,10 @@ class Identity {
);
return addr;
}

static async consumeEventsAuthority(market, dexProgramId, proxyProgramId) {
return Identity.pruneAuthority(market, dexProgramId, proxyProgramId);
}
}

module.exports = {
Expand Down

0 comments on commit f1a6be3

Please sign in to comment.