on master help sign in
Make /lib/invite use /lib/txn's sendSignedTransaction
commite4ea9af616d248581ff614b7bd714232e3e47246 authorFang <git@fang.io> dateFri 7 Jun 2019, 22:18 (7 years ago) parente66e8ab
2 files changed+17−35
src/bridge/lib/invite.js+11−34 src/bridge/lib/txn.js+6−1
src/bridge/lib/invite.js+11−34
@@ -9,10 +9,7 @@ import * as tank from './tank'
99import JSZip from 'jszip'
1010import saveAs from 'file-saver'
1111
12−import {
13− waitForTransactionConfirm,
14− isTransactionConfirmed
15−} from './txn'
12+import { sendSignedTransaction } from './txn'
1613import { BRIDGE_ERROR } from '../lib/error'
1714import { attemptSeedDerivation } from './keys'
1815import { addHexPrefix, WALLET_NAMES } from './wallet'
@@ -168,7 +165,9 @@ async function startTransactions(args) {
168165 type: "progress",
169166 value: TRANSACTION_STATES.FUNDING_INVITE
170167 });
171− await tank.ensureFundsFor(web3, point, inviteAddress, transferCost, [rawTransferStx], askForFunding);
168+ let usedTank = await tank.ensureFundsFor(
169+ web3, point, inviteAddress, transferCost, [rawTransferStx], askForFunding
170+ );
172171
173172 updateProgress({
174173 type: "progress",
@@ -176,7 +175,7 @@ async function startTransactions(args) {
176175 });
177176
178177 // send transaction
179− await sendAndAwaitConfirm(web3, [rawTransferStx]);
178+ await sendAndAwaitConfirm(web3, [rawTransferStx], usedTank);
180179
181180 //
182181 // we're gonna be operating as the new wallet from here on out, so change
@@ -236,14 +235,16 @@ async function startTransactions(args) {
236235 type: "progress",
237236 value: TRANSACTION_STATES.FUNDING_RECIPIENT
238237 });
239− await tank.ensureFundsFor(web3, point, newAddress, totalCost, rawStxs, askForFunding);
238+ usedTank = await tank.ensureFundsFor(
239+ web3, point, newAddress, totalCost, rawStxs, askForFunding
240+ );
240241
241242 updateProgress({
242243 type: "progress",
243244 value: TRANSACTION_STATES.CONFIGURING
244245 });
245246
246− await sendAndAwaitConfirm(web3, rawStxs);
247+ await sendAndAwaitConfirm(web3, rawStxs, usedTank);
247248
248249 updateProgress({
249250 type: "progress",
@@ -283,34 +284,10 @@ async function startTransactions(args) {
283284 setUrbitWallet(Just(realWallet));
284285}
285286
286−//TODO replace with txn.sendSignedTransaction? it can doubt nonce errors too
287−async function sendAndAwaitConfirm(web3, signedTxs) {
287+async function sendAndAwaitConfirm(web3, signedTxs, usedTank) {
288288 await Promise.all(signedTxs.map(tx => {
289− console.log('sending...');
290289 return new Promise((resolve, reject) => {
291− web3.eth.sendSignedTransaction(tx).then(res => {
292− console.log('sent, now waiting for confirm!', res.transactionHash);
293− waitForTransactionConfirm(web3, res.transactionHash)
294− .then(resolve);
295− }).catch(async err => {
296− // if there's an error, check if it's because the transaction was
297− // already confirmed prior to sending.
298− // this is really only the case in local dev environments.
299− const txHash = web3.utils.keccak256(tx);
300− console.log(err.message);
301− if (err.message.includes("the tx doesn't have the correct nonce.")) {
302− console.log('nonce error, awaiting confirm', err.message.slice(55));
303− //TODO max wait time before assume failure?
304− let res = await waitForTransactionConfirm(web3, txHash);
305− if (res) resolve();
306− else reject(new Error('Unexpected tx failure'));
307− } else {
308− const confirmed = await isTransactionConfirmed(web3, txHash);
309− console.log('error, but maybe confirmed:', confirmed);
310− if (confirmed) resolve();
311− else reject(err);
312− }
313− });
290+ sendSignedTransaction(web3, tx, usedTank, resolve);
314291 });
315292 }));
316293}
src/bridge/lib/txn.js+6−1
@@ -152,6 +152,10 @@ const sendSignedTransaction = (web3, stx, doubtNonceError, confirmationCb) => {
152152 .on('transactionHash', hash => {
153153 resolve(hash);
154154 })
155+ //TODO do we also reach this if network is slow? web3 only tries a set
156+ // amount of times... should we instead do waitForTransactionConfirm
157+ // in on-transactionHash? we don't care (much) about additional
158+ // confirms anyway.
155159 .on('confirmation', (confirmationNum, txn) => {
156160 confirmationCb(txn.transactionHash, confirmationNum + 1);
157161 resolve(txn.transactionHash);
@@ -160,8 +164,9 @@ const sendSignedTransaction = (web3, stx, doubtNonceError, confirmationCb) => {
160164 // if there's a nonce error, but we used the gas tank, it's likely
161165 // that it's because the tank already submitted our transaction.
162166 // we just wait for first confirmation here.
167+ console.log('sent and got error', err);
163168 if (doubtNonceError &&
164− err.message.includes("the tx doesn't have the correct nonce.")) {
169+ (err.message || '').includes("the tx doesn't have the correct nonce.")) {
165170 console.log('nonce error, likely from gas tank submission, ignoring');
166171 const txHash = web3.utils.keccak256(rawTx);
167172 //TODO can we do does-exists check first?