Struct trdelnik_client::Client

source ·
pub struct Client { /* private fields */ }
Expand description

Client allows you to send typed RPC requests to a Solana cluster.

Implementations§

source§

impl Client

source

pub fn new(payer: Keypair) -> Self

Creates a new Client instance.

source

pub fn payer(&self) -> &Keypair

Gets client’s payer.

source

pub fn anchor_client(&self) -> &AnchorClient<Rc<Keypair>>

Gets the internal Anchor client to call Anchor client’s methods directly.

source

pub fn program(&self, program_id: Pubkey) -> Program<Rc<Keypair>>

Creates Program instance to communicate with the selected program.

source

pub async fn is_localnet_running(&self, retry: bool) -> bool

Finds out if the Solana localnet is running.

Set retry to true when you want to wait for up to 15 seconds until the localnet is running (until 30 retries with 500ms delays are performed).

source

pub async fn account_data<T>(&self, account: Pubkey) -> Result<T, Error>where T: AccountDeserialize + Send + 'static,

Gets deserialized data from the chosen account serialized with Anchor

Errors

It fails when:

  • the account does not exist.
  • the Solana cluster is not running.
  • deserialization failed.
source

pub async fn account_data_bincode<T>(&self, account: Pubkey) -> Result<T, Error>where T: DeserializeOwned + Send + 'static,

Gets deserialized data from the chosen account serialized with Bincode

Errors

It fails when:

  • the account does not exist.
  • the Solana cluster is not running.
  • deserialization failed.
source

pub async fn account_data_borsh<T>(&self, account: Pubkey) -> Result<T, Error>where T: BorshDeserialize + Send + 'static,

Gets deserialized data from the chosen account serialized with Borsh

Errors

It fails when:

  • the account does not exist.
  • the Solana cluster is not running.
  • deserialization failed.
source

pub async fn get_account( &self, account: Pubkey ) -> Result<Option<Account>, Error>

Returns all information associated with the account of the provided Pubkey.

Errors

It fails when the Solana cluster is not running.

source

pub async fn send_instruction( &self, program: Pubkey, instruction: impl InstructionData + Send + 'static, accounts: impl ToAccountMetas + Send + 'static, signers: impl IntoIterator<Item = Keypair> + Send + 'static ) -> Result<EncodedConfirmedTransactionWithStatusMeta, Error>

Sends the Anchor instruction with associated accounts and signers.

Example
use trdelnik_client::*;

pub async fn initialize(
    client: &Client,
    state: Pubkey,
    user: Pubkey,
    system_program: Pubkey,
    signers: impl IntoIterator<Item = Keypair> + Send + 'static,
) -> Result<EncodedConfirmedTransactionWithStatusMeta, ClientError> {
    Ok(client
        .send_instruction(
            PROGRAM_ID,
            turnstile::instruction::Initialize {},
            turnstile::accounts::Initialize {
                state: a_state,
                user: a_user,
                system_program: a_system_program,
            },
            signers,
        )
        .await?)
}
source

pub async fn send_transaction( &self, instructions: &[Instruction], signers: impl IntoIterator<Item = &Keypair> + Send ) -> Result<EncodedConfirmedTransactionWithStatusMeta, Error>

Sends the transaction with associated instructions and signers.

Example
#[throws]
pub async fn create_account(
    &self,
    keypair: &Keypair,
    lamports: u64,
    space: u64,
    owner: &Pubkey,
) -> EncodedConfirmedTransaction {
    self.send_transaction(
        &[system_instruction::create_account(
            &self.payer().pubkey(),
            &keypair.pubkey(),
            lamports,
            space,
            owner,
        )],
        [keypair],
    )
    .await?
}
source

pub async fn airdrop(&self, address: Pubkey, lamports: u64) -> Result<(), Error>

Airdrops lamports to the chosen account.

source

pub async fn get_balance(&mut self, address: &Pubkey) -> Result<u64, Error>

Get balance of an account

source

pub async fn get_token_balance( &mut self, address: Pubkey ) -> Result<UiTokenAmount, Error>

Get token balance of an token account

source

pub async fn deploy_by_name( &self, program_keypair: &Keypair, program_name: &str ) -> Result<(), Error>

Deploys a program based on it’s name. This function wraps boilerplate code required for the successful deployment of a program, i.e. SOLs airdrop etc.

Arguments
  • program_keypair - Keypair used for the program
  • program_name - Name of the program to be deployed
Example:

Project structure

project/
- programs/
  - awesome_contract/
    - ...
    - Cargo.toml
  - turnstile/
    - ...
    - Cargo.toml
- ...
- Cargo.toml

Code

client.deploy_program(program_keypair(0), "awesome_contract");
client.deploy_program(program_keypair(1), "turnstile");
source

pub async fn create_account( &self, keypair: &Keypair, lamports: u64, space: u64, owner: &Pubkey ) -> Result<EncodedConfirmedTransactionWithStatusMeta, Error>

Creates accounts.

source

pub async fn create_account_rent_exempt( &mut self, keypair: &Keypair, space: u64, owner: &Pubkey ) -> Result<EncodedConfirmedTransactionWithStatusMeta, Error>

Creates rent exempt account.

source

pub async fn create_token_mint( &self, mint: &Keypair, authority: Pubkey, freeze_authority: Option<Pubkey>, decimals: u8 ) -> Result<EncodedConfirmedTransactionWithStatusMeta, Error>

Executes a transaction constructing a token mint.

source

pub async fn mint_tokens( &self, mint: Pubkey, authority: &Keypair, account: Pubkey, amount: u64 ) -> Result<EncodedConfirmedTransactionWithStatusMeta, Error>

Executes a transaction that mints tokens from a mint to an account belonging to that mint.

source

pub async fn create_token_account( &self, account: &Keypair, mint: &Pubkey, owner: &Pubkey ) -> Result<EncodedConfirmedTransactionWithStatusMeta, Error>

Executes a transaction constructing a token account of the specified mint. The account needs to be empty and belong to system for this to work. Prefer to use create_associated_token_account if you don’t need the provided account to contain the token account.

source

pub async fn create_associated_token_account( &self, owner: &Keypair, mint: Pubkey ) -> Result<Pubkey, Error>

Executes a transaction constructing the associated token account of the specified mint belonging to the owner. This will fail if the account already exists.

source

pub async fn create_account_with_data( &self, account: &Keypair, data: Vec<u8> ) -> Result<(), Error>

Executes a transaction creating and filling the given account with the given data. The account is required to be empty and will be owned by bpf_loader afterwards.

Auto Trait Implementations§

§

impl RefUnwindSafe for Client

§

impl !Send for Client

§

impl !Sync for Client

§

impl Unpin for Client

§

impl UnwindSafe for Client

Blanket Implementations§

§

impl<T> AbiExample for T

§

default fn example() -> T

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere T: 'a,

§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

§

impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere T: 'a,

§

fn implicit( self, class: Class, constructed: bool, tag: u32 ) -> TaggedParser<'a, Implicit, Self, E>

source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> Pointable for T

§

const ALIGN: usize = mem::align_of::<T>()

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> Same<T> for T

§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more