feat: eip-1559

This commit is contained in:
Danil Kovtonyuk 2021-10-28 01:41:14 +10:00
parent 71bfdfbfec
commit 58c65af18e
No known key found for this signature in database
GPG Key ID: E72A919BF08C3746
9 changed files with 802 additions and 933 deletions

View File

@ -37,6 +37,7 @@ export default {
} }
this.fetchDeploymentStatus() this.fetchDeploymentStatus()
this.statusPooling() this.statusPooling()
this.fetchGasParams()
this.setAirdropAddresses() this.setAirdropAddresses()
}, },
@ -44,6 +45,7 @@ export default {
...mapActions('provider', ['initProvider']), ...mapActions('provider', ['initProvider']),
...mapActions('airdrop', ['setAirdropAddresses']), ...mapActions('airdrop', ['setAirdropAddresses']),
...mapActions('steps', ['statusPooling', 'fetchDeploymentStatus']), ...mapActions('steps', ['statusPooling', 'fetchDeploymentStatus']),
...mapActions('gasPrice', ['fetchGasParams']),
}, },
} }
</script> </script>

View File

@ -17,6 +17,7 @@ const networkConfig = {
}, },
pollInterval: 60, pollInterval: 60,
isL1: true, isL1: true,
isEip1559Supported: true,
}, },
netId100: { netId100: {
@ -41,6 +42,7 @@ const networkConfig = {
}, },
pollInterval: 200, pollInterval: 200,
isL1: false, isL1: false,
isEip1559Supported: false,
}, },
} }

View File

@ -6,22 +6,23 @@
"dev": "nuxt", "dev": "nuxt",
"build": "nuxt build", "build": "nuxt build",
"start": "nuxt start", "start": "nuxt start",
"generate": "nuxt generate", "generate": "nuxt generate && cp dist/404.html dist/ipfs-404.html",
"lint:js": "eslint --ext .js,.vue --ignore-path .gitignore .", "lint:js": "eslint --ext .js,.vue --ignore-path .gitignore .",
"lint": "yarn lint:js", "lint": "yarn lint:js",
"deploy-prod": "npm run generate && push-dir --allow-unclean --dir=dist --branch=gh-pages --cleanup --remote=temp" "deploy-prod": "npm run generate && push-dir --allow-unclean --dir=dist --branch=gh-pages --cleanup --remote=temp"
}, },
"dependencies": { "dependencies": {
"nuxt-web3-provider": "^0.1.1", "@mycrypto/gas-estimation": "^1.1.0",
"core-js": "^3.6.5", "core-js": "^3.6.5",
"gas-price-oracle": "^0.3.4", "gas-price-oracle": "^0.4.0",
"node-sass": "^4.14.1", "node-sass": "^4.14.1",
"nuxt": "^2.14.6", "nuxt": "^2.14.6",
"nuxt-buefy": "^0.4.4", "nuxt-buefy": "^0.4.4",
"nuxt-web3-provider": "^0.1.4",
"push-dir": "^0.4.1", "push-dir": "^0.4.1",
"sass-loader": "^10.0.3", "sass-loader": "^10.0.3",
"vue-i18n": "^8.22.1", "vue-i18n": "^8.22.1",
"web3": "1.2.6" "web3": "1.5.2"
}, },
"devDependencies": { "devDependencies": {
"@nuxtjs/eslint-config": "^3.1.0", "@nuxtjs/eslint-config": "^3.1.0",

File diff suppressed because one or more lines are too long

View File

@ -4,13 +4,19 @@ import { hexToNumber, numberToHex } from 'web3-utils'
import deployerABI from '../abi/deployer.abi.json' import deployerABI from '../abi/deployer.abi.json'
import deploymentActions from '../static/deploymentActions.json' import deploymentActions from '../static/deploymentActions.json'
const deployerContracts = [
'deployer.contract.tornadocash.eth',
'deployerL1.contract.tornadocash.eth',
'deployerL2.contract.tornadocash.eth',
]
const state = () => { const state = () => {
return {} return {}
} }
const getters = { const getters = {
deployerContract: (state, getters, rootState, rootGetters) => (isProxy) => { deployerContract: (state, getters, rootState, rootGetters) => (isProxy) => {
const web3 = new Web3(rootGetters['provider/getNetwork'].rpcUrls.Infura.url) const web3 = new Web3(rootGetters['provider/getRpc'])
return new web3.eth.Contract( return new web3.eth.Contract(
deployerABI, deployerABI,
isProxy isProxy
@ -29,10 +35,7 @@ const actions = {
) { ) {
try { try {
dispatch('loading/enable', {}, { root: true }) dispatch('loading/enable', {}, { root: true })
const isProxy = action.domain === 'deployer.contract.tornadocash.eth' const web3 = this.$provider.getWeb3(rootGetters['provider/getRpc'])
const ethAccount = rootGetters['provider/getAccount']
const web3 = rootGetters['provider/getWeb3']
const code = await web3.eth.getCode(action.expectedAddress) const code = await web3.eth.getCode(action.expectedAddress)
console.log('code', code) console.log('code', code)
if (code !== '0x') { if (code !== '0x') {
@ -49,52 +52,35 @@ const actions = {
throw new Error('Already deployed') throw new Error('Already deployed')
} }
const gasPrice = rootGetters['gasPrice/fastGasPrice'] const ethAccount = rootGetters['provider/getAccount']
const txGasParams = rootGetters['gasPrice/txGasParams']
const data = getters console.log(txGasParams)
.deployerContract(isProxy)
.methods.deploy(action.bytecode, deploymentActions.salt) const isProxy = deployerContracts.includes(action.domain)
const deployerContract = getters.deployerContract(isProxy)
const data = deployerContract.methods
.deploy(action.bytecode, deploymentActions.salt)
.encodeABI() .encodeABI()
const callParamsEstimate = { const params = {
method: 'eth_estimateGas',
params: [
{
from: ethAccount,
to: getters.deployerContract(isProxy)._address,
// gas: numberToHex(6e6),
gasPrice,
value: `0x0`,
data,
},
],
from: ethAccount, from: ethAccount,
to: deployerContract._address,
...txGasParams,
value: '0x0',
data,
} }
const deployerContracts = [ const gasEstimate = isProxy
'deployerL1.contract.tornadocash.eth', ? numberToHex(363636)
'deployerL2.contract.tornadocash.eth', : await dispatch(
] 'provider/sendRequest',
{ method: 'eth_estimateGas', params: [params] },
{ root: true }
)
const gasEstimate = deployerContracts.includes(action.domain)
? numberToHex(1e6)
: await dispatch('provider/sendRequest', callParamsEstimate, {
root: true,
})
const gasWithBuffer = Math.ceil(hexToNumber(gasEstimate) * 1.1) const gasWithBuffer = Math.ceil(hexToNumber(gasEstimate) * 1.1)
const callParams = {
method: 'eth_sendTransaction',
params: [
{
from: ethAccount,
to: getters.deployerContract(isProxy)._address,
gas: numberToHex(gasWithBuffer),
gasPrice,
value: 0,
data,
},
],
from: ethAccount,
}
dispatch( dispatch(
'loading/changeText', 'loading/changeText',
{ {
@ -104,9 +90,21 @@ const actions = {
}, },
{ root: true } { root: true }
) )
const txHash = await dispatch('provider/sendRequest', callParams, { const txHash = await dispatch(
root: true, 'provider/sendRequest',
}) {
method: 'eth_sendTransaction',
params: [
{
...params,
gas: numberToHex(gasWithBuffer),
},
],
},
{
root: true,
}
)
console.log('txHash', txHash) console.log('txHash', txHash)
dispatch('loading/disable', {}, { root: true }) dispatch('loading/disable', {}, { root: true })
dispatch( dispatch(

View File

@ -1,58 +1,89 @@
/* eslint-disable no-console */ /* eslint-disable no-console */
import { GasPriceOracle } from 'gas-price-oracle'
import networkConfig from '@/networkConfig' import networkConfig from '@/networkConfig'
import { GasPriceOracle } from 'gas-price-oracle'
import { estimateFees } from '@mycrypto/gas-estimation'
const { toHex, toWei } = require('web3-utils') const { toHex, toWei } = require('web3-utils')
const GAS_PRICES = networkConfig.netId100.gasPrices
export const state = () => { export const state = () => {
return { return {
...GAS_PRICES, params: {
custom: null, 1: {
maxFeePerGas: '0x25FF7A6000',
maxPriorityFeePerGas: '0x77359400',
},
},
prices: {
1: Object.assign(networkConfig.netId1.gasPrices),
100: Object.assign(networkConfig.netId100.gasPrices),
},
} }
} }
export const getters = { export const getters = {
oracle: (state, getters, rootState, rootGetters) => { oracle: (state, getters, rootState, rootGetters) => {
const currentRpc = rootGetters['provider/getNetwork'].rpcUrls.Infura.url const { id: chainId, gasPrices } = rootGetters['provider/getNetwork']
const currentRpc = rootGetters['provider/getRpc']
console.log('currentRpc', currentRpc) console.log('currentRpc', currentRpc)
return new GasPriceOracle({ return new GasPriceOracle({
chainId: 100, chainId,
defaultRpc: currentRpc, defaultRpc: currentRpc,
defaultFallbackGasPrices: GAS_PRICES, defaultFallbackGasPrices: gasPrices,
}) })
}, },
fastGasPrice: (state) => { gasParams: (state) => (chainId) => {
return toHex(toWei(state.fast.toString(), 'gwei')) return state.params[chainId]
}, },
lowGasPrice: (state) => { gasPrice: (state) => (chainId) => {
return toHex(toWei(state.standard.toString(), 'gwei')) const currentGas = state.prices[chainId]
return toHex(toWei(currentGas.fast.toString(), 'gwei'))
},
txGasParams: (state, getters, rootState, rootGetters) => {
const { id: chainId, isEip1559Supported } = rootGetters[
'provider/getNetwork'
]
if (isEip1559Supported) {
return getters.gasParams(chainId)
}
return { gasPrice: getters.gasPrice(chainId) }
}, },
} }
export const mutations = { export const mutations = {
SAVE_GAS_PRICES(state, { instant, fast, standard, low }) { SAVE_GAS_PRICES(state, { chainId, ...gas }) {
this._vm.$set(state, 'instant', instant) this._vm.$set(state.prices, chainId, gas)
this._vm.$set(state, 'fast', fast)
this._vm.$set(state, 'standard', standard)
this._vm.$set(state, 'low', low)
}, },
SAVE_CUSTOM_GAS_PRICE(state, { custom }) { SET_GAS_PARAMS(state, { chainId, ...params }) {
this._vm.$set(state, 'custom', custom) this._vm.$set(state.params, chainId, params)
}, },
} }
export const actions = { export const actions = {
async fetchGasPrice({ getters, commit, dispatch, rootGetters, state }) { async fetchGasParams({ getters, commit, dispatch, rootGetters, state }) {
const { pollInterval } = rootGetters['provider/getNetwork'] const { pollInterval, id: chainId, isEip1559Supported } = rootGetters[
'provider/getNetwork'
]
const rpcUrl = rootGetters['provider/getRpc']
try { try {
const gas = await getters.oracle.gasPrices(state) if (isEip1559Supported) {
commit('SAVE_GAS_PRICES', gas) const web3 = this.$provider.getWeb3(rpcUrl)
console.log(`Got fast gas price ${state.fast}`) const { maxFeePerGas, maxPriorityFeePerGas } = await estimateFees(web3)
setTimeout(() => dispatch('fetchGasPrice'), 1000 * pollInterval)
commit('SET_GAS_PARAMS', {
maxFeePerGas: toHex(maxFeePerGas.toString()),
maxPriorityFeePerGas: toHex(maxPriorityFeePerGas.toString()),
chainId,
})
} else {
const gas = await getters.oracle.gasPrices()
commit('SAVE_GAS_PRICES', { chainId, ...gas })
console.log(`Got fast gas price ${gas.fast}`)
}
setTimeout(() => dispatch('fetchGasParams'), 1000 * pollInterval)
} catch (e) { } catch (e) {
console.error('fetchGasPrice', e) console.error('fetchGasParams', e)
setTimeout(() => dispatch('fetchGasPrice'), 1000 * pollInterval) setTimeout(() => dispatch('fetchGasParams'), 1000 * pollInterval)
} }
}, },
} }

View File

@ -13,7 +13,7 @@ import {
export default { export default {
async initProvider({ commit, state, getters, dispatch }, { name, network }) { async initProvider({ commit, state, getters, dispatch }, { name, network }) {
try { try {
const account = await this.$provider.initProvider(getters.getProvider) const account = await this.$provider.initProvider(getters.getProvider, {})
const supportedNetworks = [numberToHex(1), numberToHex(100)] const supportedNetworks = [numberToHex(1), numberToHex(100)]
if (!supportedNetworks.includes(window.ethereum.chainId)) { if (!supportedNetworks.includes(window.ethereum.chainId)) {
await dispatch( await dispatch(

View File

@ -1,5 +1,3 @@
import Web3 from 'web3'
import networkConfig from '@/networkConfig' import networkConfig from '@/networkConfig'
export default { export default {
@ -21,11 +19,6 @@ export default {
getProviderName: ({ provider }) => { getProviderName: ({ provider }) => {
return provider.name return provider.name
}, },
getWeb3: (state, getters) => {
const provider = getters.getProvider
return Object.freeze(new Web3(provider))
},
getBalance: (state) => { getBalance: (state) => {
return state.balance return state.balance
}, },
@ -36,4 +29,7 @@ export default {
getAccount: (state) => { getAccount: (state) => {
return state.account return state.account
}, },
getRpc: (state, getters) => {
return getters.getNetwork.rpcUrls.Infura.url
},
} }

1495
yarn.lock

File diff suppressed because it is too large Load Diff