solana_web3 0.0.2+6
solana_web3: ^0.0.2+6 copied to clipboard
The Solana Dart API built on Solana's JSON RPC API, with support for common Native Programs.
The Solana Dart API built on Solana's JSON RPC API.
Android and iOS supported.
Features #
The library is modelled on the solana-web3.js Javascript API and includes the following features:
- Compute Budget Program
- ED25519 Program
- JSON RPC API
- Memo Program
- Stake Program
- System Program
Example #
- Import the package and System Program.
import 'package:solana_web3/solana_web3.dart' as web3;
import 'package:solana_web3/programs/system.dart';
- Connect to a Solana cluster.
final cluster = web3.Cluster.devnet;
final connection = web3.Connection(cluster);
- Create a new wallet and airdrop [amount] tokens to the address.
/// NOTE: Keep the value of [amount] low (e.g. 1 or 2).
///
/// WARNING: Airdrops cannot be performed on the mainnet.
Future<web3.Keypair> createWalletWithBalance(
final web3.Connection connection, {
required final int amount,
}) async {
// Create a new wallet and get its public address.
final wallet = web3.Keypair.generate();
final address = wallet.publicKey;
// Airdrop some test tokens to the wallet address.
// NOTE: Airdrops cannot be performed on the mainnet.
if (amount > 0) {
final lamports = web3.lamportsPerSol * amount;
final transactionSignature = await connection.requestAirdrop(address, lamports);
await connection.confirmTransaction(transactionSignature);
}
return wallet;
}
- Transfer lamports from one account to another using the System Program.
print('Creating accounts...\n');
// Create a new wallet to transfer tokens from.
final wallet1 = await createWalletWithBalance(connection, amount: 2);
final address1 = wallet1.publicKey;
// Create a new wallet to transfer tokens to.
final wallet2 = await createWalletWithBalance(connection, amount: 0);
final address2 = wallet2.publicKey;
// Check the account balances before making the transfer.
final balance = await connection.getBalance(wallet1.publicKey);
print('Account $address1 has an initial balance of $balance lamports.');
print('Account $address2 has an initial balance of 0 lamports.\n');
// Create a System Program instruction to transfer 1 SOL from [address1] to [address2].
final transaction = web3.Transaction();
transaction.add(
SystemProgram.transfer(
fromPublicKey: address1,
toPublicKey: address2,
lamports: web3.solToLamports(1),
),
);
// Send the transaction to the cluster and wait for it to be confirmed.
print('Send and confirm transaction...\n');
await connection.sendAndConfirmTransaction(
transaction,
signers: [wallet1], // Fee payer + transaction signer.
);
// Check the updated account balances.
final wallet1balance = await connection.getBalance(wallet1.publicKey);
final wallet2balance = await connection.getBalance(wallet2.publicKey);
print('Account $address1 has an updated balance of $wallet1balance lamports.');
print('Account $address2 has an updated balance of $wallet2balance lamports.');
- Close the connection.
connection.disconnect();
Bugs and Feature Requests #
Bugs #
Report a bug by opening an issue.
Feature Requests #
Request a feature by raising a ticket.