aboutsummaryrefslogtreecommitdiffstats
path: root/ui/app/actions.js
diff options
context:
space:
mode:
authorbitpshr <mail@bitpshr.net>2018-04-16 11:37:42 +0800
committerbitpshr <mail@bitpshr.net>2018-04-16 11:46:40 +0800
commita350e80feea6747a5e10088ac6ec15171a590a65 (patch)
tree93be19006b469678ffaec07b754104827dd1e61a /ui/app/actions.js
parent3afe76bcba5eb1b84c748b13b61d1a22bed40bd3 (diff)
downloadtangerine-wallet-browser-a350e80feea6747a5e10088ac6ec15171a590a65.tar
tangerine-wallet-browser-a350e80feea6747a5e10088ac6ec15171a590a65.tar.gz
tangerine-wallet-browser-a350e80feea6747a5e10088ac6ec15171a590a65.tar.bz2
tangerine-wallet-browser-a350e80feea6747a5e10088ac6ec15171a590a65.tar.lz
tangerine-wallet-browser-a350e80feea6747a5e10088ac6ec15171a590a65.tar.xz
tangerine-wallet-browser-a350e80feea6747a5e10088ac6ec15171a590a65.tar.zst
tangerine-wallet-browser-a350e80feea6747a5e10088ac6ec15171a590a65.zip
Fetch token prices based on contract address
Diffstat (limited to 'ui/app/actions.js')
-rw-r--r--ui/app/actions.js68
1 files changed, 45 insertions, 23 deletions
diff --git a/ui/app/actions.js b/ui/app/actions.js
index 0748a5bea..6453a2bc2 100644
--- a/ui/app/actions.js
+++ b/ui/app/actions.js
@@ -220,8 +220,10 @@ var actions = {
coinBaseSubview: coinBaseSubview,
SHAPESHIFT_SUBVIEW: 'SHAPESHIFT_SUBVIEW',
shapeShiftSubview: shapeShiftSubview,
- UPDATE_TOKEN_EXCHANGE_RATE: 'UPDATE_TOKEN_EXCHANGE_RATE',
- updateTokenExchangeRate,
+ UPDATE_CONTRACT_EXCHANGE_RATES: 'UPDATE_CONTRACT_EXCHANGE_RATES',
+ UPDATE_CONTRACT_EXCHANGE_RATE: 'UPDATE_CONTRACT_EXCHANGE_RATE',
+ updateContractExchangeRates,
+ updateContractExchangeRate,
PAIR_UPDATE: 'PAIR_UPDATE',
pairUpdate: pairUpdate,
coinShiftRquest: coinShiftRquest,
@@ -1080,9 +1082,12 @@ function unlockMetamask (account) {
}
function updateMetamaskState (newState) {
- return {
- type: actions.UPDATE_METAMASK_STATE,
- value: newState,
+ return async dispatch => {
+ await dispatch({
+ type: actions.UPDATE_METAMASK_STATE,
+ value: newState,
+ })
+ dispatch(updateContractExchangeRates())
}
}
@@ -1295,9 +1300,12 @@ function addTokens (tokens) {
}
function updateTokens (newTokens) {
- return {
- type: actions.UPDATE_TOKENS,
- newTokens,
+ return async dispatch => {
+ await dispatch({
+ type: actions.UPDATE_TOKENS,
+ newTokens,
+ })
+ dispatch(updateContractExchangeRates())
}
}
@@ -1751,24 +1759,38 @@ function shapeShiftRequest (query, options, cb) {
}
}
-function updateTokenExchangeRate (token = '') {
- const pair = `${token.toLowerCase()}_eth`
+async function fetchContractRate (address) {
+ try {
+ const response = await fetch(`https://exchanges.balanc3.net/prices?from=${address}&to=ETH&autoConversion=false&summaryOnly=true`)
+ const json = await response.json()
+ const rate = json && json.length ? json[0].averagePrice : 0
+ return { address, rate }
+ } catch (error) { }
+}
- return dispatch => {
- if (!token) {
- return
+function updateContractExchangeRates () {
+ return async (dispatch, getState) => {
+ const { metamask: { tokens = [] } } = getState()
+ const newExchangeRates = {}
+
+ for (const i in tokens) {
+ const address = tokens[i].address
+ newExchangeRates[address] = (await fetchContractRate(address)).rate
}
- shapeShiftRequest('marketinfo', { pair }, marketinfo => {
- if (!marketinfo.error) {
- dispatch({
- type: actions.UPDATE_TOKEN_EXCHANGE_RATE,
- payload: {
- pair,
- marketinfo,
- },
- })
- }
+ dispatch({
+ type: actions.UPDATE_CONTRACT_EXCHANGE_RATES,
+ payload: { newExchangeRates },
+ })
+ }
+}
+
+function updateContractExchangeRate (address) {
+ return async dispatch => {
+ const { address, rate } = await fetchContractRate(address)
+ dispatch({
+ type: actions.UPDATE_CONTRACT_EXCHANGE_RATE,
+ payload: { address, rate },
})
}
}