Add oracle price to update_index logging. (#89)

* Add oracle price to update_index logging.

* Update IDL with changes to update_index instruction and logging.
This commit is contained in:
Nicholas Clarke 2022-07-05 11:59:44 -07:00 committed by GitHub
parent d74cc78a84
commit 30fc7def77
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 63 additions and 27 deletions

View File

@ -58,7 +58,9 @@ pub async fn loop_update_index(mango_client: Arc<MangoClient>, token_index: Toke
let res = tokio::task::spawn_blocking(move || -> anyhow::Result<()> { let res = tokio::task::spawn_blocking(move || -> anyhow::Result<()> {
let mint_info = client.get_mint_info(&token_index); let mint_info = client.get_mint_info(&token_index);
let banks_for_a_token = client.banks_cache_by_token_index.get(&token_index).unwrap(); let banks_for_a_token = client.banks_cache_by_token_index.get(&token_index).unwrap();
let token_name = banks_for_a_token.get(0).unwrap().1.name(); let some_bank = banks_for_a_token.get(0).unwrap().1;
let token_name = some_bank.name();
let oracle = some_bank.oracle;
let bank_pubkeys_for_a_token = banks_for_a_token let bank_pubkeys_for_a_token = banks_for_a_token
.into_iter() .into_iter()
@ -72,7 +74,7 @@ pub async fn loop_update_index(mango_client: Arc<MangoClient>, token_index: Toke
let mut ix = Instruction { let mut ix = Instruction {
program_id: mango_v4::id(), program_id: mango_v4::id(),
accounts: anchor_lang::ToAccountMetas::to_account_metas( accounts: anchor_lang::ToAccountMetas::to_account_metas(
&mango_v4::accounts::UpdateIndex { mint_info }, &mango_v4::accounts::UpdateIndex { mint_info, oracle },
None, None,
), ),
data: anchor_lang::InstructionData::data( data: anchor_lang::InstructionData::data(

View File

@ -2,17 +2,21 @@ use anchor_lang::prelude::*;
use crate::logs::UpdateIndexLog; use crate::logs::UpdateIndexLog;
use crate::{ use crate::{
accounts_zerocopy::{LoadMutZeroCopyRef, LoadZeroCopyRef}, accounts_zerocopy::{AccountInfoRef, LoadMutZeroCopyRef, LoadZeroCopyRef},
state::{Bank, MintInfo}, state::{oracle_price, Bank, MintInfo},
}; };
use checked_math as cm; use checked_math as cm;
use fixed::types::I80F48; use fixed::types::I80F48;
#[derive(Accounts)] #[derive(Accounts)]
pub struct UpdateIndex<'info> { pub struct UpdateIndex<'info> {
pub mint_info: AccountLoader<'info, MintInfo>, pub mint_info: AccountLoader<'info, MintInfo>,
pub oracle: UncheckedAccount<'info>,
} }
pub fn update_index(ctx: Context<UpdateIndex>) -> Result<()> { pub fn update_index(ctx: Context<UpdateIndex>) -> Result<()> {
let mint_info = ctx.accounts.mint_info.load()?;
require_keys_eq!(mint_info.oracle.key(), ctx.accounts.oracle.key());
ctx.accounts ctx.accounts
.mint_info .mint_info
.load()? .load()?
@ -27,7 +31,7 @@ pub fn update_index(ctx: Context<UpdateIndex>) -> Result<()> {
} }
let now_ts = Clock::get()?.unix_timestamp; let now_ts = Clock::get()?.unix_timestamp;
let (diff_ts, deposit_index, borrow_index) = { let (diff_ts, deposit_index, borrow_index, oracle_conf_filter, base_token_decimals) = {
let mut some_bank = ctx.remaining_accounts[0].load_mut::<Bank>()?; let mut some_bank = ctx.remaining_accounts[0].load_mut::<Bank>()?;
// TODO: should we enforce a minimum window between 2 update_index ix calls? // TODO: should we enforce a minimum window between 2 update_index ix calls?
@ -36,7 +40,13 @@ pub fn update_index(ctx: Context<UpdateIndex>) -> Result<()> {
let (deposit_index, borrow_index) = let (deposit_index, borrow_index) =
some_bank.compute_index(indexed_total_deposits, indexed_total_borrows, diff_ts)?; some_bank.compute_index(indexed_total_deposits, indexed_total_borrows, diff_ts)?;
(diff_ts, deposit_index, borrow_index) (
diff_ts,
deposit_index,
borrow_index,
some_bank.oracle_config.conf_filter,
some_bank.mint_decimals,
)
}; };
msg!("indexed_total_deposits {}", indexed_total_deposits); msg!("indexed_total_deposits {}", indexed_total_deposits);
@ -56,16 +66,21 @@ pub fn update_index(ctx: Context<UpdateIndex>) -> Result<()> {
bank.deposit_index = deposit_index; bank.deposit_index = deposit_index;
bank.borrow_index = borrow_index; bank.borrow_index = borrow_index;
// clarkeni TODO: add prices
emit!(UpdateIndexLog {
mango_group: bank.group.key(),
token_index: bank.token_index,
deposit_index: bank.deposit_index.to_bits(),
borrow_index: bank.borrow_index.to_bits(),
// price: oracle_price.to_bits(),
});
} }
let price = oracle_price(
&AccountInfoRef::borrow(ctx.accounts.oracle.as_ref())?,
oracle_conf_filter,
base_token_decimals,
)?;
emit!(UpdateIndexLog {
mango_group: mint_info.group.key(),
token_index: mint_info.token_index,
deposit_index: deposit_index.to_bits(),
borrow_index: borrow_index.to_bits(),
price: price.to_bits(),
});
Ok(()) Ok(())
} }

View File

@ -132,7 +132,7 @@ pub struct UpdateIndexLog {
pub token_index: u16, pub token_index: u16,
pub deposit_index: i128, // I80F48 pub deposit_index: i128, // I80F48
pub borrow_index: i128, // I80F48 pub borrow_index: i128, // I80F48
// pub price: i128, // I80F48 pub price: i128, // I80F48
} }
#[event] #[event]

View File

@ -13,8 +13,6 @@ use solana_program::instruction::Instruction;
use solana_sdk::instruction; use solana_sdk::instruction;
use solana_sdk::signature::{Keypair, Signer}; use solana_sdk::signature::{Keypair, Signer};
use solana_sdk::transport::TransportError; use solana_sdk::transport::TransportError;
use spl_associated_token_account::get_associated_token_address;
use std::str::FromStr; use std::str::FromStr;
use std::sync::Arc; use std::sync::Arc;
@ -2433,6 +2431,7 @@ impl ClientInstruction for BenchmarkInstruction {
} }
pub struct UpdateIndexInstruction { pub struct UpdateIndexInstruction {
pub mint_info: Pubkey, pub mint_info: Pubkey,
pub oracle: Pubkey,
pub banks: Vec<Pubkey>, pub banks: Vec<Pubkey>,
} }
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
@ -2447,6 +2446,7 @@ impl ClientInstruction for UpdateIndexInstruction {
let instruction = Self::Instruction {}; let instruction = Self::Instruction {};
let accounts = Self::Accounts { let accounts = Self::Accounts {
mint_info: self.mint_info, mint_info: self.mint_info,
oracle: self.oracle,
}; };
let mut instruction = make_instruction(program_id, &accounts, instruction); let mut instruction = make_instruction(program_id, &accounts, instruction);

View File

@ -136,15 +136,15 @@ async fn test_basic() -> Result<(), TransportError> {
// TEST: Close account and de register bank // TEST: Close account and de register bank
// //
let mint_info: MintInfo = solana.get_account(tokens[0].mint_info).await;
// withdraw whatever is remaining, can't close bank vault without this // withdraw whatever is remaining, can't close bank vault without this
send_tx( send_tx(
solana, solana,
UpdateIndexInstruction { UpdateIndexInstruction {
mint_info: tokens[0].mint_info, mint_info: tokens[0].mint_info,
banks: { banks: mint_info.banks.to_vec(),
let mint_info: MintInfo = solana.get_account(tokens[0].mint_info).await; oracle: mint_info.oracle,
mint_info.banks.to_vec()
},
}, },
) )
.await .await

View File

@ -1,6 +1,5 @@
#![cfg(feature = "test-bpf")] #![cfg(feature = "test-bpf")]
use fixed::types::I80F48;
use solana_program_test::*; use solana_program_test::*;
use solana_sdk::{signature::Keypair, signature::Signer, transport::TransportError}; use solana_sdk::{signature::Keypair, signature::Signer, transport::TransportError};

View File

@ -100,14 +100,14 @@ async fn test_update_index() -> Result<(), TransportError> {
solana.advance_clock().await; solana.advance_clock().await;
let mint_info: MintInfo = solana.get_account(tokens[0].mint_info).await;
send_tx( send_tx(
solana, solana,
UpdateIndexInstruction { UpdateIndexInstruction {
mint_info: tokens[0].mint_info, mint_info: tokens[0].mint_info,
banks: { banks: mint_info.banks.to_vec(),
let mint_info: MintInfo = solana.get_account(tokens[0].mint_info).await; oracle: mint_info.oracle,
mint_info.banks.to_vec()
},
}, },
) )
.await .await

View File

@ -540,6 +540,11 @@ export type MangoV4 = {
"name": "mintInfo", "name": "mintInfo",
"isMut": false, "isMut": false,
"isSigner": false "isSigner": false
},
{
"name": "oracle",
"isMut": false,
"isSigner": false
} }
], ],
"args": [] "args": []
@ -4242,6 +4247,11 @@ export type MangoV4 = {
"name": "borrowIndex", "name": "borrowIndex",
"type": "i128", "type": "i128",
"index": false "index": false
},
{
"name": "price",
"type": "i128",
"index": false
} }
] ]
}, },
@ -4922,6 +4932,11 @@ export const IDL: MangoV4 = {
"name": "mintInfo", "name": "mintInfo",
"isMut": false, "isMut": false,
"isSigner": false "isSigner": false
},
{
"name": "oracle",
"isMut": false,
"isSigner": false
} }
], ],
"args": [] "args": []
@ -8624,6 +8639,11 @@ export const IDL: MangoV4 = {
"name": "borrowIndex", "name": "borrowIndex",
"type": "i128", "type": "i128",
"index": false "index": false
},
{
"name": "price",
"type": "i128",
"index": false
} }
] ]
}, },