@@ -169,6 +169,13 @@ async function startTransactions(args) {
169169 let { realPointM, web3M, contractsM,
170170 inviteWalletM, realWalletM, setUrbitWallet, updateProgress } = args
171171
172+ const askForFunding = (address, amount, current) => {
173+ updateProgress({
174+ type: "notify",
175+ value: `Please make sure ${address} has at least ${amount} wei, we'll continue once that's true. Current balance: ${current}. Waiting`
176+ });
177+ }
178+
172179 if (Nothing.hasInstance(web3M)) {
173180 throw BRIDGE_ERROR.MISSING_WEB3;
174181 }
@@ -217,7 +224,11 @@ async function startTransactions(args) {
217224 let rawTransferStx = '0x'+transferStx.serialize().toString('hex');
218225
219226 const transferCost = transferTx.gas * transferTx.gasPrice;
220− await ensureFundsFor(web3, point, inviteAddress, transferCost, [rawTransferStx], updateProgress, true);
227+ updateProgress({
228+ type: "progress",
229+ value: TRANSACTION_STATES.FUNDING_INVITE
230+ });
231+ await tank.ensureFundsFor(web3, point, inviteAddress, transferCost, [rawTransferStx], askForFunding);
221232
222233 updateProgress({
223234 type: "progress",
@@ -281,7 +292,11 @@ async function startTransactions(args) {
281292 return '0x'+stx.serialize().toString('hex');
282293 });
283294
284− await ensureFundsFor(web3, point, newAddress, totalCost, rawStxs, updateProgress, false);
295+ updateProgress({
296+ type: "progress",
297+ value: TRANSACTION_STATES.FUNDING_RECIPIENT
298+ });
299+ await tank.ensureFundsFor(web3, point, newAddress, totalCost, rawStxs, askForFunding);
285300
286301 updateProgress({
287302 type: "progress",
@@ -328,67 +343,6 @@ async function startTransactions(args) {
328343 setUrbitWallet(Just(realWallet));
329344}
330345
331−async function ensureFundsFor(web3, point, address, cost, signedTxs, updateProgress, firstTx) {
332− let fundingState = firstTx? TRANSACTION_STATES.FUNDING_INVITE : TRANSACTION_STATES.FUNDING_RECIPIENT
333−
334− updateProgress({
335− type: "progress",
336− value: fundingState
337− });
338−
339− let balance = await web3.eth.getBalance(address);
340−
341− if (cost > balance) {
342−
343− try {
344−
345− const fundsRemaining = await tank.remainingTransactions(point);
346− if (fundsRemaining < signedTxs.length) {
347− throw new Error('request invalid');
348− }
349−
350− const res = await tank.fundTransactions(signedTxs);
351− if (!res.success) {
352− throw new Error('request rejected');
353− } else {
354− await waitForTransactionConfirm(web3, res.txHash);
355− let newBalance = await web3.eth.getBalance(address);
356− console.log('funds have confirmed', balance >= cost, balance, newBalance);
357− }
358−
359− } catch (e) {
360−
361− console.log('funding failed', e);
362− await waitForBalance(web3, address, cost, updateProgress);
363−
364− }
365−
366− } else {
367− console.log('already have sufficient funds');
368− }
369−}
370−
371−// resolves when address has at least minBalance
372−//
373−async function waitForBalance(web3, address, minBalance, updateProgress) {
374− console.log('awaiting balance', address, minBalance);
375− return new Promise((resolve, reject) => {
376− let oldBalance = null;
377− const checkForBalance = async () => {
378− const balance = await web3.eth.getBalance(address);
379− if (balance >= minBalance) {
380− resolve();
381− } else {
382− if (balance !== oldBalance) {
383− askForFunding(address, minBalance, balance, updateProgress);
384− }
385− setTimeout(checkForBalance, 13000);
386− }
387− };
388− checkForBalance();
389− });
390−}
391−
392346//TODO replace with txn.sendSignedTransaction? it can doubt nonce errors too
393347async function sendAndAwaitConfirm(web3, signedTxs) {
394348 await Promise.all(signedTxs.map(tx => {
@@ -421,13 +375,6 @@ async function sendAndAwaitConfirm(web3, signedTxs) {
421375 }));
422376}
423377
424−function askForFunding(address, amount, current, updateProgress) {
425− updateProgress({
426− type: "notify",
427− value: `Please make sure ${address} has at least ${amount} wei, we'll continue once that's true. Current balance: ${current}. Waiting`
428− });
429−}
430−
431378export {
432379 generateWallet,
433380 downloadWallet,