Skip to content
This repository has been archived by the owner on Jan 15, 2021. It is now read-only.

Commit

Permalink
No longer storing reverts on global state
Browse files Browse the repository at this point in the history
  • Loading branch information
Leandro Boscariol committed Jun 8, 2020
1 parent 758adb2 commit fad42a0
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions src/reducers-actions/trades.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ export type ActionTypes = 'OVERWRITE_TRADES' | 'APPEND_TRADES' | 'UPDATE_BLOCK'

export interface TradesState {
trades: Trade[]
reverts: TradeReversion[]
lastCheckedBlock?: number
}

type OverwriteTradesActionType = Actions<'OVERWRITE_TRADES', TradesState>
type AppendTradesActionType = Actions<'APPEND_TRADES', Required<TradesState>>
interface WithReverts {
reverts: TradeReversion[]
}

type OverwriteTradesActionType = Actions<'OVERWRITE_TRADES', TradesState & WithReverts>
type AppendTradesActionType = Actions<'APPEND_TRADES', Required<TradesState> & WithReverts>
type UpdateBlockActionType = Actions<'UPDATE_BLOCK', Required<Pick<TradesState, 'lastCheckedBlock'>>>
type ReducerActionType = Actions<ActionTypes, TradesState>
type ReducerActionType = Actions<ActionTypes, TradesState & WithReverts>

export const overwriteTrades = (
trades: Trade[],
Expand Down Expand Up @@ -126,20 +129,19 @@ function applyRevertsToTrades(trades: Trade[], reverts: TradeReversion[]): Trade
export const reducer = (state: TradesState, action: ReducerActionType): TradesState => {
switch (action.type) {
case 'APPEND_TRADES': {
const { trades: currTrades, reverts: currReverts } = state
const { trades: newTrades, reverts: newReverts, lastCheckedBlock } = action.payload
const { trades: currTrades } = state
const { trades: newTrades, reverts, lastCheckedBlock } = action.payload

const trades = applyRevertsToTrades(currTrades.concat(newTrades), currReverts.concat(newReverts))
const trades = applyRevertsToTrades(currTrades.concat(newTrades), reverts)

// TODO: is there a point in storing reverts? Don't think so. Remove it
return { trades, reverts: [], lastCheckedBlock }
return { trades, lastCheckedBlock }
}
case 'OVERWRITE_TRADES': {
const { trades: newTrades, reverts: newReverts, lastCheckedBlock } = action.payload
const { trades: newTrades, reverts, lastCheckedBlock } = action.payload

const trades = applyRevertsToTrades(newTrades, newReverts)
const trades = applyRevertsToTrades(newTrades, reverts)

return { trades, reverts: [], lastCheckedBlock }
return { trades, lastCheckedBlock }
}
case 'UPDATE_BLOCK': {
return { ...state, ...action.payload }
Expand Down

0 comments on commit fad42a0

Please sign in to comment.