Skip to content

Commit

Permalink
Group: Limit fast-listing tokens per week
Browse files Browse the repository at this point in the history
To avoid potential abuse of fast listing while even when the dao is
unresponsive.
  • Loading branch information
ckamm committed Oct 16, 2023
1 parent 8110dd1 commit 514e135
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 10 deletions.
26 changes: 24 additions & 2 deletions mango_v4.json
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,12 @@
"type": {
"option": "u64"
}
},
{
"name": "allowedFastListingsPerIntervalOpt",
"type": {
"option": "u16"
}
}
]
},
Expand Down Expand Up @@ -604,7 +610,7 @@
"accounts": [
{
"name": "group",
"isMut": false,
"isMut": true,
"isSigner": false
},
{
Expand Down Expand Up @@ -7144,12 +7150,28 @@
],
"type": "u64"
},
{
"name": "fastListingIntervalStart",
"docs": [
"Fast-listings are limited per week, this is the start of the current fast-listing interval",
"in seconds since epoch"
],
"type": "u64"
},
{
"name": "fastListingsInInterval",
"type": "u16"
},
{
"name": "allowedFastListingsPerInterval",
"type": "u16"
},
{
"name": "reserved",
"type": {
"array": [
"u8",
1824
1812
]
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ const FIRST_BANK_NUM: u32 = 0;
#[instruction(token_index: TokenIndex)]
pub struct TokenRegisterTrustless<'info> {
#[account(
constraint = group.load()?.admin == admin.key() || group.load()?.fast_listing_admin == admin.key() ,
mut,
constraint = group.load()?.admin == admin.key() || group.load()?.fast_listing_admin == admin.key(),
constraint = group.load()?.is_ix_enabled(IxGate::TokenRegisterTrustless) @ MangoError::IxIsDisabled,
)]
pub group: AccountLoader<'info, Group>,
Expand Down
10 changes: 10 additions & 0 deletions programs/mango-v4/src/instructions/group_edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub fn group_edit(
buyback_fees_swap_mango_account_opt: Option<Pubkey>,
mngo_token_index_opt: Option<TokenIndex>,
buyback_fees_expiry_interval_opt: Option<u64>,
allowed_fast_listings_per_interval_opt: Option<u16>,
) -> Result<()> {
let mut group = ctx.accounts.group.load_mut()?;

Expand Down Expand Up @@ -106,5 +107,14 @@ pub fn group_edit(
group.buyback_fees_expiry_interval = buyback_fees_expiry_interval;
}

if let Some(allowed_fast_listings_per_interval) = allowed_fast_listings_per_interval_opt {
msg!(
"Allowed fast listings per week old {:?}, new {:?}",
group.allowed_fast_listings_per_interval,
allowed_fast_listings_per_interval
);
group.allowed_fast_listings_per_interval = allowed_fast_listings_per_interval;
}

Ok(())
}
16 changes: 15 additions & 1 deletion programs/mango-v4/src/instructions/token_register_trustless.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,22 @@ pub fn token_register_trustless(
require_neq!(token_index, QUOTE_TOKEN_INDEX);
require_neq!(token_index, TokenIndex::MAX);

let net_borrow_limit_window_size_ts = 24 * 60 * 60u64;
let now_ts: u64 = Clock::get()?.unix_timestamp.try_into().unwrap();
{
let mut group = ctx.accounts.group.load_mut()?;
let week = 7 * 24 * 60 * 60;
if now_ts >= group.fast_listing_interval_start + week {
group.fast_listing_interval_start = now_ts / week * week;
group.fast_listings_in_interval = 0;
}
group.fast_listings_in_interval += 1;
require_gte!(
group.allowed_fast_listings_per_interval,
group.fast_listings_in_interval
);
}

let net_borrow_limit_window_size_ts = 24 * 60 * 60u64;

let mut bank = ctx.accounts.bank.load_init()?;
*bank = Bank {
Expand Down
2 changes: 2 additions & 0 deletions programs/mango-v4/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ pub mod mango_v4 {
buyback_fees_swap_mango_account_opt: Option<Pubkey>,
mngo_token_index_opt: Option<TokenIndex>,
buyback_fees_expiry_interval_opt: Option<u64>,
allowed_fast_listings_per_interval_opt: Option<u16>,
) -> Result<()> {
#[cfg(feature = "enable-gpl")]
instructions::group_edit(
Expand All @@ -97,6 +98,7 @@ pub mod mango_v4 {
buyback_fees_swap_mango_account_opt,
mngo_token_index_opt,
buyback_fees_expiry_interval_opt,
allowed_fast_listings_per_interval_opt,
)?;
Ok(())
}
Expand Down
11 changes: 9 additions & 2 deletions programs/mango-v4/src/state/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,18 @@ pub struct Group {
/// When set to 0, there's no expiry of buyback fees.
pub buyback_fees_expiry_interval: u64,

pub reserved: [u8; 1824],
/// Fast-listings are limited per week, this is the start of the current fast-listing interval
/// in seconds since epoch
pub fast_listing_interval_start: u64,

pub fast_listings_in_interval: u16,
pub allowed_fast_listings_per_interval: u16,

pub reserved: [u8; 1812],
}
const_assert_eq!(
size_of::<Group>(),
32 + 4 + 32 * 2 + 4 + 32 * 2 + 4 + 4 + 20 * 32 + 32 + 8 + 16 + 32 + 8 + 1824
32 + 4 + 32 * 2 + 4 + 32 * 2 + 4 + 4 + 20 * 32 + 32 + 8 + 16 + 32 + 8 + 8 + 2 * 2 + 1812
);
const_assert_eq!(size_of::<Group>(), 2736);
const_assert_eq!(size_of::<Group>() % 8, 0);
Expand Down
1 change: 1 addition & 0 deletions programs/mango-v4/tests/program_test/mango_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1787,6 +1787,7 @@ pub fn group_edit_instruction_default() -> mango_v4::instruction::GroupEdit {
buyback_fees_swap_mango_account_opt: None,
mngo_token_index_opt: None,
buyback_fees_expiry_interval_opt: None,
allowed_fast_listings_per_interval_opt: None,
}
}

Expand Down
2 changes: 2 additions & 0 deletions ts/client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ export class MangoClient {
feesSwapMangoAccount?: PublicKey,
feesMngoTokenIndex?: TokenIndex,
feesExpiryInterval?: BN,
allowedFastListingsPerInterval?: number,
): Promise<MangoSignatureStatus> {
const ix = await this.program.methods
.groupEdit(
Expand All @@ -275,6 +276,7 @@ export class MangoClient {
feesSwapMangoAccount ?? null,
feesMngoTokenIndex ?? null,
feesExpiryInterval ?? null,
allowedFastListingsPerInterval ?? null,
)
.accounts({
group: group.publicKey,
Expand Down
52 changes: 48 additions & 4 deletions ts/client/src/mango_v4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,12 @@ export type MangoV4 = {
"type": {
"option": "u64"
}
},
{
"name": "allowedFastListingsPerIntervalOpt",
"type": {
"option": "u16"
}
}
]
},
Expand Down Expand Up @@ -604,7 +610,7 @@ export type MangoV4 = {
"accounts": [
{
"name": "group",
"isMut": false,
"isMut": true,
"isSigner": false
},
{
Expand Down Expand Up @@ -7144,12 +7150,28 @@ export type MangoV4 = {
],
"type": "u64"
},
{
"name": "fastListingIntervalStart",
"docs": [
"Fast-listings are limited per week, this is the start of the current fast-listing interval",
"in seconds since epoch"
],
"type": "u64"
},
{
"name": "fastListingsInInterval",
"type": "u16"
},
{
"name": "allowedFastListingsPerInterval",
"type": "u16"
},
{
"name": "reserved",
"type": {
"array": [
"u8",
1824
1812
]
}
}
Expand Down Expand Up @@ -13374,6 +13396,12 @@ export const IDL: MangoV4 = {
"type": {
"option": "u64"
}
},
{
"name": "allowedFastListingsPerIntervalOpt",
"type": {
"option": "u16"
}
}
]
},
Expand Down Expand Up @@ -13707,7 +13735,7 @@ export const IDL: MangoV4 = {
"accounts": [
{
"name": "group",
"isMut": false,
"isMut": true,
"isSigner": false
},
{
Expand Down Expand Up @@ -20247,12 +20275,28 @@ export const IDL: MangoV4 = {
],
"type": "u64"
},
{
"name": "fastListingIntervalStart",
"docs": [
"Fast-listings are limited per week, this is the start of the current fast-listing interval",
"in seconds since epoch"
],
"type": "u64"
},
{
"name": "fastListingsInInterval",
"type": "u16"
},
{
"name": "allowedFastListingsPerInterval",
"type": "u16"
},
{
"name": "reserved",
"type": {
"array": [
"u8",
1824
1812
]
}
}
Expand Down

0 comments on commit 514e135

Please sign in to comment.