on master help sign in
Make StatelessTransaction use /lib/tank more directly

Some question-marks around the use of funds-remaining checks here...

commitad687ef245114ac4ac4dddf2d8c295de63b6acf8 authorFang <git@fang.io> dateWed 5 Jun 2019, 16:50 (7 years ago) parentc19ce42
2 files changed+28−27
src/bridge/components/StatelessTransaction.js+11−22 src/bridge/lib/tank.js+17−5
src/bridge/components/StatelessTransaction.js+11−22
@@ -224,8 +224,8 @@ class StatelessTransaction extends React.Component {
224224 let usedTank = false;
225225 // if we need to, try and fund the transaction
226226 if (!hasBalance) {
227− hasBalance = await this.ensureFundsFor(web3, address, cost, [rawTx]);
228− usedTank = hasBalance;
227+ usedTank = await this.ensureFundsFor(web3, address, cost, [rawTx]);
228+ hasBalance = usedTank;
229229 }
230230
231231 // if we still don't have sufficient balance, fail and tell the user
@@ -268,26 +268,15 @@ class StatelessTransaction extends React.Component {
268268 }
269269 }
270270
271− //TODO partially copied from InviteTransactions, try to move into tank lib
272− async ensureFundsFor(web3, address, cost, signedTxs) {
273− let balance = await web3.eth.getBalance(address);
274− if (cost > balance) {
275− try {
276−
277− const res = await tank.fundTransactions(signedTxs);
278− if (!res.success) {
279− return false;
280− } else {
281− await waitForTransactionConfirm(web3, res.txHash);
282− return true;
283− }
284−
285− } catch (e) {
286− return false;
287− }
288− } else {
289− return true;
290− }
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
274+ ensureFundsFor(web3, address, cost, signedTxs) {
275+ return new Promise((resolve, reject) => {
276+ tank.ensureFundsFor(web3, null, address, cost, signedTxs,
277+ () => { resolve(false); })
278+ .then(res => { resolve(res); });
279+ });
291280 }
292281
293282 getChainTitle(chainId) {
src/bridge/lib/tank.js+17−5
@@ -38,6 +38,10 @@ const fundTransactions = signedTxs => {
3838 return sendRequest('/request', {txs:signedTxs});
3939};
4040
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
4145const ensureFundsFor = async (web3, point, address, cost, signedTxs, askForFunding) => {
4246 let balance = await web3.eth.getBalance(address);
4347
@@ -45,9 +49,16 @@ const ensureFundsFor = async (web3, point, address, cost, signedTxs, askForFundi
4549
4650 try {
4751
48− const fundsRemaining = await remainingTransactions(point);
49− if (fundsRemaining < signedTxs.length) {
50− throw new Error('tank: request invalid');
52+ if (point !== null) {
53+ const fundsRemaining = await remainingTransactions(point);
54+ if (fundsRemaining < signedTxs.length) {
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?
5162 }
5263
5364 const res = await fundTransactions(signedTxs);
@@ -57,6 +68,7 @@ const ensureFundsFor = async (web3, point, address, cost, signedTxs, askForFundi
5768 await waitForTransactionConfirm(web3, res.txHash);
5869 let newBalance = await web3.eth.getBalance(address);
5970 console.log('tank: funds have confirmed', balance >= cost, balance, newBalance);
71+ return true;
6072 }
6173
6274 } catch (e) {
@@ -69,12 +81,12 @@ const ensureFundsFor = async (web3, point, address, cost, signedTxs, askForFundi
6981 } else {
7082 console.log('tank: already have sufficient funds');
7183 }
84+ return false;
7285};
7386
7487// resolves when address has at least minBalance
7588//
76−// askForFunding: callback that takes (address, requiredBalance, currentBalance)
77−// and tells the user to get that address the required balance
89+//TODO should maybe do a "we got it" callback also, so clients can hide msg?
7890async function waitForBalance(web3, address, minBalance, askForFunding) {
7991 console.log('tank: awaiting balance', address, minBalance);
8092 return new Promise((resolve, reject) => {