Implementation of the gill library in Rust
Installation
Quick start
- Create a Solana RPC connection
- Making Solana RPC calls
- Create a transaction
- Signing transactions
- Simulating transactions
- Sending and confirming transaction
- Get a transaction signature
- Get a Solana Explorer link
- Calculate minimum rent balance for an account
- Generating keypairs and signers
You can find some system specific helpers here
- Loading a keypair from a file
- Saving a keypair to a file
- Loading a keypair from an environment variable
- Saving a keypair to an environment variable file
- Loading a base58 keypair from an environment variable
You can find transaction builders for common tasks, including:
- Creating a token with metadata
- Minting tokens to a destination wallet
- Transfer tokens to a destination wallet
For troubleshooting and debugging your Solana transactions, see Debug mode below.
You can also consult the documentation for Anza's JavaScript client library for more information and helpful resources.
Using the modular crates from Anza
Generating Keypairs and Signers
For most "signing" transactions, you'll need a Keypair instance, which can be used to sign transactions and messages.
use ;
let signer = new;
Ensure you import the
Signertrait as it contains necessary methods to facilitate signing
Create a Solana RPC connection
Create a Solana rpc client for any RPC URL or standard Solana network moniker (i.e. devnet, localnet, mainnet etc) with a default commitment level of confirmed
use SolanaClient;
let client = new;
Using the Solana moniker will connect to the public RPC endpoints. These are subject to rate limits and should not be used in production applications. Applications should find their own RPC provider and the URL provided from them.
To create an RPC client with another commitment level:
use ;
let client = new_with_commitment;
To create an RPC client for an custom RPC provider or service:
use SolanaClient;
let client = new;
Making Solana RPC calls
After you have a Solana rpc connection, you can make all the JSON RPC method calls directly off of it.
use SolanaClient;
let client = new;
//get slot
let slot = client.rpc.getSlot;
//get the latest blockhash
let latest_blockhash = client.rpc.get_latest_blockhash;
Create a transaction
Legacy
Quickly create a Solana transaction:
use TxBuilder;
let tx = new;
Versioned Transaction
COMING SOON!
Signing Transactions
use TxBuilder;
let tx = new;
let signed_tx = tx.sign;
Simulating transactions
To simulate a transaction on the blockchain, you can use the simulate_transaction method on the initialized SolanaClient
use ;
let client = new;
let tx = new;
let signed_tx = tx.sign;
let simulation = client.rpc.simulate_transaction;
Simulation with Custom Config: COMING SOON!
Sending and confirming transactions
To send and confirm a transaction to the blockchain, you can use the sendAndConfirmTransaction method from the initialized SolanaClient. The default commitment level is processed, to set another level check out - COMING SOON!
use ;
let client = new;
let tx = new;
let signed_tx = tx.sign;
let signature = client.rpc.send_and_confirmed_transaction;
Set custom config
COMING SOON!
Get the signature from a signed transaction
After you have a transaction signed by the feePayer (either a partially or fully signed transaction), you can get the
transaction signature as follows:
use TxBuilder;
let tx = new;
let signed_tx = tx.sign;
let signature = signed_tx.signatures;
Note: After a transaction has been signed by the fee payer, it will have a transaction signature (aka transaction id). This is due to Solana transaction ids are the first item in the transaction's
signaturesarray. Therefore, client applications can potentially know the signature before it is even sent to the network for confirmation.
Get a Solana Explorer link for transactions, accounts, or blocks
Craft a Solana Explorer link for transactions, accounts, or blocks on any cluster.
Get a Solana Explorer link for a transaction
To get an explorer link for a transaction's signature (aka transaction id):
use TxBuilder;
let url = get_explorer_link_transaction;
The
clusterparameter accepts custom urls and the known monikers(i.e devnet, mainnet, localnet)
If you have a partially or fully signed transaction, you can get the Explorer link before even sending the transaction to the network:
See
Get Signature from a Signed Transactionfor better understanding.
use TxBuilder;
let tx = new;
let signed_tx = tx.sign;
let url = get_explorer_link_transaction
COMING SOON!
Get a Solana Explorer link for an account
To get an explorer link for an account on Solana's devnet:
use TxBuilder;
let url = get_explorer_link_account;
Get a Solana Explorer link for a block
To get an explorer link for a block:
use TxBuilder;
let url = get_explorer_link_block;
Calculate minimum rent for an account
To calculate the minimum rent balance for an account (aka data storage deposit fee):
For default rent
use Rent;
let rent = default.minimum_balance;
// Expected value: 890880
Rent for specified number of bytes
use Rent;
let rent = default.minimum_balance;
// Expected value: 1238880
Loading a keypair from a file
use ;
let signer = from_default_file;
Ensure you import the
KeypairExttrait as it contains extended functionality on the Keypair component
Load a Keypair from a filesystem wallet json file, like those output from the Solana CLI (i.e. a JSON array of numbers).
By default, the keypair file loaded is the Solana CLI's default keypair: ~/.config/solana/id.json
To load a Signer from a specific filepath:
use Keypair;
let signer = from_default_file;
Saving a keypair to a file
Save a Keypair to a local json file(e.g keypair.json)
use Keypair;
let signer = new;
signer.write_to_file;
Transaction Builders
Create a token with metadata
Build a transaction that can create a token with metadata, either using the original token or token extensions (token22) program. - COMING SOON!
- Tokens created with the original token program (
TOKEN_PROGRAM_ADDRESS, default) will use Metaplex's Token Metadata program for onchain metadata - Tokens created with the token extensions program (
TOKEN_2022_PROGRAM_ADDRESS) will use the metadata pointer extensions - COMING SOON! INSTRUCTION BUILDERS - COMING SOON!
use branchia:;
let create_token_tx = create_token_transaction
// ADD DEFAULT VALUES FOR CERTAIN FIELDS HERE? - TBD!
Mint tokens to destination wallet
Build a transaction that mints new tokens to the destination wallet address (raising the token's overall supply).
- ensure you set the correct
tokenProgramused by themintitself - if the
destinationowner does not have an associated token account (ata) created for themint, one will be auto-created for them - ensure you take into account the
decimalsfor themintwhen setting theamountin this transaction
use TxBuilder;
let mint_tokens_tx = mint_token_transaction
Transfer tokens to a destination wallet
Build a transaction that transfers tokens to the destination wallet address from the source (aka from sourceAta to
destinationAta).
- ensure you set the correct
tokenProgramused by themintitself - if the
destinationowner does not have an associated token account (ata) created for themint, one will be auto-created for them - ensure you take into account the
decimalsfor themintwhen setting theamountin this transaction
use
let transfer_tokens_tx = transfer_token_transaction