Move the asset/liab weights to the Bank account
This commit is contained in:
parent
8f5becc0d5
commit
2ef2424ac5
|
@ -3,10 +3,13 @@ use anchor_spl::token::Mint;
|
||||||
use anchor_spl::token::Token;
|
use anchor_spl::token::Token;
|
||||||
use anchor_spl::token::TokenAccount;
|
use anchor_spl::token::TokenAccount;
|
||||||
use fixed::types::I80F48;
|
use fixed::types::I80F48;
|
||||||
|
use fixed_macro::types::I80F48;
|
||||||
|
|
||||||
use crate::error::*;
|
use crate::error::*;
|
||||||
use crate::state::*;
|
use crate::state::*;
|
||||||
|
|
||||||
|
const INDEX_START: I80F48 = I80F48!(1_000_000);
|
||||||
|
|
||||||
#[derive(Accounts)]
|
#[derive(Accounts)]
|
||||||
pub struct RegisterToken<'info> {
|
pub struct RegisterToken<'info> {
|
||||||
#[account(
|
#[account(
|
||||||
|
@ -55,9 +58,6 @@ pub fn register_token(
|
||||||
maint_liab_weight: f32,
|
maint_liab_weight: f32,
|
||||||
init_liab_weight: f32,
|
init_liab_weight: f32,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let mut bank = ctx.accounts.bank.load_init()?;
|
|
||||||
bank.initialize();
|
|
||||||
|
|
||||||
let mut group = ctx.accounts.group.load_mut()?;
|
let mut group = ctx.accounts.group.load_mut()?;
|
||||||
// TODO: Error if mint is already configured (techincally, init of vault will fail)
|
// TODO: Error if mint is already configured (techincally, init of vault will fail)
|
||||||
// TOOD: Error type
|
// TOOD: Error type
|
||||||
|
@ -71,13 +71,23 @@ pub fn register_token(
|
||||||
group.tokens.infos[token_index] = TokenInfo {
|
group.tokens.infos[token_index] = TokenInfo {
|
||||||
mint: ctx.accounts.mint.key(),
|
mint: ctx.accounts.mint.key(),
|
||||||
decimals,
|
decimals,
|
||||||
maint_asset_weight: I80F48::from_num(maint_asset_weight),
|
|
||||||
init_asset_weight: I80F48::from_num(init_asset_weight),
|
|
||||||
maint_liab_weight: I80F48::from_num(maint_liab_weight),
|
|
||||||
init_liab_weight: I80F48::from_num(init_liab_weight),
|
|
||||||
bank_bump: *ctx.bumps.get("bank").ok_or(MangoError::SomeError)?, // TODO: error
|
bank_bump: *ctx.bumps.get("bank").ok_or(MangoError::SomeError)?, // TODO: error
|
||||||
vault_bump: *ctx.bumps.get("vault").ok_or(MangoError::SomeError)?, // TODO: error
|
vault_bump: *ctx.bumps.get("vault").ok_or(MangoError::SomeError)?, // TODO: error
|
||||||
reserved: [0u8; 30],
|
reserved: [0u8; 30],
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let mut bank = ctx.accounts.bank.load_init()?;
|
||||||
|
*bank = TokenBank {
|
||||||
|
deposit_index: INDEX_START,
|
||||||
|
borrow_index: INDEX_START,
|
||||||
|
indexed_total_deposits: I80F48::ZERO,
|
||||||
|
indexed_total_borrows: I80F48::ZERO,
|
||||||
|
maint_asset_weight: I80F48::from_num(maint_asset_weight),
|
||||||
|
init_asset_weight: I80F48::from_num(init_asset_weight),
|
||||||
|
maint_liab_weight: I80F48::from_num(maint_liab_weight),
|
||||||
|
init_liab_weight: I80F48::from_num(init_liab_weight),
|
||||||
|
token_index: token_index as TokenIndex,
|
||||||
|
};
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ use anchor_lang::prelude::*;
|
||||||
use anchor_spl::token;
|
use anchor_spl::token;
|
||||||
use anchor_spl::token::Token;
|
use anchor_spl::token::Token;
|
||||||
use anchor_spl::token::TokenAccount;
|
use anchor_spl::token::TokenAccount;
|
||||||
|
use fixed::types::I80F48;
|
||||||
|
|
||||||
use crate::error::*;
|
use crate::error::*;
|
||||||
use crate::state::*;
|
use crate::state::*;
|
||||||
|
@ -100,3 +101,14 @@ pub fn withdraw(ctx: Context<Withdraw>, amount: u64, allow_borrow: bool) -> Resu
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
fn health(account: &MangoAccount, group: &MangoGroup) -> (I80F48, I80F48) {
|
||||||
|
let mut assets = I80F48::ZERO;
|
||||||
|
let mut liabilities = I80F48::ZERO;
|
||||||
|
|
||||||
|
for indexed_pos in account.indexed_positions.values {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
|
@ -13,12 +13,6 @@ pub struct TokenInfo {
|
||||||
pub bank_bump: u8,
|
pub bank_bump: u8,
|
||||||
pub vault_bump: u8,
|
pub vault_bump: u8,
|
||||||
|
|
||||||
// This is a _lot_ of bytes (64)
|
|
||||||
pub maint_asset_weight: I80F48,
|
|
||||||
pub init_asset_weight: I80F48,
|
|
||||||
pub maint_liab_weight: I80F48,
|
|
||||||
pub init_liab_weight: I80F48,
|
|
||||||
|
|
||||||
// TODO: store oracle index here?
|
// TODO: store oracle index here?
|
||||||
pub reserved: [u8; 30], // TODO: size?
|
pub reserved: [u8; 30], // TODO: size?
|
||||||
// token's bank account is a PDA
|
// token's bank account is a PDA
|
||||||
|
@ -33,8 +27,8 @@ impl TokenInfo {
|
||||||
|
|
||||||
#[zero_copy]
|
#[zero_copy]
|
||||||
pub struct Tokens {
|
pub struct Tokens {
|
||||||
// TODO: With TokenInfo > 100 bytes, we can have < 100 tokens max due to the 10kb limit
|
// TODO: If TokenInfo is 70 bytes, we can have < 142 tokens max due to the 10kb limit
|
||||||
// We could make large accounts not be PDAs, or hope for resize()
|
// We could make large accounts not be PDAs, hope for resize(), or store tokeninfo itself in a pda?
|
||||||
pub infos: [TokenInfo; MAX_TOKENS],
|
pub infos: [TokenInfo; MAX_TOKENS],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,7 @@
|
||||||
use anchor_lang::prelude::*;
|
use anchor_lang::prelude::*;
|
||||||
use fixed::types::I80F48;
|
use fixed::types::I80F48;
|
||||||
use fixed_macro::types::I80F48;
|
|
||||||
|
|
||||||
use super::IndexedPosition;
|
use super::{IndexedPosition, TokenIndex};
|
||||||
|
|
||||||
const INDEX_START: I80F48 = I80F48!(1_000_000);
|
|
||||||
|
|
||||||
#[account(zero_copy)]
|
#[account(zero_copy)]
|
||||||
pub struct TokenBank {
|
pub struct TokenBank {
|
||||||
|
@ -20,14 +17,20 @@ pub struct TokenBank {
|
||||||
// pub optimal_util: I80F48,
|
// pub optimal_util: I80F48,
|
||||||
// pub optimal_rate: I80F48,
|
// pub optimal_rate: I80F48,
|
||||||
// pub max_rate: I80F48,
|
// pub max_rate: I80F48,
|
||||||
|
|
||||||
|
// This is a _lot_ of bytes (64) - seems unnecessary
|
||||||
|
// (could maybe store them in one byte each, as an informal U1F7?
|
||||||
|
// that could store values between 0-2 and converting to I80F48 would be a cheap expand+shift)
|
||||||
|
pub maint_asset_weight: I80F48,
|
||||||
|
pub init_asset_weight: I80F48,
|
||||||
|
pub maint_liab_weight: I80F48,
|
||||||
|
pub init_liab_weight: I80F48,
|
||||||
|
|
||||||
|
// Index into TokenInfo on the group
|
||||||
|
pub token_index: TokenIndex,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TokenBank {
|
impl TokenBank {
|
||||||
pub fn initialize(&mut self) {
|
|
||||||
self.deposit_index = INDEX_START;
|
|
||||||
self.borrow_index = INDEX_START;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn native_total_deposits(&self) -> I80F48 {
|
pub fn native_total_deposits(&self) -> I80F48 {
|
||||||
self.deposit_index * self.indexed_total_deposits
|
self.deposit_index * self.indexed_total_deposits
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue