More logic

This commit is contained in:
Greg Shuflin 2022-12-08 12:55:35 -08:00
parent 3ab93a62d8
commit f4c8da118a

View File

@ -75,13 +75,14 @@ function Profile() {
interface AddressesProps { interface AddressesProps {
addressList: string[]; addressList: string[];
setAddressListFn: any; setAddressListFn: any;
numTokens: number;
setNumTokens: any;
} }
function Addresses(props: AddressesProps) { function Addresses(props: AddressesProps) {
const [inputState, setInputState] = useState(""); const [inputState, setInputState] = useState("");
const [errorText, setErrorText] = useState(""); const [errorText, setErrorText] = useState("");
const [numTokens, setNumTokens] = useState(0); const { setAddressListFn, addressList, numTokens, setNumTokens } = props;
const { setAddressListFn, addressList } = props;
const save = () => { const save = () => {
if (!web3.utils.isAddress(inputState)) { if (!web3.utils.isAddress(inputState)) {
@ -141,22 +142,32 @@ function AddressList({addressList}: AddressListProps) {
function App() { function App() {
const [addressList, setAddressList] = useState([]); const [addressList, setAddressList] = useState([]);
const { address: ownerAddress } = useAccount() const [numTokens, setNumTokens] = useState(0);
const { address: ownerAddress, isConnected } = useAccount()
async function performAirdrop() { async function performAirdrop() {
console.log("Performing airdrop"); console.log("Performing airdrop");
const totalCoins = addressList.length * numTokens;
console.log(magnaTokenContract.methods); console.log(`Owner ${ownerAddress} Airdrop ${airdropAddress}`);
const allowance = await magnaTokenContract.methods.allowance(ownerAddress, airdropAddress).call({ from: ownerAddress }); const allowance = await magnaTokenContract.methods.allowance(ownerAddress, airdropAddress).call({ from: ownerAddress });
console.log(`Allowance: ${allowance}`); console.log(`Allowance: ${allowance}`);
if (allowance <= 0) { if (allowance < totalCoins) {
//TODO prompt to set a positive balance const output = await magnaTokenContract.methods.approve(airdropAddress, totalCoins).send({ from: ownerAddress });
return; console.log(output);
} }
const recipients = addressList;
const amounts = addressList.map((_addr) => numTokens);
const output = await airdropContract.methods.airdropTokens(recipients, amounts).send({ from: ownerAddress });
console.log(output);
} }
const airdropButtonDisabled = addressList.length == 0 || !isConnected;
return ( return (
<WagmiConfig client={wagmiClient}> <WagmiConfig client={wagmiClient}>
<div className="App"> <div className="App">
@ -165,8 +176,8 @@ function App() {
<h1>Airdrop App</h1> <h1>Airdrop App</h1>
<p>Add an address to airdrop to:</p> <p>Add an address to airdrop to:</p>
<Addresses addressList={addressList} setAddressListFn={setAddressList}/> <Addresses addressList={addressList} setAddressListFn={setAddressList} numTokens={numTokens} setNumTokens={setNumTokens}/>
<button disabled={addressList.length == 0} onClick={performAirdrop} >Perform Airdrop!</button> <button disabled={airdropButtonDisabled} onClick={performAirdrop} >Perform Airdrop!</button>
<AddressList addressList={addressList} /> <AddressList addressList={addressList} />
</div> </div>