Skip to content
This repository has been archived by the owner on Nov 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #460 from hermeznetwork/add-timeout-to-pending-dep…
Browse files Browse the repository at this point in the history
…osit-txs

Remove pending deposits after 24h without being forged or if the max gas for the tx exceeds the account balance
  • Loading branch information
elias-garcia authored Apr 30, 2021
2 parents e09ddb5 + b2e9823 commit 57e8a2d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,5 @@ export const REPORT_ISSUE_FORM_URL = 'https://docs.google.com/forms/d/e/1FAIpQLS
export const HERMEZ_HELP_CENTER_URL = 'https://docs.hermez.io/#/faq/end-users'

export const HERMEZ_WEB_URL = 'https://hermez.io'

export const DEPOSIT_TX_TIMEOUT = 86400000 // 24h
21 changes: 15 additions & 6 deletions src/store/global/global.thunks.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import hermezjs, { CoordinatorAPI, Providers, Tx, TxUtils, HermezCompressedAmount } from '@hermeznetwork/hermezjs'
import hermezjs, { CoordinatorAPI, Providers, Tx, TxUtils, HermezCompressedAmount, Addresses } from '@hermeznetwork/hermezjs'
import { push } from 'connected-react-router'
import { ethers } from 'ethers'
import HermezABI from '@hermeznetwork/hermezjs/src/abis/HermezABI'
Expand Down Expand Up @@ -343,21 +343,30 @@ function updatePendingDepositId (transactionHash, transactionId) {
}

function checkPendingDeposits () {
return (dispatch, getState) => {
return async (dispatch, getState) => {
const { global: { wallet, pendingDeposits, ethereumNetworkTask } } = getState()

dispatch(globalActions.checkPendingDeposits())

const provider = Providers.getProvider()
const accountEthBalance = BigInt(await provider.getBalance(Addresses.getEthereumAddress(wallet.hermezEthereumAddress)))
const accountPendingDeposits = storage.getItemsByHermezAddress(
pendingDeposits,
ethereumNetworkTask.data.chainId,
wallet.hermezEthereumAddress
)
const pendingDepositsHashes = accountPendingDeposits.map(deposit => deposit.hash)
const pendingDepositsTxs = pendingDepositsHashes.map((hash) => {
return provider.getTransaction(hash).then((tx) => {
const pendingDepositsTxs = accountPendingDeposits.map((pendingDeposit) => {
return provider.getTransaction(pendingDeposit.hash).then((tx) => {
if (tx === null) {
dispatch(removePendingDepositByHash(hash))
dispatch(removePendingDepositByHash(pendingDeposit.hash))
}

if (tx !== null && tx.blockNumber === null) {
const maxTxFee = BigInt(tx.gasLimit) * BigInt(tx.gasPrice)

if (Date.now() > new Date(pendingDeposit.timestamp).getTime() + constants.DEPOSIT_TX_TIMEOUT || maxTxFee > accountEthBalance) {
dispatch(removePendingDepositByHash(pendingDeposit.hash))
}
}

return tx
Expand Down

0 comments on commit 57e8a2d

Please sign in to comment.