Przeglądaj źródła

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 miesięcy temu
rodzic
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)
     }
 }
+
+#[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());
     }
 }
+
+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),
+}

+ 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: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.1",
+    "@codama/renderers-rust": "^1.0.10",
     "@iarna/toml": "^2.2.5",
-    "codama": "^1.1.0",
+    "codama": "^1.2.2",
     "tsx": "^4.19.2",
     "typescript": "^5.5.2",
     "zx": "^7.2.3"

Plik diff jest za duży
+ 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,
-  })
-);

Niektóre pliki nie zostały wyświetlone z powodu dużej ilości zmienionych plików