@@ -54,54 +54,65 @@ const ensureFundsFor = async (
5454 askForFunding,
5555 gotFunding
5656) => {
57− let balance = toBN(await web3.eth.getBalance(address));
57+ const balance = toBN(await web3.eth.getBalance(address));
5858 cost = toBN(cost);
5959
60− if (cost.gt(balance)) {
61− try {
62− if (point !== null) {
63− const fundsRemaining = await remainingTransactions(point);
64− if (fundsRemaining < signedTxs.length) {
65− throw new Error('tank: request invalid');
66− }
67− } else {
68− console.log('tank: skipping remaining-funds check');
69− //TODO if we can't always (easily) provide a point, and the
70− // fundTransactions call is gonna fail anyway, should we maybe
71− // just not bother doing this check in the first place?
72− }
60+ if (balance.gte(cost)) {
61+ console.log(
62+ `tank: already have sufficient funds: ${address} proposed ` +
63+ `transaction(s) that will cost ${cost.toString()}wei but it ` +
64+ `already has ${balance.toString}wei`
65+ );
66+ return false;
67+ }
7368
74− const res = await fundTransactions(signedTxs);
75− if (!res.success) {
76− throw new Error('tank: request rejected', res);
77− } else {
78− await waitForTransactionConfirm(web3, res.txHash);
79− let newBalance = await web3.eth.getBalance(address);
80− console.log(
81− 'tank: funds have confirmed',
82− balance.toString(),
83− newBalance
84− );
85− return true;
69+ 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?
73+ if (point !== null) {
74+ const fundsRemaining = await remainingTransactions(point);
75+ if (fundsRemaining < signedTxs.length) {
76+ throw new Error('tank: request invalid');
8677 }
87− } catch (e) {
88− console.log('tank: funding failed', e);
89− await waitForBalance(
90− web3,
91− address,
92− cost.toString(),
93− askForFunding,
94− gotFunding
78+ } else {
79+ console.log('tank: no point provided. skipping remaining-funds check...');
80+ }
81+
82+ const res = await fundTransactions(signedTxs);
83+ if (!res.success) {
84+ throw new Error(`tank: request rejected ${JSON.stringify(res)}`);
85+ }
86+
87+ await waitForTransactionConfirm(web3, res.txHash);
88+
89+ const newBalance = await web3.eth.getBalance(address);
90+
91+ // sanity check
92+ if (newBalance.lt(cost)) {
93+ throw new Error(
94+ `tank: transaction funded but the new balance of ` +
95+ `${newBalance.toString()}wei is still less than ` +
96+ `${cost.toString()}wei.`
9597 );
9698 }
97− } else {
99+
98100 console.log(
99− 'tank: already have sufficient funds',
100− cost.toString(),
101+ `tank: funds have confirmed: ${address} now has ` +
102+ ` ${newBalance.toString()}wei, up from ${balance.toString()}wei`
103+ );
104+ return true;
105+ } catch (error) {
106+ console.error(error);
107+ await waitForBalance(
108+ web3,
101109 address,
102− balance.toString()
110+ cost.toString(),
111+ askForFunding,
112+ gotFunding
103113 );
104114 }
115+
105116 return false;
106117};
107118
@@ -114,7 +125,7 @@ function waitForBalance(web3, address, minBalance, askForFunding, gotFunding) {
114125 gotFunding && gotFunding();
115126 return;
116127 } else {
117− askForFunding(address, minBalance, balance);
128+ askForFunding(address, minBalance.toString(), balance);
118129 throw new Error('retrying');
119130 }
120131 }, RETRY_OPTIONS);