on master help sign in
Make /lib/invite use /lib/txn's sendSignedTransaction
e4ea9afFang
src/bridge/lib/invite.js+11−34
@@ -9,10 +9,7 @@ import * as tank from './tank'
9import JSZip from 'jszip'
9import JSZip from 'jszip'
10import saveAs from 'file-saver'
10import saveAs from 'file-saver'
11
11
12−import {
12+import { sendSignedTransaction } from './txn'
13− waitForTransactionConfirm,
14− isTransactionConfirmed
15−} from './txn'
16import { BRIDGE_ERROR } from '../lib/error'
13import { BRIDGE_ERROR } from '../lib/error'
17import { attemptSeedDerivation } from './keys'
14import { attemptSeedDerivation } from './keys'
18import { addHexPrefix, WALLET_NAMES } from './wallet'
15import { addHexPrefix, WALLET_NAMES } from './wallet'
@@ -168,7 +165,9 @@ async function startTransactions(args) {
168 type: "progress",
165 type: "progress",
169 value: TRANSACTION_STATES.FUNDING_INVITE
166 value: TRANSACTION_STATES.FUNDING_INVITE
170 });
167 });
168+ let usedTank = await tank.ensureFundsFor(
171− await tank.ensureFundsFor(web3, point, inviteAddress, transferCost, [rawTransferStx], askForFunding);
169+ web3, point, inviteAddress, transferCost, [rawTransferStx], askForFunding
170+ );
172
171
173 updateProgress({
172 updateProgress({
174 type: "progress",
173 type: "progress",
@@ -176,7 +175,7 @@ async function startTransactions(args) {
176 });
175 });
177
176
178 // send transaction
177 // send transaction
179− await sendAndAwaitConfirm(web3, [rawTransferStx]);
178+ await sendAndAwaitConfirm(web3, [rawTransferStx], usedTank);
180
179
181 //
180 //
182 // we're gonna be operating as the new wallet from here on out, so change
181 // we're gonna be operating as the new wallet from here on out, so change
@@ -236,14 +235,16 @@ async function startTransactions(args) {
236 type: "progress",
235 type: "progress",
237 value: TRANSACTION_STATES.FUNDING_RECIPIENT
236 value: TRANSACTION_STATES.FUNDING_RECIPIENT
238 });
237 });
238+ usedTank = await tank.ensureFundsFor(
239− await tank.ensureFundsFor(web3, point, newAddress, totalCost, rawStxs, askForFunding);
239+ web3, point, newAddress, totalCost, rawStxs, askForFunding
240+ );
240
241
241 updateProgress({
242 updateProgress({
242 type: "progress",
243 type: "progress",
243 value: TRANSACTION_STATES.CONFIGURING
244 value: TRANSACTION_STATES.CONFIGURING
244 });
245 });
245
246
246− await sendAndAwaitConfirm(web3, rawStxs);
247+ await sendAndAwaitConfirm(web3, rawStxs, usedTank);
247
248
248 updateProgress({
249 updateProgress({
249 type: "progress",
250 type: "progress",
@@ -283,34 +284,10 @@ async function startTransactions(args) {
283 setUrbitWallet(Just(realWallet));
284 setUrbitWallet(Just(realWallet));
284}
285}
285
286
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) {
288 await Promise.all(signedTxs.map(tx => {
288 await Promise.all(signedTxs.map(tx => {
289− console.log('sending...');
290 return new Promise((resolve, reject) => {
289 return new Promise((resolve, reject) => {
291− web3.eth.sendSignedTransaction(tx).then(res => {
290+ sendSignedTransaction(web3, tx, usedTank, resolve);
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− });
314 });
291 });
315 }));
292 }));
316}
293}
src/bridge/lib/txn.js+6−1
@@ -152,6 +152,10 @@ const sendSignedTransaction = (web3, stx, doubtNonceError, confirmationCb) => {
152 .on('transactionHash', hash => {
152 .on('transactionHash', hash => {
153 resolve(hash);
153 resolve(hash);
154 })
154 })
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.
155 .on('confirmation', (confirmationNum, txn) => {
159 .on('confirmation', (confirmationNum, txn) => {
156 confirmationCb(txn.transactionHash, confirmationNum + 1);
160 confirmationCb(txn.transactionHash, confirmationNum + 1);
157 resolve(txn.transactionHash);
161 resolve(txn.transactionHash);
@@ -160,8 +164,9 @@ const sendSignedTransaction = (web3, stx, doubtNonceError, confirmationCb) => {
160 // if there's a nonce error, but we used the gas tank, it's likely
164 // if there's a nonce error, but we used the gas tank, it's likely
161 // that it's because the tank already submitted our transaction.
165 // that it's because the tank already submitted our transaction.
162 // we just wait for first confirmation here.
166 // we just wait for first confirmation here.
167+ console.log('sent and got error', err);
163 if (doubtNonceError &&
168 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.")) {
165 console.log('nonce error, likely from gas tank submission, ignoring');
170 console.log('nonce error, likely from gas tank submission, ignoring');
166 const txHash = web3.utils.keccak256(rawTx);
171 const txHash = web3.utils.keccak256(rawTx);
167 //TODO can we do does-exists check first?
172 //TODO can we do does-exists check first?