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

Adds calculation for priorityFee #1753

Merged
merged 6 commits into from
Jun 7, 2022
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
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(
githubdoramon marked this conversation as resolved.
Show resolved Hide resolved
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