Solana: How to airdrop to a randomly generated keypair for tests?

I can provide a sample article on how to airdrop Solana keys using a randomly generated key pair for testing purposes.

Airdropping Solana Keys: A Step-by-Step Guide

Solana: How to airdrop to a randomly generated keypair for tests?

As a developer, it is very important to be able to deploy and test your blockchain applications in practice. In this example, we will show you how to create an airdrop on Testnet Solana using a randomly generated key pair for testing purposes.

Prerequisites

  • Install the Solana CLI (Command Line Interface) on your computer.
  • Create a Testnet account in Solana with the required credentials.

Step 1: Create a new wallet and key pair

Create a new wallet in Solana CLI:

”bash

Solana key create –output json

A JSON file will be created containing your wallet's private key and public address. We will use this private key for airdrop keys later.


Step 2: Generate random Airdrop key pairs



To generate random key pairs, we can use the module "solana_sdk::keypair".

Rust

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

use solana_sdk::sysvar::Pubkey;

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

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

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

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

while true {

matches &*key_pair.public_key.and_ref() {

Pubkey::Random => break,

_ => (),

}

}

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

let wallet_out = TransactionOutput::new(

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

SolanaId::default(),

old![],

vec::new(),

);

(key_pair, wallet_out)

}

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


Step 3: Create an Airdrop Transaction

Create an airdrop transaction using the Transaction struct.

Rust

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

fn create_airdrop_transaction(

public_key_pair: key pair,

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.and_ref(),

SolanaId::default(),

old![],

old::new(),

),

);

tx.add_amount(1e18); // 1 ether

tx.sign(&public_key_pair)

}

This function creates a new transaction with the public key of the key pair, an I/O transaction to obtain the keys, and a single amount of 1 ether.


Step 4: Debit Account

To debit an account on Testnet Solana, we 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_summa: u64) -> Result<(), &'static str> {

let mut debits = DebitBuilder::new();

debits.add_transaction(

TransactionBuilder::new()

.set_system_instruction_id(“flow”)

.build(),

);

debits.set_fee(1000); // 1 ETH

matches &*key_pair.key.public_key.and_ref() {

Pubkey::Random => return OK(()),

_ => (),

}

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

debits.add_sum(sum);

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

match debits.sign(&key_pair) {

No(_) => {

// Successful transaction

println!

اترك ردّاً

لن يتم نشر عنوان بريدك الإلكتروني. الحقول الإلزامية مشار إليها بـ *