فهرست منبع

Update Codama to 1.2.4 and use Codama CLI (#34)

Loris Leiva 9 ماه پیش
والد
کامیت
c755dfa55a

تفاوت فایلی نمایش داده نمی شود زیرا این فایل بسیار بزرگ است
+ 4237 - 874
Cargo.lock


+ 3 - 0
clients/rust/Cargo.toml

@@ -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"]
 
@@ -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 }
 solana-program = "^2.1"
 thiserror = "^1.0"

+ 70 - 0
clients/rust/src/generated/accounts/nonce.rs

@@ -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()))?;
+    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 - 0
clients/rust/src/generated/errors/system.rs

@@ -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"
+    }
+}

+ 3 - 1
clients/rust/src/generated/instructions/advance_nonce_account.rs

@@ -9,6 +9,7 @@ use borsh::BorshDeserialize;
 use borsh::BorshSerialize;
 
 /// Accounts.
+#[derive(Debug)]
 pub struct AdvanceNonceAccount {
     pub nonce_account: solana_program::pubkey::Pubkey,
 
@@ -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,
 }

+ 3 - 1
clients/rust/src/generated/instructions/allocate.rs

@@ -9,6 +9,7 @@ use borsh::BorshDeserialize;
 use borsh::BorshSerialize;
 
 /// Accounts.
+#[derive(Debug)]
 pub struct Allocate {
     pub new_account: solana_program::pubkey::Pubkey,
 }
@@ -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,
 }

+ 3 - 1
clients/rust/src/generated/instructions/allocate_with_seed.rs

@@ -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,
 
@@ -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,
 }

+ 3 - 1
clients/rust/src/generated/instructions/assign.rs

@@ -10,6 +10,7 @@ use borsh::BorshSerialize;
 use solana_program::pubkey::Pubkey;
 
 /// Accounts.
+#[derive(Debug)]
 pub struct Assign {
     pub account: solana_program::pubkey::Pubkey,
 }
@@ -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,
 }

+ 3 - 1
clients/rust/src/generated/instructions/assign_with_seed.rs

@@ -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,
 
@@ -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,
 }

+ 3 - 1
clients/rust/src/generated/instructions/authorize_nonce_account.rs

@@ -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,
 
@@ -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,
 }

+ 3 - 1
clients/rust/src/generated/instructions/create_account.rs

@@ -10,6 +10,7 @@ use borsh::BorshSerialize;
 use solana_program::pubkey::Pubkey;
 
 /// Accounts.
+#[derive(Debug)]
 pub struct CreateAccount {
     pub payer: solana_program::pubkey::Pubkey,
 
@@ -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,
 }

+ 3 - 1
clients/rust/src/generated/instructions/create_account_with_seed.rs

@@ -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,
 
@@ -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,
 }

+ 3 - 1
clients/rust/src/generated/instructions/initialize_nonce_account.rs

@@ -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,
 
@@ -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,
 }

+ 3 - 1
clients/rust/src/generated/instructions/transfer_sol.rs

@@ -9,6 +9,7 @@ use borsh::BorshDeserialize;
 use borsh::BorshSerialize;
 
 /// Accounts.
+#[derive(Debug)]
 pub struct TransferSol {
     pub source: solana_program::pubkey::Pubkey,
 
@@ -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,
 }

+ 3 - 1
clients/rust/src/generated/instructions/transfer_sol_with_seed.rs

@@ -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,
 
@@ -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,
 }

+ 3 - 1
clients/rust/src/generated/instructions/upgrade_nonce_account.rs

@@ -9,6 +9,7 @@ use borsh::BorshDeserialize;
 use borsh::BorshSerialize;
 
 /// Accounts.
+#[derive(Debug)]
 pub struct UpgradeNonceAccount {
     pub nonce_account: solana_program::pubkey::Pubkey,
 }
@@ -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,
 }

+ 3 - 1
clients/rust/src/generated/instructions/withdraw_nonce_account.rs

@@ -9,6 +9,7 @@ use borsh::BorshDeserialize;
 use borsh::BorshSerialize;
 
 /// Accounts.
+#[derive(Debug)]
 pub struct WithdrawNonceAccount {
     pub nonce_account: solana_program::pubkey::Pubkey,
 
@@ -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,
 }

+ 1 - 0
clients/rust/src/generated/mod.rs

@@ -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 - 0
clients/rust/src/generated/shared.rs

@@ -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 - 0
codama.mjs

@@ -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,
+        },
+      ],
+    },
+  },
+};

+ 4 - 4
package.json

@@ -4,7 +4,7 @@
     "solana:check": "tsx ./scripts/helpers/check-solana-version.mts",
     "solana:link": "tsx ./scripts/helpers/link-solana-version.mts",
     "generate": "pnpm generate:clients",
-    "generate:clients": "tsx ./scripts/helpers/generate-clients.mts",
+    "generate:clients": "codama run --all",
     "validator:start": "tsx ./scripts/helpers/start-validator.mts",
     "validator:restart": "pnpm validator:start --restart",
     "validator:stop": "tsx ./scripts/helpers/stop-validator.mts",
@@ -30,10 +30,10 @@
     "template:upgrade": "tsx ./scripts/helpers/upgrade-template.ts"
   },
   "devDependencies": {
-    "@codama/renderers-js": "^1.1.0",
-    "@codama/renderers-rust": "^1.0.4",
+    "@codama/renderers-js": "^1.2.3",
+    "@codama/renderers-rust": "^1.0.12",
     "@iarna/toml": "^2.2.5",
-    "codama": "^1.1.0",
+    "codama": "^1.2.4",
     "tsx": "^4.19.2",
     "typescript": "^5.5.2",
     "zx": "^7.2.3"

تفاوت فایلی نمایش داده نمی شود زیرا این فایل بسیار بزرگ است
+ 445 - 184
pnpm-lock.yaml


+ 0 - 33
scripts/helpers/generate-clients.mts

@@ -1,33 +0,0 @@
-#!/usr/bin/env zx
-import 'zx/globals';
-import { createFromRoot } from 'codama';
-import { renderVisitor as renderJavaScriptVisitor } from '@codama/renderers-js';
-import { renderVisitor as renderRustVisitor } from '@codama/renderers-rust';
-import { workingDirectory } from './utils.mts';
-
-// Instanciate Codama.
-const idl = JSON.parse(
-  fs.readFileSync(path.join(workingDirectory, 'program', 'idl.json'), 'utf-8')
-);
-const codama = createFromRoot(idl);
-
-// Render JavaScript.
-const jsClient = path.join(workingDirectory, 'clients', 'js');
-const prettierOptions = JSON.parse(
-  fs.readFileSync(path.join(jsClient, '.prettierrc.json'), 'utf-8')
-);
-codama.accept(
-  renderJavaScriptVisitor(path.join(jsClient, 'src', 'generated'), {
-    prettierOptions,
-  })
-);
-
-// Render Rust.
-const rustClient = path.join(workingDirectory, 'clients', 'rust');
-codama.accept(
-  renderRustVisitor(path.join(rustClient, 'src', 'generated'), {
-    formatCode: true,
-    crateFolder: rustClient,
-    anchorTraits: false,
-  })
-);

برخی فایل ها در این مقایسه diff نمایش داده نمی شوند زیرا تعداد فایل ها بسیار زیاد است