We've now generated a wallet and connected it to the Solana blockchain. In the previous step, we confirmed that our account's balance is zero but wouldn't it be nice to fund our account so we can see the balance change?
You might be wondering whether we'll now need to transfer real money so we can test the account, but as we discussed in Step 3, a blockchain's devnet typically provides a way to test transactions without risking real economic value.
In this step, we'll be building functionality to allow users to "airdrop" SOL tokens into their devnet account. In the crypto world, an airdrop is a way for the protocol to distribute tokens to account holders for free.
Figure 5: We're about reinforce our supply lines with some devnet SOL
In this case, we'll be tapping into the native devnet airdrop functionality built into Solana to fund our account. This is in contrast to mainnet airdrops performed by blockchain protocols and crypto projects, which are usually issued to reward early adopters or contributors.
On the right side of the wallet dashboard, you'll find a button with the label Airdrop. You might notice that this button goes away if you change the network. This is because the functionality we're implementing is only active for devnet. Moreover, you'll notice a tooltip help icon informing users that they'll receive 1 devnet SOL by clicking on Airdrop.
Once we've completed this step, our balance will automatically increase when we click Airdrop. This will position us well for Step 5 where we'll build functionality to send funds to other Solana accounts.
In Step 3 we learned how to instantiate a connection to one of Solana's networks, and how to assign our account's public key property to a variable. We can apply that same code here to start writing the
Following our previous heuristic of searching the docs for keywords, we can now search for "airdrop" to see if there's a function we can leverage. Lo and behold the
Connectionclass has a
requestAirdropfunction that looks promising. It takes in two properties -
lamports: number- and returns a
promisethat resolves to a string.
You might be wondering what a "lamport" is. Solana's native token, SOL, is divisible into 1 billion lamports. You can think of lamports as the cents to SOL's dollar.
Figure 6: This is also a lamport. Leslie Lamport is the lamport's namesake. He's a computer scientist who has made key contributions to distributed systems
It's not clear from the documentation, but after a little research you can confirm that the returned string represents a confirmation ID or
signature. From a function design standpoint, it seems reasonable that to request an airdrop we should pass in the account address (public key) and the amount of funds we're requesting.
The above function will work just fine and our users will be able to airdrop 1 SOL every time they click on the button. But you can clean it up a bit by importing and passing in the
LAMPORTS_PER_SOLconstant, which itself equals one billion, from web3.js.
Finally, before we can refresh our account's balance automatically, we need a way to make sure the blockchain ledger has been updated with our airdropped funds before we call
refreshBalance. As with any database-type operations, blockchain state changes are asynchronous. In fact, given the decentralized nature of most blockchain protocols, some updates can take a while.
With that in mind, we need to wait for the airdrop to be confirmed before refreshing the balance. Searching the docs once again, we can see that our
Connectionclass includes a
confirmTransactionmethod that takes in a confirmation
commitment, and returns a
promisethat resolves once the transaction is confirmed by the network.
Note that we don't need to assign a variable to the confirmation here. Once our wallet knows that the transaction was confirmed by the network, it can call
refreshBalanceand update the balance for the account.
Avid readers might have noticed that the balance in our account looks wrong. It reads one billion SOL when it should read 1 SOL. Now that we know what lamports are, how they relate to SOL, and how to use the
LAMPORTS_PER_SOLconstant, we can go back to our
refreshBalancefunction and fix the bug by updating its return value.
Figure 7: Getting close to the summit
We've come a long way and now have a better idea of how blockchain protocols work. We built a wallet by creating a keypair, connected to the network, fetched data from it, and successfully requested test tokens. We've covered all of the basic Web 3 interactions except the most important one - transferring funds. We'll do that next in Step 5, so brace yourself.
Listing 4.2: Code for airdropping funds
If you open the browser's console from the
/walletpage, and click on the Airdrop button, you'll notice a message that reads, "Airdrop functionality not implemented yet!". Navigate to
utils/index.tsin your editor and follow the steps included as comments to finish writing the
handleAirdropfunction. We include a description along with a link to the documentation you need to review in order to implement each line. The relevant code block is also included in Listing 4.1 below.
Listing 4.1: Instructions for writing airdrop function
Step 3: Fetch a Balance ⚖️
Step 5: Transfer Funds 💵