1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-22 18:00:18 +01:00

Fetch token prices based on contract address

This commit is contained in:
bitpshr 2018-04-15 23:37:42 -04:00
parent 3afe76bcba
commit a350e80fee
8 changed files with 85 additions and 81 deletions

View File

@ -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 },
})
}
}

View File

@ -48,7 +48,7 @@ module.exports = compose(
function mapStateToProps (state, ownProps) {
const { token: { symbol }, txData } = ownProps
const { token: { address }, txData } = ownProps
const { txParams } = txData || {}
const tokenData = txParams.data && abiDecoder.decodeMethod(txParams.data)
@ -59,7 +59,7 @@ function mapStateToProps (state, ownProps) {
} = state.metamask
const accounts = state.metamask.accounts
const selectedAddress = getSelectedAddress(state)
const tokenExchangeRate = getTokenExchangeRate(state, symbol)
const tokenExchangeRate = getTokenExchangeRate(state, address)
const { balance } = accounts[selectedAddress]
return {
conversionRate,
@ -75,12 +75,9 @@ function mapStateToProps (state, ownProps) {
}
function mapDispatchToProps (dispatch, ownProps) {
const { token: { symbol } } = ownProps
return {
backToAccountDetail: address => dispatch(actions.backToAccountDetail(address)),
cancelTransaction: ({ id }) => dispatch(actions.cancelTx({ id })),
updateTokenExchangeRate: () => dispatch(actions.updateTokenExchangeRate(symbol)),
editTransaction: txMeta => {
const { token: { address } } = ownProps
const { txParams = {}, id } = txMeta
@ -203,7 +200,6 @@ ConfirmSendToken.prototype.componentWillMount = function () {
.balanceOf(selectedAddress)
.then(usersToken => {
})
this.props.updateTokenExchangeRate()
this.updateComponentSendErrors({})
}

View File

@ -66,7 +66,7 @@ function mapDispatchToProps (dispatch) {
showCustomizeGasModal: () => dispatch(actions.showModal({ name: 'CUSTOMIZE_GAS' })),
estimateGas: params => dispatch(actions.estimateGas(params)),
getGasPrice: () => dispatch(actions.getGasPrice()),
updateTokenExchangeRate: token => dispatch(actions.updateTokenExchangeRate(token)),
updateContractExchangeRate: address => dispatch(actions.updateContractExchangeRate(address)),
signTokenTx: (tokenAddress, toAddress, amount, txData) => (
dispatch(actions.signTokenTx(tokenAddress, toAddress, amount, txData))
),

View File

@ -16,7 +16,7 @@ function mapStateToProps (state) {
currentCurrency: state.metamask.currentCurrency,
selectedTokenAddress: state.metamask.selectedTokenAddress,
userAddress: selectors.getSelectedAddress(state),
tokenExchangeRates: state.metamask.tokenExchangeRates,
contractExchangeRates: state.metamask.contractExchangeRates,
conversionRate: state.metamask.conversionRate,
sidebarOpen: state.appState.sidebarOpen,
}
@ -25,7 +25,6 @@ function mapStateToProps (state) {
function mapDispatchToProps (dispatch) {
return {
setSelectedToken: address => dispatch(actions.setSelectedToken(address)),
updateTokenExchangeRate: token => dispatch(actions.updateTokenExchangeRate(token)),
hideSidebar: () => dispatch(actions.hideSidebar()),
}
}
@ -41,15 +40,6 @@ function TokenCell () {
}
}
TokenCell.prototype.componentWillMount = function () {
const {
updateTokenExchangeRate,
symbol,
} = this.props
updateTokenExchangeRate(symbol)
}
TokenCell.prototype.render = function () {
const { tokenMenuOpen } = this.state
const props = this.props
@ -60,7 +50,7 @@ TokenCell.prototype.render = function () {
network,
setSelectedToken,
selectedTokenAddress,
tokenExchangeRates,
contractExchangeRates,
conversionRate,
hideSidebar,
sidebarOpen,
@ -68,15 +58,13 @@ TokenCell.prototype.render = function () {
// userAddress,
} = props
const pair = `${symbol.toLowerCase()}_eth`
let currentTokenToFiatRate
let currentTokenInFiat
let formattedFiat = ''
if (tokenExchangeRates[pair]) {
if (contractExchangeRates[address]) {
currentTokenToFiatRate = multiplyCurrencies(
tokenExchangeRates[pair].rate,
contractExchangeRates[address],
conversionRate
)
currentTokenInFiat = conversionUtil(string, {

View File

@ -27,7 +27,7 @@ function mapStateToProps (state) {
return {
tokens: state.metamask.tokens,
currentCurrency: getCurrentCurrency(state),
tokenExchangeRates: state.metamask.tokenExchangeRates,
contractExchangeRates: state.metamask.contractExchangeRates,
selectedAddressTxList: state.metamask.selectedAddressTxList,
}
}
@ -142,31 +142,29 @@ TxListItem.prototype.getTokenInfo = async function () {
({ decimals, symbol } = await tokenInfoGetter(toAddress))
}
return { decimals, symbol }
return { decimals, symbol, address: toAddress }
}
TxListItem.prototype.getSendTokenTotal = async function () {
const {
txParams = {},
conversionRate,
tokenExchangeRates,
contractExchangeRates,
currentCurrency,
} = this.props
const decodedData = txParams.data && abiDecoder.decodeMethod(txParams.data)
const { params = [] } = decodedData || {}
const { value } = params[1] || {}
const { decimals, symbol } = await this.getTokenInfo()
const { decimals, symbol, address } = await this.getTokenInfo()
const total = calcTokenAmount(value, decimals)
const pair = symbol && `${symbol.toLowerCase()}_eth`
let tokenToFiatRate
let totalInFiat
if (tokenExchangeRates[pair]) {
if (contractExchangeRates[address]) {
tokenToFiatRate = multiplyCurrencies(
tokenExchangeRates[pair].rate,
contractExchangeRates[address],
conversionRate
)
@ -220,7 +218,6 @@ TxListItem.prototype.resubmit = function () {
TxListItem.prototype.render = function () {
const {
transactionStatus,
transactionAmount,
onClick,
transactionId,
dateString,
@ -229,7 +226,6 @@ TxListItem.prototype.render = function () {
txParams,
} = this.props
const { total, fiatTotal, isTokenTx } = this.state
const showFiatTotal = transactionAmount !== '0x0' && fiatTotal
return h(`div${className || ''}`, {
key: transactionId,
@ -288,7 +284,7 @@ TxListItem.prototype.render = function () {
h('span.tx-list-value', total),
showFiatTotal && h('span.tx-list-fiat-value', fiatTotal),
fiatTotal && h('span.tx-list-fiat-value', fiatTotal),
]),
]),

View File

@ -24,6 +24,7 @@ function reduceMetamask (state, action) {
frequentRpcList: [],
addressBook: [],
selectedTokenAddress: null,
contractExchangeRates: {},
tokenExchangeRates: {},
tokens: [],
send: {
@ -176,14 +177,22 @@ function reduceMetamask (state, action) {
conversionDate: action.value.conversionDate,
})
case actions.UPDATE_TOKEN_EXCHANGE_RATE:
const { payload: { pair, marketinfo } } = action
return extend(metamaskState, {
tokenExchangeRates: {
...metamaskState.tokenExchangeRates,
[pair]: marketinfo,
case actions.UPDATE_CONTRACT_EXCHANGE_RATES:
const { payload: { newExchangeRates } } = action
return {
...metamaskState,
contractExchangeRates: newExchangeRates,
}
case actions.UPDATE_CONTRACT_EXCHANGE_RATE:
const { payload: { address, rate } } = action
return {
...metamaskState,
contractExchangeRates: {
...metamaskState.contractExchangeRates,
[address]: rate,
},
})
}
case actions.UPDATE_TOKENS:
return extend(metamaskState, {

View File

@ -62,22 +62,15 @@ function getSelectedToken (state) {
}
function getSelectedTokenExchangeRate (state) {
const tokenExchangeRates = state.metamask.tokenExchangeRates
const contractExchangeRates = state.metamask.contractExchangeRates
const selectedToken = getSelectedToken(state) || {}
const { symbol = '' } = selectedToken
const pair = `${symbol.toLowerCase()}_eth`
const { rate: tokenExchangeRate = 0 } = tokenExchangeRates[pair] || {}
return tokenExchangeRate
const { address } = selectedToken
return contractExchangeRates[address] || 0
}
function getTokenExchangeRate (state, tokenSymbol) {
const pair = `${tokenSymbol.toLowerCase()}_eth`
const tokenExchangeRates = state.metamask.tokenExchangeRates
const { rate: tokenExchangeRate = 0 } = tokenExchangeRates[pair] || {}
return tokenExchangeRate
function getTokenExchangeRate (state, address) {
const contractExchangeRates = state.metamask.contractExchangeRates
return contractExchangeRates[address] || 0
}
function conversionRateSelector (state) {

View File

@ -89,14 +89,14 @@ SendTransactionScreen.prototype.updateSendTokenBalance = function (usersToken) {
SendTransactionScreen.prototype.componentWillMount = function () {
const {
updateTokenExchangeRate,
updateContractExchangeRate,
selectedToken = {},
} = this.props
const { symbol } = selectedToken || {}
const { address } = selectedToken || {}
if (symbol) {
updateTokenExchangeRate(symbol)
if (address) {
updateContractExchangeRate(address)
}
this.updateGas()