Skip to content

Commit

Permalink
Store a list of all processed transactions (#247)
Browse files Browse the repository at this point in the history
* Store a list of all processed transactions

* Test fixes

* Further test fixes

* Typos are not our friends

* Fixing the other instance of the same typo ...
  • Loading branch information
benwerd committed Sep 18, 2018
1 parent 88073b8 commit 48b5b09
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 30 deletions.
2 changes: 1 addition & 1 deletion unlock-app/src/__tests__/reducers/accountReducer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('account reducer', () => {
const balance = '1337'

it('should return the initial state', () => {
expect(reducer(undefined, {})).toEqual({ 'locks': [], transaction: null })
expect(reducer(undefined, {})).toEqual({ 'locks': [], transactions: {all: {}, lastUpdated: 0, latest: null} })
})

it('should set the account accordingly when receiving SET_ACCOUNT', () => {
Expand Down
2 changes: 1 addition & 1 deletion unlock-app/src/__tests__/reducers/networkReducer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('network reducer', () => {
expect(reducer(undefined, {})).toEqual({
account: {
locks: [],
transaction: null,
transactions: {all: {}, lastUpdated: 0, latest: null},
},
key: {
data: '',
Expand Down
41 changes: 32 additions & 9 deletions unlock-app/src/__tests__/reducers/transactionReduce.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { SET_TRANSACTION, UPDATE_TRANSACTION } from '../../actions/transaction'
describe('transaction reducer', () => {

it('should return the initial state', () => {
expect(reducer(undefined, {})).toEqual(null)
expect(reducer(undefined, {})).toEqual({all: {}, lastUpdated: 0, latest: null})
})

describe('when receiving SET_TRANSACTION', () => {
Expand All @@ -16,16 +16,23 @@ describe('transaction reducer', () => {
createdAt: new Date().getTime(),
}

const transactions = {
all: {},
lastUpdated: 0,
latest: transaction,
}

expect(reducer(undefined, {
type: SET_TRANSACTION,
transaction,
})).toEqual(transaction)
})).toEqual(transactions)
})

it('should unset the transaction with no transaction', () => {
expect(reducer(undefined, {
let transactions = reducer(undefined, {
type: SET_TRANSACTION,
})).toEqual(null)
})
expect(transactions.latest).toEqual(null)
})
})

Expand All @@ -38,13 +45,21 @@ describe('transaction reducer', () => {
createdAt: new Date().getTime(),
}

const transactions = {
all: {},
lastUpdated: 0,
latest: transaction,
}

const updatedTransaction = Object.assign({}, transaction)
updatedTransaction.status = 'mined'

expect(reducer(transaction, {
let transactionsResponse = reducer(transactions, {
type: UPDATE_TRANSACTION,
transaction: updatedTransaction,
}).status).toEqual('mined')
})

expect(transactionsResponse.latest.status).toEqual('mined')
})

it('should not update the transaction if the previous one is different', () => {
Expand All @@ -54,14 +69,22 @@ describe('transaction reducer', () => {
createdAt: new Date().getTime(),
}

const updatedTransaction = Object.assign({}, transaction)
const transactions = {
all: {},
lastUpdated: 0,
latest: transaction,
}

let updatedTransaction = Object.assign({}, transaction)
updatedTransaction.status = 'mined'
updatedTransaction.createdAt = transaction.createdAt + 100

expect(reducer(transaction, {
let transactionsResponse = reducer(transactions, {
type: UPDATE_TRANSACTION,
transaction: updatedTransaction,
}).status).toEqual('pending')
})

expect(transactionsResponse.latest.status).toEqual('pending')
})
})

Expand Down
2 changes: 1 addition & 1 deletion unlock-app/src/components/consumer/Lock.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ const mapStateToProps = state => {
currentKey: state.network.key, // key is a reserved props name
account: state.network.account,
lock: state.network.lock,
transaction: state.network.account && state.network.account.transaction,
transaction: state.network.account && state.network.account.transactions.latest,
}
}

Expand Down
4 changes: 2 additions & 2 deletions unlock-app/src/components/creator/TransactionModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ export function TransactionModal({ hideTransactionModal, transaction }) {

TransactionModal.propTypes = {
hideTransactionModal: PropTypes.func,
transaction: UnlockPropTypes.account,
transaction: UnlockPropTypes.transaction,
}

const mapStateToProps = state => {
return {
transaction: state.network.account.transaction,
transaction: state.network.account.transactions.latest,
}
}

Expand Down
4 changes: 4 additions & 0 deletions unlock-app/src/propTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ export const lock = PropTypes.shape({

export const transaction = PropTypes.shape({
status: PropTypes.string,
confirmations: PropTypes.number,
createdAt: PropTypes.number,
hash: PropTypes.string,
lock: PropTypes.shape({}),
})

export const children = PropTypes.shape({})
Expand Down
2 changes: 1 addition & 1 deletion unlock-app/src/reducers/accountReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const accountReducer = (state = initialState, action) => {
return {
...state,
locks: locksReducer(state.locks, action),
transaction: transactionReducer(state.transaction, action),
transactions: transactionReducer(state.transactions, action),
}
}

Expand Down
31 changes: 16 additions & 15 deletions unlock-app/src/reducers/transactionReducer.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
import { SET_TRANSACTION, UPDATE_TRANSACTION } from '../actions/transaction'

const initialState = null
const initialState = {
latest: null,
all: {},
lastUpdated: 0,
}

const transactionReducer = (state = initialState, action) => {
const transactionReducer = (transactions = initialState, action) => {
let newTransactions = Object.assign({}, transactions)

if (action.type === SET_TRANSACTION) {
if (action.transaction) {
return {
...action.transaction,
}
newTransactions.latest = Object.assign({}, action.transaction)
if (action.transaction.hash) newTransactions.all[action.transaction.hash] = Object.assign({}, action.transaction)
} else {
newTransactions.latest = null // Unset the latest transaction if SET_TRANSACTION was called with no transaction
}
return null
}

if (action.type === UPDATE_TRANSACTION) {
// Only change the transaction if it is the same as before
if (action.transaction.createdAt === state.createdAt ) {
return {
...action.transaction,
}
if (action.transaction) {
if (action.type === UPDATE_TRANSACTION && action.transaction.createdAt === transactions.latest.createdAt) {
newTransactions.latest = Object.assign({}, action.transaction)
if (action.transaction.hash) newTransactions.all[action.transaction.hash] = Object.assign({}, action.transaction)
}
}

return state
return newTransactions
}

export default transactionReducer

0 comments on commit 48b5b09

Please sign in to comment.