-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Airdrop SOL automatically to wallets #253
Changes from all commits
ea7cabd
f59a0a2
f324caa
07d4594
2bb4ac1
00421e1
4095c8d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import { | ||
ConnectionProvider, | ||
useWallet, | ||
WalletProvider, | ||
} from '@solana/wallet-adapter-react'; | ||
// import { LedgerWalletAdapter } from '@solana/wallet-adapter-wallets'; | ||
|
@@ -12,7 +13,7 @@ import { | |
import '@solana/wallet-adapter-react-ui/styles.css'; | ||
import * as sol from '@solana/web3.js'; | ||
import isElectron from 'is-electron'; | ||
import React, { FC, useMemo, useState } from 'react'; | ||
import { FC, useEffect, useMemo, useState } from 'react'; | ||
import { Button, Form } from 'react-bootstrap'; | ||
import Container from 'react-bootstrap/Container'; | ||
import OverlayTrigger from 'react-bootstrap/OverlayTrigger'; | ||
|
@@ -41,9 +42,11 @@ import { | |
} from './data/Config/configState'; | ||
import ValidatorNetwork from './data/ValidatorNetwork/ValidatorNetwork'; | ||
import { | ||
Net, | ||
netToURL, | ||
selectValidatorNetworkState, | ||
} from './data/ValidatorNetwork/validatorNetworkState'; | ||
import { NetStatus } from '../types/types'; | ||
|
||
// So we can electron | ||
declare global { | ||
|
@@ -120,6 +123,52 @@ function Sidebar() { | |
} | ||
|
||
function Topbar() { | ||
const wallet = useWallet(); | ||
const validator = useAppSelector(selectValidatorNetworkState); | ||
const { net } = validator; | ||
|
||
useEffect(() => { | ||
const airdropIfNeeded = async () => { | ||
if (validator.status !== NetStatus.Running) return; | ||
let amount = 0; | ||
|
||
// arbitrary -- want to let people top up | ||
// on devnet SOL when they can | ||
let airdropThreshold = 50; | ||
switch (net) { | ||
case Net.Dev: | ||
// limit per devnet aidrop request | ||
amount = 2; | ||
break; | ||
case Net.Test: | ||
// limit per testnet airdrop request | ||
amount = 1; | ||
// arbitrary | ||
airdropThreshold = 5; | ||
break; | ||
case Net.MainnetBeta: | ||
return; | ||
default: | ||
amount = 1000; | ||
} | ||
const solConn = new sol.Connection(netToURL(net)); | ||
if (wallet.publicKey) { | ||
try { | ||
const balance = await solConn.getBalance(wallet.publicKey); | ||
if (balance < airdropThreshold) { | ||
solConn.requestAirdrop( | ||
wallet.publicKey, | ||
sol.LAMPORTS_PER_SOL * amount | ||
); | ||
} | ||
} catch (e) { | ||
logger.error(e); | ||
} | ||
} | ||
}; | ||
airdropIfNeeded(); | ||
}, [net, wallet.publicKey, validator]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So this will happen once per change of network connection / wallet? (and so if at that point the network was offline, we won't try again? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yea. And validator status. So if a brand new local validator starts up without airdrop, it will airdrop again. Was quick to implement and felt a bit less aggressive than polling. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. polling bad :) |
||
|
||
return ( | ||
<div className="flex items-center p-1 px-2 bg-surface-400"> | ||
<span>Solana Workbench</span> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these should all be in the settings dialog (in a future release)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
agreed!