use crate::generated::types::RoundState;
use crate::generated::types::TileStake;
use borsh::BorshSerialize;
use borsh::BorshDeserialize;
#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
pub struct Round {
pub discriminator: [u8; 8],
pub version: u16,
pub bump: u8,
pub id: u32,
pub state: RoundState,
pub blockhash_entropy: [u8; 32],
pub winning_tile: Option<u8>,
pub deployed_pending_usd_amount: u64,
pub deployed_usd_amount: u64,
pub deployed_btc_amount: u64,
pub deployed_usd_on_winning_tile_amount: u64,
pub miners_count: u32,
pub revealed_miners_count: u32,
pub winners_count: u32,
pub settled_miners_count: u32,
pub strike_bonus_usd: u64,
pub strike_bonus_btc: u64,
pub public_tile_stakes: [TileStake; 21],
pub deploy_entropy_acc: [u8; 32],
pub settled_at_slot: u64,
pub pending_epoch_fee_usd_amount: u64,
pub pending_one_btc_fee_usd_amount: u64,
pub pending_protocol_fee_usd_amount: u64,
pub reserved: [u8; 40],
}
pub const ROUND_DISCRIMINATOR: [u8; 8] = [87, 127, 165, 51, 73, 78, 116, 174];
impl Round {
#[inline(always)]
pub fn from_bytes(data: &[u8]) -> Result<Self, std::io::Error> {
let mut data = data;
Self::deserialize(&mut data)
}
}
impl<'a> TryFrom<&solana_account_info::AccountInfo<'a>> for Round {
type Error = std::io::Error;
fn try_from(account_info: &solana_account_info::AccountInfo<'a>) -> Result<Self, Self::Error> {
let mut data: &[u8] = &(*account_info.data).borrow();
Self::deserialize(&mut data)
}
}
#[cfg(feature = "fetch")]
pub fn fetch_round(
rpc: &solana_rpc_client::rpc_client::RpcClient,
address: &solana_address::Address,
) -> Result<crate::shared::DecodedAccount<Round>, std::io::Error> {
let accounts = fetch_all_round(rpc, &[*address])?;
Ok(accounts[0].clone())
}
#[cfg(feature = "fetch")]
pub fn fetch_all_round(
rpc: &solana_rpc_client::rpc_client::RpcClient,
addresses: &[solana_address::Address],
) -> Result<Vec<crate::shared::DecodedAccount<Round>>, std::io::Error> {
let accounts = rpc.get_multiple_accounts(addresses)
.map_err(|e| std::io::Error::other(e.to_string()))?;
let mut decoded_accounts: Vec<crate::shared::DecodedAccount<Round>> = Vec::new();
for i in 0..addresses.len() {
let address = addresses[i];
let account = accounts[i].as_ref()
.ok_or(std::io::Error::other(format!("Account not found: {address}")))?;
let data = Round::from_bytes(&account.data)?;
decoded_accounts.push(crate::shared::DecodedAccount { address, account: account.clone(), data });
}
Ok(decoded_accounts)
}
#[cfg(feature = "fetch")]
pub fn fetch_maybe_round(
rpc: &solana_rpc_client::rpc_client::RpcClient,
address: &solana_address::Address,
) -> Result<crate::shared::MaybeAccount<Round>, std::io::Error> {
let accounts = fetch_all_maybe_round(rpc, &[*address])?;
Ok(accounts[0].clone())
}
#[cfg(feature = "fetch")]
pub fn fetch_all_maybe_round(
rpc: &solana_rpc_client::rpc_client::RpcClient,
addresses: &[solana_address::Address],
) -> Result<Vec<crate::shared::MaybeAccount<Round>>, std::io::Error> {
let accounts = rpc.get_multiple_accounts(addresses)
.map_err(|e| std::io::Error::other(e.to_string()))?;
let mut decoded_accounts: Vec<crate::shared::MaybeAccount<Round>> = Vec::new();
for i in 0..addresses.len() {
let address = addresses[i];
if let Some(account) = accounts[i].as_ref() {
let data = Round::from_bytes(&account.data)?;
decoded_accounts.push(crate::shared::MaybeAccount::Exists(crate::shared::DecodedAccount { address, account: account.clone(), data }));
} else {
decoded_accounts.push(crate::shared::MaybeAccount::NotFound(address));
}
}
Ok(decoded_accounts)
}
#[cfg(feature = "anchor")]
impl anchor_lang::AccountDeserialize for Round {
fn try_deserialize_unchecked(buf: &mut &[u8]) -> anchor_lang::Result<Self> {
Ok(Self::deserialize(buf)?)
}
}
#[cfg(feature = "anchor")]
impl anchor_lang::AccountSerialize for Round {}
#[cfg(feature = "anchor")]
impl anchor_lang::Owner for Round {
fn owner() -> anchor_lang::solana_program::pubkey::Pubkey {
anchor_lang::solana_program::pubkey::Pubkey::from(
crate::SATRUSH_ID.to_bytes()
)
}
}
#[cfg(feature = "anchor-idl-build")]
impl anchor_lang::IdlBuild for Round {}
#[cfg(feature = "anchor-idl-build")]
impl anchor_lang::Discriminator for Round {
const DISCRIMINATOR: &[u8] = &[0; 8];
}