Struct 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

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

Source

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

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<EncodedConfirmedTransaction, 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 Freeze for Client

§

impl !RefUnwindSafe for Client

§

impl !Send for Client

§

impl !Sync for Client

§

impl Unpin for Client

§

impl !UnwindSafe for Client

Blanket Implementations§

Source§

impl<T> AbiExample for T

Source§

default fn example() -> T

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where 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 T
where 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.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

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

Initializes a with the given initializer. Read more
Source§

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

Dereferences the given pointer. Read more
Source§

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

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

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

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

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 T
where U: TryFrom<T>,

Source§

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.
Source§

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

Source§

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
Source§

impl<T> ErasedDestructor for T
where T: 'static,