fixes from review

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
This commit is contained in:
microwavedcola1 2022-08-03 13:50:49 +02:00 committed by microwavedcola1
parent 59dd3aa66e
commit 505cbd48aa
7 changed files with 52 additions and 257 deletions

View File

@ -31,13 +31,14 @@ pub struct LiqTokenBankruptcy<'info> {
#[account(
has_one = group,
constraint = liab_mint_info.load()?.token_index == liab_token_index,
constraint = liab_mint_info.load()?.elligible_for_group_insurance_fund()
)]
pub liab_mint_info: AccountLoader<'info, MintInfo>,
#[account(mut)]
pub quote_vault: Account<'info, TokenAccount>,
// future: this would be an insurance fund vault specific to a
// trustless token, separate from the shared one on the group
#[account(mut)]
pub insurance_vault: Account<'info, TokenAccount>,
@ -105,12 +106,17 @@ pub fn liq_token_bankruptcy(
let liab_price_adjusted = cm!(liab_price * liab_fee_factor);
let liab_transfer_unrounded = remaining_liab_loss.min(max_liab_transfer);
let insurance_transfer = cm!(liab_transfer_unrounded * liab_price_adjusted)
.checked_ceil()
.unwrap()
.checked_to_num::<u64>()
.unwrap()
.min(ctx.accounts.insurance_vault.amount);
let insurance_transfer = if liab_mint_info.elligible_for_group_insurance_fund() {
cm!(liab_transfer_unrounded * liab_price_adjusted)
.checked_ceil()
.unwrap()
.checked_to_num::<u64>()
.unwrap()
.min(ctx.accounts.insurance_vault.amount)
} else {
0
};
let insurance_fund_exhausted = insurance_transfer == ctx.accounts.insurance_vault.amount;
let insurance_transfer_i80f48 = I80F48::from(insurance_transfer);
@ -170,7 +176,9 @@ pub fn liq_token_bankruptcy(
drop(account_retriever);
// Socialize loss
if insurance_fund_exhausted && remaining_liab_loss.is_positive() {
if (insurance_fund_exhausted || !liab_mint_info.elligible_for_group_insurance_fund())
&& remaining_liab_loss.is_positive()
{
// find the total deposits
let mut indexed_total_deposits = I80F48::ZERO;
for bank_ai in bank_ais.iter() {

View File

@ -10,7 +10,7 @@ use crate::util::fill16_from_str;
pub const INDEX_START: I80F48 = I80F48!(1_000_000);
#[derive(Accounts)]
#[instruction(token_index: TokenIndex, bank_num: u32)]
#[instruction(token_index: TokenIndex)]
pub struct TokenRegister<'info> {
#[account(
has_one = admin,
@ -23,7 +23,7 @@ pub struct TokenRegister<'info> {
#[account(
init,
// using the token_index in this seed guards against reusing it
seeds = [group.key().as_ref(), b"Bank".as_ref(), &token_index.to_le_bytes(), &bank_num.to_le_bytes()],
seeds = [group.key().as_ref(), b"Bank".as_ref(), &token_index.to_le_bytes(), &0u32.to_le_bytes()],
bump,
payer = payer,
space = 8 + std::mem::size_of::<Bank>(),
@ -32,7 +32,7 @@ pub struct TokenRegister<'info> {
#[account(
init,
seeds = [group.key().as_ref(), b"Vault".as_ref(), &token_index.to_le_bytes(), &bank_num.to_le_bytes()],
seeds = [group.key().as_ref(), b"Vault".as_ref(), &token_index.to_le_bytes(), &0u32.to_le_bytes()],
bump,
token::authority = group,
token::mint = mint,
@ -77,7 +77,6 @@ pub struct InterestRateParams {
pub fn token_register(
ctx: Context<TokenRegister>,
token_index: TokenIndex,
bank_num: u32,
name: String,
oracle_config: OracleConfig,
interest_rate_params: InterestRateParams,
@ -89,10 +88,6 @@ pub fn token_register(
init_liab_weight: f32,
liquidation_fee: f32,
) -> Result<()> {
// TODO: Error if mint is already configured (technically, init of vault will fail)
require_eq!(bank_num, 0);
// Require token 0 to be in the insurance token
if token_index == QUOTE_TOKEN_INDEX {
require_keys_eq!(

View File

@ -1,15 +1,14 @@
use anchor_lang::prelude::*;
use anchor_spl::token::{Mint, Token, TokenAccount};
use fixed::types::I80F48;
use fixed_macro::types::I80F48;
use crate::error::*;
use crate::instructions::{InterestRateParams, INDEX_START};
use crate::instructions::INDEX_START;
use crate::state::*;
use crate::util::fill16_from_str;
#[derive(Accounts)]
#[instruction(token_index: TokenIndex, bank_num: u32)]
#[instruction(token_index: TokenIndex)]
pub struct TokenRegisterTrustless<'info> {
#[account(
has_one = fast_listing_admin,
@ -22,7 +21,7 @@ pub struct TokenRegisterTrustless<'info> {
#[account(
init,
// using the token_index in this seed guards against reusing it
seeds = [group.key().as_ref(), b"Bank".as_ref(), &token_index.to_le_bytes(), &bank_num.to_le_bytes()],
seeds = [group.key().as_ref(), b"Bank".as_ref(), &token_index.to_le_bytes(), &0u32.to_le_bytes()],
bump,
payer = payer,
space = 8 + std::mem::size_of::<Bank>(),
@ -31,7 +30,7 @@ pub struct TokenRegisterTrustless<'info> {
#[account(
init,
seeds = [group.key().as_ref(), b"Vault".as_ref(), &token_index.to_le_bytes(), &bank_num.to_le_bytes()],
seeds = [group.key().as_ref(), b"Vault".as_ref(), &token_index.to_le_bytes(), &0u32.to_le_bytes()],
bump,
token::authority = group,
token::mint = mint,
@ -64,10 +63,8 @@ pub struct TokenRegisterTrustless<'info> {
pub fn token_register_trustless(
ctx: Context<TokenRegisterTrustless>,
token_index: TokenIndex,
bank_num: u32,
name: String,
) -> Result<()> {
require_eq!(bank_num, 0);
require_neq!(token_index, 0);
let mut bank = ctx.accounts.bank.load_init()?;
@ -89,20 +86,20 @@ pub fn token_register_trustless(
index_last_updated: Clock::get()?.unix_timestamp,
bank_rate_last_updated: Clock::get()?.unix_timestamp,
avg_utilization: I80F48::ZERO,
adjustment_factor: I80F48::from_num(0.001),
util0: I80F48::from_num(0.6),
rate0: I80F48::from_num(0.15),
adjustment_factor: I80F48::from_num(0.02),
util0: I80F48::from_num(0.7),
rate0: I80F48::from_num(0.1),
util1: I80F48::from_num(0.8),
rate1: I80F48::from_num(0.95),
max_rate: I80F48::from_num(3.0),
rate1: I80F48::from_num(0.2),
max_rate: I80F48::from_num(2.0),
collected_fees_native: I80F48::ZERO,
loan_origination_fee_rate: I80F48::from_num(0.005),
loan_origination_fee_rate: I80F48::from_num(0.001),
loan_fee_rate: I80F48::from_num(0.005),
maint_asset_weight: I80F48::from_num(0),
init_asset_weight: I80F48::from_num(0),
maint_liab_weight: I80F48::from_num(1.4),
init_liab_weight: I80F48::from_num(1.5),
liquidation_fee: I80F48::from_num(0.2),
maint_liab_weight: I80F48::from_num(1.25),
init_liab_weight: I80F48::from_num(2.0),
liquidation_fee: I80F48::from_num(0.125),
dust: I80F48::ZERO,
flash_loan_vault_initial: u64::MAX,
flash_loan_approved_amount: 0,

View File

@ -57,7 +57,6 @@ pub mod mango_v4 {
pub fn token_register(
ctx: Context<TokenRegister>,
token_index: TokenIndex,
bank_num: u32,
name: String,
oracle_config: OracleConfig,
interest_rate_params: InterestRateParams,
@ -72,7 +71,6 @@ pub mod mango_v4 {
instructions::token_register(
ctx,
token_index,
bank_num,
name,
oracle_config,
interest_rate_params,
@ -89,10 +87,9 @@ pub mod mango_v4 {
pub fn token_register_trustless(
ctx: Context<TokenRegisterTrustless>,
token_index: TokenIndex,
bank_num: u32,
name: String,
) -> Result<()> {
instructions::token_register_trustless(ctx, token_index, bank_num, name)
instructions::token_register_trustless(ctx, token_index, name)
}
#[allow(clippy::too_many_arguments)]

View File

@ -611,7 +611,6 @@ impl<'keypair> ClientInstruction for TokenRegisterInstruction<'keypair> {
self.token_index.to_string()
),
token_index: self.token_index,
bank_num: 0,
oracle_config: OracleConfig {
conf_filter: I80F48::from_num::<f32>(0.10),
},

View File

@ -180,7 +180,6 @@ export class MangoClient {
return await this.program.methods
.tokenRegister(
tokenIndex,
0,
name,
{
confFilter: {
@ -215,7 +214,7 @@ export class MangoClient {
name: string,
): Promise<TransactionSignature> {
return await this.program.methods
.tokenRegisterTrustless(tokenIndex, 0, name)
.tokenRegisterTrustless(tokenIndex, name)
.accounts({
group: group.publicKey,
fastListingAdmin: (this.program.provider as AnchorProvider).wallet

View File

@ -171,60 +171,12 @@ export type MangoV4 = {
{
"name": "bank",
"isMut": true,
"isSigner": false,
"pda": {
"seeds": [
{
"kind": "account",
"type": "publicKey",
"path": "group"
},
{
"kind": "const",
"type": "string",
"value": "Bank"
},
{
"kind": "arg",
"type": "u16",
"path": "token_index"
},
{
"kind": "arg",
"type": "u32",
"path": "bank_num"
}
]
}
"isSigner": false
},
{
"name": "vault",
"isMut": true,
"isSigner": false,
"pda": {
"seeds": [
{
"kind": "account",
"type": "publicKey",
"path": "group"
},
{
"kind": "const",
"type": "string",
"value": "Vault"
},
{
"kind": "arg",
"type": "u16",
"path": "token_index"
},
{
"kind": "arg",
"type": "u32",
"path": "bank_num"
}
]
}
"isSigner": false
},
{
"name": "mintInfo",
@ -282,10 +234,6 @@ export type MangoV4 = {
"name": "tokenIndex",
"type": "u16"
},
{
"name": "bankNum",
"type": "u32"
},
{
"name": "name",
"type": "string"
@ -353,60 +301,12 @@ export type MangoV4 = {
{
"name": "bank",
"isMut": true,
"isSigner": false,
"pda": {
"seeds": [
{
"kind": "account",
"type": "publicKey",
"path": "group"
},
{
"kind": "const",
"type": "string",
"value": "Bank"
},
{
"kind": "arg",
"type": "u16",
"path": "token_index"
},
{
"kind": "arg",
"type": "u32",
"path": "bank_num"
}
]
}
"isSigner": false
},
{
"name": "vault",
"isMut": true,
"isSigner": false,
"pda": {
"seeds": [
{
"kind": "account",
"type": "publicKey",
"path": "group"
},
{
"kind": "const",
"type": "string",
"value": "Vault"
},
{
"kind": "arg",
"type": "u16",
"path": "token_index"
},
{
"kind": "arg",
"type": "u32",
"path": "bank_num"
}
]
}
"isSigner": false
},
{
"name": "mintInfo",
@ -464,10 +364,6 @@ export type MangoV4 = {
"name": "tokenIndex",
"type": "u16"
},
{
"name": "bankNum",
"type": "u32"
},
{
"name": "name",
"type": "string"
@ -3124,12 +3020,16 @@ export type MangoV4 = {
"name": "registrationTime",
"type": "i64"
},
{
"name": "groupInsuranceFund",
"type": "u8"
},
{
"name": "reserved",
"type": {
"array": [
"u8",
256
255
]
}
}
@ -5179,60 +5079,12 @@ export const IDL: MangoV4 = {
{
"name": "bank",
"isMut": true,
"isSigner": false,
"pda": {
"seeds": [
{
"kind": "account",
"type": "publicKey",
"path": "group"
},
{
"kind": "const",
"type": "string",
"value": "Bank"
},
{
"kind": "arg",
"type": "u16",
"path": "token_index"
},
{
"kind": "arg",
"type": "u32",
"path": "bank_num"
}
]
}
"isSigner": false
},
{
"name": "vault",
"isMut": true,
"isSigner": false,
"pda": {
"seeds": [
{
"kind": "account",
"type": "publicKey",
"path": "group"
},
{
"kind": "const",
"type": "string",
"value": "Vault"
},
{
"kind": "arg",
"type": "u16",
"path": "token_index"
},
{
"kind": "arg",
"type": "u32",
"path": "bank_num"
}
]
}
"isSigner": false
},
{
"name": "mintInfo",
@ -5290,10 +5142,6 @@ export const IDL: MangoV4 = {
"name": "tokenIndex",
"type": "u16"
},
{
"name": "bankNum",
"type": "u32"
},
{
"name": "name",
"type": "string"
@ -5361,60 +5209,12 @@ export const IDL: MangoV4 = {
{
"name": "bank",
"isMut": true,
"isSigner": false,
"pda": {
"seeds": [
{
"kind": "account",
"type": "publicKey",
"path": "group"
},
{
"kind": "const",
"type": "string",
"value": "Bank"
},
{
"kind": "arg",
"type": "u16",
"path": "token_index"
},
{
"kind": "arg",
"type": "u32",
"path": "bank_num"
}
]
}
"isSigner": false
},
{
"name": "vault",
"isMut": true,
"isSigner": false,
"pda": {
"seeds": [
{
"kind": "account",
"type": "publicKey",
"path": "group"
},
{
"kind": "const",
"type": "string",
"value": "Vault"
},
{
"kind": "arg",
"type": "u16",
"path": "token_index"
},
{
"kind": "arg",
"type": "u32",
"path": "bank_num"
}
]
}
"isSigner": false
},
{
"name": "mintInfo",
@ -5472,10 +5272,6 @@ export const IDL: MangoV4 = {
"name": "tokenIndex",
"type": "u16"
},
{
"name": "bankNum",
"type": "u32"
},
{
"name": "name",
"type": "string"
@ -8132,12 +7928,16 @@ export const IDL: MangoV4 = {
"name": "registrationTime",
"type": "i64"
},
{
"name": "groupInsuranceFund",
"type": "u8"
},
{
"name": "reserved",
"type": {
"array": [
"u8",
256
255
]
}
}