Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5,401 changes: 4,517 additions & 884 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions clients/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ readme = "README.md"
license-file = "../../LICENSE"

[features]
fetch = ["dep:solana-client", "dep:solana-sdk"]
test-sbf = []
serde = ["dep:serde", "dep:serde_with", "kaigan/serde"]

Expand All @@ -18,5 +19,7 @@ num-derive = "^0.3"
num-traits = "^0.2"
serde = { version = "^1.0", features = ["derive"], optional = true }
serde_with = { version = "^3.0", optional = true }
solana-client = { version = "^2.1", optional = true }
solana-sdk = { version = "^2.1", optional = true }
Comment on lines +22 to +23
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that we are currently having to use these crates instead of the more granular ones to stay compatible with clients using Solana below 2.1.

We have documented a way forward on the Codama Rust renderer here.

This comment was marked as spam.

solana-program = "^2.1"
thiserror = "^1.0"
70 changes: 70 additions & 0 deletions clients/rust/src/generated/accounts/nonce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,73 @@ impl<'a> TryFrom<&solana_program::account_info::AccountInfo<'a>> for Nonce {
Self::deserialize(&mut data)
}
}

#[cfg(feature = "fetch")]
pub fn fetch_nonce(
rpc: &solana_client::rpc_client::RpcClient,
address: &Pubkey,
) -> Result<crate::shared::DecodedAccount<Nonce>, std::io::Error> {
let accounts = fetch_all_nonce(rpc, &[*address])?;
Ok(accounts[0].clone())
}

#[cfg(feature = "fetch")]
pub fn fetch_all_nonce(
rpc: &solana_client::rpc_client::RpcClient,
addresses: &[Pubkey],
) -> Result<Vec<crate::shared::DecodedAccount<Nonce>>, std::io::Error> {
let accounts = rpc
.get_multiple_accounts(&addresses)
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e.to_string()))?;
Comment on lines +67 to +69
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some RPCs meter the get_multiple_accounts call differently than the normal get_account_info, so it might be worth avoiding get_multiple_accounts for just one input

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah good to know. This is something that would needs updating on the Rust renderer (was introduced in this PR). I made an issue for it.

This comment was marked as spam.

let mut decoded_accounts: Vec<crate::shared::DecodedAccount<Nonce>> = Vec::new();
for i in 0..addresses.len() {
let address = addresses[i];
let account = accounts[i].as_ref().ok_or(std::io::Error::new(
std::io::ErrorKind::Other,
format!("Account not found: {}", address),
))?;
let data = Nonce::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_nonce(
rpc: &solana_client::rpc_client::RpcClient,
address: &Pubkey,
) -> Result<crate::shared::MaybeAccount<Nonce>, std::io::Error> {
let accounts = fetch_all_maybe_nonce(rpc, &[*address])?;
Ok(accounts[0].clone())
}

#[cfg(feature = "fetch")]
pub fn fetch_all_maybe_nonce(
rpc: &solana_client::rpc_client::RpcClient,
addresses: &[Pubkey],
) -> Result<Vec<crate::shared::MaybeAccount<Nonce>>, std::io::Error> {
let accounts = rpc
.get_multiple_accounts(&addresses)
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e.to_string()))?;
let mut decoded_accounts: Vec<crate::shared::MaybeAccount<Nonce>> = Vec::new();
for i in 0..addresses.len() {
let address = addresses[i];
if let Some(account) = accounts[i].as_ref() {
let data = Nonce::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)
}
6 changes: 6 additions & 0 deletions clients/rust/src/generated/errors/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,9 @@ impl solana_program::program_error::PrintProgramError for SystemError {
solana_program::msg!(&self.to_string());
}
}

impl<T> solana_program::decode_error::DecodeError<T> for SystemError {
fn type_of() -> &'static str {
"SystemError"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use borsh::BorshDeserialize;
use borsh::BorshSerialize;

/// Accounts.
#[derive(Debug)]
pub struct AdvanceNonceAccount {
pub nonce_account: solana_program::pubkey::Pubkey,

Expand Down Expand Up @@ -52,7 +53,8 @@ impl AdvanceNonceAccount {
}
}

#[derive(BorshDeserialize, BorshSerialize)]
#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct AdvanceNonceAccountInstructionData {
discriminator: u32,
}
Expand Down
4 changes: 3 additions & 1 deletion clients/rust/src/generated/instructions/allocate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use borsh::BorshDeserialize;
use borsh::BorshSerialize;

/// Accounts.
#[derive(Debug)]
pub struct Allocate {
pub new_account: solana_program::pubkey::Pubkey,
}
Expand Down Expand Up @@ -44,7 +45,8 @@ impl Allocate {
}
}

#[derive(BorshDeserialize, BorshSerialize)]
#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct AllocateInstructionData {
discriminator: u32,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use kaigan::types::U64PrefixString;
use solana_program::pubkey::Pubkey;

/// Accounts.
#[derive(Debug)]
pub struct AllocateWithSeed {
pub new_account: solana_program::pubkey::Pubkey,

Expand Down Expand Up @@ -52,7 +53,8 @@ impl AllocateWithSeed {
}
}

#[derive(BorshDeserialize, BorshSerialize)]
#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct AllocateWithSeedInstructionData {
discriminator: u32,
}
Expand Down
4 changes: 3 additions & 1 deletion clients/rust/src/generated/instructions/assign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use borsh::BorshSerialize;
use solana_program::pubkey::Pubkey;

/// Accounts.
#[derive(Debug)]
pub struct Assign {
pub account: solana_program::pubkey::Pubkey,
}
Expand Down Expand Up @@ -45,7 +46,8 @@ impl Assign {
}
}

#[derive(BorshDeserialize, BorshSerialize)]
#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct AssignInstructionData {
discriminator: u32,
}
Expand Down
4 changes: 3 additions & 1 deletion clients/rust/src/generated/instructions/assign_with_seed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use kaigan::types::U64PrefixString;
use solana_program::pubkey::Pubkey;

/// Accounts.
#[derive(Debug)]
pub struct AssignWithSeed {
pub account: solana_program::pubkey::Pubkey,

Expand Down Expand Up @@ -52,7 +53,8 @@ impl AssignWithSeed {
}
}

#[derive(BorshDeserialize, BorshSerialize)]
#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct AssignWithSeedInstructionData {
discriminator: u32,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use borsh::BorshSerialize;
use solana_program::pubkey::Pubkey;

/// Accounts.
#[derive(Debug)]
pub struct AuthorizeNonceAccount {
pub nonce_account: solana_program::pubkey::Pubkey,

Expand Down Expand Up @@ -53,7 +54,8 @@ impl AuthorizeNonceAccount {
}
}

#[derive(BorshDeserialize, BorshSerialize)]
#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct AuthorizeNonceAccountInstructionData {
discriminator: u32,
}
Expand Down
4 changes: 3 additions & 1 deletion clients/rust/src/generated/instructions/create_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use borsh::BorshSerialize;
use solana_program::pubkey::Pubkey;

/// Accounts.
#[derive(Debug)]
pub struct CreateAccount {
pub payer: solana_program::pubkey::Pubkey,

Expand Down Expand Up @@ -50,7 +51,8 @@ impl CreateAccount {
}
}

#[derive(BorshDeserialize, BorshSerialize)]
#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CreateAccountInstructionData {
discriminator: u32,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use kaigan::types::U64PrefixString;
use solana_program::pubkey::Pubkey;

/// Accounts.
#[derive(Debug)]
pub struct CreateAccountWithSeed {
pub payer: solana_program::pubkey::Pubkey,

Expand Down Expand Up @@ -59,7 +60,8 @@ impl CreateAccountWithSeed {
}
}

#[derive(BorshDeserialize, BorshSerialize)]
#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CreateAccountWithSeedInstructionData {
discriminator: u32,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use borsh::BorshSerialize;
use solana_program::pubkey::Pubkey;

/// Accounts.
#[derive(Debug)]
pub struct InitializeNonceAccount {
pub nonce_account: solana_program::pubkey::Pubkey,

Expand Down Expand Up @@ -59,7 +60,8 @@ impl InitializeNonceAccount {
}
}

#[derive(BorshDeserialize, BorshSerialize)]
#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct InitializeNonceAccountInstructionData {
discriminator: u32,
}
Expand Down
4 changes: 3 additions & 1 deletion clients/rust/src/generated/instructions/transfer_sol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use borsh::BorshDeserialize;
use borsh::BorshSerialize;

/// Accounts.
#[derive(Debug)]
pub struct TransferSol {
pub source: solana_program::pubkey::Pubkey,

Expand Down Expand Up @@ -50,7 +51,8 @@ impl TransferSol {
}
}

#[derive(BorshDeserialize, BorshSerialize)]
#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct TransferSolInstructionData {
discriminator: u32,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use kaigan::types::U64PrefixString;
use solana_program::pubkey::Pubkey;

/// Accounts.
#[derive(Debug)]
pub struct TransferSolWithSeed {
pub source: solana_program::pubkey::Pubkey,

Expand Down Expand Up @@ -60,7 +61,8 @@ impl TransferSolWithSeed {
}
}

#[derive(BorshDeserialize, BorshSerialize)]
#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct TransferSolWithSeedInstructionData {
discriminator: u32,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use borsh::BorshDeserialize;
use borsh::BorshSerialize;

/// Accounts.
#[derive(Debug)]
pub struct UpgradeNonceAccount {
pub nonce_account: solana_program::pubkey::Pubkey,
}
Expand Down Expand Up @@ -40,7 +41,8 @@ impl UpgradeNonceAccount {
}
}

#[derive(BorshDeserialize, BorshSerialize)]
#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct UpgradeNonceAccountInstructionData {
discriminator: u32,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use borsh::BorshDeserialize;
use borsh::BorshSerialize;

/// Accounts.
#[derive(Debug)]
pub struct WithdrawNonceAccount {
pub nonce_account: solana_program::pubkey::Pubkey,

Expand Down Expand Up @@ -70,7 +71,8 @@ impl WithdrawNonceAccount {
}
}

#[derive(BorshDeserialize, BorshSerialize)]
#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct WithdrawNonceAccountInstructionData {
discriminator: u32,
}
Expand Down
1 change: 1 addition & 0 deletions clients/rust/src/generated/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub mod accounts;
pub mod errors;
pub mod instructions;
pub mod programs;
pub mod shared;
pub mod types;

pub(crate) use programs::*;
21 changes: 21 additions & 0 deletions clients/rust/src/generated/shared.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//! This code was AUTOGENERATED using the codama library.
//! Please DO NOT EDIT THIS FILE, instead use visitors
//! to add features, then rerun codama to update it.
//!
//! <https://github.com/codama-idl/codama>
//!

#[cfg(feature = "fetch")]
#[derive(Debug, Clone)]
pub struct DecodedAccount<T> {
pub address: solana_program::pubkey::Pubkey,
pub account: solana_sdk::account::Account,
pub data: T,
}

#[cfg(feature = "fetch")]
#[derive(Debug, Clone)]
pub enum MaybeAccount<T> {
Exists(DecodedAccount<T>),
NotFound(solana_program::pubkey::Pubkey),
}
28 changes: 28 additions & 0 deletions codama.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import path from 'node:path';

const { default: prettierOptions } = await import(
path.resolve('clients', 'js', '.prettierrc.json'),
{ with: { type: 'json' } }
);

export default {
idl: 'program/idl.json',
before: [],
scripts: {
js: {
from: '@codama/renderers-js',
args: ['clients/js/src/generated', { prettierOptions }],
},
rust: {
from: '@codama/renderers-rust',
args: [
'clients/rust/src/generated',
{
anchorTraits: false,
crateFolder: 'clients/rust',
formatCode: true,
},
],
},
},
};
Loading