Skip to content
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

Merged
merged 7 commits into from
Jul 19, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 50 additions & 1 deletion src/renderer/App.tsx
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';
Expand All @@ -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';
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Copy link
Member

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)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed!

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]);
Copy link
Member

Choose a reason for hiding this comment

The 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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The 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>
Expand Down