From 2ef2424ac53ca0a009daf910c2113b76d44b3420 Mon Sep 17 00:00:00 2001 From: Christian Kamm Date: Sat, 26 Feb 2022 12:04:42 +0100 Subject: [PATCH] Move the asset/liab weights to the Bank account --- .../src/instructions/register_token.rs | 24 +++++++++++++------ .../mango-v4/src/instructions/withdraw.rs | 12 ++++++++++ programs/mango-v4/src/state/mango_group.rs | 10 ++------ programs/mango-v4/src/state/token_bank.rs | 21 +++++++++------- 4 files changed, 43 insertions(+), 24 deletions(-) diff --git a/programs/mango-v4/src/instructions/register_token.rs b/programs/mango-v4/src/instructions/register_token.rs index f194bc3d0..b38f40483 100644 --- a/programs/mango-v4/src/instructions/register_token.rs +++ b/programs/mango-v4/src/instructions/register_token.rs @@ -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(()) } diff --git a/programs/mango-v4/src/instructions/withdraw.rs b/programs/mango-v4/src/instructions/withdraw.rs index 9c434886b..7bd90a9f2 100644 --- a/programs/mango-v4/src/instructions/withdraw.rs +++ b/programs/mango-v4/src/instructions/withdraw.rs @@ -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, 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 { + + } +} +*/ \ No newline at end of file diff --git a/programs/mango-v4/src/state/mango_group.rs b/programs/mango-v4/src/state/mango_group.rs index 5bee09d01..477844cca 100644 --- a/programs/mango-v4/src/state/mango_group.rs +++ b/programs/mango-v4/src/state/mango_group.rs @@ -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], } diff --git a/programs/mango-v4/src/state/token_bank.rs b/programs/mango-v4/src/state/token_bank.rs index 0a258c1bd..859439f23 100644 --- a/programs/mango-v4/src/state/token_bank.rs +++ b/programs/mango-v4/src/state/token_bank.rs @@ -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 }