Browse Source

Update Codama to 1.2.2 and use Codama CLI

This PR updates Codama and its renderers to the latest versions.

It also uses the new Codama CLI to render the clients of the System program.
Loris Leiva 9 tháng trước cách đây
mục cha
commit
c7b494072d

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

@@ -49,3 +49,98 @@ impl<'a> TryFrom<&solana_program::account_info::AccountInfo<'a>> for Nonce {
         Self::deserialize(&mut data)
         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)
+}
+
+#[cfg(feature = "anchor")]
+impl anchor_lang::AccountDeserialize for Nonce {
+    fn try_deserialize_unchecked(buf: &mut &[u8]) -> anchor_lang::Result<Self> {
+        Ok(Self::deserialize(buf)?)
+    }
+}
+
+#[cfg(feature = "anchor")]
+impl anchor_lang::AccountSerialize for Nonce {}
+
+#[cfg(feature = "anchor")]
+impl anchor_lang::Owner for Nonce {
+    fn owner() -> Pubkey {
+        crate::SYSTEM_ID
+    }
+}
+
+#[cfg(feature = "anchor-idl-build")]
+impl anchor_lang::IdlBuild for Nonce {}
+
+#[cfg(feature = "anchor-idl-build")]
+impl anchor_lang::Discriminator for Nonce {
+    const DISCRIMINATOR: [u8; 8] = [0; 8];
+}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

@@ -9,6 +9,7 @@ pub mod accounts;
 pub mod errors;
 pub mod errors;
 pub mod instructions;
 pub mod instructions;
 pub mod programs;
 pub mod programs;
+pub mod shared;
 pub mod types;
 pub mod types;
 
 
 pub(crate) use programs::*;
 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),
+}

+ 21 - 0
codama.json

@@ -0,0 +1,21 @@
+{
+  "idl": "program/idl.json",
+  "before": [],
+  "scripts": {
+    "js": {
+      "from": "@codama/renderers-js",
+      "args": ["clients/js/src/generated"]
+    },
+    "rust": {
+      "from": "@codama/renderers-rust",
+      "args": [
+        "clients/rust/src/generated",
+        {
+          "anchorTraits": true,
+          "crateFolder": "clients/rust",
+          "formatCode": true
+        }
+      ]
+    }
+  }
+}

+ 4 - 4
package.json

@@ -4,7 +4,7 @@
     "solana:check": "tsx ./scripts/helpers/check-solana-version.mts",
     "solana:check": "tsx ./scripts/helpers/check-solana-version.mts",
     "solana:link": "tsx ./scripts/helpers/link-solana-version.mts",
     "solana:link": "tsx ./scripts/helpers/link-solana-version.mts",
     "generate": "pnpm generate:clients",
     "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:start": "tsx ./scripts/helpers/start-validator.mts",
     "validator:restart": "pnpm validator:start --restart",
     "validator:restart": "pnpm validator:start --restart",
     "validator:stop": "tsx ./scripts/helpers/stop-validator.mts",
     "validator:stop": "tsx ./scripts/helpers/stop-validator.mts",
@@ -30,10 +30,10 @@
     "template:upgrade": "tsx ./scripts/helpers/upgrade-template.ts"
     "template:upgrade": "tsx ./scripts/helpers/upgrade-template.ts"
   },
   },
   "devDependencies": {
   "devDependencies": {
-    "@codama/renderers-js": "^1.1.0",
-    "@codama/renderers-rust": "^1.0.4",
+    "@codama/renderers-js": "^1.2.1",
+    "@codama/renderers-rust": "^1.0.10",
     "@iarna/toml": "^2.2.5",
     "@iarna/toml": "^2.2.5",
-    "codama": "^1.1.0",
+    "codama": "^1.2.2",
     "tsx": "^4.19.2",
     "tsx": "^4.19.2",
     "typescript": "^5.5.2",
     "typescript": "^5.5.2",
     "zx": "^7.2.3"
     "zx": "^7.2.3"

Những thai đổi đã bị hủy bỏ vì nó quá lớn
+ 414 - 188
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,
-  })
-);

Một số tệp đã không được hiển thị bởi vì quá nhiều tập tin thay đổi trong này khác