Skip to content

Commit

Permalink
fix: add 1559 tx speedup/cancelation (#1452)
Browse files Browse the repository at this point in the history
* fix: espace support 1559

* chore: update version

* fix: add 1559 tx sppedup/cancel

* chore: update version

* chore: enhance comment

* feat: when speedup or cancel tx use maxFeePerGas = maxPriorityPerGas
  • Loading branch information
AngeliaQ authored Dec 5, 2024
1 parent ab35f5e commit baa71cf
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 23 deletions.
3 changes: 3 additions & 0 deletions .yarn/versions/aa00b5c3.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declined:
- helios
- helios-popup
15 changes: 13 additions & 2 deletions packages/popup/src/pages/EditGasFee/components/GasStation.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ function GasStation({
suggestedGasPrice,
isCfxChain,
estimateGasLimit,
resendType,
}) {
const {t} = useTranslation()
const history = useHistory()
Expand All @@ -127,6 +128,7 @@ function GasStation({
<span>{isTxTreatedAsEIP1559 ? t('maxFeePerGas') : t('gasPrice')}</span>
</div>
{isTxTreatedAsEIP1559 &&
!resendType &&
gasArray.map((level, index) => (
<GasStationItem
key={index}
Expand All @@ -148,7 +150,7 @@ function GasStation({
}}
/>
))}
{!isTxTreatedAsEIP1559 && (
{(!isTxTreatedAsEIP1559 || !!resendType) && (
<GasStationItem
level="suggested"
data={{
Expand Down Expand Up @@ -203,7 +205,15 @@ function GasStation({
pathname: ADVANCED_GAS,
search: `?isHistoryTx=${isHistoryTx}&${
isTxTreatedAsEIP1559
? `suggestedMaxFeePerGas=${suggestedMaxFeePerGas}&suggestedMaxPriorityFeePerGas=${suggestedMaxPriorityFeePerGas}&selectedGasLevel=${selectedGasLevel}`
? `suggestedMaxFeePerGas=${
!resendType
? suggestedMaxFeePerGas
: convertDataToValue(suggestedGasPrice, GWEI_DECIMALS)
}&suggestedMaxPriorityFeePerGas=${
!resendType
? suggestedMaxPriorityFeePerGas
: convertDataToValue(suggestedGasPrice, GWEI_DECIMALS)
}&selectedGasLevel=${selectedGasLevel}`
: ''
}${
!isTxTreatedAsEIP1559
Expand All @@ -227,6 +237,7 @@ GasStation.propTypes = {
suggestedGasPrice: PropTypes.string,
isCfxChain: PropTypes.bool,
estimateGasLimit: PropTypes.string,
resendType: PropTypes.string,
}

export default GasStation
33 changes: 20 additions & 13 deletions packages/popup/src/pages/EditGasFee/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,19 +131,25 @@ function EditGasFee({
gas: formatDecimalToHex(gasLimit) || estimateGasLimit,
nonce: formatDecimalToHex(nonce),
storageLimit: formatDecimalToHex(storageLimit),
maxFeePerGas: suggestedMaxFeePerGas
? convertValueToData(
new Big(suggestedMaxFeePerGas).round(9).toString(10),
GWEI_DECIMALS,
)
: '',
maxPriorityFeePerGas: suggestedMaxPriorityFeePerGas
? convertValueToData(
new Big(suggestedMaxPriorityFeePerGas).round(9).toString(10),
GWEI_DECIMALS,
)
: '',
gasPrice: suggestedGasPrice,
maxFeePerGas: !resendType
? suggestedMaxFeePerGas
? convertValueToData(
new Big(suggestedMaxFeePerGas).round(9).toString(10),
GWEI_DECIMALS,
)
: ''
: // 1559 tx resend maxFeePerGas use suggest gas price
suggestedGasPrice,
maxPriorityFeePerGas: !resendType
? suggestedMaxPriorityFeePerGas
? convertValueToData(
new Big(suggestedMaxPriorityFeePerGas).round(9).toString(10),
GWEI_DECIMALS,
)
: ''
: // 1559 tx resend maxPriorityFeePerGas use suggest gas price
suggestedGasPrice,
gasPrice: !isTxTreatedAsEIP1559 ? suggestedGasPrice : '',
}
}
if (!sendParams.maxFeePerGas) delete sendParams.maxFeePerGas
Expand Down Expand Up @@ -236,6 +242,7 @@ function EditGasFee({
isTxTreatedAsEIP1559={isTxTreatedAsEIP1559}
isHistoryTx={!isSendTx}
gasInfoEip1559={gasInfoEip1559}
resendType={resendType}
suggestedGasPrice={suggestedGasPrice}
selectedGasLevel={selectedGasLevel}
setSelectedGasLevel={setSelectedGasLevel}
Expand Down
38 changes: 30 additions & 8 deletions packages/popup/src/pages/ResendTransaction/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import {useState, useEffect} from 'react'
import {useState, useEffect, useMemo} from 'react'
import {useTranslation} from 'react-i18next'
import {useHistory} from 'react-router-dom'
import {
Big,
formatDecimalToHex,
formatHexToDecimal,
convertDecimal,
GWEI_DECIMALS,
} from '@fluent-wallet/data-format'
import EditGasFee from '../EditGasFee'
import {
Expand Down Expand Up @@ -85,13 +87,16 @@ function ResendTransaction() {
to,
nonce,
value,
gasPrice: lastGasPrice,
gasPrice,
maxFeePerGas,
type: eipVersionType,
} = txPayload
const reSendTxStatus = formatStatus(status)

const isTxTreatedAsEIP1559 = useIsTxTreatedAsEIP1559(eipVersionType)

const lastGasPrice = isTxTreatedAsEIP1559 ? maxFeePerGas : gasPrice

// decode erc20 data
const {decodeData} = useDecodeData(
resendType === 'speedup'
Expand Down Expand Up @@ -142,6 +147,25 @@ function ResendTransaction() {
const originEstimateRst =
useEstimateTx({...originParams}, token20Params) || {}

const {
gasPrice: estimateGasPrice,
gasInfoEip1559 = {},
loading,
} = originEstimateRst

const originEstimateGasPrice = useMemo(() => {
if (loading || Object.keys(originEstimateRst).length === 0) return null
return !isTxTreatedAsEIP1559
? estimateGasPrice
: convertDecimal(
new Big(gasInfoEip1559?.['medium']?.suggestedMaxFeePerGas)
.round(9)
.toString(10),
'multiply',
GWEI_DECIMALS,
)
}, [isTxTreatedAsEIP1559, estimateGasPrice, gasInfoEip1559, loading])

const resendTransaction = async params => {
try {
await request(WALLET_SEND_TRANSACTION_WITH_ACTION, {
Expand All @@ -168,7 +192,7 @@ function ResendTransaction() {
}

const onResend = async feeParams => {
if (originEstimateRst?.loading || !accountType) {
if (loading || !accountType) {
return
}

Expand Down Expand Up @@ -220,11 +244,9 @@ function ResendTransaction() {

// set default gas price (legacy tx)
useEffect(() => {
if (lastGasPrice && originEstimateRst?.gasPrice) {
if (lastGasPrice && originEstimateGasPrice) {
const decimalGasPrice = formatHexToDecimal(lastGasPrice)
const decimalEstimateGasPrice = formatHexToDecimal(
originEstimateRst.gasPrice,
)
const decimalEstimateGasPrice = formatHexToDecimal(originEstimateGasPrice)

const biggerGasPrice = new Big(decimalGasPrice).times(1.1)

Expand All @@ -238,7 +260,7 @@ function ResendTransaction() {

setSuggestedGasPrice(formatDecimalToHex(recommendGasPrice))
}
}, [originEstimateRst.gasPrice, lastGasPrice])
}, [originEstimateGasPrice, lastGasPrice])

//cancel resend tx when tx status is not pending
useEffect(() => {
Expand Down

0 comments on commit baa71cf

Please sign in to comment.