Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

revoke whitelist logic #188

Merged
merged 7 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions rfq/program/src/constants.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub const MAX_WHITELIST_SIZE: u8 = 20;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're storing constants in state files, you can check other types for examples

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolved , added to state

2 changes: 0 additions & 2 deletions rfq/program/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,6 @@ pub enum ProtocolError {
DefaultValueIsNotPermitted,
#[msg("Specified oracle source is missing")]
OracleSourceIsMissing,
#[msg("Address Already Exists on Whitelist")]
AddressAlreadyExistsOnWhitelist,
#[msg("Whitelist Maximum Capacity Reached")]
WhitelistMaximumCapacityReached,
#[msg("Cannot Respond as Maker Address not Whitelisted")]
Expand Down
37 changes: 0 additions & 37 deletions rfq/program/src/instructions/whitelist/add_address_to_whitelist.rs

This file was deleted.

26 changes: 12 additions & 14 deletions rfq/program/src/instructions/whitelist/create_whitelist.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
use crate::{errors::ProtocolError, state::whitelist::Whitelist};

use crate::{errors::ProtocolError, state::whitelist::Whitelist,constants::MAX_WHITELIST_SIZE};
use anchor_lang::prelude::*;
use std::mem;

#[derive(Accounts)]
#[instruction(
expected_whitelist_size: u16
length: u8
)]
pub struct CreateWhitelistAccounts<'info> {
#[account(
init_if_needed,
payer = creator,
space = 8 + mem::size_of::<Whitelist>() + (expected_whitelist_size as usize)
space = get_whitelist_size_from_length(length),
)]
pub whitelist_account: Box<Account<'info, Whitelist>>,
#[account(mut)]
Expand All @@ -20,11 +21,10 @@ pub struct CreateWhitelistAccounts<'info> {

pub fn create_whitelist_instruction(
ctx: Context<CreateWhitelistAccounts>,
expected_whitelist_size: u16,
length:u8,
whitelist_to_add: Vec<Pubkey>,
) -> Result<()> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're passing it in one ix, you already have a length in this vector, no need to pass it as a parameter

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolved

let expected_whitelist_capacity = calculate_expected_capacity(expected_whitelist_size);
validate_whitelist_inputs(expected_whitelist_capacity, &whitelist_to_add)?;
validate_whitelist_inputs(length)?;
let CreateWhitelistAccounts {
creator,
whitelist_account,
Expand All @@ -34,25 +34,23 @@ pub fn create_whitelist_instruction(
whitelist_account.set_inner(Whitelist {
creator: creator.key(),
whitelist: whitelist_to_add,
capacity: expected_whitelist_capacity,
});

Ok(())
}

fn validate_whitelist_inputs(
expected_whitelist_capacity: u8,
whitelist_to_add: &Vec<Pubkey>,
length:u8,
) -> Result<()> {
require!(
whitelist_to_add.len() <= (expected_whitelist_capacity as usize),
length <= MAX_WHITELIST_SIZE ,
ProtocolError::WhitelistMaximumCapacityReached
);

Ok(())
}

fn calculate_expected_capacity(expected_whitelist_size: u16) -> u8 {
let pubkey_size = 32;
(expected_whitelist_size / pubkey_size) as u8
}

fn get_whitelist_size_from_length(length:u8) -> usize {
8 + mem::size_of::<Whitelist>() + (length as u64 * 32 as u64) as usize
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(length as u64 * 32 as u64) as usize can be simplified to length as usize * 32

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolved

2 changes: 0 additions & 2 deletions rfq/program/src/instructions/whitelist/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
pub mod add_address_to_whitelist;
pub mod cleanup_whitelist;
pub mod create_whitelist;
pub mod remove_address_from_whitelist;

This file was deleted.

20 changes: 3 additions & 17 deletions rfq/program/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub mod interfaces;
pub mod seeds;
pub mod state;
pub mod utils;
pub mod constants;

use instructions::collateral::fund_collateral::*;
use instructions::collateral::initialize_collateral::*;
Expand Down Expand Up @@ -45,10 +46,8 @@ use instructions::rfq::settle_one_party_default::*;
use instructions::rfq::settle_two_party_default::*;
use instructions::rfq::unlock_response_collateral::*;
use instructions::rfq::unlock_rfq_collateral::*;
use instructions::whitelist::add_address_to_whitelist::*;
use instructions::whitelist::cleanup_whitelist::*;
use instructions::whitelist::create_whitelist::*;
use instructions::whitelist::remove_address_from_whitelist::*;
use state::*;

security_txt! {
Expand Down Expand Up @@ -329,27 +328,14 @@ pub mod rfq {

pub fn create_whitelist<'info>(
ctx: Context<'_, '_, '_, 'info, CreateWhitelistAccounts<'info>>,
expected_whitelist_size: u16,
length:u8,
whitelist: Vec<Pubkey>,
) -> Result<()> {
create_whitelist_instruction(ctx, expected_whitelist_size, whitelist)
create_whitelist_instruction(ctx,length,whitelist)
}

pub fn clean_up_whitelist(ctx: Context<CleanUpWhitelistAccounts>) -> Result<()> {
clean_up_whitelist_instruction(ctx)
}

pub fn add_address_to_whitelist(
ctx: Context<AddAddressToWhitelistAccounts>,
address: Pubkey,
) -> Result<()> {
add_address_to_whitelist_instruction(ctx, address)
}

pub fn remove_address_from_whitelist(
ctx: Context<RemoveAddressToWhitelistAccounts>,
address: Pubkey,
) -> Result<()> {
remove_address_from_whitelist_instruction(ctx, address)
}
}
1 change: 0 additions & 1 deletion rfq/program/src/state/whitelist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use anchor_lang::prelude::*;
#[account]
pub struct Whitelist {
pub creator: Pubkey,
pub capacity: u8,
pub whitelist: Vec<Pubkey>,
}
Copy link
Collaborator

@EquilateralDelta EquilateralDelta Jan 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add an associated rfq field, which would work as follows:

  1. Default when the whitelist is created
  2. When rfq references a whitelist, this field is set(can't associate with two rfqs)
  3. Automatically clean up whitelist on the rfq cleanup
  4. An ix to clean up a whitelist if it isn't associated with an rfq(probably wouldn't be often used but for completeness)
    Please note that you can only use your own whitelist when associating it with an rfq

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolved


Expand Down
20 changes: 8 additions & 12 deletions tests/integration/spot.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -459,12 +459,10 @@ describe("RFQ Spot instrument integration tests", () => {

it("Create two-way RFQ with one spot leg add a whitelist of 3 addresses , respond ", async () => {
const whitelistKeypair = Keypair.generate();
let whitelist = await context.createWhitelist(
whitelistKeypair,
context.taker.publicKey,
[context.maker.publicKey, context.dao.publicKey],
10
);
let whitelist = await context.createWhitelist(whitelistKeypair, context.taker.publicKey, [
context.maker.publicKey,
context.dao.publicKey,
]);

// create a two way RFQ specifying 1 bitcoin as a leg
const rfq = await context.createRfq({
Expand All @@ -485,12 +483,10 @@ describe("RFQ Spot instrument integration tests", () => {
it("Create two-way RFQ with one spot leg add a whitelist of 3 addresses , respond but maker not in list ", async () => {
const whitelistKeypair = Keypair.generate();
const newPubkey = new PublicKey("2Jpwh3rvtHe2X67TxpAGEB4x751FNMwWzDyQHhBjqfKg");
let whitelist = await context.createWhitelist(
whitelistKeypair,
context.taker.publicKey,
[newPubkey, context.dao.publicKey],
10
);
let whitelist = await context.createWhitelist(whitelistKeypair, context.taker.publicKey, [
newPubkey,
context.dao.publicKey,
]);

// create a two way RFQ specifying 1 bitcoin as a leg
const rfq = await context.createRfq({
Expand Down
48 changes: 15 additions & 33 deletions tests/unit/createWhitelist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,49 +11,31 @@ describe("Create Whitelist", () => {
it("Create a whitelist", async () => {
const whitelistKeypair = Keypair.generate();

await context.createWhitelist(
whitelistKeypair,
context.taker.publicKey,
[context.maker.publicKey, context.dao.publicKey],
10
);
await context.createWhitelist(whitelistKeypair, context.taker.publicKey, [
context.maker.publicKey,
context.dao.publicKey,
]);
});

it("Add an address to whitelist", async () => {
const whitelistKeypair = Keypair.generate();
const newPublickey = new PublicKey("Eyv3PBdmp5PUVzrT3orDVad8roBMK8au9nKBazZXkKtA");

const whitelist = await context.createWhitelist(
whitelistKeypair,
context.taker.publicKey,
[context.dao.publicKey, newPublickey],
50
);

await whitelist.addAddressToWhitelist(context.maker.publicKey);
});
it("Create a whitelist with MAX_WHITELIST_SIZE", async () => {
const pubkeys: PublicKey[] = [];

it("remove an address from whitelist", async () => {
for (let i = 0; i < 20; i++) {
const keypair = Keypair.generate();
pubkeys.push(keypair.publicKey);
}
const whitelistKeypair = Keypair.generate();

const whitelist = await context.createWhitelist(
whitelistKeypair,
context.taker.publicKey,
[context.maker.publicKey, context.dao.publicKey],
10
);
await whitelist.removeAddressFromWhitelist(context.maker.publicKey);
await context.createWhitelist(whitelistKeypair, context.taker.publicKey, [...pubkeys]);
});

it("clean up", async () => {
const whitelistKeypair = Keypair.generate();

const whitelist = await context.createWhitelist(
whitelistKeypair,
context.taker.publicKey,
[context.maker.publicKey, context.dao.publicKey],
10
);
const whitelist = await context.createWhitelist(whitelistKeypair, context.taker.publicKey, [
context.maker.publicKey,
context.dao.publicKey,
]);

await whitelist.cleanUp();
});
Expand Down
4 changes: 0 additions & 4 deletions tests/utilities/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,3 @@ export function addPubkeyExplanations(context: Context, input: string) {

return result;
}

export function calculateWhitelistSize(expectedWhitelistCapacity: number) {
return 32 * expectedWhitelistCapacity;
}
Loading
Loading