@@ -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,9 +54,10 @@ const ensureFundsFor = async (
5354 askForFunding,
5455 gotFunding
5556) => {
56− let balance = await web3.eth.getBalance(address);
57+ let balance = toBN(await web3.eth.getBalance(address));
58+ cost = toBN(cost);
5759
58− if (web3.utils.toBN(cost).gt(web3.utils.toBN(balance))) {
60+ if (cost.gt(balance)) {
5961 try {
6062 if (point !== null) {
6163 const fundsRemaining = await remainingTransactions(point);
@@ -77,36 +79,42 @@ const ensureFundsFor = async (
7779 let newBalance = await web3.eth.getBalance(address);
7880 console.log(
7981 'tank: funds have confirmed',
80− balance >= cost,
81− balance,
82+ balance.toString(),
8283 newBalance
8384 );
8485 return true;
8586 }
8687 } catch (e) {
8788 console.log('tank: funding failed', e);
88− await waitForBalance(web3, address, cost, askForFunding, gotFunding);
89+ await waitForBalance(
90+ web3,
91+ address,
92+ cost.toString(),
93+ askForFunding,
94+ gotFunding
95+ );
8996 }
9097 } else {
91− console.log('tank: already have sufficient funds', cost, address, balance);
98+ console.log(
99+ 'tank: already have sufficient funds',
100+ cost.toString(),
101+ address,
102+ balance.toString()
103+ );
92104 }
93105 return false;
94106};
95107
96108// returns a promise that resolves when address has at least minBalance
97109function waitForBalance(web3, address, minBalance, askForFunding, gotFunding) {
110+ minBalance = toBN(minBalance);
98111 return retry(async (bail, n) => {
99− const balance = await web3.eth.getBalance(address);
100− if (web3.utils.toBN(balance).gte(web3.utils.toBN(minBalance))) {
112+ const balance = toBN(await web3.eth.getBalance(address));
113+ if (balance.gte(minBalance)) {
101114 gotFunding && gotFunding();
102115 return;
103116 } else {
104− // TODO: minBalance is a `number` type
105− // but we want to display ETH, which will never accept a number
106− // but instead wants string or BN/BigNumber.
107− // this will be heavily refactored to correctly do BN math, but until
108− // then we'll manually convert this number to an integer string
109− askForFunding(address, minBalance.toFixed(), balance);
117+ askForFunding(address, minBalance, balance);
110118 throw new Error('retrying');
111119 }
112120 }, RETRY_OPTIONS);