Update funding for keeper

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
This commit is contained in:
microwavedcola1 2022-05-17 15:59:47 +02:00
parent 7cd81f42b2
commit 6ecd4a0da9
9 changed files with 165 additions and 6 deletions

View File

@ -1,6 +1,6 @@
use std::sync::Arc;
use crate::{consume_events, update_index, MangoClient};
use crate::{consume_events, update_index, MangoClient, update_funding};
use anyhow::ensure;
@ -71,9 +71,17 @@ pub async fn runner(
})
.collect::<Vec<_>>();
let handles3 = perp_markets
.iter()
.map(|(pk, perp_market)| {
update_funding::loop_blocking(mango_client.clone(), *pk, *perp_market)
})
.collect::<Vec<_>>();
futures::join!(
futures::future::join_all(handles1),
futures::future::join_all(handles2),
futures::future::join_all(handles3),
debugging_handle
);

View File

@ -1,5 +1,6 @@
mod consume_events;
mod crank;
mod update_funding;
mod update_index;
use std::env;

View File

@ -0,0 +1,62 @@
use std::{sync::Arc, time::Duration};
use mango_v4::state::{PerpMarket};
use solana_sdk::{
instruction::{Instruction},
pubkey::Pubkey,
};
use tokio::time;
use crate::MangoClient;
pub async fn loop_blocking(mango_client: Arc<MangoClient>, pk: Pubkey, perp_market: PerpMarket) {
let mut interval = time::interval(Duration::from_secs(5));
loop {
interval.tick().await;
let client = mango_client.clone();
tokio::task::spawn_blocking(move || {
perform_operation(client, pk, perp_market).expect("Something went wrong here...");
})
.await
.expect("Something went wrong here...");
}
}
pub fn perform_operation(
mango_client: Arc<MangoClient>,
pk: Pubkey,
perp_market: PerpMarket,
) -> anyhow::Result<()> {
let sig_result = mango_client
.program()
.request()
.instruction(Instruction {
program_id: mango_v4::id(),
accounts: anchor_lang::ToAccountMetas::to_account_metas(
&mango_v4::accounts::PerpUpdateFunding {
perp_market: pk,
asks: perp_market.asks,
bids: perp_market.bids,
oracle: perp_market.oracle,
},
None,
),
data: anchor_lang::InstructionData::data(&mango_v4::instruction::PerpUpdateFunding {}),
})
.send();
match sig_result {
Ok(sig) => {
log::info!(
"Crank: update funding for perp_market {:?} ix signature: {:?}",
format!("{: >6}", perp_market.name()),
sig
);
}
Err(e) => log::error!("Crank: {:?}", e),
}
Ok(())
}

View File

@ -41,7 +41,7 @@ pub fn perform_operation(
Ok(sig) => {
log::info!(
"Crank: update_index for bank {:?} ix signature: {:?}",
format!("{: >6}", bank.name()),
bank.name(),
sig
);
}

View File

@ -21,7 +21,7 @@ pub use serum3_place_order::*;
pub use serum3_register_market::*;
pub use serum3_settle_funds::*;
pub use set_stub_oracle::*;
pub use update_funding::*;
pub use perp_update_funding::*;
pub use update_index::*;
pub use withdraw::*;
@ -48,6 +48,6 @@ mod serum3_place_order;
mod serum3_register_market;
mod serum3_settle_funds;
mod set_stub_oracle;
mod update_funding;
mod perp_update_funding;
mod update_index;
mod withdraw;

View File

@ -3,7 +3,7 @@ use anchor_lang::prelude::*;
use crate::state::{oracle_price, Book, PerpMarket};
#[derive(Accounts)]
pub struct UpdateFunding<'info> {
pub struct PerpUpdateFunding<'info> {
#[account(
mut,
has_one = bids,
@ -18,7 +18,7 @@ pub struct UpdateFunding<'info> {
pub oracle: UncheckedAccount<'info>,
}
pub fn update_funding(ctx: Context<UpdateFunding>) -> Result<()> {
pub fn perp_update_funding(ctx: Context<PerpUpdateFunding>) -> Result<()> {
// TODO: should we enforce a minimum window between 2 update_funding ix calls?
let now_ts = Clock::get()?.unix_timestamp;

View File

@ -284,6 +284,10 @@ pub mod mango_v4 {
instructions::perp_consume_events(ctx, limit)
}
pub fn perp_update_funding(ctx: Context<PerpUpdateFunding>) -> Result<()> {
instructions::perp_update_funding(ctx)
}
// TODO
// perp_force_cancel_order

View File

@ -1552,6 +1552,38 @@ impl ClientInstruction for PerpConsumeEventsInstruction {
}
}
pub struct PerpUpdateFundingInstruction {
pub perp_market: Pubkey,
pub bids: Pubkey,
pub asks: Pubkey,
pub oracle: Pubkey,
}
#[async_trait::async_trait(?Send)]
impl ClientInstruction for PerpUpdateFundingInstruction {
type Accounts = mango_v4::accounts::PerpUpdateFunding;
type Instruction = mango_v4::instruction::PerpUpdateFunding;
async fn to_instruction(
&self,
_loader: impl ClientAccountLoader + 'async_trait,
) -> (Self::Accounts, instruction::Instruction) {
let program_id = mango_v4::id();
let instruction = Self::Instruction {};
let accounts = Self::Accounts {
perp_market: self.perp_market,
bids: self.bids,
asks: self.asks,
oracle: self.oracle,
};
let instruction = make_instruction(program_id, &accounts, instruction);
(accounts, instruction)
}
fn signers(&self) -> Vec<&Keypair> {
vec![]
}
}
pub struct BenchmarkInstruction {}
#[async_trait::async_trait(?Send)]
impl ClientInstruction for BenchmarkInstruction {

View File

@ -1528,6 +1528,32 @@ export type MangoV4 = {
}
]
},
{
"name": "perpUpdateFunding",
"accounts": [
{
"name": "perpMarket",
"isMut": true,
"isSigner": false
},
{
"name": "asks",
"isMut": true,
"isSigner": false
},
{
"name": "bids",
"isMut": true,
"isSigner": false
},
{
"name": "oracle",
"isMut": false,
"isSigner": false
}
],
"args": []
},
{
"name": "benchmark",
"accounts": [],
@ -4320,6 +4346,32 @@ export const IDL: MangoV4 = {
}
]
},
{
"name": "perpUpdateFunding",
"accounts": [
{
"name": "perpMarket",
"isMut": true,
"isSigner": false
},
{
"name": "asks",
"isMut": true,
"isSigner": false
},
{
"name": "bids",
"isMut": true,
"isSigner": false
},
{
"name": "oracle",
"isMut": false,
"isSigner": false
}
],
"args": []
},
{
"name": "benchmark",
"accounts": [],