Skip to content

Commit

Permalink
Adds calculation for priorityFee (#1753)
Browse files Browse the repository at this point in the history
* feat: priorityFee calculation for create DAO transactions

* fix: improves priority fee calculation for creation and signerPanel

* fix: eslint issues

* fix: add check after filtering the feeHistory

* fix: moves priorityFee estimation into helper file

* fix: make eslint happy
  • Loading branch information
mathewmeconry authored Jun 7, 2022
1 parent a49602c commit e9cfcd9
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/components/SignerPanel/SignerPanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Transition, animated } from 'react-spring'
import { useWallet } from '../../contexts/wallet'
import { ActivityContext } from '../../contexts/ActivityContext'
import { AppType, EthereumAddressType } from '../../prop-types'
import { addressesEqual } from '../../util/web3'
import { addressesEqual, getPriorityFeeEstimation } from '../../util/web3'
import ConfirmTransaction from './ConfirmTransaction'
import ConfirmMsgSign from './ConfirmMsgSign'
import SigningStatus from './SigningStatus'
Expand Down Expand Up @@ -44,6 +44,9 @@ const WEB3_TX_OBJECT_KEYS = new Set([
'gasPrice',
'data',
'nonce',
'maxPriorityFeePerGas',
'gasPrice',
'maxFeePerGas',
])

const getAppName = (apps, proxyAddress) => {
Expand Down Expand Up @@ -237,9 +240,13 @@ class SignerPanel extends React.PureComponent {

try {
if (pretransaction) {
pretransaction = await this.applyGasAndPriorityEstimation(
pretransaction
)
await this.signTransaction(pretransaction, intent, true)
}

transaction = await this.applyGasAndPriorityEstimation(transaction)
const transactionHash = await this.signTransaction(
transaction,
intent,
Expand Down Expand Up @@ -308,6 +315,16 @@ class SignerPanel extends React.PureComponent {
}
}

// adds maxPriorityFeePerGas, gasPrice and maxFeePerGas to the transaction if the RPC supports these
applyGasAndPriorityEstimation = async transaction => {
const { walletWeb3 } = this.props
const estimatedPriorityFee = await getPriorityFeeEstimation(walletWeb3)
return {
...transaction,
maxPriorityFeePerGas: estimatedPriorityFee,
}
}

render() {
const { account, apps, walletProviderId, walletWeb3 } = this.props

Expand Down
15 changes: 15 additions & 0 deletions src/onboarding/Create/Create.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { getIpfsGateway } from '../../local-settings'
import { web3Provider } from '../../Web3Provider'
import { trackEvent, events } from '../../analytics'
import { completeDomain } from '../../check-domain'
import { getPriorityFeeEstimation } from '../../util/web3'

const MAX_RETRY = 5

Expand Down Expand Up @@ -269,6 +270,7 @@ function useTemplateRepoInformation(templateRepoAddress, setError) {
function useDeploymentState(
account,
applyEstimateGas,
applyEstimatePriorityFee,
attempts,
status,
template,
Expand Down Expand Up @@ -341,6 +343,7 @@ function useDeploymentState(
}
try {
transaction = await applyEstimateGas(transaction)
transaction = await applyEstimatePriorityFee(transaction)
} catch (_) {}

if (!cancelled) {
Expand Down Expand Up @@ -509,6 +512,17 @@ const Create = React.memo(function Create({
[web3]
)

const applyEstimatePriorityFee = useCallback(
async transaction => {
const estimatedPriorityFee = await getPriorityFeeEstimation(web3)
return {
...transaction,
maxPriorityFeePerGas: estimatedPriorityFee,
}
},
[web3]
)

const [attempts, setAttempts] = useState(0)

const {
Expand All @@ -519,6 +533,7 @@ const Create = React.memo(function Create({
} = useDeploymentState(
account,
applyEstimateGas,
applyEstimatePriorityFee,
attempts,
status,
template,
Expand Down
23 changes: 23 additions & 0 deletions src/util/web3.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,29 @@ export function transformAddresses(str, callback) {
)
}

/**
* Calculates the current priority fee estimation
*
* @export
* @param {*} web3 The connected web3 instance
* @return {number | undefined} Returns the estimated priority fee or undefined
*/
export async function getPriorityFeeEstimation(web3) {
const priorityFeeHistory = await web3.eth.getFeeHistory('4', 'latest', [10])
if (priorityFeeHistory?.reward?.length > 0) {
// takes the top 10 of the last 4 blocks and take the average after removing zero values
const feeHistories = priorityFeeHistory.reward
.map(fee => web3.utils.hexToNumber(fee[0]))
.filter(fee => fee > 0)
if (feeHistories.length > 0) {
return Math.round(
feeHistories.reduce((acc, fee) => acc + fee, 0) / feeHistories.length
)
}
}
return undefined
}

// Re-export some utilities from web3-utils
export {
fromWei,
Expand Down

0 comments on commit e9cfcd9

Please sign in to comment.