1// /lib/tank: functions for funding transactions
3import { waitForTransactionConfirm } from './txn';
5//NOTE if accessing this in a localhost configuration fails with "CORS request
6// did not succeed", you might need to visit localhost:3001 or whatever
7// explicitly and tell your browser that's safe to access.
8// https://stackoverflow.com/a/53011185/1334324
9const baseUrl = 'https://localhost:3001';
11function sendRequest(where, what) {
12 return new Promise((resolve, reject) => {
13 fetch(baseUrl + where, {
17 'Content-Type': 'application/json',
19 body: JSON.stringify(what),
23 resolve(response.json());
32const remainingTransactions = point => {
33 if (typeof point === 'string') point = Number(point);
34 return sendRequest('/point', { point: point });
37const fundTransactions = signedTxs => {
38 return sendRequest('/request', { txs: signedTxs });
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
45// gotFunding: optional callback that stops telling the user to go get funding
46const ensureFundsFor = async (
55 let balance = await web3.eth.getBalance(address);
60 const fundsRemaining = await remainingTransactions(point);
61 if (fundsRemaining < signedTxs.length) {
62 throw new Error('tank: request invalid');
65 console.log('tank: skipping remaining-funds check');
66 //TODO if we can't always (easily) provide a point, and the
67 // fundTransactions call is gonna fail anyway, should we maybe
68 // just not bother doing this check in the first place?
71 const res = await fundTransactions(signedTxs);
73 throw new Error('tank: request rejected');
75 await waitForTransactionConfirm(web3, res.txHash);
76 let newBalance = await web3.eth.getBalance(address);
78 'tank: funds have confirmed',
86 console.log('tank: funding failed', e);
87 await waitForBalance(web3, address, cost, askForFunding, gotFunding);
90 console.log('tank: already have sufficient funds');
95// resolves when address has at least minBalance
97//TODO should maybe do a "we got it" callback also, so clients can hide msg?
98async function waitForBalance(
105 console.log('tank: awaiting balance', address, minBalance);
106 return new Promise((resolve, reject) => {
107 let oldBalance = null;
108 const checkForBalance = async () => {
109 const balance = await web3.eth.getBalance(address);
110 if (balance >= minBalance) {
111 // if we ever asked for funding, retract that request now
112 if (gotFunding && oldBalance !== null) {
117 if (balance !== oldBalance) {
118 askForFunding(address, minBalance, balance);
120 setTimeout(checkForBalance, 13000);
127export { remainingTransactions, fundTransactions, ensureFundsFor };
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list