Skip to content

Commit 1ac1b6c

Browse files
committed
update gas-price-oracle
1 parent f8cb27e commit 1ac1b6c

File tree

9 files changed

+125
-103
lines changed

9 files changed

+125
-103
lines changed

.eslintrc.json

Lines changed: 48 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,50 @@
11
{
2-
"env": {
3-
"node": true,
4-
"browser": true,
5-
"es6": true,
6-
"mocha": true
7-
},
8-
"extends": "eslint:recommended",
9-
"globals": {
10-
"Atomics": "readonly",
11-
"SharedArrayBuffer": "readonly"
12-
},
13-
"parserOptions": {
14-
"ecmaVersion": 2018
15-
},
16-
"rules": {
17-
"indent": [
18-
"error",
19-
2,
20-
{"SwitchCase": 1}
21-
],
22-
"linebreak-style": [
23-
"error",
24-
"unix"
25-
],
26-
"quotes": [
27-
"error",
28-
"single"
29-
],
30-
"semi": [
31-
"error",
32-
"never"
33-
],
34-
"object-curly-spacing": [
35-
"error",
36-
"always"
37-
],
38-
"require-await": "error"
39-
}
2+
"env": {
3+
"node": true,
4+
"browser": true,
5+
"es6": true,
6+
"mocha": true
7+
},
8+
"extends": "eslint:recommended",
9+
"globals": {
10+
"Atomics": "readonly",
11+
"SharedArrayBuffer": "readonly"
12+
},
13+
"parserOptions": {
14+
"ecmaVersion": 2018
15+
},
16+
"rules": {
17+
"indent": [
18+
"error",
19+
2,
20+
{
21+
"SwitchCase": 1
22+
}
23+
],
24+
"linebreak-style": [
25+
"error",
26+
"unix"
27+
],
28+
"quotes": [
29+
"error",
30+
"single"
31+
],
32+
"semi": [
33+
"error",
34+
"never"
35+
],
36+
"object-curly-spacing": [
37+
"error",
38+
"always"
39+
],
40+
"require-await": "error",
41+
"space-before-function-paren": [
42+
"error",
43+
{
44+
"anonymous": "always",
45+
"named": "never",
46+
"asyncArrow": "always"
47+
}
48+
]
49+
}
4050
}

config.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require('dotenv').config()
22

33
module.exports = {
4-
version: 2.7,
4+
version: 2.8,
55
netId: Number(process.env.NET_ID) || 42,
66
redisUrl: process.env.REDIS_URL,
77
rpcUrl: process.env.RPC_URL || 'https://kovan.infura.io/',
@@ -145,7 +145,6 @@ module.exports = {
145145
}
146146
},
147147
defaultGasPrice: 20,
148-
gasOracleUrls: ['https://ethgasstation.info/json/ethgasAPI.json', 'https://gas-oracle.zoltu.io/'],
149148
port: process.env.APP_PORT,
150149
relayerServiceFee: Number(process.env.RELAYER_FEE),
151150
maxGasPrice: process.env.MAX_GAS_PRICE || 200,

package-lock.json

Lines changed: 40 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "relay",
33
"version": "1.0.0",
4-
"description": "Relayer for Tornado mixer. https://tornado.cash",
4+
"description": "Relayer for Tornado.cash privacy solution. https://tornado.cash",
55
"main": "app.js",
66
"scripts": {
77
"start": "node app.js",
@@ -14,6 +14,7 @@
1414
"bull": "^3.12.1",
1515
"dotenv": "^8.2.0",
1616
"express": "^4.17.1",
17+
"gas-price-oracle": "^0.1.4",
1718
"ioredis": "^4.14.1",
1819
"node-fetch": "^2.6.0",
1920
"web3": "^1.2.2",

src/Fetcher.js

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
const fetch = require('node-fetch')
21
const Web3 = require('web3')
3-
const { gasOracleUrls, defaultGasPrice, oracleRpcUrl, oracleAddress } = require('../config')
2+
const { defaultGasPrice, oracleRpcUrl, oracleAddress } = require('../config')
43
const { getArgsForOracle } = require('./utils')
54
const { redisClient } = require('./redis')
65
const priceOracleABI = require('../abis/PriceOracle.abi.json')
@@ -39,46 +38,17 @@ class Fetcher {
3938
return acc
4039
}, {})
4140
setTimeout(() => this.fetchPrices(), 1000 * 30)
42-
} catch(e) {
41+
} catch (e) {
4342
console.error('fetchPrices', e.message)
4443
setTimeout(() => this.fetchPrices(), 1000 * 30)
4544
}
4645
}
47-
async fetchGasPrice({ oracleIndex = 0 } = {}) {
48-
oracleIndex = (oracleIndex + 1) % gasOracleUrls.length
49-
const url = gasOracleUrls[oracleIndex]
50-
const delimiter = url === 'https://ethgasstation.info/json/ethgasAPI.json' ? 10 : 1
51-
try {
52-
const response = await fetch(url)
53-
if (response.status === 200) {
54-
const json = await response.json()
55-
if (Number(json.fast) === 0) {
56-
throw new Error('Fetch gasPrice failed')
57-
}
58-
59-
if (json.fast) {
60-
this.gasPrices.fast = Number(json.fast) / delimiter
61-
}
62-
63-
if (json.percentile_97) {
64-
this.gasPrices.fast = parseInt(json.percentile_90) + 1 / delimiter
65-
}
66-
// console.log('gas price fetch', this.gasPrices)
67-
} else {
68-
throw Error('Fetch gasPrice failed')
69-
}
70-
setTimeout(() => this.fetchGasPrice({ oracleIndex }), 15000)
71-
} catch (e) {
72-
console.log('fetchGasPrice', e.message)
73-
setTimeout(() => this.fetchGasPrice({ oracleIndex }), 15000)
74-
}
75-
}
7646
async fetchNonce() {
7747
try {
7848
const nonce = await this.web3.eth.getTransactionCount(this.web3.eth.defaultAccount)
7949
await redisClient.set('nonce', nonce)
8050
console.log(`Current nonce: ${nonce}`)
81-
} catch(e) {
51+
} catch (e) {
8252
console.error('fetchNonce failed', e.message)
8353
setTimeout(this.fetchNonce, 3000)
8454
}

src/index.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const {
1010
maxGasPrice
1111
} = require('../config')
1212
const relayController = require('./relayController')
13-
const { fetcher, web3 } = require('./instances')
13+
const { fetcher, web3, gasPriceOracle } = require('./instances')
1414
const { getMixers } = require('./utils')
1515
const mixers = getMixers()
1616
const { redisClient } = require('./redis')
@@ -26,7 +26,7 @@ app.use((err, req, res, next) => {
2626
}
2727
})
2828

29-
app.use(function(req, res, next) {
29+
app.use(function (req, res, next) {
3030
res.header('Access-Control-Allow-Origin', '*')
3131
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept')
3232
next()
@@ -39,11 +39,11 @@ app.get('/', function (req, res) {
3939

4040
app.get('/status', async function (req, res) {
4141
let nonce = await redisClient.get('nonce')
42-
const { ethPrices, gasPrices } = fetcher
42+
const { ethPrices } = fetcher
4343
res.json({
4444
relayerAddress: web3.eth.defaultAccount,
4545
mixers,
46-
gasPrices,
46+
gasPrices: await gasPriceOracle.gasPrices(),
4747
netId,
4848
ethPrices,
4949
relayerServiceFee,
@@ -57,30 +57,28 @@ app.post('/relay', relayController)
5757
let server = app.listen(port || 8000)
5858
server.setTimeout(600000)
5959
console.log('Gas price oracle started.')
60-
fetcher.fetchGasPrice()
6160
fetcher.fetchPrices()
6261
fetcher.fetchNonce()
6362

6463
console.log('Relayer started on port', port || 8000)
6564
console.log(`relayerAddress: ${web3.eth.defaultAccount}`)
6665
console.log(`mixers: ${JSON.stringify(mixers)}`)
67-
console.log(`gasPrices: ${JSON.stringify(fetcher.gasPrices)}`)
6866
console.log(`netId: ${netId}`)
6967
console.log(`ethPrices: ${JSON.stringify(fetcher.ethPrices)}`)
7068

7169
const { GAS_PRICE_BUMP_PERCENTAGE, ALLOWABLE_PENDING_TX_TIMEOUT, NONCE_WATCHER_INTERVAL, MAX_GAS_PRICE } = process.env
72-
if(!NONCE_WATCHER_INTERVAL) {
70+
if (!NONCE_WATCHER_INTERVAL) {
7371
console.log(`NONCE_WATCHER_INTERVAL is not set. Using default value ${watherInterval / 1000} sec`)
7472
}
7573

76-
if(!GAS_PRICE_BUMP_PERCENTAGE) {
74+
if (!GAS_PRICE_BUMP_PERCENTAGE) {
7775
console.log(`GAS_PRICE_BUMP_PERCENTAGE is not set. Using default value ${gasBumpPercentage}%`)
7876
}
7977

80-
if(!ALLOWABLE_PENDING_TX_TIMEOUT) {
78+
if (!ALLOWABLE_PENDING_TX_TIMEOUT) {
8179
console.log(`ALLOWABLE_PENDING_TX_TIMEOUT is not set. Using default value ${pendingTxTimeout / 1000} sec`)
8280
}
8381

84-
if(!MAX_GAS_PRICE) {
82+
if (!MAX_GAS_PRICE) {
8583
console.log(`ALLOWABLE_PENDING_TX_TIMEOUT is not set. Using default value ${maxGasPrice} Gwei`)
8684
}

src/instances.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1+
const { rpcUrl } = require('../config')
12
const Fetcher = require('./Fetcher')
23
const Sender = require('./sender')
4+
const { GasPriceOracle } = require('gas-price-oracle')
35
const web3 = require('./setupWeb3')
46
const fetcher = new Fetcher(web3)
57
const sender = new Sender(web3)
8+
const gasPriceOracle = new GasPriceOracle({ defaultRpc: rpcUrl })
69

710
module.exports = {
811
fetcher,
912
web3,
10-
sender
13+
sender,
14+
gasPriceOracle
1115
}

src/relayController.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const {
77
const config = require('../config')
88
const { redisClient, redisOpts } = require('./redis')
99

10-
const { web3, fetcher, sender } = require('./instances')
10+
const { web3, fetcher, sender, gasPriceOracle } = require('./instances')
1111
const withdrawQueue = new Queue('withdraw', redisOpts)
1212

1313
const reponseCbs = {}
@@ -22,26 +22,26 @@ async function relayController(req, resp) {
2222
let requestJob
2323

2424
const { proof, args, contract } = req.body
25-
let { valid , reason } = isValidProof(proof)
25+
let { valid, reason } = isValidProof(proof)
2626
if (!valid) {
2727
console.log('Proof is invalid:', reason)
2828
return resp.status(400).json({ error: 'Proof format is invalid' })
2929
}
3030

31-
({ valid , reason } = isValidArgs(args))
31+
({ valid, reason } = isValidArgs(args))
3232
if (!valid) {
3333
console.log('Args are invalid:', reason)
3434
return resp.status(400).json({ error: 'Withdraw arguments are invalid' })
3535
}
3636

3737
let currency, amount
38-
( { valid, currency, amount } = isKnownContract(contract))
38+
({ valid, currency, amount } = isKnownContract(contract))
3939
if (!valid) {
4040
console.log('Contract does not exist:', contract)
4141
return resp.status(400).json({ error: 'This relayer does not support the token' })
4242
}
4343

44-
const [ root, nullifierHash, recipient, relayer, fee, refund ] = [
44+
const [root, nullifierHash, recipient, relayer, fee, refund] = [
4545
args[0],
4646
args[1],
4747
toChecksumAddress(args[2]),
@@ -65,9 +65,9 @@ async function relayController(req, resp) {
6565
reponseCbs[requestJob.id] = resp
6666
}
6767

68-
withdrawQueue.process(async function(job, done){
68+
withdrawQueue.process(async function (job, done) {
6969
console.log(Date.now(), ' withdraw started', job.id)
70-
const gasPrices = fetcher.gasPrices
70+
const gasPrices = await gasPriceOracle.gasPrices()
7171
const { contract, nullifierHash, root, proof, args, refund, currency, amount, fee } = job.data
7272
console.log(JSON.stringify(job.data))
7373
// job.data contains the custom data passed when the job was created
@@ -128,7 +128,7 @@ withdrawQueue.process(async function(job, done){
128128
nonce
129129
}
130130
tx.date = Date.now()
131-
await redisClient.set('tx:' + nonce, JSON.stringify(tx) )
131+
await redisClient.set('tx:' + nonce, JSON.stringify(tx))
132132
nonce += 1
133133
await redisClient.set('nonce', nonce)
134134
sender.sendTx(tx, done)

0 commit comments

Comments
 (0)