Skip to content

Commit

Permalink
feat(sb): create price_24h actions
Browse files Browse the repository at this point in the history
  • Loading branch information
Philip London committed Jul 14, 2020
1 parent 00f4799 commit 57266c7
Show file tree
Hide file tree
Showing 13 changed files with 272 additions and 70 deletions.
@@ -1,12 +1,10 @@
import { actions, selectors } from 'data'
import { isEmpty } from 'ramda'
import { put, select } from 'redux-saga/effects'
import { Remote } from 'blockchain-wallet-v4/src'

export default () => {
const initialized = function * () {
try {
const logsR = yield select(selectors.core.data.misc.getLogs)
const btcTransactions = yield select(
selectors.core.data.btc.getTransactions
)
Expand All @@ -19,9 +17,6 @@ export default () => {
const xlmTransactions = yield select(
selectors.core.data.xlm.getTransactions
)
if (!Remote.Success.is(logsR)) {
yield put(actions.core.data.misc.fetchLogs())
}
if (isEmpty(btcTransactions)) {
yield put(actions.core.data.btc.fetchTransactions('', true))
}
Expand Down
15 changes: 15 additions & 0 deletions packages/blockchain-wallet-v4/src/network/api/misc/index.ts
@@ -1,4 +1,7 @@
import { CoinType, FiatType } from 'core/types'
import { equals, toUpper } from 'ramda'
import { Moment } from 'moment'
import { PriceIndexResponseType } from './types'

export default ({ rootUrl, apiUrl, get, post }) => {
const getCaptchaImage = (timestamp, sessionToken) =>
Expand Down Expand Up @@ -27,6 +30,17 @@ export default ({ rootUrl, apiUrl, get, post }) => {
data: { guid, sharedKey, method: 'list-logs', format: 'json' }
})

const getPriceIndex = (
base: CoinType,
quote: FiatType,
time: Moment
): PriceIndexResponseType =>
get({
url: apiUrl,
endPoint: '/price/index',
data: { base, quote, time: time.milliseconds }
})

const getPriceIndexSeries = (coin, currency, start, scale) =>
get({
url: apiUrl,
Expand Down Expand Up @@ -61,6 +75,7 @@ export default ({ rootUrl, apiUrl, get, post }) => {
getCaptchaImage,
getTransactionHistory,
getLogs,
getPriceIndex,
getPriceIndexSeries,
getPriceTimestampSeries,
getRandomBytes,
Expand Down
5 changes: 5 additions & 0 deletions packages/blockchain-wallet-v4/src/network/api/misc/types.ts
@@ -0,0 +1,5 @@
export type PriceIndexResponseType = {
price: number
timestamp: number
volume24h: number
}
5 changes: 0 additions & 5 deletions packages/blockchain-wallet-v4/src/network/types.ts
Expand Up @@ -2,8 +2,3 @@ export type NabuApiErrorType = {
description: string
type: string
}

export * from './api/borrow/types'
export * from './api/eth/types'
export * from './api/interest/types'
export * from './api/simpleBuy/types'
Expand Up @@ -10,6 +10,12 @@ export const FETCH_LOGS_LOADING = '@CORE.FETCH_LOGS_LOADING'
export const FETCH_LOGS_SUCCESS = '@CORE.FETCH_LOGS_SUCCESS'
export const FETCH_LOGS_FAILURE = '@CORE.FETCH_LOGS_FAILURE'

// FETCH_PRICE_24H
export const FETCH_PRICE_24H = '@CORE.FETCH_PRICE_24H'
export const FETCH_PRICE_24H_LOADING = '@CORE.FETCH_PRICE_24H_LOADING'
export const FETCH_PRICE_24H_SUCCESS = '@CORE.FETCH_PRICE_24H_SUCCESS'
export const FETCH_PRICE_24H_FAILURE = '@CORE.FETCH_PRICE_24H_FAILURE'

// FETCH_PRICE_INDEX_SERIES
export const FETCH_PRICE_INDEX_SERIES = '@CORE.FETCH_PRICE_INDEX_SERIES'
export const FETCH_PRICE_INDEX_SERIES_LOADING =
Expand Down
@@ -1,4 +1,11 @@
import * as AT from './actionTypes'
import {
CoinType,
FiatType,
MiscActionTypes,
PriceIndexResponseType
} from 'core/types'
import { Moment } from 'moment'

// FETCH_CAPTCHA
export const fetchCaptcha = () => ({ type: AT.FETCH_CAPTCHA })
Expand All @@ -12,16 +19,35 @@ export const fetchCaptchaFailure = error => ({
payload: error
})

// FETCH_LOGS
export const fetchLogs = () => ({ type: AT.FETCH_LOGS })
export const fetchLogsLoading = () => ({ type: AT.FETCH_LOGS_LOADING })
export const fetchLogsSuccess = data => ({
type: AT.FETCH_LOGS_SUCCESS,
payload: data
})
export const fetchLogsFailure = error => ({
type: AT.FETCH_LOGS_FAILURE,
payload: error
// FETCH_PRICE_24H
export const fetchPrice24H = (
base: CoinType,
quote: FiatType,
time: Moment
) => ({
type: AT.FETCH_PRICE_24H,
payload: { base, quote, time }
})
export const fetchPrice24HLoading = (base: CoinType): MiscActionTypes => ({
type: AT.FETCH_PRICE_24H_LOADING,
payload: { base }
})
export const fetchPrice24HSuccess = (
base: CoinType,
data: PriceIndexResponseType
): MiscActionTypes => ({
type: AT.FETCH_PRICE_24H_SUCCESS,
payload: {
base,
data
}
})
export const fetchPrice24HFailure = (base, error): MiscActionTypes => ({
type: AT.FETCH_PRICE_24H_FAILURE,
payload: {
error,
base
}
})

// FETCH_PRICE_INDEX_SERIES
Expand Down
@@ -1,83 +1,85 @@
import * as AT from './actionTypes.js'
import * as AT from './actionTypes'
import { assoc } from 'ramda'
import { MiscActionTypes, MiscStateType } from './types'
import Remote from '../../../remote'

const INITIAL_STATE = {
const INITIAL_STATE: MiscStateType = {
logs: Remote.NotAsked,
captcha: Remote.NotAsked,
pairing_code: Remote.NotAsked,
price_24h: {
BTC: Remote.NotAsked,
ETH: Remote.NotAsked,
BCH: Remote.NotAsked,
XLM: Remote.NotAsked,
ALGO: Remote.NotAsked,
PAX: Remote.NotAsked,
USDT: Remote.NotAsked
},
price_index_series: Remote.NotAsked,
verify_email_token: Remote.NotAsked,
handle_2fa_reset: Remote.NotAsked,
authorize_login: Remote.NotAsked
}

const miscReducer = (state = INITIAL_STATE, action) => {
const { type, payload } = action

switch (type) {
export const miscReducer = (
state = INITIAL_STATE,
action: MiscActionTypes
): MiscStateType => {
switch (action.type) {
case AT.FETCH_CAPTCHA_FAILURE: {
return assoc('captcha', Remote.Failure(action.payload), state)
}
case AT.FETCH_CAPTCHA_LOADING: {
return assoc('captcha', Remote.Loading, state)
}
case AT.FETCH_CAPTCHA_SUCCESS: {
return assoc('captcha', Remote.Success(payload), state)
}
case AT.FETCH_CAPTCHA_FAILURE: {
return assoc('captcha', Remote.Failure(payload), state)
}
case AT.FETCH_LOGS_LOADING: {
return assoc('logs', Remote.Loading, state)
}
case AT.FETCH_LOGS_SUCCESS: {
return assoc('logs', Remote.Success(payload), state)
}
case AT.FETCH_LOGS_FAILURE: {
return assoc('logs', Remote.Failure(payload), state)
return assoc('captcha', Remote.Success(action.payload), state)
}
case AT.FETCH_PRICE_INDEX_SERIES_LOADING: {
return assoc('price_index_series', Remote.Loading, state)
}
case AT.FETCH_PRICE_INDEX_SERIES_SUCCESS: {
return assoc('price_index_series', Remote.Success(payload), state)
return assoc('price_index_series', Remote.Success(action.payload), state)
}
case AT.FETCH_PRICE_INDEX_SERIES_FAILURE: {
return assoc('price_index_series', Remote.Failure(payload), state)
return assoc('price_index_series', Remote.Failure(action.payload), state)
}
case AT.ENCODE_PAIRING_CODE_LOADING: {
return assoc('pairing_code', Remote.Loading, state)
}
case AT.ENCODE_PAIRING_CODE_SUCCESS: {
return assoc('pairing_code', Remote.Success(payload), state)
return assoc('pairing_code', Remote.Success(action.payload), state)
}
case AT.ENCODE_PAIRING_CODE_FAILURE: {
return assoc('pairing_code', Remote.Failure(payload), state)
return assoc('pairing_code', Remote.Failure(action.payload), state)
}
case AT.AUTHORIZE_LOGIN_LOADING: {
return assoc('authorize_login', Remote.Loading, state)
}
case AT.AUTHORIZE_LOGIN_SUCCESS: {
return assoc('authorize_login', Remote.Success(payload), state)
return assoc('authorize_login', Remote.Success(action.payload), state)
}
case AT.AUTHORIZE_LOGIN_FAILURE: {
return assoc('authorize_login', Remote.Failure(payload), state)
return assoc('authorize_login', Remote.Failure(action.payload), state)
}
case AT.HANDLE_2FA_RESET_LOADING: {
return assoc('handle_2fa_reset', Remote.Loading, state)
}
case AT.HANDLE_2FA_RESET_SUCCESS: {
return assoc('handle_2fa_reset', Remote.Success(payload), state)
return assoc('handle_2fa_reset', Remote.Success(action.payload), state)
}
case AT.HANDLE_2FA_RESET_FAILURE: {
return assoc('handle_2fa_reset', Remote.Failure(payload), state)
return assoc('handle_2fa_reset', Remote.Failure(action.payload), state)
}
case AT.VERIFY_EMAIL_TOKEN_LOADING: {
return assoc('verify_email_token', Remote.Loading, state)
}
case AT.VERIFY_EMAIL_TOKEN_SUCCESS: {
return assoc('verify_email_token', Remote.Success(payload), state)
return assoc('verify_email_token', Remote.Success(action.payload), state)
}
case AT.VERIFY_EMAIL_TOKEN_FAILURE: {
return assoc('verify_email_token', Remote.Failure(payload), state)
return assoc('verify_email_token', Remote.Failure(action.payload), state)
}
default:
return state
Expand Down
Expand Up @@ -8,7 +8,6 @@ export default ({ api }) => {
return function * coreDataMiscSaga () {
yield takeLatest(AT.AUTHORIZE_LOGIN, dataMiscSagas.authorizeLogin)
yield takeLatest(AT.FETCH_CAPTCHA, dataMiscSagas.fetchCaptcha)
yield takeLatest(AT.FETCH_LOGS, dataMiscSagas.fetchLogs)
yield takeLatest(
AT.FETCH_PRICE_INDEX_SERIES,
dataMiscSagas.fetchPriceIndexSeries
Expand Down
@@ -1,14 +1,16 @@
import * as A from './actions'
import * as pairing from '../../../pairing'
import * as selectors from '../../selectors'
import * as wS from '../../wallet/selectors'
import { APIType } from 'core/network/api'
import { call, put, select } from 'redux-saga/effects'
import { errorHandler } from 'blockchain-wallet-v4/src/utils'
import moment from 'moment'
import readBlob from 'read-blob'

const taskToPromise = t =>
new Promise((resolve, reject) => t.fork(reject, resolve))

export default ({ api }) => {
export default ({ api }: { api: APIType }) => {
const fetchCaptcha = function * () {
try {
const timestamp = new Date().getTime()
Expand All @@ -22,6 +24,23 @@ export default ({ api }) => {
}
}

const fetchPrice24H = function * (action: ReturnType<typeof A.fetchPrice24H>) {
const { base, quote } = action.payload
try {
yield put(A.fetchPrice24HLoading(base))
const response: ReturnType<typeof api.getPriceIndex> = yield call(
api.getPriceIndex,
base,
quote,
moment().subtract(1, 'day')
)
yield put(A.fetchPrice24HSuccess(base, response))
} catch (e) {
const error = errorHandler(e)
yield put(A.fetchPrice24HFailure(base, error))
}
}

const fetchPriceIndexSeries = function * (action) {
try {
const { coin, currency, start, scale } = action.payload
Expand All @@ -39,18 +58,6 @@ export default ({ api }) => {
}
}

const fetchLogs = function * ({ address }) {
try {
const guid = yield select(selectors.wallet.getGuid)
const sharedKey = yield select(selectors.wallet.getSharedKey)
yield put(A.fetchLogsLoading())
const data = yield call(api.getLogs, guid, sharedKey)
yield put(A.fetchLogsSuccess(data.results))
} catch (e) {
yield put(A.fetchLogsFailure(e.message))
}
}

const encodePairingCode = function * () {
try {
yield put(A.encodePairingCodeLoading())
Expand Down Expand Up @@ -117,7 +124,7 @@ export default ({ api }) => {
return {
authorizeLogin,
fetchCaptcha,
fetchLogs,
fetchPrice24H,
fetchPriceIndexSeries,
encodePairingCode,
verifyEmailToken,
Expand Down
Expand Up @@ -4,6 +4,7 @@ import { dataPath } from '../../paths'
import { INVALID_COIN_TYPE } from 'blockchain-wallet-v4/src/model'
import { path } from 'ramda'
import { Remote } from 'blockchain-wallet-v4/src'
import { RootState } from 'data/rootReducer'

export const getCaptcha = path([dataPath, 'misc', 'captcha'])

Expand All @@ -23,6 +24,9 @@ export const handle2FAReset = path([dataPath, 'misc', 'handle_2fa_reset'])

export const verifyEmailToken = path([dataPath, 'misc', 'verify_email_token'])

export const getPrice24H = (state: RootState, coin: CoinType) =>
state.dataPath.misc.price_24h[coin]

export const getRatesSelector = (
coin: CoinType,
state
Expand Down

0 comments on commit 57266c7

Please sign in to comment.