keeper token rate update - group multiple ixs into one (#151)

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
This commit is contained in:
microwavedcola1 2022-08-08 13:40:33 +02:00 committed by GitHub
parent 4f72985f2d
commit 24c359ef92
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 69 additions and 51 deletions

1
Cargo.lock generated
View File

@ -2919,6 +2919,7 @@ dependencies = [
"fixed", "fixed",
"fixed-macro", "fixed-macro",
"futures 0.3.21", "futures 0.3.21",
"itertools 0.10.3",
"log 0.4.17", "log 0.4.17",
"mango-v4", "mango-v4",
"pyth-sdk-solana", "pyth-sdk-solana",

View File

@ -1,4 +0,0 @@
RPC_URL=https://mango.devnet.rpcpool.com
PAYER_KEYPAIR=~/.config/solana/mango-devnet.json
GROUP_FROM_ADMIN_KEYPAIR=~/.config/solana/admin.json
MANGO_ACCOUNT_NAME=Account

View File

@ -1,4 +0,0 @@
RPC_URL=https://mango.rpcpool.com/
PAYER_KEYPAIR=~/.config/solana/mango-mainnet.json
GROUP=grouppubkey
MANGO_ACCOUNT_NAME=Account

View File

@ -17,6 +17,7 @@ env_logger = "0.8.4"
fixed = { version = "=1.11.0", features = ["serde", "borsh"] } fixed = { version = "=1.11.0", features = ["serde", "borsh"] }
fixed-macro = "^1.1.1" fixed-macro = "^1.1.1"
futures = "0.3.21" futures = "0.3.21"
itertools = "0.10.3"
log = "0.4.0" log = "0.4.0"
mango-v4 = { path = "../programs/mango-v4", features = ["no-entrypoint", "client"] } mango-v4 = { path = "../programs/mango-v4", features = ["no-entrypoint", "client"] }
pyth-sdk-solana = "0.1.0" pyth-sdk-solana = "0.1.0"

View File

@ -1,6 +1,7 @@
use std::{sync::Arc, time::Duration}; use std::{sync::Arc, time::Duration};
use crate::MangoClient; use crate::MangoClient;
use itertools::Itertools;
use anchor_lang::{__private::bytemuck::cast_ref, solana_program}; use anchor_lang::{__private::bytemuck::cast_ref, solana_program};
use client::prettify_client_error; use client::prettify_client_error;
@ -22,7 +23,16 @@ pub async fn runner(
.context .context
.tokens .tokens
.keys() .keys()
.map(|&token_index| loop_update_index_and_rate(mango_client.clone(), token_index)) // TokenUpdateIndexAndRate is known to take max 71k cu
// from cargo test-bpf local tests
.chunks(15)
.into_iter()
.map(|chunk| {
loop_update_index_and_rate(
mango_client.clone(),
chunk.copied().collect::<Vec<TokenIndex>>(),
)
})
.collect::<Vec<_>>(); .collect::<Vec<_>>();
let handles2 = mango_client let handles2 = mango_client
@ -49,58 +59,65 @@ pub async fn runner(
Ok(()) Ok(())
} }
pub async fn loop_update_index_and_rate(mango_client: Arc<MangoClient>, token_index: TokenIndex) { pub async fn loop_update_index_and_rate(
mango_client: Arc<MangoClient>,
token_indices: Vec<TokenIndex>,
) {
let mut interval = time::interval(Duration::from_secs(5)); let mut interval = time::interval(Duration::from_secs(5));
loop { loop {
interval.tick().await; interval.tick().await;
let client = mango_client.clone(); let client = mango_client.clone();
let res = tokio::task::spawn_blocking(move || -> anyhow::Result<()> { let token_indices_clone = token_indices.clone();
let token = client.context.token(token_index);
let banks_for_a_token = token.mint_info.banks();
let token_name = &token.name;
let oracle = token.mint_info.oracle;
let sig_result = client let res = tokio::task::spawn_blocking(move || -> anyhow::Result<()> {
.program() let token_names = token_indices_clone
.request() .iter()
.instruction({ .map(|token_index| client.context.token(*token_index).name.to_owned())
let mut ix = Instruction { .join(", ");
program_id: mango_v4::id(),
accounts: anchor_lang::ToAccountMetas::to_account_metas( let program = client.program();
&mango_v4::accounts::TokenUpdateIndexAndRate { let mut req = program.request();
group: token.mint_info.group, for token_index in token_indices_clone.iter() {
mint_info: token.mint_info_address, let token = client.context.token(*token_index);
oracle, let banks_for_a_token = token.mint_info.banks();
instructions: solana_program::sysvar::instructions::id(), let oracle = token.mint_info.oracle;
},
None, let mut ix = Instruction {
), program_id: mango_v4::id(),
data: anchor_lang::InstructionData::data( accounts: anchor_lang::ToAccountMetas::to_account_metas(
&mango_v4::instruction::TokenUpdateIndexAndRate {}, &mango_v4::accounts::TokenUpdateIndexAndRate {
), group: token.mint_info.group,
}; mint_info: token.mint_info_address,
let mut banks = banks_for_a_token oracle,
.iter() instructions: solana_program::sysvar::instructions::id(),
.map(|bank_pubkey| AccountMeta { },
pubkey: *bank_pubkey, None,
is_signer: false, ),
is_writable: true, data: anchor_lang::InstructionData::data(
}) &mango_v4::instruction::TokenUpdateIndexAndRate {},
.collect::<Vec<_>>(); ),
ix.accounts.append(&mut banks); };
ix let mut banks = banks_for_a_token
}) .iter()
.send() .map(|bank_pubkey| AccountMeta {
.map_err(prettify_client_error); pubkey: *bank_pubkey,
is_signer: false,
is_writable: true,
})
.collect::<Vec<_>>();
ix.accounts.append(&mut banks);
req = req.instruction(ix);
}
let sig_result = req.send().map_err(prettify_client_error);
if let Err(e) = sig_result { if let Err(e) = sig_result {
log::error!("{:?}", e) log::error!("{:?}", e)
} else { } else {
log::info!( log::info!(
"update_index_and_rate {} {:?}", "update_index_and_rate {} {:?}",
token_name, token_names,
sig_result.unwrap() sig_result.unwrap()
) )
} }

View File

@ -76,7 +76,7 @@ fn main() -> Result<(), anyhow::Error> {
}; };
let mango_client = Arc::new(MangoClient::new_for_existing_account( let mango_client = Arc::new(MangoClient::new_for_existing_account(
Client::new(cluster, commitment, &owner, Some(Duration::from_secs(1))), Client::new(cluster, commitment, &owner, Some(Duration::from_secs(10))),
cli.mango_account, cli.mango_account,
owner, owner,
)?); )?);

View File

@ -96,7 +96,7 @@ async fn main() -> anyhow::Result<()> {
let rpc_url = cli.rpc_url; let rpc_url = cli.rpc_url;
let ws_url = rpc_url.replace("https", "wss"); let ws_url = rpc_url.replace("https", "wss");
let rpc_timeout = Duration::from_secs(1); let rpc_timeout = Duration::from_secs(10);
let cluster = Cluster::Custom(rpc_url.clone(), ws_url.clone()); let cluster = Cluster::Custom(rpc_url.clone(), ws_url.clone());
let commitment = CommitmentConfig::processed(); let commitment = CommitmentConfig::processed();
let client = Client::new(cluster.clone(), commitment, &liqor_owner, Some(rpc_timeout)); let client = Client::new(cluster.clone(), commitment, &liqor_owner, Some(rpc_timeout));

View File

@ -200,7 +200,12 @@ async function main() {
console.log( console.log(
`Editing group, setting existing admin as fastListingAdmin to be able to add MNGO truslessly...`, `Editing group, setting existing admin as fastListingAdmin to be able to add MNGO truslessly...`,
); );
await client.groupEdit(group, group.admin, group.admin); let sig = await client.groupEdit(
group,
group.admin,
new PublicKey('Efhak3qj3MiyzgJr3cUUqXXz5wr3oYHt9sPzuqJf9eBN'),
);
console.log(`sig https://explorer.solana.com/tx/${sig}?cluster=devnet`);
console.log(`Registering MNGO...`); console.log(`Registering MNGO...`);
const mngoDevnetMint = new PublicKey(DEVNET_MINTS.get('MNGO')!); const mngoDevnetMint = new PublicKey(DEVNET_MINTS.get('MNGO')!);
const mngoDevnetOracle = new PublicKey(DEVNET_ORACLES.get('MNGO')!); const mngoDevnetOracle = new PublicKey(DEVNET_ORACLES.get('MNGO')!);
@ -296,6 +301,7 @@ async function main() {
'USDC', 'USDC',
btcDevnetOracle, btcDevnetOracle,
0.1, 0.1,
undefined,
0.01, 0.01,
0.3, 0.3,
0.08, 0.08,
@ -323,6 +329,7 @@ async function main() {
'USDC', 'USDC',
usdcDevnetOracle.publicKey, usdcDevnetOracle.publicKey,
0.1, 0.1,
undefined,
0.01, 0.01,
0.4, 0.4,
0.07, 0.07,