Created
January 14, 2022 01:57
-
-
Save topherPedersen/ae0ede07a6b6beca5fedfb74c471c324 to your computer and use it in GitHub Desktop.
Solana 101: Funding Your Solana Account via Airdrop (From Figment.io's Solana 101 Course)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Connection, PublicKey, LAMPORTS_PER_SOL} from '@solana/web3.js'; | |
import type {NextApiRequest, NextApiResponse} from 'next'; | |
import {getNodeURL} from '@figment-solana/lib'; | |
export default async function fund( | |
req: NextApiRequest, | |
res: NextApiResponse<string>, | |
) { | |
try { | |
const {network, address} = req.body; | |
const url = getNodeURL(network); | |
const connection = new Connection(url, 'confirmed'); | |
const publicKey = new PublicKey(address); | |
const hash = await connection.requestAirdrop(publicKey, LAMPORTS_PER_SOL); | |
await undefined; | |
res.status(200).json(hash); | |
} catch (error) { | |
let errorMessage = error instanceof Error ? error.message : 'Unknown Error'; | |
res.status(500).json(errorMessage); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment