MoveAndUp

I can provide you with a sample article on how to airdrop Solana keys using a randomly generated keypair for testing purposes.

Airdropping Solana Keys: A Step-by-Step Guide

As a developer, it’s essential to practice deploying and testing your blockchain applications. In this example, we’ll demonstrate how to create an airdrop in Testnet Solana using a randomly generated keypair for testing purposes.

Prerequisites

  • Install the Solana CLI (Command Line Interface) on your machine.

  • Set up a Testnet account on Solana with the necessary credentials.

Step 1: Create a New Wallet and Keypair

Create a new wallet in Solana CLI:

solana key create < wallet_name > --output json

This will create a JSON file containing your wallet’s private key and public address. We’ll use this private key to airdrop keys later.

Step 2: Generate Random Keypairs for Airdrop

To generate random keypairs, we can use the solana_sdk::keypair module.

use solana_sdk::signer::{Key, Signer};

use solana_sdk::sysresponse::Pubkey;

use solana_sdk::transaction::{TransactionId, TransactionOutput};

use solana_sdk::keypair::{Keypair, KeypairBuilder};

fn generate_airdrop_keypair() -> (Keypair, Keypair) {

let mut key_pair = KeypairBuilder::new().generate_keyid().unwrap();

while true {

match &*key_pair.public_key.as_ref() {

Pubkey::Random => break,

_ => (),

} }

} }

let wallet_id = Key::from_bytes(&key_pair.key.id).unwrap();

let wallet_out = TransactionOutput::new(

Keyword::from_bytes(&wallet_id).unwrap(),

SolanaId::default(),

vec![],

Vec::new(),

);

(key_pair, wallet_out)

} }

This function generates a new random keypair and creates a transaction output with the public key of the keypair. We’ll use this output to airdrop keys later.

Step 3: Create an Airdrop Transaction

Create an airdrop transaction using the Transaction struct.

use solana_sdk::transaction::{Transaction, TransactionBuilder};

fn create_airdrop_transaction(

public_key_pair: Keypair,

airdrop_id: Pubkey,

) -> Transaction {

let mut tx = TransactionBuilder::new()

.set_system_instruction_id("airdrop") .

.build ( ) ;

tx.add_input_output_transaction(

TransactionOutput::new(

public_key_pair.public_key.as_ref(),

SolanaId::default(),

vec![],

Vec::new(),

),

);

thx add_amount ( 1e18 ) ; // 1 ether

thx sign ( & public_key_pair ) .

} }

This function creates a new transaction with the public key of the keypair, an input output transaction to receive keys, and a single amount of 1 ether.

Step 4: Debit an Account

To debit an account in Testnet Solana, you need to use the solana_sdk::debits module. You can find more information about debiting accounts in the [Solana SDK Documentation] (

For example:

« `rust

use solana_sdk::debits;

fn debit_account(key_pair: Keypair, debit_amount: u64) -> Result<(), &'static str> {

let mute debits = DebitsBuilder::new();

debits. add_transaction(

TransactionBuilder::new()

.set_system_instruction_id(« debit ») .

.build(),

);

debits.set_fee(1000); // 1 ETH

match &*key_pair.key.public_key.as_ref() {

Pubkey :: Random = > return Ok ( ( ( ) )

_ => (),

} }

let amount = u64::from_debited_amount(debit_amount, 1e18);

debits. add_amount (amount);

let transaction_id = key_pair.public_key.as_ref().unwrap();

match debits.sign(&key_pair) {

Ok(_) => {

// Successful transaction

println!