solana-test-validator, you can fund
an address by requesting an airdrop, as the examples below do. The public devnet
and testnet endpoints no longer serve airdrops, so on devnet use the
faucet instead (see
more ways to get devnet SOL).
- Kit
- Web3.js v3
- Rust
- Python
- Go
- Web3.js (legacy)
Kit
import {
airdropFactory,
createSolanaRpc,
createSolanaRpcSubscriptions,
generateKeyPairSigner,
lamports,
} from "@solana/kit";
const rpc = createSolanaRpc("http://localhost:8899");
const rpcSubscriptions = createSolanaRpcSubscriptions("ws://localhost:8900");
const wallet = await generateKeyPairSigner();
const LAMPORTS_PER_SOL = 1_000_000_000n;
await airdropFactory({ rpc, rpcSubscriptions })({
recipientAddress: wallet.address,
lamports: lamports(LAMPORTS_PER_SOL), // 1 SOL
commitment: "confirmed",
});
const { value } = await rpc.getBalance(wallet.address).send();
console.log(`Balance: ${value / LAMPORTS_PER_SOL} SOL`);
Output from a local Surfpool network
Output
Balance: 1 SOL
Web3.js v3
import { Connection, Keypair, LAMPORTS_PER_SOL } from "@solana/web3.js";
const connection = new Connection("http://localhost:8899", "confirmed");
const wallet = await Keypair.generate();
const signature = await connection.requestAirdrop(
wallet.publicKey,
LAMPORTS_PER_SOL,
);
const { blockhash, lastValidBlockHeight } =
await connection.getLatestBlockhash();
await connection.confirmTransaction({
blockhash,
lastValidBlockHeight,
signature,
});
const balance = await connection.getBalance(wallet.publicKey);
console.log(`Balance: ${Number(balance) / LAMPORTS_PER_SOL} SOL`);
Output from a local Surfpool network
Output
Balance: 1 SOL
Rust
use anyhow::Result;
use solana_client::nonblocking::rpc_client::RpcClient;
use solana_commitment_config::CommitmentConfig;
use solana_sdk::{native_token::LAMPORTS_PER_SOL, signature::Signer, signer::keypair::Keypair};
#[tokio::main]
async fn main() -> Result<()> {
let connection = RpcClient::new_with_commitment(
"http://localhost:8899".to_string(),
CommitmentConfig::confirmed(),
);
let wallet = Keypair::new();
let airdrop_signature = connection
.request_airdrop(&wallet.pubkey(), LAMPORTS_PER_SOL)
.await?;
loop {
let confirmed = connection.confirm_transaction(&airdrop_signature).await?;
if confirmed {
break;
}
}
let balance = connection.get_balance(&wallet.pubkey()).await?;
println!("Balance: {} SOL", balance as f64 / LAMPORTS_PER_SOL as f64);
Ok(())
}
Output from a local Surfpool network
Output
Balance: 1 SOL
Python
import asyncio
from solana.rpc.async_api import AsyncClient
from solders.keypair import Keypair
LAMPORTS_PER_SOL = 1_000_000_000
async def main():
async with AsyncClient("http://localhost:8899") as client:
wallet = Keypair()
airdrop = await client.request_airdrop(wallet.pubkey(), LAMPORTS_PER_SOL) # 1 SOL
print("Airdrop Signature:", airdrop.value)
asyncio.run(main())
Output from a local Surfpool network
Output
Airdrop Signature: je9LhSt1xzbjKBro8qpBW9rV2hQRLYCnamohta5YzJoQxfEB3A4jjj7ZzSyJdhuGdrdEcrZUzFCp189iH5ndCqk
Go
package main
import (
"context"
"fmt"
"github.com/solana-foundation/solana-go/v2"
"github.com/solana-foundation/solana-go/v2/rpc"
)
func main() {
client := rpc.New("http://localhost:8899")
wallet := solana.NewWallet()
sig, err := client.RequestAirdrop(
context.Background(), wallet.PublicKey(), solana.LAMPORTS_PER_SOL, rpc.CommitmentConfirmed, // 1 SOL
)
if err != nil {
panic(err)
}
fmt.Println("Airdrop Signature:", sig)
}
Output from a local Surfpool network
Output
Airdrop Signature: 3efa5dts3ugjHYzhqnd82FLUgfG7cUTS81ctUBtEyRkvP9ZFT979sgWsD6D4i3cLvmd4EZ52gwjY2NRhHcJxbdXK
Web3.js (legacy)
@solana/web3.js 1.x is no longer actively maintainedIt receives critical fixes only. Use Kit for new code. To migrate existing code, follow the Kit upgrade guide, or the web3.js v3 migration guide to keep the class-based API.To run this code, install 1.x with npm install @solana/web3.js@1. A plain npm install @solana/web3.js installs v3, which this code doesn’t run on.import { Connection, Keypair, LAMPORTS_PER_SOL } from "@solana/web3.js";
const connection = new Connection("http://localhost:8899", "confirmed");
const wallet = Keypair.generate();
const signature = await connection.requestAirdrop(
wallet.publicKey,
LAMPORTS_PER_SOL,
);
const { blockhash, lastValidBlockHeight } =
await connection.getLatestBlockhash();
await connection.confirmTransaction({
blockhash,
lastValidBlockHeight,
signature,
});
const balance = await connection.getBalance(wallet.publicKey);
console.log(`Balance: ${balance / LAMPORTS_PER_SOL} SOL`);
Output from a local Surfpool network
Output
Balance: 1 SOL