Skip to content

Commit

Permalink
feat: compare addresses util
Browse files Browse the repository at this point in the history
  • Loading branch information
nicosantangelo committed May 27, 2021
1 parent 7b074d0 commit b87cbda
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
7 changes: 4 additions & 3 deletions src/modules/authorization/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { toBN } from 'web3x-es/utils'
import { isSameAddress } from '../wallet/utils'
import { Authorization, AuthorizationType } from './types'

export function getTokenAmountToApprove(): ReturnType<typeof toBN> {
Expand All @@ -19,9 +20,9 @@ export function hasAuthorization(
export function areEqual(left: Authorization, right: Authorization) {
return (
left.type === right.type &&
left.authorizedAddress === right.authorizedAddress &&
left.contractAddress === right.contractAddress &&
left.chainId === right.chainId
left.chainId === right.chainId &&
isSameAddress(left.authorizedAddress, right.authorizedAddress) &&
isSameAddress(left.contractAddress, right.contractAddress)
)
}

Expand Down
5 changes: 3 additions & 2 deletions src/modules/transaction/sagas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import {
import { isPending, buildActionRef } from './utils'
import { getTransaction as getTransactionFromChain } from './txUtils'
import { getAddress } from '../wallet/selectors'
import { isSameAddress } from '../wallet/utils'
import { getConnectedProvider } from '../../lib/eth'

export function* transactionSaga(): IterableIterator<ForkEffect> {
Expand Down Expand Up @@ -239,13 +240,13 @@ function* handleReplaceTransactionRequest(

// look for a replacement tx, if so break the loop
replacedBy = transactions.find(
tx => tx.nonce === nonce && tx.from.toString() === account
tx => tx.nonce === nonce && isSameAddress(tx.from, account)
)
if (replacedBy) break

// if no replacement is found, keep track of the highest nonce for the account
highestNonce = transactions
.filter(tx => tx.from.toString() === account)
.filter(tx => isSameAddress(tx.from, account))
.reduce((max, tx) => Math.max(max, tx.nonce), highestNonce)
}

Expand Down
11 changes: 6 additions & 5 deletions src/modules/transaction/selectors.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isSameAddress } from '../wallet/utils'
import { Transaction, TransactionStatus } from './types'
import { TransactionState } from './reducer'
import { isPending } from './utils'
Expand All @@ -23,28 +24,28 @@ export const getTransactionsByStatus = (
status: TransactionStatus
): Transaction[] =>
getData(state)
.filter(tx => tx.from === address && tx.status === status)
.filter(tx => isSameAddress(tx.from, address) && tx.status === status)
.sort(sortByTimestamp)

export const getTransactions = (state: any, address: string): Transaction[] =>
getData(state)
.filter(tx => tx.from === address)
.filter(tx => isSameAddress(tx.from, address))
.sort(sortByTimestamp)

export const getPendingTransactions = (
state: any,
address: string
): Transaction[] =>
getData(state)
.filter(tx => tx.from === address && isPending(tx.status))
.filter(tx => isSameAddress(tx.from, address) && isPending(tx.status))
.sort(sortByTimestamp)

export const getTransactionHistory = (
state: any,
address: string
): Transaction[] =>
getData(state)
.filter(tx => tx.from === address && !isPending(tx.status))
.filter(tx => isSameAddress(tx.from, address) && !isPending(tx.status))
.sort(sortByTimestamp)

export const getTransactionsByType = (
Expand All @@ -53,5 +54,5 @@ export const getTransactionsByType = (
type: string
): Transaction[] =>
getData(state)
.filter(tx => tx.from === address && tx.actionType === type)
.filter(tx => isSameAddress(tx.from, address) && tx.actionType === type)
.sort(sortByTimestamp)
7 changes: 7 additions & 0 deletions src/modules/wallet/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,10 @@ export async function buildWallet(): Promise<Wallet> {
chainId
}
}

export function isSameAddress(
address1: Address | string,
address2: Address | string
) {
return address1.toString().toLowerCase() === address2.toString().toLowerCase()
}

0 comments on commit b87cbda

Please sign in to comment.