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

fix: error handle #1460

Merged
merged 2 commits into from
Dec 9, 2024
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: 19 additions & 0 deletions packages/db/src/main/cfxjs/db/queries.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2126,6 +2126,24 @@
(assoc :app (and app-id (e :app app-id)))
(assoc :token (and token-id (e :token token-id))))))

(defn get-tx-with-same-nonce [{:keys [hash]}]
(let [txs
(q '[:find [?txs ...]
:in $ ?tx
:where
[?address :address/tx ?tx]
[?tx :tx/txPayload ?payload]
[?payload :txPayload/nonce ?nonce]
[?address :address/tx ?txs]
[?txs :tx/txPayload ?tx-payload]
[?tx-payload :txPayload/nonce ?nonce]
[?address :address/value ?addrv]
[?tx-payload :txPayload/from ?addrv]
[?txs :tx/hash ?hash]]
[:tx/hash hash])]
(when txs
(mapv tx-id->data txs))))

(defn get-tx [{:keys [hash]}]
(let [tx (q '[:find ?tx .
:in $ ?tx
Expand Down Expand Up @@ -2315,6 +2333,7 @@
:upsertMemo upsert-memo
:queryNonceGapTxs query-nonce-gap-txs
:insertExternalTx insert-external-tx
:queryTxWithSameNonce get-tx-with-same-nonce

:queryqueryRecentInterestingAddress get-recent-interesting-address-from-tx
:queryqueryApp get-apps
Expand Down
53 changes: 39 additions & 14 deletions packages/rpcs/wallet_handleUnfinishedCFXTx/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ import {identity} from '@fluent-wallet/compose'

export const NAME = 'wallet_handleUnfinishedCFXTx'

function getGasPrice(tx) {
const payload = tx.txPayload
return payload.type === '0x2' ? payload.maxFeePerGas : payload.gasPrice
}

function defs(...args) {
const s = stream({
id: args.length === 2 ? args[0] : undefined,
Expand Down Expand Up @@ -75,6 +80,7 @@ export const permissions = {
'setTxConfirmed',
'setTxUnsent',
'setTxChainSwitched',
'queryTxWithSameNonce',
],
}

Expand Down Expand Up @@ -102,6 +108,7 @@ export const main = ({
setTxConfirmed,
setTxUnsent,
setTxChainSwitched,
queryTxWithSameNonce,
},
params: {tx, address, okCb, failedCb},
network,
Expand Down Expand Up @@ -207,16 +214,33 @@ export const main = ({
// failed to send
setTxUnsent({hash})

const {errorType, shouldDiscard} = processError(err)
let {errorType, shouldDiscard} = processError(err)
const isDuplicateTx = errorType === 'duplicateTx'
const resendNonceTooStale =
tx.resendAt && errorType === 'tooStaleNonce'

const sameAsSuccess = isDuplicateTx || resendNonceTooStale
const resendPriceTooLow =
tx.resendAt && errorType === 'replacedWithHigherGasPriceTx'
if (resendPriceTooLow) errorType = 'replacedByAnotherTx'
const sameNonceTxs = queryTxWithSameNonce({hash}) || []
let latestTx = sameNonceTxs.sort((a, b) =>
BigNumber.from(getGasPrice(a)).sub(getGasPrice(b)).toNumber(),
)[0]
if (latestTx?.hash === hash) latestTx = null
const sameAsSuccess =
isDuplicateTx ||
resendNonceTooStale ||
(resendPriceTooLow &&
latestTx &&
latestTx.status >= 0 &&
latestTx.status < 5)
const failed = !sameAsSuccess && shouldDiscard

defs({
failed: failed && {errorType, err},
failed: failed && {
errorType,
err,
disableNotification: !!latestTx,
},
sameAsSuccess,
resend: !shouldDiscard && !sameAsSuccess,
})
Expand All @@ -227,17 +251,18 @@ export const main = ({
({err}) =>
typeof failedCb === 'function' && failedCb(err),
),
sideEffect(({errorType}) => {
sideEffect(({errorType, disableNotification}) => {
if (setTxFailed({hash, error: errorType})) {
getExt().then(ext =>
ext.notifications.create(hash, {
title: 'Failed transaction',
message: `Transaction ${parseInt(
tx.txPayload.nonce,
16,
)} failed! ${err?.data || err?.message || ''}`,
}),
)
!disableNotification &&
getExt().then(ext =>
ext.notifications.create(hash, {
title: 'Failed transaction',
message: `Transaction ${parseInt(
tx.txPayload.nonce,
16,
)} failed! ${err?.data || err?.message || ''}`,
}),
)
}
}),
sideEffect(() => {
Expand Down
47 changes: 35 additions & 12 deletions packages/rpcs/wallet_handleUnfinishedETHTx/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ import {identity} from '@fluent-wallet/compose'

export const NAME = 'wallet_handleUnfinishedETHTx'

function getGasPrice(tx) {
const payload = tx.txPayload
return payload.type === '0x2' ? payload.maxFeePerGas : payload.gasPrice
}

function defs(...args) {
const s = stream({
id: args.length === 2 ? args[0] : undefined,
Expand Down Expand Up @@ -75,6 +80,7 @@ export const permissions = {
'setTxConfirmed',
'setTxUnsent',
'setTxChainSwitched',
'queryTxWithSameNonce',
],
}

Expand Down Expand Up @@ -102,6 +108,7 @@ export const main = ({
setTxConfirmed,
setTxUnsent,
setTxChainSwitched,
queryTxWithSameNonce,
},
params: {tx, address, okCb, failedCb},
network,
Expand Down Expand Up @@ -216,13 +223,28 @@ export const main = ({
const resendPriceTooLow =
tx.resendAt && errorType === 'replaceUnderpriced'
if (resendPriceTooLow) errorType = 'replacedByAnotherTx'
const sameNonceTxs = queryTxWithSameNonce({hash}) || []
let latestTx = sameNonceTxs.sort((a, b) =>
BigNumber.from(getGasPrice(a)).sub(getGasPrice(b)).toNumber(),
)[0]
if (latestTx?.hash === hash) latestTx = null
const sameAsSuccess =
isDuplicateTx ||
resendNonceTooStale ||
(resendPriceTooLow &&
latestTx &&
latestTx.status >= 0 &&
latestTx.status < 5)

const sameAsSuccess = isDuplicateTx || resendNonceTooStale
const failed =
!sameAsSuccess && (shouldDiscard || resendPriceTooLow)

defs({
failed: failed && {errorType, err},
failed: failed && {
errorType,
err,
disableNotification: !!latestTx,
},
sameAsSuccess,
resend: !shouldDiscard && !sameAsSuccess,
})
Expand All @@ -233,17 +255,18 @@ export const main = ({
({err}) =>
typeof failedCb === 'function' && failedCb(err),
),
sideEffect(({errorType}) => {
sideEffect(({errorType, disableNotification}) => {
if (setTxFailed({hash, error: errorType})) {
getExt().then(ext =>
ext.notifications.create(hash, {
title: 'Failed transaction',
message: `Transaction ${parseInt(
tx.txPayload.nonce,
16,
)} failed! ${err?.data || err?.message || ''}`,
}),
)
!disableNotification &&
getExt().then(ext =>
ext.notifications.create(hash, {
title: 'Failed transaction',
message: `Transaction ${parseInt(
tx.txPayload.nonce,
16,
)} failed! ${err?.data || err?.message || ''}`,
}),
)
}
}),
sideEffect(() => {
Expand Down
Loading