on master help sign in
Merge pull request #523 from urbit/m/tip-the-tank
17f04f3fang
src/lib/reticket.js+37−2
@@ -203,22 +203,57 @@ export async function reticketPointBetweenWallets({
203
203
204 progress(TRANSACTION_PROGRESS.TRANSFERRING);
204 progress(TRANSACTION_PROGRESS.TRANSFERRING);
205
205
206− await sendAndAwaitAllSerial(
206+ const receipts = await sendAndAwaitAllSerial(
207 web3,
207 web3,
208 txPairs.map(p => p.signed),
208 txPairs.map(p => p.signed),
209 usedTank
209 usedTank
210 );
210 );
211
211
212+ const realCost = gasPriceWeiBN.mul(
213+ receipts.reduce((sum, receipt) => {
214+ return sum.add(toBN(receipt.gasUsed));
215+ }, toBN(0))
216+ );
217+
212 //
218 //
213 // move leftover eth
219 // move leftover eth
214 //
220 //
215
221
216 progress(TRANSACTION_PROGRESS.CLEANING);
222 progress(TRANSACTION_PROGRESS.CLEANING);
217
223
218− // if non-trivial eth left in invite wallet, transfer to new ownership
219 let balance = toBN(await web3.eth.getBalance(fromWallet.address));
224 let balance = toBN(await web3.eth.getBalance(fromWallet.address));
220 const gasLimit = GAS_LIMITS.SEND_ETH;
225 const gasLimit = GAS_LIMITS.SEND_ETH;
221 const sendEthCost = gasPriceWeiBN.mul(toBN(gasLimit));
226 const sendEthCost = gasPriceWeiBN.mul(toBN(gasLimit));
227+
228+ // if we have funds left over from what the gas tank gave us, send those back
229+ if (usedTank && realCost.add(sendEthCost).lt(totalCost)) {
230+ try {
231+ const remainder = totalCost.sub(realCost).sub(sendEthCost);
232+ console.log('returning to gas tank', remainder.toString());
233+ const txn = { to: tank.TANK_ADDRESS, value: remainder };
234+ const stx = await signTransaction({
235+ wallet: fromWallet,
236+ walletType: fromWalletType,
237+ walletHdPath: fromWalletHdPath,
238+ txn,
239+ chainId,
240+ networkType,
241+ gasPrice: gasPriceGwei,
242+ gasLimit,
243+ nonce: inviteNonce,
244+ });
245+ sendSignedTransaction(web3, stx);
246+
247+ // only update these after the above succeeded
248+ inviteNonce++;
249+ balance = balance.sub(remainder).sub(sendEthCost);
250+ } catch (err) {
251+ console.log('error returning gas, safely ignored:');
252+ console.log(err);
253+ }
254+ }
255+
256+ // if non-trivial eth left in invite wallet, transfer to new ownership
222 if (transferEth && balance.gt(sendEthCost)) {
257 if (transferEth && balance.gt(sendEthCost)) {
223 try {
258 try {
224 const value = balance.sub(sendEthCost);
259 const value = balance.sub(sendEthCost);
src/lib/tank.js+8−1
@@ -5,6 +5,8 @@ import { toBN } from 'web3-utils';
5import { RETRY_OPTIONS, waitForTransactionConfirm } from './txn';
5import { RETRY_OPTIONS, waitForTransactionConfirm } from './txn';
6import { WALLET_TYPES } from 'lib/wallet';
6import { WALLET_TYPES } from 'lib/wallet';
7
7
8+const TANK_ADDRESS = '0x40f0A6db85f8D7A54fF3eA915b040dE8Cd4A0Eb5';
9+
8//NOTE if accessing this in a localhost configuration fails with "CORS request
10//NOTE if accessing this in a localhost configuration fails with "CORS request
9// did not succeed", you might need to visit localhost:3001 or whatever
11// did not succeed", you might need to visit localhost:3001 or whatever
10// explicitly and tell your browser that's safe to access.
12// explicitly and tell your browser that's safe to access.
@@ -142,4 +144,9 @@ function waitForBalance(web3, address, minBalance, askForFunding, gotFunding) {
142 }, RETRY_OPTIONS);
144 }, RETRY_OPTIONS);
143}
145}
144
146
147+export {
148+ TANK_ADDRESS,
145−export { remainingTransactions, fundTransactions, ensureFundsFor };
149+ remainingTransactions,
150+ fundTransactions,
151+ ensureFundsFor,
152+};
src/lib/txn.js+3−3
@@ -164,7 +164,7 @@ const sendAndAwaitAll = (web3, stxs, doubtNonceError) => {
164 return Promise.all(
164 return Promise.all(
165 stxs.map(async tx => {
165 stxs.map(async tx => {
166 const txHash = await sendSignedTransaction(web3, tx, doubtNonceError);
166 const txHash = await sendSignedTransaction(web3, tx, doubtNonceError);
167− await waitForTransactionConfirm(web3, txHash);
167+ return await waitForTransactionConfirm(web3, txHash);
168 })
168 })
169 );
169 );
170};
170};
@@ -172,9 +172,9 @@ const sendAndAwaitAll = (web3, stxs, doubtNonceError) => {
172const sendAndAwaitAllSerial = (web3, stxs, doubtNonceError) => {
172const sendAndAwaitAllSerial = (web3, stxs, doubtNonceError) => {
173 return stxs.reduce(
173 return stxs.reduce(
174 (promise, stx) =>
174 (promise, stx) =>
175− promise.then(async () => {
175+ promise.then(async (receipts = []) => {
176 const txHash = await sendSignedTransaction(web3, stx, doubtNonceError);
176 const txHash = await sendSignedTransaction(web3, stx, doubtNonceError);
177− await waitForTransactionConfirm(web3, txHash);
177+ return [...receipts, await waitForTransactionConfirm(web3, txHash)];
178 }),
178 }),
179 Promise.resolve()
179 Promise.resolve()
180 );
180 );