WA-232 Using custom fetch and active tokens extractor

This commit is contained in:
apanizo 2018-07-11 13:01:08 +02:00
parent 0f4d5cb33a
commit be9bfe0df9
3 changed files with 60 additions and 15 deletions

19
src/utils/fetch.js Normal file
View File

@ -0,0 +1,19 @@
// @flow
export const enhancedFetch = async (url: string, errMsg: string) => {
const header = new Headers({
'Access-Control-Allow-Origin': '*',
})
const sentData = {
mode: 'cors',
header,
}
const response = await fetch(url, sentData)
if (!response.ok) {
throw new Error(errMsg)
}
return response.json()
}

View File

@ -1,2 +1,39 @@
// @flow
import { List } from 'immutable'
import logo from '~/assets/icons/icon_etherTokens.svg'
import { getBalanceInEtherOf } from '~/wallets/getWeb3'
import { makeToken, type Token } from '~/routes/tokens/store/model/token'
export const isEther = (symbol: string) => symbol === 'ETH'
export const getSafeEthToken = async (safeAddress: string) => {
const balance = await getBalanceInEtherOf(safeAddress)
const ethBalance = makeToken({
address: '0',
name: 'Ether',
symbol: 'ETH',
decimals: 18,
logoUrl: logo,
funds: balance,
})
return ethBalance
}
export const calculateActiveErc20TokensFrom = (tokens: List<Token>) => {
const addresses = List().withMutations(list =>
tokens.forEach((token: Token) => {
if (isEther(token.get('symbol'))) {
return
}
if (!token.get('status')) {
return
}
list.push(token.address)
}))
return addresses
}

View File

@ -2,6 +2,7 @@
import { BigNumber } from 'bignumber.js'
import { getWeb3 } from '~/wallets/getWeb3'
import { promisify } from '~/utils/promisify'
import { enhancedFetch } from '~/utils/fetch'
// const MAINNET_NETWORK = 1
export const EMPTY_DATA = '0x'
@ -40,21 +41,9 @@ export const calculateGasPrice = async () => {
return '20000000000'
}
const header = new Headers({
'Access-Control-Allow-Origin': '*',
})
const sentData = {
mode: 'cors',
header,
}
const response = await fetch('https://ethgasstation.info/json/ethgasAPI.json', sentData)
if (!response.ok) {
throw new Error('Error querying gast station')
}
const json = await response.json()
const url = 'https://ethgasstation.info/json/ethgasAPI.json'
const errMsg = 'Error querying gas station'
const json = await enhancedFetch(url, errMsg)
return new BigNumber(json.average).multipliedBy(1e8).toString()
}