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

View File

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

View File

@ -6,22 +6,23 @@
"dev": "nuxt",
"build": "nuxt build",
"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": "yarn lint:js",
"deploy-prod": "npm run generate && push-dir --allow-unclean --dir=dist --branch=gh-pages --cleanup --remote=temp"
},
"dependencies": {
"nuxt-web3-provider": "^0.1.1",
"@mycrypto/gas-estimation": "^1.1.0",
"core-js": "^3.6.5",
"gas-price-oracle": "^0.3.4",
"gas-price-oracle": "^0.4.0",
"node-sass": "^4.14.1",
"nuxt": "^2.14.6",
"nuxt-buefy": "^0.4.4",
"nuxt-web3-provider": "^0.1.4",
"push-dir": "^0.4.1",
"sass-loader": "^10.0.3",
"vue-i18n": "^8.22.1",
"web3": "1.2.6"
"web3": "1.5.2"
},
"devDependencies": {
"@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 deploymentActions from '../static/deploymentActions.json'
const deployerContracts = [
'deployer.contract.tornadocash.eth',
'deployerL1.contract.tornadocash.eth',
'deployerL2.contract.tornadocash.eth',
]
const state = () => {
return {}
}
const getters = {
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(
deployerABI,
isProxy
@ -29,10 +35,7 @@ const actions = {
) {
try {
dispatch('loading/enable', {}, { root: true })
const isProxy = action.domain === 'deployer.contract.tornadocash.eth'
const ethAccount = rootGetters['provider/getAccount']
const web3 = rootGetters['provider/getWeb3']
const web3 = this.$provider.getWeb3(rootGetters['provider/getRpc'])
const code = await web3.eth.getCode(action.expectedAddress)
console.log('code', code)
if (code !== '0x') {
@ -49,52 +52,35 @@ const actions = {
throw new Error('Already deployed')
}
const gasPrice = rootGetters['gasPrice/fastGasPrice']
const ethAccount = rootGetters['provider/getAccount']
const txGasParams = rootGetters['gasPrice/txGasParams']
const data = getters
.deployerContract(isProxy)
.methods.deploy(action.bytecode, deploymentActions.salt)
console.log(txGasParams)
const isProxy = deployerContracts.includes(action.domain)
const deployerContract = getters.deployerContract(isProxy)
const data = deployerContract.methods
.deploy(action.bytecode, deploymentActions.salt)
.encodeABI()
const callParamsEstimate = {
method: 'eth_estimateGas',
params: [
{
from: ethAccount,
to: getters.deployerContract(isProxy)._address,
// gas: numberToHex(6e6),
gasPrice,
value: `0x0`,
data,
},
],
const params = {
from: ethAccount,
to: deployerContract._address,
...txGasParams,
value: '0x0',
data,
}
const deployerContracts = [
'deployerL1.contract.tornadocash.eth',
'deployerL2.contract.tornadocash.eth',
]
const gasEstimate = isProxy
? numberToHex(363636)
: 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 callParams = {
method: 'eth_sendTransaction',
params: [
{
from: ethAccount,
to: getters.deployerContract(isProxy)._address,
gas: numberToHex(gasWithBuffer),
gasPrice,
value: 0,
data,
},
],
from: ethAccount,
}
dispatch(
'loading/changeText',
{
@ -104,9 +90,21 @@ const actions = {
},
{ root: true }
)
const txHash = await dispatch('provider/sendRequest', callParams, {
root: true,
})
const txHash = await dispatch(
'provider/sendRequest',
{
method: 'eth_sendTransaction',
params: [
{
...params,
gas: numberToHex(gasWithBuffer),
},
],
},
{
root: true,
}
)
console.log('txHash', txHash)
dispatch('loading/disable', {}, { root: true })
dispatch(

View File

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

View File

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

View File

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