Giter Club home page Giter Club logo

solana-client-helpers's Introduction

solana-client-helpers

This is a set of Solana client utilities to ease communication through the Solana RPC API.

Usage

Add this to your Cargo.toml:

[dependencies]
solana-client-helpers = "1.0"

Now you can use client helper like this:

use solana_client::rpc_client::RpcClient;
use solana_client_helpers::{Client, ClientResult, SplToken};
use solana_sdk::{
    commitment_config::CommitmentConfig,
    signature::{Keypair, Signer},
};

fn main() -> ClientResult<()> {
    let payer = Keypair::new();
    let sender = Keypair::new();
    let recipient = Keypair::new();

    let client = RpcClient::new_with_commitment("http://localhost:8899".into(), CommitmentConfig::confirmed());
    let client = Client { client, payer };

    client.airdrop(&client.payer_pubkey(), 10_000_000_000)?;
    assert_eq!(client.get_balance(&client.payer_pubkey())?, 10_000_000_000);

    let token_mint = client.create_token_mint(&sender.pubkey(), 2)?;
    let sender_token_account = client.create_associated_token_account_by_payer(&sender.pubkey(), &token_mint.pubkey())?;
    let recipient_token_account = client.create_associated_token_account_by_payer(&recipient.pubkey(), &token_mint.pubkey())?;

    client.mint_to(&sender, &token_mint.pubkey(), &sender_token_account, 1000, 2)?;

    let sender_balance = client.get_token_account_balance(&sender_token_account)?;
    assert_eq!(sender_balance.ui_amount, Some(10.00));

    client.transfer_to(&sender, &token_mint.pubkey(), &sender_token_account, &recipient_token_account, 500, 2)?;

    let sender_balance = client.get_token_account_balance(&sender_token_account)?;
    assert_eq!(sender_balance.ui_amount, Some(5.00));
   
    let recipient_balance = client.get_token_account_balance(&recipient_token_account)?;
    assert_eq!(recipient_balance.ui_amount, Some(5.00));

   Ok(())
}

solana-client-helpers's People

Contributors

xx avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.