1// /lib/tank: functions for funding transactions
3import retry from 'async-retry';
4import { toBN } from 'web3-utils';
5import { RETRY_OPTIONS, waitForTransactionConfirm } from './txn';
6import { isGoerli } from './flags';
8const TANK_ADDRESS = '0x40f0A6db85f8D7A54fF3eA915b040dE8Cd4A0Eb5';
10//NOTE if accessing this in a localhost configuration fails with "CORS request
11// did not succeed", you might need to visit localhost:3001 or whatever
12// explicitly and tell your browser that's safe to access.
13// https://stackoverflow.com/a/53011185/1334324
14const port = isGoerli ? '3011' : '3001';
15const baseUrl = `https://gas-tank.urbit.org:${port}`;
17function sendRequest(where, what) {
18 return new Promise((resolve, reject) => {
19 fetch(baseUrl + where, {
23 'Content-Type': 'application/json',
25 body: JSON.stringify(what),
29 resolve(response.json());
38const remainingTransactions = point => {
39 if (typeof point === 'string') point = Number(point);
40 return sendRequest('/point', { point: point });
43const fundTransactions = signedTxs => {
44 return sendRequest('/request', { txs: signedTxs });
47// resolves when address has at least cost balance
48// returns true if gas tank was used, false otherwise
49// askForFunding: callback that takes (address, requiredBalance, currentBalance)
50// and tells the user to get that address the required balance
51// gotFunding: optional callback that stops telling the user to go get funding
52const ensureFundsFor = async (
62 if (typeof signedTxs !== 'object' || signedTxs.length === 0) {
63 throw new Error('tank: no transactions provided!');
66 //TODO this depends on FakeSignResult serialization in a somewhat ugly way
67 if (signedTxs.some(tx => tx === '0x???')) {
68 console.log('tank: disabling for unsigned sender wallet');
72 const balance = toBN(await web3.eth.getBalance(address));
75 if (balance.gte(cost)) {
77 `tank: already have sufficient funds: ${address} proposed ` +
78 `transaction(s) that will cost ${cost.toString()}wei but it ` +
79 `already has ${balance.toString()}wei`
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