Account: Change type of net_deposts / net_settled

Because f32 doesn't have enough significant digits for large deposits.
This commit is contained in:
Christian Kamm 2022-08-15 09:50:22 +02:00
parent 18857c7b72
commit 0265b9b0a9
5 changed files with 27 additions and 31 deletions

View File

@ -75,8 +75,8 @@ pub fn token_deposit(ctx: Context<TokenDeposit>, amount: u64) -> Result<()> {
retriever.bank_and_oracle(&ctx.accounts.group.key(), active_token_index, token_index)?;
// Update the net deposits - adjust by price so different tokens are on the same basis (in USD terms)
account.fixed.net_deposits +=
cm!(amount_i80f48 * oracle_price * QUOTE_NATIVE_TO_UI).to_num::<f32>();
let amount_usd = cm!(amount_i80f48 * oracle_price).to_num::<i64>();
account.fixed.net_deposits = cm!(account.fixed.net_deposits + amount_usd);
emit!(TokenBalanceLog {
mango_group: ctx.accounts.group.key(),

View File

@ -48,10 +48,6 @@ impl<'info> TokenWithdraw<'info> {
}
}
// TODO: It may make sense to have the token_index passed in from the outside.
// That would save a lot of computation that needs to go into finding the
// right index for the mint.
// TODO: https://github.com/blockworks-foundation/mango-v4/commit/15961ec81c7e9324b37d79d0e2a1650ce6bd981d comments
pub fn token_withdraw(ctx: Context<TokenWithdraw>, amount: u64, allow_borrow: bool) -> Result<()> {
require_msg!(amount > 0, "withdraw amount must be positive");
@ -120,8 +116,8 @@ pub fn token_withdraw(ctx: Context<TokenWithdraw>, amount: u64, allow_borrow: bo
retriever.bank_and_oracle(&ctx.accounts.group.key(), active_token_index, token_index)?;
// Update the net deposits - adjust by price so different tokens are on the same basis (in USD terms)
account.fixed.net_deposits -=
cm!(amount_i80f48 * oracle_price * QUOTE_NATIVE_TO_UI).to_num::<f32>();
let amount_usd = cm!(amount_i80f48 * oracle_price).to_num::<i64>();
account.fixed.net_deposits = cm!(account.fixed.net_deposits - amount_usd);
emit!(TokenBalanceLog {
mango_group: ctx.accounts.group.key(),

View File

@ -62,13 +62,13 @@ pub struct MangoAccount {
// Cumulative (deposits - withdraws)
// using USD prices at the time of the deposit/withdraw
// in UI USD units
pub net_deposits: f32,
// in USD units with 6 decimals
pub net_deposits: i64,
// Cumulative settles on perp positions
// TODO: unimplemented
pub net_settled: f32,
pub net_settled: i64,
pub reserved: [u8; 256],
pub reserved: [u8; 248],
// dynamic
pub header_version: u8,
@ -100,9 +100,9 @@ impl Default for MangoAccount {
account_num: 0,
bump: 0,
padding: Default::default(),
net_deposits: 0.0,
net_settled: 0.0,
reserved: [0; 256],
net_deposits: 0,
net_settled: 0,
reserved: [0; 248],
header_version: DEFAULT_MANGO_ACCOUNT_VERSION,
padding0: Default::default(),
padding1: Default::default(),
@ -183,7 +183,7 @@ fn test_dynamic_offsets() {
}
// Mango Account fixed part for easy zero copy deserialization
#[derive(Copy, Clone, bytemuck::Zeroable, bytemuck::Pod)]
#[derive(Copy, Clone)]
#[repr(C)]
pub struct MangoAccountFixed {
pub group: Pubkey,
@ -195,13 +195,16 @@ pub struct MangoAccountFixed {
padding2: u8,
pub bump: u8,
pub padding: [u8; 1],
pub net_deposits: f32,
pub net_settled: f32,
pub reserved: [u8; 256],
pub net_deposits: i64,
pub net_settled: i64,
pub reserved: [u8; 248],
}
const_assert_eq!(size_of::<MangoAccountFixed>(), 32 * 4 + 8 + 2 * 4 + 256);
const_assert_eq!(size_of::<MangoAccountFixed>(), 32 * 4 + 8 + 2 * 8 + 248);
const_assert_eq!(size_of::<MangoAccountFixed>() % 8, 0);
unsafe impl bytemuck::Pod for MangoAccountFixed {}
unsafe impl bytemuck::Zeroable for MangoAccountFixed {}
impl MangoAccountFixed {
pub fn name(&self) -> &str {
std::str::from_utf8(&self.name)

View File

@ -105,10 +105,7 @@ async fn test_basic() -> Result<(), TransportError> {
let account_data: MangoAccount = solana.get_account(account).await;
// Assumes oracle price of 1
assert_eq!(
account_data.net_deposits,
(I80F48::from_num(deposit_amount) * QUOTE_NATIVE_TO_UI).to_num::<f32>()
);
assert_eq!(account_data.net_deposits, deposit_amount as i64);
}
//
@ -165,7 +162,7 @@ async fn test_basic() -> Result<(), TransportError> {
// Assumes oracle price of 1
assert_eq!(
account_data.net_deposits,
(I80F48::from_num(start_amount - withdraw_amount) * QUOTE_NATIVE_TO_UI).to_num::<f32>()
(start_amount - withdraw_amount) as i64
);
}

View File

@ -3021,18 +3021,18 @@ export type MangoV4 = {
},
{
"name": "netDeposits",
"type": "f32"
"type": "i64"
},
{
"name": "netSettled",
"type": "f32"
"type": "i64"
},
{
"name": "reserved",
"type": {
"array": [
"u8",
256
248
]
}
},
@ -8057,18 +8057,18 @@ export const IDL: MangoV4 = {
},
{
"name": "netDeposits",
"type": "f32"
"type": "i64"
},
{
"name": "netSettled",
"type": "f32"
"type": "i64"
},
{
"name": "reserved",
"type": {
"array": [
"u8",
256
248
]
}
},