@@ -1,6 +1,7 @@
11// /lib/tank: functions for funding transactions
22
3−import { waitForTransactionConfirm } from './txn';
3+import * as retry from 'async-retry';
4+import { RETRY_OPTIONS, waitForTransactionConfirm } from './txn';
45
56//NOTE if accessing this in a localhost configuration fails with "CORS request
67// did not succeed", you might need to visit localhost:3001 or whatever
@@ -92,41 +93,29 @@ const ensureFundsFor = async (
9293 return false;
9394};
9495
95−// resolves when address has at least minBalance
96−//
97−//TODO should maybe do a "we got it" callback also, so clients can hide msg?
98−async function waitForBalance(
99− web3,
100− address,
101− minBalance,
102− askForFunding,
103− gotFunding
104−) {
105− console.log('tank: awaiting balance', address, minBalance);
106− return new Promise((resolve, reject) => {
107− let oldBalance = null;
108− const checkForBalance = async () => {
109− const balance = await web3.eth.getBalance(address);
110− if (balance >= minBalance) {
111− // if we ever asked for funding, retract that request now
112− if (gotFunding && oldBalance !== null) {
113− gotFunding();
114− }
115− resolve();
116− } else {
117− if (balance !== oldBalance) {
118− // TODO: minBalance is a `number` type
119− // but we want to display ETH, which will never accept a number
120− // but instead wants string or BN/BigNumber.
121− // this will be heavily refactored to correctly do BN math, but until
122− // then we'll manually convert this number to an integer string
123− askForFunding(address, minBalance.toFixed(), balance);
124− }
125− setTimeout(checkForBalance, 13000);
96+// returns a promise that resolves when address has at least minBalance
97+function waitForBalance(web3, address, minBalance, askForFunding, gotFunding) {
98+ let oldBalance = null;
99+ return retry(async (bail, n) => {
100+ const balance = await web3.eth.getBalance(address);
101+ if (balance >= minBalance) {
102+ // if we ever asked for funding, retract that request now
103+ if (gotFunding && oldBalance !== null) {
104+ gotFunding();
126105 }
127− };
128− checkForBalance();
129− });
106+ return;
107+ } else {
108+ if (balance !== oldBalance) {
109+ // TODO: minBalance is a `number` type
110+ // but we want to display ETH, which will never accept a number
111+ // but instead wants string or BN/BigNumber.
112+ // this will be heavily refactored to correctly do BN math, but until
113+ // then we'll manually convert this number to an integer string
114+ askForFunding(address, minBalance.toFixed(), balance);
115+ }
116+ throw new Error('retrying');
117+ }
118+ }, RETRY_OPTIONS);
130119}
131120
132121export { remainingTransactions, fundTransactions, ensureFundsFor };