on master help sign in
Make StatelessTransaction use /lib/tank more directly
ad687efFang
src/bridge/components/StatelessTransaction.js+11−22
@@ -224,8 +224,8 @@ class StatelessTransaction extends React.Component {
224 let usedTank = false;
224 let usedTank = false;
225 // if we need to, try and fund the transaction
225 // if we need to, try and fund the transaction
226 if (!hasBalance) {
226 if (!hasBalance) {
227− hasBalance = await this.ensureFundsFor(web3, address, cost, [rawTx]);
227+ usedTank = await this.ensureFundsFor(web3, address, cost, [rawTx]);
228− usedTank = hasBalance;
228+ hasBalance = usedTank;
229 }
229 }
230
230
231 // if we still don't have sufficient balance, fail and tell the user
231 // if we still don't have sufficient balance, fail and tell the user
@@ -268,26 +268,15 @@ class StatelessTransaction extends React.Component {
268 }
268 }
269 }
269 }
270
270
271− //TODO partially copied from InviteTransactions, try to move into tank lib
271+ // uses the gas tank to ensure funds, but if we have to ask the user
272+ // for funding, just resolve instead of waiting
273+ // returns true if the gas tank was used, false otherwise
272− async ensureFundsFor(web3, address, cost, signedTxs) {
274+ ensureFundsFor(web3, address, cost, signedTxs) {
273− let balance = await web3.eth.getBalance(address);
275+ return new Promise((resolve, reject) => {
274− if (cost > balance) {
276+ tank.ensureFundsFor(web3, null, address, cost, signedTxs,
275− try {
276−
277− const res = await tank.fundTransactions(signedTxs);
278− if (!res.success) {
279− return false;
277+ () => { resolve(false); })
280− } else {
278+ .then(res => { resolve(res); });
281− await waitForTransactionConfirm(web3, res.txHash);
279+ });
282− return true;
283− }
284−
285− } catch (e) {
286− return false;
287− }
288− } else {
289− return true;
290− }
291 }
280 }
292
281
293 getChainTitle(chainId) {
282 getChainTitle(chainId) {
src/bridge/lib/tank.js+17−5
@@ -38,6 +38,10 @@ const fundTransactions = signedTxs => {
38 return sendRequest('/request', {txs:signedTxs});
38 return sendRequest('/request', {txs:signedTxs});
39};
39};
40
40
41+// resolves when address has at least cost balance
42+// returns true if gas tank was used, false otherwise
43+// askForFunding: callback that takes (address, requiredBalance, currentBalance)
44+// and tells the user to get that address the required balance
41const ensureFundsFor = async (web3, point, address, cost, signedTxs, askForFunding) => {
45const ensureFundsFor = async (web3, point, address, cost, signedTxs, askForFunding) => {
42 let balance = await web3.eth.getBalance(address);
46 let balance = await web3.eth.getBalance(address);
43
47
@@ -45,9 +49,16 @@ const ensureFundsFor = async (web3, point, address, cost, signedTxs, askForFundi
45
49
46 try {
50 try {
47
51
52+ if (point !== null) {
48− const fundsRemaining = await remainingTransactions(point);
53+ const fundsRemaining = await remainingTransactions(point);
49− if (fundsRemaining < signedTxs.length) {
54+ if (fundsRemaining < signedTxs.length) {
50− throw new Error('tank: request invalid');
55+ throw new Error('tank: request invalid');
56+ }
57+ } else {
58+ console.log('tank: skipping remaining-funds check');
59+ //TODO if we can't always (easily) provide a point, and the
60+ // fundTransactions call is gonna fail anyway, should we maybe
61+ // just not bother doing this check in the first place?
51 }
62 }
52
63
53 const res = await fundTransactions(signedTxs);
64 const res = await fundTransactions(signedTxs);
@@ -57,6 +68,7 @@ const ensureFundsFor = async (web3, point, address, cost, signedTxs, askForFundi
57 await waitForTransactionConfirm(web3, res.txHash);
68 await waitForTransactionConfirm(web3, res.txHash);
58 let newBalance = await web3.eth.getBalance(address);
69 let newBalance = await web3.eth.getBalance(address);
59 console.log('tank: funds have confirmed', balance >= cost, balance, newBalance);
70 console.log('tank: funds have confirmed', balance >= cost, balance, newBalance);
71+ return true;
60 }
72 }
61
73
62 } catch (e) {
74 } catch (e) {
@@ -69,12 +81,12 @@ const ensureFundsFor = async (web3, point, address, cost, signedTxs, askForFundi
69 } else {
81 } else {
70 console.log('tank: already have sufficient funds');
82 console.log('tank: already have sufficient funds');
71 }
83 }
84+ return false;
72};
85};
73
86
74// resolves when address has at least minBalance
87// resolves when address has at least minBalance
75//
88//
76−// askForFunding: callback that takes (address, requiredBalance, currentBalance)
89+//TODO should maybe do a "we got it" callback also, so clients can hide msg?
77−// and tells the user to get that address the required balance
78async function waitForBalance(web3, address, minBalance, askForFunding) {
90async function waitForBalance(web3, address, minBalance, askForFunding) {
79 console.log('tank: awaiting balance', address, minBalance);
91 console.log('tank: awaiting balance', address, minBalance);
80 return new Promise((resolve, reject) => {
92 return new Promise((resolve, reject) => {