work-sample/src/App.tsx

103 lines
2.6 KiB
TypeScript
Raw Normal View History

2022-12-07 18:47:26 -08:00
import React, {useState} from 'react';
2022-12-07 18:34:36 -08:00
import logo from './logo.svg';
import './App.css';
2022-12-07 18:55:13 -08:00
import Web3 from 'web3';
2022-12-07 20:56:39 -08:00
const web3 = new Web3(Web3.givenProvider);
2022-12-07 21:31:13 -08:00
import Airdrop from "./Airdrop.json";
2022-12-07 20:56:39 -08:00
const airdropAddress = "0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512";
2022-12-07 21:31:13 -08:00
const airdropContract = new web3.eth.Contract(Airdrop.abi as any, airdropAddress);
2022-12-07 20:56:39 -08:00
2022-12-07 18:47:26 -08:00
interface AddressesProps {
addressList: string[];
setAddressListFn: any;
}
function Addresses(props: AddressesProps) {
const [inputState, setInputState] = useState("");
2022-12-07 18:55:13 -08:00
const [errorText, setErrorText] = useState("");
2022-12-07 21:31:13 -08:00
const [numTokens, setNumTokens] = useState(0);
2022-12-07 18:47:26 -08:00
const { setAddressListFn, addressList } = props;
const save = () => {
2022-12-07 20:56:39 -08:00
if (!web3.utils.isAddress(inputState)) {
2022-12-07 18:55:13 -08:00
setErrorText("Invalid ETH address");
return;
}
setErrorText("");
2022-12-07 20:56:39 -08:00
const checksumAddr = web3.utils.toChecksumAddress(inputState);
2022-12-07 18:55:13 -08:00
if (addressList.includes(checksumAddr)) {
2022-12-07 18:47:26 -08:00
//Do nothing
} else {
2022-12-07 18:55:13 -08:00
setAddressListFn([...addressList, checksumAddr]);
2022-12-07 18:47:26 -08:00
}
setInputState("");
}
2022-12-07 21:31:13 -08:00
2022-12-07 21:34:45 -08:00
var num;
2022-12-07 23:16:46 -08:00
if (isNaN(numTokens) || numTokens < 0) {
2022-12-07 21:34:45 -08:00
num = 0;
} else {
num = numTokens;
}
2022-12-07 21:31:13 -08:00
2022-12-07 18:47:26 -08:00
return (
2022-12-07 21:31:13 -08:00
<div>
<div>
2022-12-07 18:47:26 -08:00
<input id="address" value={inputState} onChange={ (evt) => setInputState(evt.target.value) } ></input>
2022-12-07 18:55:13 -08:00
<div className="addressError">{errorText}</div>
2022-12-07 18:47:26 -08:00
<button onClick={save}>Add address</button>
2022-12-07 21:31:13 -08:00
</div>
<div>
Number of tokens:
<input type="numeric" value={num} onChange={ (evt) => setNumTokens(parseInt(evt.target.value)) }></input>
</div>
</div>
);
2022-12-07 18:47:26 -08:00
}
interface AddressListProps {
addressList: string[];
};
function AddressList({addressList}: AddressListProps) {
const addresses = addressList.length == 0 ?
<p>No addresses specified yet</p> :
addressList.map((addr: string) => <div key={addr} className="addressItem">{addr}</div>);
return (
<div>
<h2>Addresses to airdrop to: </h2>
{ addresses }
</div>
)
}
2022-12-07 18:34:36 -08:00
function App() {
2022-12-07 18:47:26 -08:00
const [addressList, setAddressList] = useState([]);
2022-12-07 23:16:46 -08:00
async function performAirdrop() {
console.log("Performing airdrop");
}
2022-12-07 18:34:36 -08:00
return (
<div className="App">
2022-12-07 18:47:26 -08:00
<h1>Airdrop App</h1>
<p>Add an address to airdrop to:</p>
<Addresses addressList={addressList} setAddressListFn={setAddressList}/>
2022-12-07 23:16:46 -08:00
<button disabled={addressList.length == 0} onClick={performAirdrop} >Perform Airdrop!</button>
2022-12-07 18:47:26 -08:00
<AddressList addressList={addressList} />
2022-12-07 18:34:36 -08:00
</div>
);
}
export default App;