Skip to content

Commit

Permalink
feat: add new Into* trait for anchor client programs
Browse files Browse the repository at this point in the history
  • Loading branch information
ifiokjr committed Oct 4, 2024
1 parent 8ffa708 commit 62206c8
Showing 1 changed file with 47 additions and 35 deletions.
82 changes: 47 additions & 35 deletions crates/wasm_client_anchor/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,50 +222,62 @@ macro_rules! create_program_client_macro {
#[macro_export]
macro_rules! create_program_client {
($id:expr, $program_client_name:ident) => {
#[derive(::std::fmt::Debug, ::core::clone::Clone)]
pub struct $program_client_name<W: $crate::WalletAnchor>($crate::AnchorProgram<W>);

impl<W: $crate::WalletAnchor> core::ops::Deref for $program_client_name<W> {
type Target = $crate::AnchorProgram<W>;

fn deref(&self) -> &Self::Target {
&self.0
$crate::__private::paste::paste! {
pub trait [<Into $program_client_name>]<W: $crate::WalletAnchor> {
fn [<into_ $program_client_name:snake>](self) -> $program_client_name<W>;
}
}

impl<W: $crate::WalletAnchor> From<$crate::AnchorProgram<W>> for $program_client_name<W> {
fn from(program: $crate::AnchorProgram<W>) -> Self {
$program_client_name(program)
impl<W: $crate::WalletAnchor> [<Into $program_client_name>]<W> for $crate::AnchorProgram<W> {
fn [<into_ $program_client_name:snake>](self) -> $program_client_name<W> {
self.into()
}
}
}

impl<W: $crate::WalletAnchor> $program_client_name<W> {
/// Start the `AnchorProgram` builder with the `program_id` already set to
/// the default.
pub fn builder() -> $crate::AnchorProgramPartialBuilder<W> {
$crate::AnchorProgram::builder().program_id($id)
}
#[derive(::std::fmt::Debug, ::core::clone::Clone)]
pub struct $program_client_name<W: $crate::WalletAnchor>($crate::AnchorProgram<W>);

/// Start the `AnchorProgram` builder with a custom `program_id`.
pub fn builder_with_program(
program_id: &$crate::__private::solana_sdk::pubkey::Pubkey,
) -> $crate::AnchorProgramPartialBuilder<W> {
$crate::AnchorProgram::builder().program_id(*program_id)
impl<W: $crate::WalletAnchor> core::ops::Deref for $program_client_name<W> {
type Target = $crate::AnchorProgram<W>;

fn deref(&self) -> &Self::Target {
&self.0
}
}

/// Get the program
pub fn program(&self) -> &$crate::AnchorProgram<W> {
self
impl<W: $crate::WalletAnchor> From<$crate::AnchorProgram<W>> for $program_client_name<W> {
fn from(program: $crate::AnchorProgram<W>) -> Self {
$program_client_name(program)
}
}

/// Request an airdrop to the payer account
pub async fn request_airdrop(
&self,
pubkey: &$crate::__private::solana_sdk::pubkey::Pubkey,
lamports: u64,
) -> $crate::AnchorClientResult<$crate::__private::solana_sdk::signature::Signature> {
let signature = self.rpc().request_airdrop(pubkey, lamports).await?;
Ok(signature)
impl<W: $crate::WalletAnchor> $program_client_name<W> {
/// Start the `AnchorProgram` builder with the `program_id` already set to
/// the default.
pub fn builder() -> $crate::AnchorProgramPartialBuilder<W> {
$crate::AnchorProgram::builder().program_id($id)
}

/// Start the `AnchorProgram` builder with a custom `program_id`.
pub fn builder_with_program(
program_id: &$crate::__private::solana_sdk::pubkey::Pubkey,
) -> $crate::AnchorProgramPartialBuilder<W> {
$crate::AnchorProgram::builder().program_id(*program_id)
}

/// Get the program
pub fn program(&self) -> &$crate::AnchorProgram<W> {
self
}

/// Request an airdrop to the payer account
pub async fn request_airdrop(
&self,
pubkey: &$crate::__private::solana_sdk::pubkey::Pubkey,
lamports: u64,
) -> $crate::AnchorClientResult<$crate::__private::solana_sdk::signature::Signature> {
let signature = self.rpc().request_airdrop(pubkey, lamports).await?;
Ok(signature)
}
}
}
};
Expand Down

0 comments on commit 62206c8

Please sign in to comment.