Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit 4d2c151

Browse files
committed
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.
1 parent ed01eb8 commit 4d2c151

File tree

7 files changed

+92
-26
lines changed

7 files changed

+92
-26
lines changed

Cargo.lock

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

token/transfer-hook/interface/Cargo.toml

+12-2
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,27 @@ edition = "2021"
1010
[dependencies]
1111
arrayref = "0.3.9"
1212
bytemuck = { version = "1.19.0", features = ["derive"] }
13-
solana-program = "2.1.0"
14-
spl-discriminator = { version = "0.4.0", path = "../../../libraries/discriminator" }
13+
num-derive = "0.4"
14+
num-traits = "0.2"
15+
solana-account-info = "2.1.0"
16+
solana-cpi = "2.1.0"
17+
solana-decode-error = "2.1.0"
18+
solana-instruction = { version = "2.1.0", features = ["std"] }
19+
solana-msg = "2.1.0"
20+
solana-program-error = "2.1.0"
21+
solana-pubkey = "2.1.0"
22+
spl-discriminator = { version = "0.4.0" , path = "../../../libraries/discriminator" }
1523
spl-program-error = { version = "0.6.0", path = "../../../libraries/program-error" }
1624
spl-tlv-account-resolution = { version = "0.8.1", path = "../../../libraries/tlv-account-resolution" }
1725
spl-type-length-value = { version = "0.6.0", path = "../../../libraries/type-length-value" }
1826
spl-pod = { version = "0.5.0", path = "../../../libraries/pod" }
27+
thiserror = "1.0"
1928

2029
[lib]
2130
crate-type = ["cdylib", "lib"]
2231

2332
[dev-dependencies]
33+
solana-program = "2.1.0"
2434
tokio = { version = "1.41.0", features = ["full"] }
2535

2636
[package.metadata.docs.rs]
+46-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
//! Error types
22
3-
use spl_program_error::*;
3+
use {
4+
solana_decode_error::DecodeError,
5+
solana_msg::msg,
6+
solana_program_error::{PrintProgramError, ProgramError},
7+
};
48

59
/// Errors that may be returned by the interface.
6-
#[spl_program_error(hash_error_code_start = 2_110_272_652)]
10+
#[repr(u32)]
11+
#[derive(Clone, Debug, Eq, thiserror::Error, num_derive::FromPrimitive, PartialEq)]
712
pub enum TransferHookError {
813
/// Incorrect account provided
914
#[error("Incorrect account provided")]
10-
IncorrectAccount,
15+
IncorrectAccount = 2_110_272_652,
1116
/// Mint has no mint authority
1217
#[error("Mint has no mint authority")]
1318
MintHasNoMintAuthority,
@@ -18,3 +23,41 @@ pub enum TransferHookError {
1823
#[error("Program called outside of a token transfer")]
1924
ProgramCalledOutsideOfTransfer,
2025
}
26+
27+
impl From<TransferHookError> for ProgramError {
28+
fn from(e: TransferHookError) -> Self {
29+
ProgramError::Custom(e as u32)
30+
}
31+
}
32+
33+
impl<T> DecodeError<T> for TransferHookError {
34+
fn type_of() -> &'static str {
35+
"TransferHookError"
36+
}
37+
}
38+
39+
impl PrintProgramError for TransferHookError {
40+
fn print<E>(&self)
41+
where
42+
E: 'static
43+
+ std::error::Error
44+
+ DecodeError<E>
45+
+ PrintProgramError
46+
+ num_traits::FromPrimitive,
47+
{
48+
match self {
49+
TransferHookError::IncorrectAccount => {
50+
msg!("Incorrect account provided")
51+
}
52+
TransferHookError::MintHasNoMintAuthority => {
53+
msg!("Mint has no mint authority")
54+
}
55+
TransferHookError::IncorrectMintAuthority => {
56+
msg!("Incorrect mint authority has signed the instruction")
57+
}
58+
TransferHookError::ProgramCalledOutsideOfTransfer => {
59+
msg!("Program called outside of a token transfer")
60+
}
61+
}
62+
}
63+
}

token/transfer-hook/interface/src/instruction.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
//! Instruction types
22
33
use {
4-
solana_program::{
5-
instruction::{AccountMeta, Instruction},
6-
program_error::ProgramError,
7-
pubkey::Pubkey,
8-
system_program,
9-
},
4+
solana_instruction::{AccountMeta, Instruction},
5+
solana_program_error::ProgramError,
6+
solana_pubkey::Pubkey,
107
spl_discriminator::{ArrayDiscriminator, SplDiscriminate},
118
spl_pod::{bytemuck::pod_slice_to_bytes, slice::PodSlice},
129
spl_tlv_account_resolution::account::ExtraAccountMeta,
1310
std::convert::TryInto,
1411
};
1512

13+
const SYSTEM_PROGRAM_ID: Pubkey = Pubkey::from_str_const("11111111111111111111111111111111");
14+
1615
/// Instructions supported by the transfer hook interface.
1716
#[repr(C)]
1817
#[derive(Clone, Debug, PartialEq)]
@@ -215,7 +214,7 @@ pub fn initialize_extra_account_meta_list(
215214
AccountMeta::new(*extra_account_metas_pubkey, false),
216215
AccountMeta::new_readonly(*mint_pubkey, false),
217216
AccountMeta::new_readonly(*authority_pubkey, true),
218-
AccountMeta::new_readonly(system_program::id(), false),
217+
AccountMeta::new_readonly(SYSTEM_PROGRAM_ID, false),
219218
];
220219

221220
Instruction {
@@ -255,6 +254,11 @@ pub fn update_extra_account_meta_list(
255254
mod test {
256255
use {super::*, crate::NAMESPACE, solana_program::hash, spl_pod::bytemuck::pod_from_bytes};
257256

257+
#[test]
258+
fn system_program_id() {
259+
assert_eq!(solana_program::system_program::id(), SYSTEM_PROGRAM_ID);
260+
}
261+
258262
#[test]
259263
fn validate_packing() {
260264
let amount = 111_111_111;

token/transfer-hook/interface/src/lib.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@ pub mod onchain;
1414

1515
// Export current sdk types for downstream users building with a different sdk
1616
// version
17-
pub use solana_program;
18-
use solana_program::pubkey::Pubkey;
17+
use solana_pubkey::Pubkey;
18+
pub use {
19+
solana_account_info, solana_cpi, solana_decode_error, solana_instruction, solana_msg,
20+
solana_program_error, solana_pubkey,
21+
};
1922

2023
/// Namespace for all programs implementing transfer-hook
2124
pub const NAMESPACE: &str = "spl-transfer-hook-interface";

token/transfer-hook/interface/src/offchain.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@ use {
77
get_extra_account_metas_address,
88
instruction::{execute, ExecuteInstruction},
99
},
10-
solana_program::{
11-
instruction::{AccountMeta, Instruction},
12-
program_error::ProgramError,
13-
pubkey::Pubkey,
14-
},
10+
solana_instruction::{AccountMeta, Instruction},
11+
solana_program_error::ProgramError,
12+
solana_pubkey::Pubkey,
1513
spl_tlv_account_resolution::state::ExtraAccountMetaList,
1614
std::future::Future,
1715
};

token/transfer-hook/interface/src/onchain.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@
33
44
use {
55
crate::{error::TransferHookError, get_extra_account_metas_address, instruction},
6-
solana_program::{
7-
account_info::AccountInfo,
8-
entrypoint::ProgramResult,
9-
instruction::{AccountMeta, Instruction},
10-
program::invoke,
11-
pubkey::Pubkey,
12-
},
6+
solana_account_info::AccountInfo,
7+
solana_cpi::invoke,
8+
solana_instruction::{AccountMeta, Instruction},
9+
solana_program_error::ProgramResult,
10+
solana_pubkey::Pubkey,
1311
spl_tlv_account_resolution::state::ExtraAccountMetaList,
1412
};
1513
/// Helper to CPI into a transfer-hook program on-chain, looking through the

0 commit comments

Comments
 (0)