@@ -203,22 +203,57 @@ export async function reticketPointBetweenWallets({
204 progress(TRANSACTION_PROGRESS.TRANSFERRING);
204 progress(TRANSACTION_PROGRESS.TRANSFERRING);
206− await sendAndAwaitAllSerial(
206+ const receipts = await sendAndAwaitAllSerial(
208 txPairs.map(p => p.signed),
208 txPairs.map(p => p.signed),
212+ const realCost = gasPriceWeiBN.mul(
213+ receipts.reduce((sum, receipt) => {
214+ return sum.add(toBN(receipt.gasUsed));
213 // move leftover eth
219 // move leftover eth
216 progress(TRANSACTION_PROGRESS.CLEANING);
222 progress(TRANSACTION_PROGRESS.CLEANING);
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));
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)) {
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({
236+ walletType: fromWalletType,
237+ walletHdPath: fromWalletHdPath,
241+ gasPrice: gasPriceGwei,
245+ sendSignedTransaction(web3, stx);
247+ // only update these after the above succeeded
249+ balance = balance.sub(remainder).sub(sendEthCost);
251+ console.log('error returning gas, safely ignored:');
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)) {
224 const value = balance.sub(sendEthCost);
259 const value = balance.sub(sendEthCost);