I can provide you with an example article on how to airdrop Solana keys using a randomly generated key pair for testing purposes.
Solana Key Dropping: A Step-by-Step Guide
As a developer, you need to practice deploying and testing your blockchain applications. In this example, we will show you how to airdrop Solana Testnet using a randomly generated key pair for testing purposes.
Prerequisites
- Install Solana CLI (command line interface) on your computer.
- Set up a Testnet account in Solana with the necessary credentials.
Step 1: Create a new wallet and key pair
Create a new wallet in Solana CLI:
solana key create --output json
This will create a JSON file containing your wallet’s private key and public address. We will use this private key to airdrop keys later.
Step 2: Generating Random Key Pairs for Airdrop
To generate random key pairs, we can use the solana_sdk::keypair
module.
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 {
match &*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(),
vec![],
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 for key dumping 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: 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(),
),
);
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 input/output transaction to receive the keys, and a single amount of 1 ether.
Step 4: Debiting the account
To debit an account in Testnet Solana, we need to use the solana_sdk::debits
module. More information on debiting accounts 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
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(_) => {
// Transaction completed successfully
println!
Leave a Reply