@@ -169,6 +169,13 @@ async function startTransactions(args) {
169 let { realPointM, web3M, contractsM,
169 let { realPointM, web3M, contractsM,
170 inviteWalletM, realWalletM, setUrbitWallet, updateProgress } = args
170 inviteWalletM, realWalletM, setUrbitWallet, updateProgress } = args
172+ const askForFunding = (address, amount, current) => {
175+ value: `Please make sure ${address} has at least ${amount} wei, we'll continue once that's true. Current balance: ${current}. Waiting`
172 if (Nothing.hasInstance(web3M)) {
179 if (Nothing.hasInstance(web3M)) {
173 throw BRIDGE_ERROR.MISSING_WEB3;
180 throw BRIDGE_ERROR.MISSING_WEB3;
@@ -217,7 +224,11 @@ async function startTransactions(args) {
217 let rawTransferStx = '0x'+transferStx.serialize().toString('hex');
224 let rawTransferStx = '0x'+transferStx.serialize().toString('hex');
219 const transferCost = transferTx.gas * transferTx.gasPrice;
226 const transferCost = transferTx.gas * transferTx.gasPrice;
229+ value: TRANSACTION_STATES.FUNDING_INVITE
220− await ensureFundsFor(web3, point, inviteAddress, transferCost, [rawTransferStx], updateProgress, true);
231+ await tank.ensureFundsFor(web3, point, inviteAddress, transferCost, [rawTransferStx], askForFunding);
222 updateProgress({
233 updateProgress({
223 type: "progress",
234 type: "progress",
@@ -281,7 +292,11 @@ async function startTransactions(args) {
281 return '0x'+stx.serialize().toString('hex');
292 return '0x'+stx.serialize().toString('hex');
297+ value: TRANSACTION_STATES.FUNDING_RECIPIENT
284− await ensureFundsFor(web3, point, newAddress, totalCost, rawStxs, updateProgress, false);
299+ await tank.ensureFundsFor(web3, point, newAddress, totalCost, rawStxs, askForFunding);
286 updateProgress({
301 updateProgress({
287 type: "progress",
302 type: "progress",
@@ -328,67 +343,6 @@ async function startTransactions(args) {
328 setUrbitWallet(Just(realWallet));
343 setUrbitWallet(Just(realWallet));
331−async function ensureFundsFor(web3, point, address, cost, signedTxs, updateProgress, firstTx) {
332− let fundingState = firstTx? TRANSACTION_STATES.FUNDING_INVITE : TRANSACTION_STATES.FUNDING_RECIPIENT
339− let balance = await web3.eth.getBalance(address);
341− if (cost > balance) {
345− const fundsRemaining = await tank.remainingTransactions(point);
346− if (fundsRemaining < signedTxs.length) {
347− throw new Error('request invalid');
350− const res = await tank.fundTransactions(signedTxs);
352− throw new Error('request rejected');
354− await waitForTransactionConfirm(web3, res.txHash);
355− let newBalance = await web3.eth.getBalance(address);
356− console.log('funds have confirmed', balance >= cost, balance, newBalance);
361− console.log('funding failed', e);
362− await waitForBalance(web3, address, cost, updateProgress);
367− console.log('already have sufficient funds');
371−// resolves when address has at least minBalance
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) {
382− if (balance !== oldBalance) {
383− askForFunding(address, minBalance, balance, updateProgress);
385− setTimeout(checkForBalance, 13000);
392//TODO replace with txn.sendSignedTransaction? it can doubt nonce errors too
346//TODO replace with txn.sendSignedTransaction? it can doubt nonce errors too
393async function sendAndAwaitConfirm(web3, signedTxs) {
347async function sendAndAwaitConfirm(web3, signedTxs) {
394 await Promise.all(signedTxs.map(tx => {
348 await Promise.all(signedTxs.map(tx => {
@@ -421,13 +375,6 @@ async function sendAndAwaitConfirm(web3, signedTxs) {
424−function askForFunding(address, amount, current, updateProgress) {
427− value: `Please make sure ${address} has at least ${amount} wei, we'll continue once that's true. Current balance: ${current}. Waiting`
432 generateWallet,
379 generateWallet,
433 downloadWallet,
380 downloadWallet,