I can provide you with a sample article on how to perform Solana key airdrop using a randomly generated key pair for testing purposes.
Solana Key Airdropping: A Step-by-Step Guide
As a developer, it is important to practice deploying and testing blockchain applications. In this example, we will show you how to airdrop to Solana Testnet using a randomly generated key pair for testing purposes.
Prerequisites
- Install the Solana CLI (Command Line Interface) on your computer.
- Set up a Testnet account on Solana with the required credentials.
Step 1. Create a new wallet and key pair
Create a new wallet in Solana CLI:
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 to perform the key airdrop later.
Step 2: Generate random key pairs for Airdrop
To generate random key pairs, we can use the module "solana_sdk::keypair".
use solana_sdk::signer::{Keyid, Signer};
use solana_sdk::sysvar::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 {
matches &*key_pair.public_key.as_ref() {
Pubkey::Random => break,
_ => (),
}
}
let wallet_id = Keyid::from_bytes(&key_pair.key.id).unwrap();
let wallet_out = TransactionOutput::new(
Keyid::from_bytes(&wallet_id).unwrap(),
SolanaId::default(),
old![],
old::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 use the airdrop keys later.
Step 3: Create an airdrop transaction
Create an airdrop transaction using the Transaction structure.
use solana_sdk::transaction::{Transaction, TransactionBuilder};
fn create_airdrop_transaction(
public_key_pair: key pair,
airdrop_id: public key,
) -> 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(),
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, the input and output transaction keys to receive, and one amount of ether.
Step 4: Debit the Account
To debit an account on Solana Testnet, we need to use the solana_sdk::debits module. More information about account debits can be found 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 mut debits = DebitsBuilder::new();
debits.add_transaction(
TransactionBuilder::new()
.set_system_instruction_id(“debit”)
.build(),
);
debits.set_fee(1000); // 1 ETH
matches &*key_pair.key.public_key.as_ref() {
Pubkey::Random => return OK(()),
_ => (),
}
let sum = u64::from_debited_sum(debit_amount, 1e18);
debits.add_sum(sum);
let transaction_id = key_pair.public_key.as_ref().unwrap();
match debits.sign(&key_pair) {
OK(_) => {
//Successful transaction
println!