Move the asset/liab weights to the Bank account

This commit is contained in:
Christian Kamm 2022-02-26 12:04:42 +01:00
parent 8f5becc0d5
commit 2ef2424ac5
4 changed files with 43 additions and 24 deletions

View File

@ -3,10 +3,13 @@ use anchor_spl::token::Mint;
use anchor_spl::token::Token;
use anchor_spl::token::TokenAccount;
use fixed::types::I80F48;
use fixed_macro::types::I80F48;
use crate::error::*;
use crate::state::*;
const INDEX_START: I80F48 = I80F48!(1_000_000);
#[derive(Accounts)]
pub struct RegisterToken<'info> {
#[account(
@ -55,9 +58,6 @@ pub fn register_token(
maint_liab_weight: f32,
init_liab_weight: f32,
) -> Result<()> {
let mut bank = ctx.accounts.bank.load_init()?;
bank.initialize();
let mut group = ctx.accounts.group.load_mut()?;
// TODO: Error if mint is already configured (techincally, init of vault will fail)
// TOOD: Error type
@ -71,13 +71,23 @@ pub fn register_token(
group.tokens.infos[token_index] = TokenInfo {
mint: ctx.accounts.mint.key(),
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
vault_bump: *ctx.bumps.get("vault").ok_or(MangoError::SomeError)?, // TODO: error
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(())
}

View File

@ -2,6 +2,7 @@ use anchor_lang::prelude::*;
use anchor_spl::token;
use anchor_spl::token::Token;
use anchor_spl::token::TokenAccount;
use fixed::types::I80F48;
use crate::error::*;
use crate::state::*;
@ -100,3 +101,14 @@ pub fn withdraw(ctx: Context<Withdraw>, amount: u64, allow_borrow: bool) -> Resu
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 {
}
}
*/

View File

@ -13,12 +13,6 @@ pub struct TokenInfo {
pub bank_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?
pub reserved: [u8; 30], // TODO: size?
// token's bank account is a PDA
@ -33,8 +27,8 @@ impl TokenInfo {
#[zero_copy]
pub struct Tokens {
// TODO: With TokenInfo > 100 bytes, we can have < 100 tokens max due to the 10kb limit
// We could make large accounts not be PDAs, or hope for resize()
// 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, hope for resize(), or store tokeninfo itself in a pda?
pub infos: [TokenInfo; MAX_TOKENS],
}

View File

@ -1,10 +1,7 @@
use anchor_lang::prelude::*;
use fixed::types::I80F48;
use fixed_macro::types::I80F48;
use super::IndexedPosition;
const INDEX_START: I80F48 = I80F48!(1_000_000);
use super::{IndexedPosition, TokenIndex};
#[account(zero_copy)]
pub struct TokenBank {
@ -20,14 +17,20 @@ pub struct TokenBank {
// pub optimal_util: I80F48,
// pub optimal_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 {
pub fn initialize(&mut self) {
self.deposit_index = INDEX_START;
self.borrow_index = INDEX_START;
}
pub fn native_total_deposits(&self) -> I80F48 {
self.deposit_index * self.indexed_total_deposits
}