on master help sign in
Add gotFunding callback to ensureFundsFor
201d3ecFang
src/bridge/lib/tank.js+10−3
@@ -42,7 +42,9 @@ const fundTransactions = signedTxs => {
42// returns true if gas tank was used, false otherwise
42// returns true if gas tank was used, false otherwise
43// askForFunding: callback that takes (address, requiredBalance, currentBalance)
43// askForFunding: callback that takes (address, requiredBalance, currentBalance)
44// and tells the user to get that address the required balance
44// and tells the user to get that address the required balance
45+// gotFunding: optional callback that stops telling the user to go get funding
45−const ensureFundsFor = async (web3, point, address, cost, signedTxs, askForFunding) => {
46+const ensureFundsFor = async (web3, point, address, cost, signedTxs,
47+ askForFunding, gotFunding) => {
46 let balance = await web3.eth.getBalance(address);
48 let balance = await web3.eth.getBalance(address);
47
49
48 if (cost > balance) {
50 if (cost > balance) {
@@ -74,7 +76,7 @@ const ensureFundsFor = async (web3, point, address, cost, signedTxs, askForFundi
74 } catch (e) {
76 } catch (e) {
75
77
76 console.log('tank: funding failed', e);
78 console.log('tank: funding failed', e);
77− await waitForBalance(web3, address, cost, askForFunding);
79+ await waitForBalance(web3, address, cost, askForFunding, gotFunding);
78
80
79 }
81 }
80
82
@@ -87,13 +89,18 @@ const ensureFundsFor = async (web3, point, address, cost, signedTxs, askForFundi
87// resolves when address has at least minBalance
89// resolves when address has at least minBalance
88//
90//
89//TODO should maybe do a "we got it" callback also, so clients can hide msg?
91//TODO should maybe do a "we got it" callback also, so clients can hide msg?
90−async function waitForBalance(web3, address, minBalance, askForFunding) {
92+async function waitForBalance(web3, address, minBalance,
93+ askForFunding, gotFunding) {
91 console.log('tank: awaiting balance', address, minBalance);
94 console.log('tank: awaiting balance', address, minBalance);
92 return new Promise((resolve, reject) => {
95 return new Promise((resolve, reject) => {
93 let oldBalance = null;
96 let oldBalance = null;
94 const checkForBalance = async () => {
97 const checkForBalance = async () => {
95 const balance = await web3.eth.getBalance(address);
98 const balance = await web3.eth.getBalance(address);
96 if (balance >= minBalance) {
99 if (balance >= minBalance) {
100+ // if we ever asked for funding, retract that request now
101+ if (gotFunding && (oldBalance !== null)) {
102+ gotFunding();
103+ }
97 resolve();
104 resolve();
98 } else {
105 } else {
99 if (balance !== oldBalance) {
106 if (balance !== oldBalance) {
src/bridge/views/InvitesSend.js+2−2
@@ -192,9 +192,9 @@ class InvitesSend extends React.Component {
192 const tankWasUsed = await tank.ensureFundsFor(
192 const tankWasUsed = await tank.ensureFundsFor(
193 web3, this.point, this.address,
193 web3, this.point, this.address,
194 (INVITE_COST * invites.length),
194 (INVITE_COST * invites.length),
195− invites.map(i => i.rawTx), this.askForFunding
195+ invites.map(i => i.rawTx), this.askForFunding,
196+ () => { this.setState({ fundingMessage: Nothing() }); }
196 );
197 );
197− this.setState({ fundingMessage: Nothing() }); //TODO see /lib/tank waitForBalance
198
198
199 this.setState({ status: STATUS.SENDING });
199 this.setState({ status: STATUS.SENDING });
200
200