Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove broken ethgasstation.info usage, let default gas be set #6248

Merged
merged 1 commit into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 1 addition & 68 deletions packages/identity-service/src/relay/ethTxRelay.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const { Lock } = require('../redis')

const ENVIRONMENT = config.get('environment')
const DEFAULT_GAS_LIMIT = config.get('defaultGasLimit')
const GANACHE_GAS_PRICE = config.get('ganacheGasPrice')

// L1 relayer wallets
const ethRelayerWallets = config.get('ethRelayerWallets') // { publicKey, privateKey }
Expand Down Expand Up @@ -70,12 +69,6 @@ const sendEthTransaction = async (req, txProps, reqBodySHA) => {
req.logger.info(
`L1 txRelay - selected relayerPublicWallet=${selectedEthRelayerWallet.publicKey}`
)
const ethGasPriceInfo = await getProdGasInfo(req.app.get('redis'), req.logger)

// Select the 'fast' gas price
let ethRelayGasPrice = ethGasPriceInfo[config.get('ethRelayerProdGasTier')]
ethRelayGasPrice =
ethRelayGasPrice * parseFloat(config.get('ethGasMultiplier'))

let resp
try {
Expand All @@ -88,7 +81,6 @@ const sendEthTransaction = async (req, txProps, reqBodySHA) => {
'0x00',
ethWeb3,
req.logger,
ethRelayGasPrice,
gasLimit,
encodedABI
)
Expand Down Expand Up @@ -129,7 +121,6 @@ const createAndSendEthTransaction = async (
value,
web3,
logger,
gasPrice,
gasLimit = null,
data = null
) => {
Expand All @@ -144,7 +135,6 @@ const createAndSendEthTransaction = async (
const nonce = await web3.eth.getTransactionCount(address)
let txParams = {
nonce: web3.utils.toHex(nonce),
gasPrice,
gasLimit: gasLimit ? web3.utils.numberToHex(gasLimit) : DEFAULT_GAS_LIMIT,
to: receiverAddress,
value: web3.utils.toHex(value)
Expand All @@ -157,69 +147,13 @@ const createAndSendEthTransaction = async (
tx.sign(privateKeyBuffer)
const signedTx = '0x' + tx.serialize().toString('hex')
logger.info(
`L1 txRelay - sending a transaction for sender ${
sender.publicKey
} to ${receiverAddress}, gasPrice ${parseInt(
gasPrice,
16
)}, gasLimit ${DEFAULT_GAS_LIMIT}, nonce ${nonce}`
`L1 txRelay - sending a transaction for sender ${sender.publicKey} to ${receiverAddress}, gasLimit ${DEFAULT_GAS_LIMIT}, nonce ${nonce}`
)
const receipt = await web3.eth.sendSignedTransaction(signedTx)

return { txHash: receipt.transactionHash, txParams }
}

// Query mainnet ethereum gas prices
/*
Sample call:https://data-api.defipulse.com/api/v1/egs/api/ethgasAPI.json?api-key=some_key
*/
const getProdGasInfo = async (redis, logger) => {
if (ENVIRONMENT === 'development') {
return {
fastGweiHex: GANACHE_GAS_PRICE,
averageGweiHex: GANACHE_GAS_PRICE,
fastestGweiHex: GANACHE_GAS_PRICE
}
}
const prodGasPriceKey = 'eth-gas-prod-price-info'
let gasInfo = await redis.get(prodGasPriceKey)
if (!gasInfo) {
logger.info(`Redis cache miss, querying remote`)
let prodGasInfo
const defiPulseKey = config.get('defiPulseApiKey')
if (defiPulseKey !== '') {
logger.info(`L1 txRelay querying ethGas with apiKey`)
prodGasInfo = await axios({
method: 'get',
url: `https://data-api.defipulse.com/api/v1/egs/api/ethgasAPI.json?api-key=${defiPulseKey}`
})
} else {
prodGasInfo = await axios({
method: 'get',
url: 'https://ethgasstation.info/api/ethgasAPI.json'
})
}
const { fast, fastest, safeLow, average } = prodGasInfo.data
gasInfo = { fast, fastest, safeLow, average }
// Convert returned values into gwei to be used during relay and cache
// Must divide by 10 to get gwei price (Math.pow(10, 9) -> Math.pow(10, 8))
// https://docs.ethgasstation.info/gas-price
gasInfo.fastGwei = parseInt(gasInfo.fast) * Math.pow(10, 8)
gasInfo.fastestGwei = parseInt(gasInfo.fastest) * Math.pow(10, 8)
gasInfo.averageGwei = parseInt(gasInfo.average) * Math.pow(10, 8)
gasInfo.fastGweiHex = ethWeb3.utils.numberToHex(gasInfo.fastGwei)
gasInfo.fastestGweiHex = ethWeb3.utils.numberToHex(gasInfo.fastestGwei)
gasInfo.averageGweiHex = ethWeb3.utils.numberToHex(gasInfo.averageGwei)
gasInfo.cachedResponse = false
redis.set(prodGasPriceKey, JSON.stringify(gasInfo), 'EX', 30)
logger.info(`L1 txRelay - Updated gasInfo: ${JSON.stringify(gasInfo)}`)
} else {
gasInfo = JSON.parse(gasInfo)
gasInfo.cachedResponse = true
}
return gasInfo
}

/**
* Fund L1 wallets as necessary to facilitate multiple relayers
*/
Expand Down Expand Up @@ -271,6 +205,5 @@ module.exports = {
sendEthTransaction,
queryEthRelayerWallet,
getEthRelayerFunds,
getProdGasInfo,
generateETHWalletLockKey
}
12 changes: 0 additions & 12 deletions packages/identity-service/src/routes/ethRelay.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,4 @@ module.exports = function (app) {
return successResponse({ selectedEthWallet })
})
)

// Serves latest state of production gas tracking on identity service
app.get(
'/eth_gas_price',
handleResponse(async (req, res, next) => {
const gasInfo = await ethTxRelay.getProdGasInfo(
req.app.get('redis'),
req.logger
)
return successResponse(gasInfo)
})
)
}
20 changes: 2 additions & 18 deletions packages/libs/initScripts/manageProdRelayerWallets.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,7 @@ const loadProdRelayerWallets = async () => {
return { funder, relayerWallets }
}

const getGasPrice = async () => {
const { data } = await axios({
url: 'https://identityservice.staging.audius.co/eth_gas_price',
method: 'get',
timeout: 3000
})
return data
}

const createAndSendTransaction = async (sender, receiverAddress, value, web3, gasPrice) => {
const createAndSendTransaction = async (sender, receiverAddress, value, web3) => {
const privateKeyBuffer = Buffer.from(sender.privateKey, 'hex')
const walletAddress = EthereumWallet.fromPrivateKey(privateKeyBuffer)
const address = walletAddress.getAddressString()
Expand All @@ -123,7 +114,6 @@ const createAndSendTransaction = async (sender, receiverAddress, value, web3, ga
console.log(`Sending tx from ${sender.publicKey} to ${receiverAddress} with nonce=${nonce}, gasPrice=${gasPrice}, gasLimit=${gasLimit}`)
const txParams = {
nonce: web3.utils.toHex(nonce),
gasPrice: gasPrice,
gasLimit: gasLimit,
to: receiverAddress,
value: web3.utils.toHex(value)
Expand All @@ -143,9 +133,6 @@ const fundEthRelayerIfEmpty = async () => {

const relayerWallets = walletInfo.relayerWallets

let gasInfo
let gasPrice

for (let i = 0; i < relayerWallets.length; i++) {
const relayerPublicKey = relayerWallets[i].publicKey
let balance = await ethWeb3.eth.getBalance(relayerPublicKey)
Expand All @@ -154,14 +141,11 @@ const fundEthRelayerIfEmpty = async () => {
if (!validBalance) {
const missingBalance = minimumBalance - balance
console.log(`${i + 1} - Funding ${relayerPublicKey} with ${missingBalance}, currently ${balance}/${minimumBalance}`)
gasInfo = await getGasPrice()
gasPrice = gasInfo.fastestGweiHex
await createAndSendTransaction(
walletInfo.funder, // Always send from the designated FUNDER
relayerPublicKey, // Public key of receiving account
missingBalance, // Min
ethWeb3,
gasPrice
ethWeb3
)
console.log(`${i + 1} - Finished Funding ${relayerPublicKey} with ${minimumBalance}`)
balance = await ethWeb3.eth.getBalance(relayerPublicKey)
Expand Down