1// /lib/tank: functions for funding transactions
3import retry from 'async-retry';
4import { toBN } from 'web3-utils';
5import { RETRY_OPTIONS, waitForTransactionConfirm } from './txn';
6import { WALLET_TYPES } from 'lib/constants';
7import { isRopsten } from './flags';
9const TANK_ADDRESS = '0x40f0A6db85f8D7A54fF3eA915b040dE8Cd4A0Eb5';
11//NOTE if accessing this in a localhost configuration fails with "CORS request
12// did not succeed", you might need to visit localhost:3001 or whatever
13// explicitly and tell your browser that's safe to access.
14// https://stackoverflow.com/a/53011185/1334324
15const port = isRopsten ? '3011' : '3001';
16const baseUrl = `https://gas-tank.urbit.org:${port}`;
18function sendRequest(where, what) {
19 return new Promise((resolve, reject) => {
20 fetch(baseUrl + where, {
24 'Content-Type': 'application/json',
26 body: JSON.stringify(what),
30 resolve(response.json());
39const remainingTransactions = point => {
40 if (typeof point === 'string') point = Number(point);
41 return sendRequest('/point', { point: point });
44const fundTransactions = signedTxs => {
45 return sendRequest('/request', { txs: signedTxs });
48// resolves when address has at least cost balance
49// returns true if gas tank was used, false otherwise
50// askForFunding: callback that takes (address, requiredBalance, currentBalance)
51// and tells the user to get that address the required balance
52// gotFunding: optional callback that stops telling the user to go get funding
53const ensureFundsFor = async (
63 if (typeof signedTxs !== 'object' || signedTxs.length === 0) {
64 throw new Error('tank: no transactions provided!');
67 const balance = toBN(await web3.eth.getBalance(address));
70 if (balance.gte(cost)) {
72 `tank: already have sufficient funds: ${address} proposed ` +
73 `transaction(s) that will cost ${cost.toString()}wei but it ` +
74 `already has ${balance.toString()}wei`
79 if (walletType === WALLET_TYPES.METAMASK) {
80 console.log('tank: disabling for metamask login');
85 // TODO: if we can't always (easily) provide a point, and the
86 // fundTransactions call is gonna fail anyway, should we maybe
87 // just not bother doing this check in the first place?
89 const fundsRemaining = await remainingTransactions(point);
90 if (fundsRemaining < signedTxs.length) {
91 throw new Error('tank: request invalid');
94 console.log('tank: no point provided. skipping remaining-funds check...');
97 const res = await fundTransactions(signedTxs);
99 throw new Error(`tank: request rejected ${JSON.stringify(res)}`);
102 await waitForTransactionConfirm(web3, res.txHash);
104 const newBalance = toBN(await web3.eth.getBalance(address));
107 if (newBalance.lt(cost)) {
109 `tank: transaction funded but the new balance of ` +
110 `${newBalance.toString()}wei is still less than ` +
111 `${cost.toString()}wei.`
116 `tank: funds have confirmed: ${address} now has ` +
117 `${newBalance.toString()}wei, up from ${balance.toString()}wei`
121 console.error(error);
122 await waitForBalance(
134// returns a promise that resolves when address has at least minBalance
135function waitForBalance(web3, address, minBalance, askForFunding, gotFunding) {
136 minBalance = toBN(minBalance);
137 return retry(async (bail, n) => {
138 const balance = toBN(await web3.eth.getBalance(address));
139 if (balance.gte(minBalance)) {
140 gotFunding && gotFunding();
143 askForFunding(address, minBalance.toString(), balance);
144 throw new Error('retrying');
151 remainingTransactions,
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