@@ -1,6 +1,7 @@
11// /lib/tank: functions for funding transactions
22
33import retry from 'async-retry';
4+import { toBN } from 'web3-utils';
45import { RETRY_OPTIONS, waitForTransactionConfirm } from './txn';
56
67//NOTE if accessing this in a localhost configuration fails with "CORS request
@@ -53,24 +54,29 @@ const ensureFundsFor = async (
5354 askForFunding,
5455 gotFunding
5556) => {
56− let balance = await web3.eth.getBalance(address);
57+ const balance = toBN(await web3.eth.getBalance(address));
58+ cost = toBN(cost);
5759
58− if (cost <= balance) {
59− console.log('tank: already have sufficient funds', cost, address, balance);
60+ if (balance.gte(cost)) {
61+ console.log(
62+ `tank: already have sufficient funds: ${address} proposed ` +
63+ `a transacton(s) that will cost ${cost.toString()}wei but it ` +
64+ `already has ${balance.toString}wei`
65+ );
6066 return false;
6167 }
6268
6369 try {
70+ // TODO: if we can't always (easily) provide a point, and the
71+ // fundTransactions call is gonna fail anyway, should we maybe
72+ // just not bother doing this check in the first place?
6473 if (point !== null) {
6574 const fundsRemaining = await remainingTransactions(point);
6675 if (fundsRemaining < signedTxs.length) {
6776 throw new Error('tank: request invalid');
6877 }
6978 } else {
70− console.log('tank: no point provided. skipping remaining-funds check');
71− //TODO if we can't always (easily) provide a point, and the
72− // fundTransactions call is gonna fail anyway, should we maybe
73− // just not bother doing this check in the first place?
79+ console.log('tank: no point provided. skipping remaining-funds check...');
7480 }
7581
7682 const res = await fundTransactions(signedTxs);
@@ -79,16 +85,15 @@ const ensureFundsFor = async (
7985 }
8086
8187 await waitForTransactionConfirm(web3, res.txHash);
88+
8289 const newBalance = await web3.eth.getBalance(address);
8390 console.log(
84− 'tank: funds have confirmed',
85− balance >= cost,
86− balance,
87− newBalance
91+ `tank: funds have confirmed: ${address} now has ` +
92+ ` ${newBalance.toString()}wei, up from ${balance.toString()}wei`
8893 );
8994 return true;
9095 } catch (e) {
91− console.log('tank: funding failed, waiting on user funds.', e);
96+ console.log('tank: funding failed, waiting on user funds...', e);
9297 await waitForBalance(web3, address, cost, askForFunding, gotFunding);
9398 }
9499
@@ -98,17 +103,12 @@ const ensureFundsFor = async (
98103// returns a promise that resolves when address has at least minBalance
99104function waitForBalance(web3, address, minBalance, askForFunding, gotFunding) {
100105 return retry(async (bail, n) => {
101− const balance = await web3.eth.getBalance(address);
102− if (balance >= minBalance) {
106+ const balance = toBN(await web3.eth.getBalance(address));
107+ if (balance.gte(minBalance)) {
103108 gotFunding && gotFunding();
104109 return;
105110 } else {
106− // TODO: minBalance is a `number` type
107− // but we want to display ETH, which will never accept a number
108− // but instead wants string or BN/BigNumber.
109− // this will be heavily refactored to correctly do BN math, but until
110− // then we'll manually convert this number to an integer string
111− askForFunding(address, minBalance.toFixed(), balance);
111+ askForFunding(address, minBalance.toString(), balance);
112112 throw new Error('retrying');
113113 }
114114 }, RETRY_OPTIONS);