on master help sign in
Merge pull request #376 from urbit/m/augment

Invite error msg enhancement & fromWei augmentation

commit6ae4f030bd5cf9f0f3443c6c98f2250fa3122a4a authorFang <git@fang.io> dateFri 31 Jan 2020, 10:55 (7 years ago) parentb057853c53aba4
9 files changed+50−19
src/components/NeedFundsNotice.js+3−3 src/lib/error.js+3−3 src/lib/lib.js+27 src/lib/reticket.js+3−2 src/lib/txn.js+4−3 src/lib/useEthereumTransaction.js+3−2 src/lib/useInviter.js+3−2 src/views/Invite/Email.js+2−2 src/views/Invite/InviteEmail.js+2−2
src/components/NeedFundsNotice.js+3−3
@@ -1,5 +1,5 @@
11import React from 'react';
2−import { fromWei, toBN } from 'web3-utils';
2+import { safeFromWei } from '../lib/lib';
33
44import Highlighted from './Highlighted';
55import CopiableAddress from './CopiableAddress';
@@ -16,8 +16,8 @@ export default function NeedFundsNotice({
1616 <WarningBox {...rest}>
1717 <Highlighted warning>
1818 Your ownership address <CopiableAddress>{address}</CopiableAddress>{' '}
19− needs at least {fromWei(toBN(minBalance))} ETH and currently has{' '}
20− {fromWei(toBN(balance))} ETH. The transaction will automatically resume
19+ needs at least {safeFromWei(minBalance)} ETH and currently has{' '}
20+ {safeFromWei(balance)} ETH. The transaction will automatically resume
2121 once enough ETH is available. Waiting... <Blinky />
2222 </Highlighted>
2323 </WarningBox>
src/lib/error.js+3−3
@@ -1,4 +1,4 @@
1−import { fromWei, toBN } from 'web3-utils';
1+import { safeFromWei } from './lib';
22
33const BRIDGE_ERROR = {
44 MISSING_WEB3: 'no web3 object found',
@@ -18,13 +18,13 @@ const BRIDGE_ERROR = {
1818const renderTxnError = (web3, msg) => {
1919 const txnCost = (web3, msg) => {
2020 const costWei = msg.match(/upfront cost is: ([0-9]+)/);
21− const costEth = fromWei(toBN(costWei[1]), 'ether');
21+ const costEth = safeFromWei(costWei[1], 'ether');
2222 return parseFloat(costEth).toFixed(4);
2323 };
2424
2525 const acctBalance = (web3, msg) => {
2626 const balWei = msg.match(/sender's account only has: ([0-9]+)/);
27− const balEth = fromWei(toBN(balWei[1]), 'ether');
27+ const balEth = safeFromWei(balWei[1], 'ether');
2828 return parseFloat(balEth).toFixed(4);
2929 };
3030
src/lib/lib.js+27
@@ -1,5 +1,30 @@
11import ob from 'urbit-ob';
22import patp2dec from './patp2dec';
3+import { fromWei, toWei, toBN } from 'web3-utils';
4+
5+const safeFromWei = (num, target) => {
6+ try {
7+ return fromWei(toBN(num), target);
8+ } catch (e) {
9+ e.message =
10+ `(safeFromWei got ${typeof num} (${num}), made ${typeof toBN(
11+ num
12+ )}. Please report this issue!) ` + e.message;
13+ throw e;
14+ }
15+};
16+
17+const safeToWei = (num, source) => {
18+ try {
19+ return toWei(toBN(num), source);
20+ } catch (e) {
21+ e.message =
22+ `(safeToWei got ${typeof num} (${num}), made ${typeof toBN(
23+ num
24+ )}. Please report this issue!) ` + e.message;
25+ throw e;
26+ }
27+};
328
429const compose = (...fs) =>
530 fs.reduceRight((pF, nF) => (...args) => nF(pF(...args)), v => v);
@@ -43,6 +68,8 @@ const patpStringLength = byteLength =>
4368 byteLength * 3 + Math.ceil(byteLength / 2);
4469
4570export {
71+ safeFromWei,
72+ safeToWei,
4673 compose,
4774 allFalse,
4875 isLast,
src/lib/reticket.js+3−2
@@ -12,7 +12,8 @@ import { addHexPrefix } from './wallet';
1212import { sendAndAwaitAll } from './txn';
1313import getSuggestedGasPrice from './getSuggestedGasPrice';
1414import { GAS_LIMITS } from './constants';
15−import { toWei, toBN } from 'web3-utils';
15+import { toBN } from 'web3-utils';
16+import { safeToWei } from './lib';
1617
1718// the initial network key revision is always 1
1819const INITIAL_NETWORK_KEY_REVISION = 1;
@@ -143,7 +144,7 @@ export async function reticketPointBetweenWallets({
143144 progress(TRANSACTION_PROGRESS.SIGNING);
144145
145146 const suggestedGasPrice = await getSuggestedGasPrice(networkType);
146− const gasPrice = toWei(suggestedGasPrice.toFixed(), 'gwei');
147+ const gasPrice = safeToWei(suggestedGasPrice.toFixed(), 'gwei');
147148 const gasPriceBN = toBN(gasPrice);
148149 let totalCost = toBN(0);
149150 let inviteNonce = await web3.eth.getTransactionCount(fromWallet.address);
src/lib/txn.js+4−3
@@ -1,5 +1,6 @@
11import Tx from 'ethereumjs-tx';
2−import { toWei, fromWei, toHex, toBN } from 'web3-utils';
2+import { toHex } from 'web3-utils';
3+import { safeFromWei, safeToWei } from './lib';
34import retry from 'async-retry';
45
56import { NETWORK_TYPES } from './network';
@@ -31,7 +32,7 @@ const signTransaction = async ({
3132 // TODO: require these in txn object
3233 nonce = toHex(nonce);
3334 chainId = toHex(chainId);
34− gasPrice = toHex(toWei(gasPrice, 'gwei'));
35+ gasPrice = toHex(safeToWei(gasPrice, 'gwei'));
3536 gasLimit = toHex(gasLimit);
3637
3738 const txParams = { nonce, chainId, gasPrice, gasLimit };
@@ -193,7 +194,7 @@ const getTxnInfo = async (web3, addr) => {
193194 return {
194195 nonce: nonce,
195196 chainId: chainId,
196− gasPrice: fromWei(toBN(gasPrice), 'gwei'),
197+ gasPrice: safeFromWei(gasPrice, 'gwei'),
197198 };
198199};
199200
src/lib/useEthereumTransaction.js+3−2
@@ -1,6 +1,6 @@
11import { useCallback, useEffect, useState } from 'react';
22import { Just } from 'folktale/maybe';
3−import { toWei, toBN } from 'web3-utils';
3+import { toBN } from 'web3-utils';
44
55import { useNetwork } from 'store/network';
66import { useWallet } from 'store/wallet';
@@ -22,6 +22,7 @@ import { ensureFundsFor } from 'lib/tank';
2222import useDeepEqualReference from 'lib/useDeepEqualReference';
2323import useGasPrice from 'lib/useGasPrice';
2424import timeout from 'lib/timeout';
25+import { safeToWei } from 'lib/lib';
2526
2627const STATE = {
2728 NONE: 'NONE',
@@ -148,7 +149,7 @@ export default function useEthereumTransaction(
148149 const costGwei = toBN(gasLimit)
149150 .mul(toBN(gasPrice))
150151 .mul(toBN(rawTxs.length));
151− const cost = toWei(costGwei.toString(), 'gwei');
152+ const cost = safeToWei(costGwei, 'gwei');
152153 let usedTank = false;
153154 // if this ethereum transaction is being executed by a specific point
154155 // see if we can use the tank
src/lib/useInviter.js+3−2
@@ -1,5 +1,5 @@
11import { useState, useCallback, useEffect } from 'react';
2−import { toWei, toBN } from 'web3-utils';
2+import { toBN } from 'web3-utils';
33import * as azimuth from 'azimuth-js';
44import { FORM_ERROR } from 'final-form';
55
@@ -16,6 +16,7 @@ import {
1616} from 'lib/txn';
1717import useGasPrice from 'lib/useGasPrice';
1818import { MIN_PLANET } from 'lib/constants';
19+import { safeToWei } from 'lib/lib';
1920
2021import { useNetwork } from 'store/network';
2122import { useWallet } from 'store/wallet';
@@ -148,7 +149,7 @@ const useInviter = () => {
148149 _web3,
149150 point,
150151 _wallet.address,
151− toWei(totalCost.toString(), 'gwei'),
152+ safeToWei(totalCost, 'gwei'),
152153 Object.keys(invites).map(name => invites[name].rawTx),
153154 (address, minBalance, balance) =>
154155 setNeedFunds({ address, minBalance, balance }),
src/views/Invite/Email.js+2−2
@@ -25,7 +25,7 @@ import BridgeForm from 'form/BridgeForm';
2525import NeedFundsNotice from 'components/NeedFundsNotice';
2626import ProgressButton from 'components/ProgressButton';
2727
28−const HAS_RECEIVED_TEXT = 'This email has already received an invite.';
28+const HAS_RECEIVED_TEXT = 'This email has already received an invite: ';
2929
3030const STATUS = {
3131 INPUT: 'INPUT',
@@ -99,7 +99,7 @@ const InviteMail = ({ setSubmitting }) => {
9999 async email => {
100100 const hasReceived = await getHasReceived(email);
101101 if (hasReceived) {
102− return HAS_RECEIVED_TEXT;
102+ return HAS_RECEIVED_TEXT + email;
103103 }
104104 },
105105 [getHasReceived]
src/views/Invite/InviteEmail.js+2−2
@@ -66,7 +66,7 @@ import timeout from 'lib/timeout';
6666const INITIAL_VALUES = { emails: [''] };
6767
6868const GAS_LIMIT = GAS_LIMITS.GIFT_PLANET;
69−const HAS_RECEIVED_TEXT = 'This email has already received an invite.';
69+const HAS_RECEIVED_TEXT = 'This email has already received an invite: ';
7070
7171const STATUS = {
7272 INPUT: 'INPUT',
@@ -146,7 +146,7 @@ export default function InviteEmail() {
146146 async email => {
147147 const hasReceived = await getHasReceived(email);
148148 if (hasReceived) {
149− return HAS_RECEIVED_TEXT;
149+ return HAS_RECEIVED_TEXT + email;
150150 }
151151 },
152152 [getHasReceived]