on master help sign in
Merge branch 'mino' into feat/final-form
commit39bc8174a1d1a5728d60212b8817570e0aaa52b7 authorMatt Condon (shrugs) <mattgcondon@gmail.com> dateTue 6 Aug 2019, 12:03 (7 years ago) parent228dce8d04c2c1
6 files changed+88−20
src/lib/reticket.js+6−3 src/lib/useEthereumTransaction.js+21−7 src/lib/useSuggestedGasPrice.js+40 src/views/Activate/PassportTransfer.js+3−1 src/views/Admin/Reticket/ReticketExecute.js+2−1 src/views/Invite/InviteEmail.js+16−8
src/lib/reticket.js+6−3
@@ -10,7 +10,8 @@ import {
1010} from './keys';
1111import { addHexPrefix } from './wallet';
1212import { sendAndAwaitAll } from './txn';
13−import { GAS_LIMITS, DEFAULT_GAS_PRICE_GWEI } from './constants';
13+import { getSuggestedGasPrice } from './useSuggestedGasPrice';
14+import { GAS_LIMITS } from './constants';
1415import { toWei } from 'web3-utils';
1516
1617// the initial network key revision is always 1
@@ -31,6 +32,7 @@ export async function reticketPointBetweenWallets({
3132 point,
3233 web3,
3334 contracts,
35+ networkType,
3436 onUpdate,
3537 transferEth = false,
3638 nextRevision = INITIAL_NETWORK_KEY_REVISION,
@@ -140,12 +142,14 @@ export async function reticketPointBetweenWallets({
140142
141143 progress(TRANSACTION_PROGRESS.SIGNING);
142144
145+ const suggestedGasPrice = await getSuggestedGasPrice(networkType);
146+ const gasPrice = parseInt(toWei(suggestedGasPrice.toFixed(), 'gwei'), 10);
143147 let totalCost = 0;
144148 let inviteNonce = await web3.eth.getTransactionCount(fromWallet.address);
145149 txs = txs.map(tx => {
146150 tx.from = fromWallet.address;
147151 tx.nonce = inviteNonce++;
148− tx.gasPrice = parseInt(toWei(DEFAULT_GAS_PRICE_GWEI.toFixed(), 'gwei'), 10);
152+ tx.gasPrice = gasPrice;
149153 totalCost = totalCost + tx.gasPrice * tx.gas;
150154 return tx;
151155 });
@@ -191,7 +195,6 @@ export async function reticketPointBetweenWallets({
191195
192196 // if non-trivial eth left in invite wallet, transfer to new ownership
193197 let balance = await web3.eth.getBalance(fromWallet.address);
194− const gasPrice = parseInt(toWei(DEFAULT_GAS_PRICE_GWEI.toFixed(), 'gwei'));
195198 const gasLimit = GAS_LIMITS.SEND_ETH;
196199 const sendEthCost = gasPrice * gasLimit;
197200 if (transferEth && balance > sendEthCost) {
src/lib/useEthereumTransaction.js+21−7
@@ -5,6 +5,7 @@ import { fromWei, toWei } from 'web3-utils';
55import { useNetwork } from 'store/network';
66import { useWallet } from 'store/wallet';
77import { usePointCursor } from 'store/pointCursor';
8+import { useSuggestedGasPrice } from 'lib/useSuggestedGasPrice';
89
910import { GAS_LIMITS } from './constants';
1011import {
@@ -60,6 +61,7 @@ export default function useEthereumTransaction(
6061 const [nonce, setNonce] = useState();
6162 const [gasPrice, setGasPrice] = useState(initialGasPrice); // gwei
6263 const [suggestedGasPrice, setSuggestedGasPrice] = useState(gasPrice); // gwei
64+ const { gasPrice: estimatedGasPrice } = useSuggestedGasPrice();
6365 const [gasLimit] = useState(initialGasLimit);
6466 const [unsignedTransaction, setUnsignedTransaction] = useState();
6567 const [signedTransaction, setSignedTransaction] = useState();
@@ -188,6 +190,15 @@ export default function useEthereumTransaction(
188190 suggestedGasPrice,
189191 ]);
190192
193+ useEffect(() => {
194+ const gasPrice = parseInt(
195+ fromWei(estimatedGasPrice.toString(), 'gwei'),
196+ 10
197+ );
198+ setSuggestedGasPrice(gasPrice);
199+ setGasPrice(gasPrice);
200+ }, [estimatedGasPrice]);
201+
191202 useEffect(() => {
192203 let mounted = true;
193204 // if nonce or chainId is undefined, re-fetch on-chain info
@@ -198,29 +209,32 @@ export default function useEthereumTransaction(
198209 (async () => {
199210 try {
200211 setError(undefined);
201− const [nonce, chainId, estimatedGasPrice] = await Promise.all([
212+ const [nonce, chainId] = await Promise.all([
202213 _web3.eth.getTransactionCount(_wallet.address),
203214 _web3.eth.net.getId(),
204− _web3.eth.getGasPrice(),
205215 ]);
206216
207− const gasPrice = parseInt(fromWei(estimatedGasPrice, 'gwei'), 10);
208−
209217 if (!mounted) {
210218 return;
211219 }
212220
213221 setNonce(nonce);
214222 setChainId(chainId);
215− setSuggestedGasPrice(gasPrice);
216− setGasPrice(gasPrice);
217223 } catch (error) {
218224 setError(error);
219225 }
220226 })();
221227
222228 return () => (mounted = false);
223− }, [_wallet, _web3, setError, nonce, chainId]);
229+ }, [
230+ _wallet,
231+ _web3,
232+ setError,
233+ nonce,
234+ chainId,
235+ networkType,
236+ estimatedGasPrice,
237+ ]);
224238
225239 useEffect(() => {
226240 let mounted = true;
src/lib/useSuggestedGasPrice.jsnew file+40
@@ -0,0 +1,40 @@
1+import { useState, useEffect } from 'react';
2+import { NETWORK_TYPES } from './network';
3+import { DEFAULT_GAS_PRICE_GWEI } from './constants';
4+
5+async function getSuggestedGasPrice(networkType) {
6+ switch (networkType) {
7+ case NETWORK_TYPES.ROPSTEN:
8+ return 10;
9+ case NETWORK_TYPES.OFFLINE:
10+ return DEFAULT_GAS_PRICE_GWEI;
11+ default:
12+ try {
13+ const response = await fetch(
14+ 'https://ethgasstation.info/json/ethgasAPI.json',
15+ {
16+ method: 'GET',
17+ cache: 'no-cache',
18+ }
19+ );
20+ const json = await response.json();
21+ return Math.min(json.fast / 10, 20); // to gwei
22+ } catch (e) {
23+ return DEFAULT_GAS_PRICE_GWEI;
24+ }
25+ }
26+}
27+
28+function useSuggestedGasPrice(networkType) {
29+ const [gasPrice, _setGasPrice] = useState(DEFAULT_GAS_PRICE_GWEI);
30+
31+ useEffect(() => {
32+ (async () => {
33+ _setGasPrice(await getSuggestedGasPrice(networkType));
34+ })();
35+ }, [networkType]);
36+
37+ return { gasPrice };
38+}
39+
40+export { getSuggestedGasPrice, useSuggestedGasPrice };
src/views/Activate/PassportTransfer.js+3−1
@@ -45,7 +45,7 @@ export default function PassportTransfer({ className, resetActivateRouter }) {
4545 const { replaceWith, names } = useHistory();
4646 const { setUrbitWallet } = useWallet();
4747 const { setPointCursor } = usePointCursor();
48− const { web3, contracts } = useNetwork();
48+ const { web3, contracts, networkType } = useNetwork();
4949 const {
5050 derivedWallet,
5151 derivedPoint,
@@ -112,6 +112,7 @@ export default function PassportTransfer({ className, resetActivateRouter }) {
112112 point: _point,
113113 web3: _web3,
114114 contracts: _contracts,
115+ networkType,
115116 onUpdate: handleUpdate,
116117 transferEth: true,
117118 });
@@ -133,6 +134,7 @@ export default function PassportTransfer({ className, resetActivateRouter }) {
133134 derivedPoint,
134135 web3,
135136 contracts,
137+ networkType,
136138 setUrbitWallet,
137139 setPointCursor,
138140 handleUpdate,
src/views/Admin/Reticket/ReticketExecute.js+2−1
@@ -42,7 +42,7 @@ const labelForProgress = progress => {
4242
4343export default function ReticketExecute({ newWallet, setNewWallet }) {
4444 const { popTo, names, reset } = useHistory();
45− const { web3, contracts } = useNetwork();
45+ const { web3, contracts, networkType } = useNetwork();
4646 const { wallet, setWalletType, resetWallet, setUrbitWallet } = useWallet();
4747 const { pointCursor } = usePointCursor();
4848 const { getDetails } = usePointCache();
@@ -65,6 +65,7 @@ export default function ReticketExecute({ newWallet, setNewWallet }) {
6565 point: point,
6666 web3: need.web3(web3),
6767 contracts: need.contracts(contracts),
68+ networkType,
6869 onUpdate: handleUpdate,
6970 nextRevision: networkRevision + 1,
7071 });
src/views/Invite/InviteEmail.js+16−8
@@ -34,7 +34,8 @@ import {
3434 hexify,
3535} from 'lib/txn';
3636import * as tank from 'lib/tank';
37−import { MIN_PLANET, GAS_LIMITS, DEFAULT_GAS_PRICE_GWEI } from 'lib/constants';
37+import { MIN_PLANET, GAS_LIMITS } from 'lib/constants';
38+import { useSuggestedGasPrice } from 'lib/useSuggestedGasPrice';
3839import * as need from 'lib/need';
3940import * as wg from 'lib/walletgen';
4041import useSetState from 'lib/useSetState';
@@ -59,10 +60,6 @@ import FormError from 'form/FormError';
5960const INITIAL_VALUES = { emails: [''] };
6061
6162const GAS_LIMIT = GAS_LIMITS.GIFT_PLANET;
62−const INVITE_COST = toWei(
63− (DEFAULT_GAS_PRICE_GWEI * GAS_LIMIT).toString(),
64− 'gwei'
65−);
6663const HAS_RECEIVED_TEXT = 'This email has already received an invite.';
6764
6865const STATUS = {
@@ -106,6 +103,7 @@ export default function InviteEmail() {
106103 const { pointCursor } = usePointCursor();
107104 const point = need.point(pointCursor);
108105 const { getHasReceived, sendMail } = useMailer();
106+ const { gasPrice } = useSuggestedGasPrice(networkType);
109107
110108 const cachedEmails = useRef([]);
111109
@@ -245,7 +243,7 @@ export default function InviteEmail() {
245243 nonce: nonce + i,
246244 // TODO: ^ make a useTransactionSigner to encapsulate this logic
247245 txn: inviteTx,
248− gasPrice: DEFAULT_GAS_PRICE_GWEI.toString(),
246+ gasPrice: gasPrice.toString(),
249247 gasLimit: GAS_LIMIT.toString(),
250248 });
251249
@@ -278,6 +276,7 @@ export default function InviteEmail() {
278276 walletType,
279277 walletHdPath,
280278 networkType,
279+ gasPrice,
281280 addInvite,
282281 ]
283282 );
@@ -295,7 +294,7 @@ export default function InviteEmail() {
295294 _web3,
296295 point,
297296 _wallet.address,
298− INVITE_COST * emails.length,
297+ toWei((gasPrice * GAS_LIMIT * emails.length).toString(), 'gwei'),
299298 emails.map(email => invites[email].rawTx),
300299 (address, minBalance, balance) =>
301300 setNeedFunds({ address, minBalance, balance }),
@@ -361,7 +360,16 @@ export default function InviteEmail() {
361360 if (errorString !== '') {
362361 throw new Error(errorString);
363362 }
364− }, [web3, addReceipt, clearReceipts, invites, point, wallet, sendMail]);
363+ }, [
364+ web3,
365+ wallet,
366+ point,
367+ gasPrice,
368+ clearReceipts,
369+ invites,
370+ addReceipt,
371+ sendMail,
372+ ]);
365373
366374 const onSubmit = useCallback(
367375 async values => {