@@ -55,41 +55,43 @@ const ensureFundsFor = async (
5555) => {
5656 let balance = await web3.eth.getBalance(address);
5757
58− if (cost > balance) {
59− try {
60− if (point !== null) {
61− const fundsRemaining = await remainingTransactions(point);
62− if (fundsRemaining < signedTxs.length) {
63− throw new Error('tank: request invalid');
64− }
65− } else {
66− console.log('tank: skipping remaining-funds check');
67− //TODO if we can't always (easily) provide a point, and the
68− // fundTransactions call is gonna fail anyway, should we maybe
69− // just not bother doing this check in the first place?
70− }
58+ if (cost <= balance) {
59+ console.log('tank: already have sufficient funds', cost, address, balance);
60+ return false;
61+ }
7162
72− const res = await fundTransactions(signedTxs);
73− if (!res.success) {
74− throw new Error('tank: request rejected', res);
75− } else {
76− await waitForTransactionConfirm(web3, res.txHash);
77− let newBalance = await web3.eth.getBalance(address);
78− console.log(
79− 'tank: funds have confirmed',
80− balance >= cost,
81− balance,
82− newBalance
83− );
84− return true;
63+ try {
64+ if (point !== null) {
65+ const fundsRemaining = await remainingTransactions(point);
66+ if (fundsRemaining < signedTxs.length) {
67+ throw new Error('tank: request invalid');
8568 }
86− } catch (e) {
87− console.log('tank: funding failed', e);
88− await waitForBalance(web3, address, cost, askForFunding, gotFunding);
69+ } else {
70+ 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?
8974 }
90− } else {
91− console.log('tank: already have sufficient funds', cost, address, balance);
75+
76+ const res = await fundTransactions(signedTxs);
77+ if (!res.success) {
78+ throw new Error('tank: request rejected', res);
79+ }
80+
81+ await waitForTransactionConfirm(web3, res.txHash);
82+ const newBalance = await web3.eth.getBalance(address);
83+ console.log(
84+ 'tank: funds have confirmed',
85+ balance >= cost,
86+ balance,
87+ newBalance
88+ );
89+ return true;
90+ } catch (e) {
91+ console.log('tank: funding failed, waiting on user funds.', e);
92+ await waitForBalance(web3, address, cost, askForFunding, gotFunding);
9293 }
94+
9395 return false;
9496};
9597