@@ -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,24 +54,29 @@ const ensureFundsFor = async (
53 askForFunding,
54 askForFunding,
54 gotFunding
55 gotFunding
56− let balance = await web3.eth.getBalance(address);
57+ const balance = toBN(await web3.eth.getBalance(address));
58− if (cost <= balance) {
60+ if (balance.gte(cost)) {
59− console.log('tank: already have sufficient funds', cost, address, balance);
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`
60 return false;
66 return false;
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?
64 if (point !== null) {
73 if (point !== null) {
65 const fundsRemaining = await remainingTransactions(point);
74 const fundsRemaining = await remainingTransactions(point);
66 if (fundsRemaining < signedTxs.length) {
75 if (fundsRemaining < signedTxs.length) {
67 throw new Error('tank: request invalid');
76 throw new Error('tank: request invalid');
70− console.log('tank: no point provided. skipping remaining-funds check');
79+ 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?
76 const res = await fundTransactions(signedTxs);
82 const res = await fundTransactions(signedTxs);
@@ -79,16 +85,15 @@ const ensureFundsFor = async (
81 await waitForTransactionConfirm(web3, res.txHash);
87 await waitForTransactionConfirm(web3, res.txHash);
82 const newBalance = await web3.eth.getBalance(address);
89 const newBalance = await web3.eth.getBalance(address);
83 console.log(
90 console.log(
84− 'tank: funds have confirmed',
91+ `tank: funds have confirmed: ${address} now has ` +
85− balance >= cost,
92+ ` ${newBalance.toString()}wei, up from ${balance.toString()}wei`
89 return true;
94 return true;
90 } catch (e) {
95 } catch (e) {
91− console.log('tank: funding failed, waiting on user funds.', e);
96+ console.log('tank: funding failed, waiting on user funds...', e);
92 await waitForBalance(web3, address, cost, askForFunding, gotFunding);
97 await waitForBalance(web3, address, cost, askForFunding, gotFunding);
@@ -98,17 +103,12 @@ const ensureFundsFor = async (
98// returns a promise that resolves when address has at least minBalance
103// returns a promise that resolves when address has at least minBalance
99function waitForBalance(web3, address, minBalance, askForFunding, gotFunding) {
104function waitForBalance(web3, address, minBalance, askForFunding, gotFunding) {
100 return retry(async (bail, n) => {
105 return retry(async (bail, n) => {
101− const balance = await web3.eth.getBalance(address);
106+ const balance = toBN(await web3.eth.getBalance(address));
102− if (balance >= minBalance) {
107+ if (balance.gte(minBalance)) {
103 gotFunding && gotFunding();
108 gotFunding && gotFunding();
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);
112 throw new Error('retrying');
112 throw new Error('retrying');
114 }, RETRY_OPTIONS);
114 }, RETRY_OPTIONS);