on master help sign in
Merge pull request #236 from urbit/fix/tank-messaging
e88e4a3Matt Condon
src/lib/tank.js+50−39
@@ -54,54 +54,65 @@ const ensureFundsFor = async (
54 askForFunding,
54 askForFunding,
55 gotFunding
55 gotFunding
56) => {
56) => {
57− let balance = toBN(await web3.eth.getBalance(address));
57+ const balance = toBN(await web3.eth.getBalance(address));
58 cost = toBN(cost);
58 cost = toBN(cost);
59
59
60− if (cost.gt(balance)) {
60+ if (balance.gte(cost)) {
61− try {
61+ console.log(
62− if (point !== null) {
62+ `tank: already have sufficient funds: ${address} proposed ` +
63− const fundsRemaining = await remainingTransactions(point);
63+ `transaction(s) that will cost ${cost.toString()}wei but it ` +
64− if (fundsRemaining < signedTxs.length) {
64+ `already has ${balance.toString}wei`
65− throw new Error('tank: request invalid');
65+ );
66− }
66+ return false;
67− } else {
68− console.log('tank: skipping remaining-funds check');
69− //TODO if we can't always (easily) provide a point, and the
70− // fundTransactions call is gonna fail anyway, should we maybe
71− // just not bother doing this check in the first place?
72− }
67+ }
73
68
74− const res = await fundTransactions(signedTxs);
69+ try {
75− if (!res.success) {
70+ // TODO: if we can't always (easily) provide a point, and the
71+ // fundTransactions call is gonna fail anyway, should we maybe
72+ // just not bother doing this check in the first place?
73+ if (point !== null) {
74+ const fundsRemaining = await remainingTransactions(point);
75+ if (fundsRemaining < signedTxs.length) {
76− throw new Error('tank: request rejected', res);
76+ throw new Error('tank: request invalid');
77− } else {
78− await waitForTransactionConfirm(web3, res.txHash);
79− let newBalance = await web3.eth.getBalance(address);
80− console.log(
81− 'tank: funds have confirmed',
82− balance.toString(),
83− newBalance
84− );
85− return true;
86 }
77 }
87− } catch (e) {
78+ } else {
88− console.log('tank: funding failed', e);
79+ console.log('tank: no point provided. skipping remaining-funds check...');
89− await waitForBalance(
80+ }
90− web3,
81+
91− address,
82+ const res = await fundTransactions(signedTxs);
92− cost.toString(),
83+ if (!res.success) {
93− askForFunding,
84+ throw new Error(`tank: request rejected ${JSON.stringify(res)}`);
94− gotFunding
85+ }
86+
87+ await waitForTransactionConfirm(web3, res.txHash);
88+
89+ const newBalance = await web3.eth.getBalance(address);
90+
91+ // sanity check
92+ if (newBalance.lt(cost)) {
93+ throw new Error(
94+ `tank: transaction funded but the new balance of ` +
95+ `${newBalance.toString()}wei is still less than ` +
96+ `${cost.toString()}wei.`
95 );
97 );
96 }
98 }
97− } else {
99+
98 console.log(
100 console.log(
99− 'tank: already have sufficient funds',
101+ `tank: funds have confirmed: ${address} now has ` +
100− cost.toString(),
102+ ` ${newBalance.toString()}wei, up from ${balance.toString()}wei`
103+ );
104+ return true;
105+ } catch (error) {
106+ console.error(error);
107+ await waitForBalance(
108+ web3,
101 address,
109 address,
102− balance.toString()
110+ cost.toString(),
111+ askForFunding,
112+ gotFunding
103 );
113 );
104 }
114 }
115+
105 return false;
116 return false;
106};
117};
107
118
@@ -114,7 +125,7 @@ function waitForBalance(web3, address, minBalance, askForFunding, gotFunding) {
114 gotFunding && gotFunding();
125 gotFunding && gotFunding();
115 return;
126 return;
116 } else {
127 } else {
117− askForFunding(address, minBalance, balance);
128+ askForFunding(address, minBalance.toString(), balance);
118 throw new Error('retrying');
129 throw new Error('retrying');
119 }
130 }
120 }, RETRY_OPTIONS);
131 }, RETRY_OPTIONS);