-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a49d769
commit 40b5045
Showing
1 changed file
with
66 additions
and
53 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,70 @@ | ||
import { useConnection, useWallet } from '@solana/wallet-adapter-react'; | ||
import { LAMPORTS_PER_SOL, TransactionSignature } from '@solana/web3.js'; | ||
import { FC, useCallback } from 'react'; | ||
import { useConnection, useWallet } from "@solana/wallet-adapter-react"; | ||
import { LAMPORTS_PER_SOL, TransactionSignature } from "@solana/web3.js"; | ||
import { FC, useCallback } from "react"; | ||
import { notify } from "../utils/notifications"; | ||
import useUserSOLBalanceStore from '../stores/useUserSOLBalanceStore'; | ||
import useUserSOLBalanceStore from "../stores/useUserSOLBalanceStore"; | ||
|
||
export const RequestAirdrop: FC = () => { | ||
const { connection } = useConnection(); | ||
const { publicKey } = useWallet(); | ||
const { getUserSOLBalance } = useUserSOLBalanceStore(); | ||
|
||
const onClick = useCallback(async () => { | ||
if (!publicKey) { | ||
console.log('error', 'Wallet not connected!'); | ||
notify({ type: 'error', message: 'error', description: 'Wallet not connected!' }); | ||
return; | ||
} | ||
|
||
let signature: TransactionSignature = ''; | ||
|
||
try { | ||
signature = await connection.requestAirdrop(publicKey, LAMPORTS_PER_SOL); | ||
|
||
// Get the lates block hash to use on our transaction and confirmation | ||
let latestBlockhash = await connection.getLatestBlockhash() | ||
await connection.confirmTransaction({ signature, ...latestBlockhash }, 'confirmed'); | ||
|
||
notify({ type: 'success', message: 'Airdrop successful!', txid: signature }); | ||
|
||
getUserSOLBalance(publicKey, connection); | ||
} catch (error: any) { | ||
notify({ type: 'error', message: `Airdrop failed!`, description: error?.message, txid: signature }); | ||
console.log('error', `Airdrop failed! ${error?.message}`, signature); | ||
} | ||
}, [publicKey, connection, getUserSOLBalance]); | ||
|
||
return ( | ||
|
||
<div className="flex flex-row justify-center"> | ||
<div className="relative group items-center"> | ||
<div className="m-1 absolute -inset-0.5 bg-gradient-to-r from-indigo-500 to-fuchsia-500 | ||
rounded-lg blur opacity-20 group-hover:opacity-100 transition duration-1000 group-hover:duration-200 animate-tilt"></div> | ||
|
||
<button | ||
className="px-8 m-2 btn animate-pulse bg-gradient-to-br from-indigo-500 to-fuchsia-500 hover:from-white hover:to-purple-300 text-black" | ||
onClick={onClick} | ||
> | ||
<span>Airdrop 1 </span> | ||
|
||
</button> | ||
</div> | ||
</div> | ||
|
||
|
||
); | ||
}; | ||
const { connection } = useConnection(); | ||
const { publicKey } = useWallet(); | ||
const { getUserSOLBalance } = useUserSOLBalanceStore(); | ||
|
||
const onClick = useCallback(async () => { | ||
if (!publicKey) { | ||
console.log("error", "Wallet not connected!"); | ||
notify({ | ||
type: "error", | ||
message: "error", | ||
description: "Wallet not connected!", | ||
}); | ||
return; | ||
} | ||
|
||
let signature: TransactionSignature = ""; | ||
|
||
try { | ||
signature = await connection.requestAirdrop(publicKey, LAMPORTS_PER_SOL); | ||
|
||
// Get the lates block hash to use on our transaction and confirmation | ||
let latestBlockhash = await connection.getLatestBlockhash(); | ||
await connection.confirmTransaction( | ||
{ signature, ...latestBlockhash }, | ||
"confirmed" | ||
); | ||
|
||
notify({ | ||
type: "success", | ||
message: "Airdrop successful!", | ||
txid: signature, | ||
}); | ||
|
||
getUserSOLBalance(publicKey, connection); | ||
} catch (error: any) { | ||
notify({ | ||
type: "error", | ||
message: `Airdrop failed!`, | ||
description: error?.message, | ||
txid: signature, | ||
}); | ||
console.log("error", `Airdrop failed! ${error?.message}`, signature); | ||
} | ||
}, [publicKey, connection, getUserSOLBalance]); | ||
|
||
return ( | ||
<div className="flex flex-row justify-center"> | ||
<div className="relative group items-center"> | ||
<div | ||
className="m-1 absolute -inset-0.5 bg-gradient-to-r from-indigo-500 to-fuchsia-500 | ||
rounded-lg blur opacity-20 group-hover:opacity-100 transition duration-1000 group-hover:duration-200 animate-tilt" | ||
></div> | ||
|
||
<button | ||
className="px-8 m-2 btn animate-pulse bg-gradient-to-br from-indigo-500 to-fuchsia-500 hover:from-white hover:to-purple-300 text-black" | ||
onClick={onClick} | ||
> | ||
<span>Recieve 1 Sol on Devnet</span> | ||
</button> | ||
</div> | ||
</div> | ||
); | ||
}; |