Преглед изворни кода

transfer-hook-interface: Remove solana-program dependency (#7442)

* transfer-hook-interface: Remove solana-program dependency

#### Problem

The transfer-hook-interface pulls in solana-program, but it doesn't need
to.

#### Summary of changes

Use the component crates. The only breaking change is the re-exports. It
still uses a dev-dependency on solana-program since the system program
id isn't available yet outside of solana-program.

* Add missing solana-pubkey feature

* Add all test programs

* Run cargo fmt

* Fixup imports
Jon C пре 1 година
родитељ
комит
0d8c161928

+ 12 - 2
interface/Cargo.toml

@@ -10,17 +10,27 @@ edition = "2021"
 [dependencies]
 arrayref = "0.3.9"
 bytemuck = { version = "1.19.0", features = ["derive"] }
-solana-program = "2.1.0"
-spl-discriminator = { version = "0.4.0", path = "../../../libraries/discriminator" }
+num-derive = "0.4"
+num-traits = "0.2"
+solana-account-info = "2.1.0"
+solana-cpi = "2.1.0"
+solana-decode-error = "2.1.0"
+solana-instruction = { version = "2.1.0", features = ["std"] }
+solana-msg = "2.1.0"
+solana-program-error = "2.1.0"
+solana-pubkey = { version = "2.1.0", features = ["curve25519"] }
+spl-discriminator = { version = "0.4.0" , path = "../../../libraries/discriminator" }
 spl-program-error = { version = "0.6.0", path = "../../../libraries/program-error" }
 spl-tlv-account-resolution = { version = "0.9.0", path = "../../../libraries/tlv-account-resolution" }
 spl-type-length-value = { version = "0.7.0", path = "../../../libraries/type-length-value" }
 spl-pod = { version = "0.5.0", path = "../../../libraries/pod" }
+thiserror = "1.0"
 
 [lib]
 crate-type = ["cdylib", "lib"]
 
 [dev-dependencies]
+solana-program = "2.1.0"
 tokio = { version = "1.41.0", features = ["full"] }
 
 [package.metadata.docs.rs]

+ 46 - 3
interface/src/error.rs

@@ -1,13 +1,18 @@
 //! Error types
 
-use spl_program_error::*;
+use {
+    solana_decode_error::DecodeError,
+    solana_msg::msg,
+    solana_program_error::{PrintProgramError, ProgramError},
+};
 
 /// Errors that may be returned by the interface.
-#[spl_program_error(hash_error_code_start = 2_110_272_652)]
+#[repr(u32)]
+#[derive(Clone, Debug, Eq, thiserror::Error, num_derive::FromPrimitive, PartialEq)]
 pub enum TransferHookError {
     /// Incorrect account provided
     #[error("Incorrect account provided")]
-    IncorrectAccount,
+    IncorrectAccount = 2_110_272_652,
     /// Mint has no mint authority
     #[error("Mint has no mint authority")]
     MintHasNoMintAuthority,
@@ -18,3 +23,41 @@ pub enum TransferHookError {
     #[error("Program called outside of a token transfer")]
     ProgramCalledOutsideOfTransfer,
 }
+
+impl From<TransferHookError> for ProgramError {
+    fn from(e: TransferHookError) -> Self {
+        ProgramError::Custom(e as u32)
+    }
+}
+
+impl<T> DecodeError<T> for TransferHookError {
+    fn type_of() -> &'static str {
+        "TransferHookError"
+    }
+}
+
+impl PrintProgramError for TransferHookError {
+    fn print<E>(&self)
+    where
+        E: 'static
+            + std::error::Error
+            + DecodeError<E>
+            + PrintProgramError
+            + num_traits::FromPrimitive,
+    {
+        match self {
+            TransferHookError::IncorrectAccount => {
+                msg!("Incorrect account provided")
+            }
+            TransferHookError::MintHasNoMintAuthority => {
+                msg!("Mint has no mint authority")
+            }
+            TransferHookError::IncorrectMintAuthority => {
+                msg!("Incorrect mint authority has signed the instruction")
+            }
+            TransferHookError::ProgramCalledOutsideOfTransfer => {
+                msg!("Program called outside of a token transfer")
+            }
+        }
+    }
+}

+ 11 - 7
interface/src/instruction.rs

@@ -1,18 +1,17 @@
 //! Instruction types
 
 use {
-    solana_program::{
-        instruction::{AccountMeta, Instruction},
-        program_error::ProgramError,
-        pubkey::Pubkey,
-        system_program,
-    },
+    solana_instruction::{AccountMeta, Instruction},
+    solana_program_error::ProgramError,
+    solana_pubkey::Pubkey,
     spl_discriminator::{ArrayDiscriminator, SplDiscriminate},
     spl_pod::{bytemuck::pod_slice_to_bytes, slice::PodSlice},
     spl_tlv_account_resolution::account::ExtraAccountMeta,
     std::convert::TryInto,
 };
 
+const SYSTEM_PROGRAM_ID: Pubkey = Pubkey::from_str_const("11111111111111111111111111111111");
+
 /// Instructions supported by the transfer hook interface.
 #[repr(C)]
 #[derive(Clone, Debug, PartialEq)]
@@ -215,7 +214,7 @@ pub fn initialize_extra_account_meta_list(
         AccountMeta::new(*extra_account_metas_pubkey, false),
         AccountMeta::new_readonly(*mint_pubkey, false),
         AccountMeta::new_readonly(*authority_pubkey, true),
-        AccountMeta::new_readonly(system_program::id(), false),
+        AccountMeta::new_readonly(SYSTEM_PROGRAM_ID, false),
     ];
 
     Instruction {
@@ -255,6 +254,11 @@ pub fn update_extra_account_meta_list(
 mod test {
     use {super::*, crate::NAMESPACE, solana_program::hash, spl_pod::bytemuck::pod_from_bytes};
 
+    #[test]
+    fn system_program_id() {
+        assert_eq!(solana_program::system_program::id(), SYSTEM_PROGRAM_ID);
+    }
+
     #[test]
     fn validate_packing() {
         let amount = 111_111_111;

+ 5 - 2
interface/src/lib.rs

@@ -14,8 +14,11 @@ pub mod onchain;
 
 // Export current sdk types for downstream users building with a different sdk
 // version
-pub use solana_program;
-use solana_program::pubkey::Pubkey;
+use solana_pubkey::Pubkey;
+pub use {
+    solana_account_info, solana_cpi, solana_decode_error, solana_instruction, solana_msg,
+    solana_program_error, solana_pubkey,
+};
 
 /// Namespace for all programs implementing transfer-hook
 pub const NAMESPACE: &str = "spl-transfer-hook-interface";

+ 3 - 5
interface/src/offchain.rs

@@ -7,11 +7,9 @@ use {
         get_extra_account_metas_address,
         instruction::{execute, ExecuteInstruction},
     },
-    solana_program::{
-        instruction::{AccountMeta, Instruction},
-        program_error::ProgramError,
-        pubkey::Pubkey,
-    },
+    solana_instruction::{AccountMeta, Instruction},
+    solana_program_error::ProgramError,
+    solana_pubkey::Pubkey,
     spl_tlv_account_resolution::state::ExtraAccountMetaList,
     std::future::Future,
 };

+ 5 - 7
interface/src/onchain.rs

@@ -3,13 +3,11 @@
 
 use {
     crate::{error::TransferHookError, get_extra_account_metas_address, instruction},
-    solana_program::{
-        account_info::AccountInfo,
-        entrypoint::ProgramResult,
-        instruction::{AccountMeta, Instruction},
-        program::invoke,
-        pubkey::Pubkey,
-    },
+    solana_account_info::AccountInfo,
+    solana_cpi::invoke,
+    solana_instruction::{AccountMeta, Instruction},
+    solana_program_error::ProgramResult,
+    solana_pubkey::Pubkey,
     spl_tlv_account_resolution::state::ExtraAccountMetaList,
 };
 /// Helper to CPI into a transfer-hook program on-chain, looking through the