@@ -54,54 +54,65 @@ const ensureFundsFor = async (
54 askForFunding,
54 askForFunding,
55 gotFunding
55 gotFunding
57− let balance = toBN(await web3.eth.getBalance(address));
57+ const balance = toBN(await web3.eth.getBalance(address));
58 cost = toBN(cost);
58 cost = toBN(cost);
60− if (cost.gt(balance)) {
60+ if (balance.gte(cost)) {
61− try {
61+ console.log(
62− if (point !== null) {
62+ `tank: already have sufficient funds: ${address} proposed ` +
63− const fundsRemaining = await remainingTransactions(point);
63+ `transaction(s) that will cost ${cost.toString()}wei but it ` +
64− if (fundsRemaining < signedTxs.length) {
64+ `already has ${balance.toString}wei`
65− throw new Error('tank: request invalid');
65+ );
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?
74− const res = await fundTransactions(signedTxs);
69+ try {
75− if (!res.success) {
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 rejected', res);
76+ throw new Error('tank: request invalid');
78− await waitForTransactionConfirm(web3, res.txHash);
79− let newBalance = await web3.eth.getBalance(address);
81− 'tank: funds have confirmed',
87− } catch (e) {
78+ } else {
88− console.log('tank: funding failed', e);
79+ console.log('tank: no point provided. skipping remaining-funds check...');
89− await waitForBalance(
80+ }
91− address,
82+ const res = await fundTransactions(signedTxs);
92− cost.toString(),
83+ if (!res.success) {
93− askForFunding,
84+ throw new Error(`tank: request rejected ${JSON.stringify(res)}`);
87+ await waitForTransactionConfirm(web3, res.txHash);
89+ const newBalance = await web3.eth.getBalance(address);
92+ if (newBalance.lt(cost)) {
94+ `tank: transaction funded but the new balance of ` +
95+ `${newBalance.toString()}wei is still less than ` +
96+ `${cost.toString()}wei.`
98 console.log(
100 console.log(
99− 'tank: already have sufficient funds',
101+ `tank: funds have confirmed: ${address} now has ` +
100− cost.toString(),
102+ ` ${newBalance.toString()}wei, up from ${balance.toString()}wei`
106+ console.error(error);
107+ await waitForBalance(
102− balance.toString()
110+ cost.toString(),
105 return false;
116 return false;
@@ -114,7 +125,7 @@ function waitForBalance(web3, address, minBalance, askForFunding, gotFunding) {
114 gotFunding && gotFunding();
125 gotFunding && gotFunding();
117− askForFunding(address, minBalance, balance);
128+ askForFunding(address, minBalance.toString(), balance);
118 throw new Error('retrying');
129 throw new Error('retrying');
120 }, RETRY_OPTIONS);
131 }, RETRY_OPTIONS);