From 787014e8a68cd9d0199dd3b40b3dc2e65f23cf1f Mon Sep 17 00:00:00 2001 From: Hardhat Chad Date: Mon, 9 Jun 2025 08:25:56 -0700 Subject: [PATCH] pda --- cli/src/main.rs | 18 +++++++++++++----- ore/api/src/state/block.rs | 8 ++++++++ ore/api/src/state/config.rs | 8 ++++++++ ore/api/src/state/miner.rs | 8 ++++++++ ore/api/src/state/treasury.rs | 8 ++++++++ 5 files changed, 45 insertions(+), 5 deletions(-) diff --git a/cli/src/main.rs b/cli/src/main.rs index 53e1330..83aa06c 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -12,6 +12,7 @@ use solana_sdk::{ signature::{read_keypair_file, Signer}, transaction::Transaction, }; +use spl_token::amount_to_ui_amount; use steel::{AccountDeserialize, Clock, Discriminator}; #[tokio::main] @@ -87,13 +88,17 @@ fn print_block(block: Block) { println!(" Id: {:?}", block.id); println!(" Start slot: {:?}", block.start_slot); println!(" Best miner: {:?}", block.best_miner); - println!(" Reward: {:?}", block.reward); + println!( + " Reward: {:?}", + amount_to_ui_amount(block.reward, TOKEN_DECIMALS) + ); println!(" Slot hash: {:?}", block.slot_hash); println!(" Best hash: {:?}\n", block.best_hash); } async fn log_blocks(rpc: &RpcClient) -> Result<(), anyhow::Error> { - let blocks = get_blocks(&rpc).await?; + let mut blocks = get_blocks(&rpc).await?; + blocks.sort_by_key(|(_, block)| block.id); for (_, block) in blocks { print_block(block); } @@ -186,9 +191,12 @@ where Ok(accounts) => { let accounts = accounts .into_iter() - .map(|(pubkey, account)| { - let account = T::try_from_bytes(&account.data).unwrap().clone(); - (pubkey, account) + .filter_map(|(pubkey, account)| { + if let Ok(account) = T::try_from_bytes(&account.data) { + Some((pubkey, account.clone())) + } else { + None + } }) .collect(); Ok(accounts) diff --git a/ore/api/src/state/block.rs b/ore/api/src/state/block.rs index f057dd3..612b286 100644 --- a/ore/api/src/state/block.rs +++ b/ore/api/src/state/block.rs @@ -1,5 +1,7 @@ use steel::*; +use crate::state::block_pda; + use super::OreAccount; #[repr(C)] @@ -27,4 +29,10 @@ pub struct Block { pub total_hashes: u64, } +impl Block { + pub fn pda(&self) -> (Pubkey, u8) { + block_pda(self.id) + } +} + account!(OreAccount, Block); diff --git a/ore/api/src/state/config.rs b/ore/api/src/state/config.rs index 10e9c11..931f4d4 100644 --- a/ore/api/src/state/config.rs +++ b/ore/api/src/state/config.rs @@ -1,5 +1,7 @@ use steel::*; +use crate::state::config_pda; + use super::OreAccount; // TODO Config stuff @@ -8,4 +10,10 @@ use super::OreAccount; #[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)] pub struct Config {} +impl Config { + pub fn pda() -> (Pubkey, u8) { + config_pda() + } +} + account!(OreAccount, Config); diff --git a/ore/api/src/state/miner.rs b/ore/api/src/state/miner.rs index b8be78f..e6558eb 100644 --- a/ore/api/src/state/miner.rs +++ b/ore/api/src/state/miner.rs @@ -1,5 +1,7 @@ use steel::*; +use crate::state::miner_pda; + use super::OreAccount; #[repr(C)] @@ -21,4 +23,10 @@ pub struct Miner { pub total_rewards: u64, } +impl Miner { + pub fn pda(&self) -> (Pubkey, u8) { + miner_pda(self.authority) + } +} + account!(OreAccount, Miner); diff --git a/ore/api/src/state/treasury.rs b/ore/api/src/state/treasury.rs index 2268ae0..dad0b44 100644 --- a/ore/api/src/state/treasury.rs +++ b/ore/api/src/state/treasury.rs @@ -1,5 +1,7 @@ use steel::*; +use crate::state::treasury_pda; + use super::OreAccount; /// Treasury is a singleton account which is the mint authority for the ORE token and the authority of @@ -8,4 +10,10 @@ use super::OreAccount; #[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)] pub struct Treasury {} +impl Treasury { + pub fn pda() -> (Pubkey, u8) { + treasury_pda() + } +} + account!(OreAccount, Treasury);