@@ -1,6 +1,7 @@
1// /lib/tank: functions for funding transactions
1// /lib/tank: functions for funding transactions
3import retry from 'async-retry';
3import retry from 'async-retry';
4+import { toBN } from 'web3-utils';
4import { RETRY_OPTIONS, waitForTransactionConfirm } from './txn';
5import { RETRY_OPTIONS, waitForTransactionConfirm } from './txn';
6//NOTE if accessing this in a localhost configuration fails with "CORS request
7//NOTE if accessing this in a localhost configuration fails with "CORS request
@@ -53,9 +54,10 @@ const ensureFundsFor = async (
53 askForFunding,
54 askForFunding,
54 gotFunding
55 gotFunding
56− let balance = await web3.eth.getBalance(address);
57+ let balance = toBN(await web3.eth.getBalance(address));
58− if (cost > balance) {
60+ if (cost.gt(balance)) {
60 if (point !== null) {
62 if (point !== null) {
61 const fundsRemaining = await remainingTransactions(point);
63 const fundsRemaining = await remainingTransactions(point);
@@ -77,36 +79,42 @@ const ensureFundsFor = async (
77 let newBalance = await web3.eth.getBalance(address);
79 let newBalance = await web3.eth.getBalance(address);
78 console.log(
80 console.log(
79 'tank: funds have confirmed',
81 'tank: funds have confirmed',
80− balance >= cost,
82+ balance.toString(),
82 newBalance
83 newBalance
84 return true;
85 return true;
86 } catch (e) {
87 } catch (e) {
87 console.log('tank: funding failed', e);
88 console.log('tank: funding failed', e);
88− await waitForBalance(web3, address, cost, askForFunding, gotFunding);
89+ await waitForBalance(
91− console.log('tank: already have sufficient funds', cost, address, balance);
99+ 'tank: already have sufficient funds',
93 return false;
105 return false;
96// returns a promise that resolves when address has at least minBalance
108// returns a promise that resolves when address has at least minBalance
97function waitForBalance(web3, address, minBalance, askForFunding, gotFunding) {
109function waitForBalance(web3, address, minBalance, askForFunding, gotFunding) {
110+ minBalance = toBN(minBalance);
98 return retry(async (bail, n) => {
111 return retry(async (bail, n) => {
99− const balance = await web3.eth.getBalance(address);
112+ const balance = toBN(await web3.eth.getBalance(address));
100− if (balance >= minBalance) {
113+ if (balance.gte(minBalance)) {
101 gotFunding && gotFunding();
114 gotFunding && gotFunding();
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);
110 throw new Error('retrying');
118 throw new Error('retrying');
112 }, RETRY_OPTIONS);
120 }, RETRY_OPTIONS);