1import { useState, useCallback, useEffect } from 'react';
2import { toBN } from 'web3-utils';
3import * as azimuth from 'azimuth-js';
4import { FORM_ERROR } from 'final-form';
6import * as wg from 'lib/walletgen';
7import * as tank from 'lib/tank';
8import * as need from 'lib/need';
9import pluralize from 'lib/pluralize';
10import { GAS_LIMITS } from 'lib/constants';
13 sendSignedTransaction,
14 waitForTransactionConfirm,
17import useGasPrice from 'lib/useGasPrice';
18import { MIN_PLANET } from 'lib/constants';
19import { safeToWei } from 'lib/lib';
21import { useNetwork } from 'store/network';
22import { useWallet } from 'store/wallet';
23import { usePointCursor } from 'store/pointCursor';
24import { usePointCache } from 'store/pointCache';
26import { WARNING } from 'form/helpers';
28const GAS_LIMIT = GAS_LIMITS.GIFT_PLANET;
36const useInviter = () => {
37 const { contracts, web3, networkType } = useNetwork();
38 const { wallet, walletType, walletHdPath } = useWallet();
39 const { pointCursor } = usePointCursor();
40 const { syncInvites } = usePointCache();
41 const { gasPrice } = useGasPrice();
42 const point = need.point(pointCursor);
44 const [needFunds, setNeedFunds] = useState(null);
45 const [invites, setInvites] = useState([]);
47 const [progress, setProgress] = useState(0);
48 const [txStatus, setTxStatus] = useState(STATUS.INPUT);
50 const isDone = txStatus === STATUS.SUCCESS;
55 }, [isDone, syncInvites, point]);
57 const resetInvites = useCallback(() => {
60 setTxStatus(STATUS.INPUT);
61 }, [setProgress, setInvites, setTxStatus]);
63 const generateInvites = useCallback(
65 const _contracts = contracts.getOrElse(null);
66 const _web3 = web3.getOrElse(null);
67 const _wallet = wallet.getOrElse(null);
68 if (!_contracts || !_web3 || !_wallet) {
69 // not using need because we want a custom error
70 throw new Error('Internal Error: Missing Contracts/Web3/Wallet');
73 const nonce = await _web3.eth.getTransactionCount(_wallet.address);
74 const chainId = await _web3.eth.net.getId();
75 const planets = await azimuth.delegatedSending.getPlanetsToSend(
81 setTxStatus(STATUS.SENDING);
82 // account for the race condition where invites got used up while we were
83 // composing our target list
84 if (planets.length < numInvites) {
85 // resync invites to the cache, since they're out of date
88 setTxStatus(STATUS.FAILURE);
93 `Can currently only send ${planets.length} invites. ` +
94 `Please remove invites until you are within the limit.`,
99 // NB(shrugs) - must be processed in serial because main thread, etc
100 let signedInvites = [];
102 for (let i = 0; i < numInvites; i++) {
103 setProgress(x => x + 1);
105 const planet = planets[i];
107 const { ticket, owner } = await wg.generateTemporaryTicketAndWallet(
109 // we're always giving planets, so generate a ticket of the correct size
112 const inviteTx = azimuth.delegatedSending.sendPoint(
119 const signedTx = await signTransaction({
126 // TODO: ^ make a useTransactionSigner to encapsulate this logic
128 gasPrice: gasPrice.toFixed(), // expects string gwei
129 gasLimit: GAS_LIMIT.toFixed(),
132 const rawTx = hexify(signedTx.serialize());
134 signedInvites.push({ ticket, signedTx, rawTx });
136 console.error(error);
139 [WARNING]: `There was an error while generating wallets.`,
145 const totalCost = toBN(GAS_LIMIT)
147 .mul(toBN(numInvites));
148 const tankWasUsed = await tank.ensureFundsFor(
153 safeToWei(totalCost, 'gwei'),
154 signedInvites.map(si => si.rawTx),
155 (address, minBalance, balance) =>
156 setNeedFunds({ address, minBalance, balance }),
157 () => setNeedFunds(undefined)
160 let unsentInvites = [];
161 let confirmedInvites = [];
162 const txAndMailings = signedInvites.map(async invite => {
164 const txHash = await sendSignedTransaction(
170 await waitForTransactionConfirm(_web3, txHash);
171 confirmedInvites.push(invite);
173 console.error(error);
174 unsentInvites.push(invite);
179 await Promise.all(txAndMailings);
181 setTxStatus(STATUS.SUCCESS);
182 setInvites(confirmedInvites);
184 if (unsentInvites.length > 0) {
185 return { errors: { [FORM_ERROR]: unsentInvites } };
188 if (errorCount > 0) {
191 [WARNING]: `There ${pluralize(
198 )} while generating wallets. You can still send the invites that generated correctly.`,
202 return { invites: confirmedInvites };
227export default useInviter;
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list