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