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

feat: lowercase eth addresses #147

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion src/modules/authorization/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const INITIAL_STATE = {
error: null
}

type AuthorizationReducerAction =
export type AuthorizationReducerAction =
| FetchAuthorizationsRequestAction
| FetchAuthorizationsSuccessAction
| FetchAuthorizationsFailureAction
Expand Down
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
73 changes: 73 additions & 0 deletions src/modules/common/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { RootMiddleware } from '../../types'
import {
FETCH_AUTHORIZATIONS_SUCCESS,
GRANT_TOKEN_SUCCESS,
REVOKE_TOKEN_SUCCESS
} from '../authorization/actions'
import { AuthorizationReducerAction } from '../authorization/reducer'
import { CHANGE_PROFILE, LOAD_PROFILE_SUCCESS } from '../profile/actions'
import { ProfileReducerAction } from '../profile/reducer'
import {
CLEAR_TRANSACTIONS,
FETCH_TRANSACTION_REQUEST,
FETCH_TRANSACTION_SUCCESS
} from '../transaction/actions'
import { TransactionReducerAction } from '../transaction/reducer'
import {
CHANGE_ACCOUNT,
CHANGE_NETWORK,
CONNECT_WALLET_SUCCESS,
FETCH_WALLET_SUCCESS
} from '../wallet/actions'
import { WalletReducerAction } from '../wallet/reducer'

export const createCommonMiddleware = () => {
const middleware: RootMiddleware = () => next => (
action:
| WalletReducerAction
| ProfileReducerAction
| TransactionReducerAction
| AuthorizationReducerAction
) => {
switch (action.type) {
case FETCH_WALLET_SUCCESS:
case CONNECT_WALLET_SUCCESS:
case CHANGE_ACCOUNT:
case CHANGE_NETWORK:
action.payload.wallet.address = action.payload.wallet.address.toLowerCase()
break
case LOAD_PROFILE_SUCCESS:
case FETCH_TRANSACTION_REQUEST:
case CLEAR_TRANSACTIONS:
case CHANGE_PROFILE:
action.payload.address = action.payload.address.toLowerCase()
break
case FETCH_AUTHORIZATIONS_SUCCESS:
action.payload.authorizations = action.payload.authorizations.map(
authorization => ({
...authorization,
contractAddress: authorization.contractAddress.toLowerCase(),
authorizedAddress: authorization.authorizedAddress.toLowerCase()
})
)
break
case FETCH_TRANSACTION_SUCCESS:
const transaction = action.payload.transaction

switch (transaction.actionType) {
case GRANT_TOKEN_SUCCESS:
case REVOKE_TOKEN_SUCCESS: {
const { authorization } = transaction.payload
authorization.contractAddress = authorization.contractAddress.toLowerCase()
authorization.authorizedAddress = authorization.authorizedAddress.toLowerCase()
break
}
}
break
}

return next(action)
}

return middleware
}
10 changes: 4 additions & 6 deletions src/modules/modal/actions.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import { action } from 'typesafe-actions'

export let modals: { [key: string]: any } = {}

// Open Modal

export const OPEN_MODAL = 'Open modal'

export const getModalActions = <T>() => ({
openModal: function(name: T, metadata: any = null) {
return action(OPEN_MODAL, { name, metadata })
Expand All @@ -20,6 +14,10 @@ export const getModalActions = <T>() => ({

const { openModal, closeModal, toggleModal } = getModalActions<string>()

// Open Modal

export const OPEN_MODAL = 'Open modal'

export { openModal }

export type OpenModalAction = ReturnType<typeof openModal>
Expand Down
4 changes: 2 additions & 2 deletions src/modules/transaction/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ export const replaceTransactionRequest = (hash: string, nonce: number) =>
nonce
})

export const replaceTransactionSuccess = (hash: string, replaceBy: string) =>
action(REPLACE_TRANSACTION_SUCCESS, { hash, replaceBy })
export const replaceTransactionSuccess = (hash: string, replacedBy: string) =>
action(REPLACE_TRANSACTION_SUCCESS, { hash, replacedBy })

export const replaceTransactionFailure = (hash: string, error: string) =>
action(REPLACE_TRANSACTION_FAILURE, {
Expand Down
74 changes: 24 additions & 50 deletions src/modules/transaction/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export function transactionReducer(
): TransactionState {
switch (action.type) {
case FETCH_TRANSACTION_REQUEST: {
const actionRef = action.payload.action
const { address, action: actionRef } = action.payload
const transaction = getTransactionFromAction(actionRef)
const otherTransactions = state.data.filter(
otherTransaction => otherTransaction.hash !== transaction.hash
Expand All @@ -64,7 +64,7 @@ export function transactionReducer(
{
...transaction,
timestamp: Date.now(),
from: action.payload.address.toLowerCase(),
from: address,
actionType: actionRef.type,
// these always start as null, and they get updated by the saga
status: null,
Expand All @@ -75,18 +75,14 @@ export function transactionReducer(
}
}
case FETCH_TRANSACTION_SUCCESS: {
const actionTransaction = action.payload.transaction
const { transaction } = action.payload
return {
loading: loadingReducer(state.loading, action),
error: null,
data: state.data.map((transaction: Transaction) =>
// prettier-ignore
actionTransaction.hash === transaction.hash
? {
...transaction,
...actionTransaction
}
: transaction
data: state.data.map((stateTransaction: Transaction) =>
transaction.hash === stateTransaction.hash
? { ...stateTransaction, ...transaction }
: stateTransaction
)
}
}
Expand All @@ -96,92 +92,70 @@ export function transactionReducer(
loading: loadingReducer(state.loading, action),
error: message,
data: state.data.map((transaction: Transaction) =>
// prettier-ignore
hash === transaction.hash
? {
...transaction,
status
}
: transaction
hash === transaction.hash ? { ...transaction, status } : transaction
)
}
}
case UPDATE_TRANSACTION_STATUS: {
const { hash, status } = action.payload
return {
loading: loadingReducer(state.loading, action),
error: null,
data: state.data.map((transaction: Transaction) =>
// prettier-ignore
action.payload.hash === transaction.hash
? {
...transaction,
status: action.payload.status
}
: transaction
hash === transaction.hash ? { ...transaction, status } : transaction
)
}
}
case FIX_REVERTED_TRANSACTION: {
const { hash } = action.payload
return {
loading: loadingReducer(state.loading, action),
error: null,
data: state.data.map((transaction: Transaction) =>
// prettier-ignore
action.payload.hash === transaction.hash
? {
...transaction,
status: TransactionStatus.CONFIRMED
}
: transaction
hash === transaction.hash
? { ...transaction, status: TransactionStatus.CONFIRMED }
: transaction
)
}
}
case UPDATE_TRANSACTION_NONCE: {
const { hash, nonce } = action.payload
return {
loading: loadingReducer(state.loading, action),
error: null,
data: state.data.map((transaction: Transaction) =>
action.payload.hash === transaction.hash
? {
...transaction,
nonce: action.payload.nonce
}
: transaction
hash === transaction.hash ? { ...transaction, nonce } : transaction
)
}
}
case REPLACE_TRANSACTION_SUCCESS: {
const { hash, replacedBy } = action.payload
return {
loading: loadingReducer(state.loading, action),
error: null,
data: state.data.map((transaction: Transaction) =>
action.payload.hash === transaction.hash
? {
...transaction,
status: TransactionStatus.REPLACED,
replacedBy: action.payload.replaceBy
}
hash === transaction.hash
? { ...transaction, status: TransactionStatus.REPLACED, replacedBy }
: transaction
)
}
}
case CLEAR_TRANSACTIONS: {
const { address, clearPendings } = action.payload
return {
...state,
data: state.data.filter(
transaction =>
transaction.from.toLowerCase() !==
action.payload.address.toLowerCase() &&
(action.payload.clearPendings || !isPending(transaction.status))
transaction.from.toLowerCase() !== address.toLowerCase() &&
(clearPendings || !isPending(transaction.status))
)
}
}
case CLEAR_TRANSACTION: {
const { hash } = action.payload
return {
...state,
data: state.data.filter(
transaction => transaction.hash !== action.payload.hash
)
data: state.data.filter(transaction => transaction.hash !== hash)
}
}
default:
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)
2 changes: 1 addition & 1 deletion src/modules/wallet/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const isEnabling = (state: any) =>
isLoadingType(getLoading(state), ENABLE_WALLET_REQUEST)

export const getAddress = (state: any) =>
isConnected(state) ? getData(state)!.address : undefined
isConnected(state) ? getData(state)!.address.toLowerCase() : undefined

export const getChainId = (state: any) =>
isConnected(state) ? getData(state)!.chainId : undefined
Expand Down
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()
}