Skip to content

Commit

Permalink
Rename fromCurrency to nativeCurrency in CurrencyController
Browse files Browse the repository at this point in the history
  • Loading branch information
whymarrh committed Oct 25, 2018
1 parent aa4d0df commit 47c4508
Show file tree
Hide file tree
Showing 26 changed files with 122 additions and 117 deletions.
32 changes: 18 additions & 14 deletions app/scripts/controllers/currency.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ class CurrencyController {
* since midnight of January 1, 1970
* @property {number} conversionInterval The id of the interval created by the scheduleConversionInterval method.
* Used to clear an existing interval on subsequent calls of that method.
* @property {string} nativeCurrency The ticker/symbol of the native chain currency
*
*/
constructor (opts = {}) {
const initState = extend({
fromCurrency: 'ETH',
currentCurrency: 'usd',
conversionRate: 0,
conversionDate: 'N/A',
nativeCurrency: 'ETH',
}, opts.initState)
this.store = new ObservableStore(initState)
}
Expand All @@ -38,23 +39,26 @@ class CurrencyController {
//

/**
* A getter for the fromCurrency property
* A getter for the nativeCurrency property
*
* @returns {string} A 2-4 character shorthand that describes the specific currency
*
*/
getFromCurrency () {
return this.store.getState().fromCurrency
getNativeCurrency () {
return this.store.getState().nativeCurrency
}

/**
* A setter for the fromCurrency property
* A setter for the nativeCurrency property
*
* @param {string} fromCurrency The new currency to set as the fromCurrency in the store
* @param {string} nativeCurrency The new currency to set as the nativeCurrency in the store
*
*/
setFromCurrency (fromCurrency) {
this.store.updateState({ ticker: fromCurrency, fromCurrency })
setNativeCurrency (nativeCurrency) {
this.store.updateState({
nativeCurrency,
ticker: nativeCurrency,
})
}

/**
Expand Down Expand Up @@ -125,19 +129,19 @@ class CurrencyController {
*
*/
async updateConversionRate () {
let currentCurrency, fromCurrency
let currentCurrency, nativeCurrency
try {
currentCurrency = this.getCurrentCurrency()
fromCurrency = this.getFromCurrency()
nativeCurrency = this.getNativeCurrency()
let apiUrl
if (fromCurrency === 'ETH') {
if (nativeCurrency === 'ETH') {
apiUrl = `https://api.infura.io/v1/ticker/eth${currentCurrency.toLowerCase()}`
} else {
apiUrl = `https://min-api.cryptocompare.com/data/price?fsym=${fromCurrency.toUpperCase()}&tsyms=${currentCurrency.toUpperCase()}`
apiUrl = `https://min-api.cryptocompare.com/data/price?fsym=${nativeCurrency.toUpperCase()}&tsyms=${currentCurrency.toUpperCase()}`
}
const response = await fetch(apiUrl)
const parsedResponse = await response.json()
if (fromCurrency === 'ETH') {
if (nativeCurrency === 'ETH') {
this.setConversionRate(Number(parsedResponse.bid))
this.setConversionDate(Number(parsedResponse.timestamp))
} else {
Expand All @@ -150,7 +154,7 @@ class CurrencyController {
}
}
} catch (err) {
log.warn(`MetaMask - Failed to query currency conversion:`, fromCurrency, currentCurrency, err)
log.warn(`MetaMask - Failed to query currency conversion:`, nativeCurrency, currentCurrency, err)
this.setConversionRate(0)
this.setConversionDate('N/A')
}
Expand Down
8 changes: 4 additions & 4 deletions app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,12 @@ module.exports = class MetamaskController extends EventEmitter {
this.accountTracker.stop()
}
})

// ensure accountTracker updates balances after network change
this.networkController.on('networkDidChange', () => {
this.accountTracker._updateAccounts()
})

// key mgmt
const additionalKeyrings = [TrezorKeyring, LedgerBridgeKeyring]
this.keyringController = new KeyringController({
Expand Down Expand Up @@ -1416,11 +1416,11 @@ module.exports = class MetamaskController extends EventEmitter {
setCurrentCurrency (currencyCode, cb) {
const { ticker } = this.networkController.getNetworkConfig()
try {
this.currencyController.setFromCurrency(ticker)
this.currencyController.setNativeCurrency(ticker)
this.currencyController.setCurrentCurrency(currencyCode)
this.currencyController.updateConversionRate()
const data = {
fromCurrency: ticker || 'ETH',
nativeCurrency: ticker || 'ETH',
conversionRate: this.currencyController.getConversionRate(),
currentCurrency: this.currencyController.getCurrentCurrency(),
conversionDate: this.currencyController.getConversionDate(),
Expand Down
2 changes: 1 addition & 1 deletion test/data/mock-state.json
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@
},
"ticker": "ETH",
"currentCurrency": "usd",
"fromCurrency": "ETH",
"nativeCurrency": "ETH",
"conversionRate": 556.12,
"addressBook": [
{
Expand Down
8 changes: 4 additions & 4 deletions ui/app/components/balance-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const TokenBalance = require('./token-balance')
const Identicon = require('./identicon')
import UserPreferencedCurrencyDisplay from './user-preferenced-currency-display'
import { PRIMARY, SECONDARY } from '../constants/common'
const { getFromCurrency, getAssetImages, conversionRateSelector, getCurrentCurrency} = require('../selectors')
const { getNativeCurrency, getAssetImages, conversionRateSelector, getCurrentCurrency} = require('../selectors')

const { formatBalance } = require('../util')

Expand All @@ -21,7 +21,7 @@ function mapStateToProps (state) {
return {
account,
network,
fromCurrency: getFromCurrency(state),
nativeCurrency: getNativeCurrency(state),
conversionRate: conversionRateSelector(state),
currentCurrency: getCurrentCurrency(state),
assetImages: getAssetImages(state),
Expand Down Expand Up @@ -67,10 +67,10 @@ BalanceComponent.prototype.renderTokenBalance = function () {

BalanceComponent.prototype.renderBalance = function () {
const props = this.props
const { account, fromCurrency } = props
const { account, nativeCurrency } = props
const balanceValue = account && account.balance
const needsParse = 'needsParse' in props ? props.needsParse : true
const formattedBalance = balanceValue ? formatBalance(balanceValue, 6, needsParse, fromCurrency) : '...'
const formattedBalance = balanceValue ? formatBalance(balanceValue, 6, needsParse, nativeCurrency) : '...'
const showFiat = 'showFiat' in props ? props.showFiat : true

if (formattedBalance === 'None' || formattedBalance === '...') {
Expand Down
10 changes: 5 additions & 5 deletions ui/app/components/currency-display/currency-display.container.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ import CurrencyDisplay from './currency-display.component'
import { getValueFromWeiHex, formatCurrency } from '../../helpers/confirm-transaction/util'

const mapStateToProps = state => {
const { metamask: { fromCurrency, currentCurrency, conversionRate } } = state
const { metamask: { nativeCurrency, currentCurrency, conversionRate } } = state

return {
currentCurrency,
conversionRate,
fromCurrency,
nativeCurrency,
}
}

const mergeProps = (stateProps, dispatchProps, ownProps) => {
const { fromCurrency, currentCurrency, conversionRate, ...restStateProps } = stateProps
const { nativeCurrency, currentCurrency, conversionRate, ...restStateProps } = stateProps
const {
value,
numberOfDecimals = 2,
Expand All @@ -23,9 +23,9 @@ const mergeProps = (stateProps, dispatchProps, ownProps) => {
...restOwnProps
} = ownProps

const toCurrency = currency === 'ETH' ? fromCurrency || currency : currency || currentCurrency
const toCurrency = currency === 'ETH' ? nativeCurrency || currency : currency || currentCurrency
const convertedValue = getValueFromWeiHex({
value, fromCurrency, toCurrency, conversionRate, numberOfDecimals, toDenomination: denomination,
value, nativeCurrency, toCurrency, conversionRate, numberOfDecimals, toDenomination: denomination,
})
const displayValue = formatCurrency(convertedValue, toCurrency)
const suffix = hideLabel ? undefined : toCurrency.toUpperCase()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import CurrencyDisplay from '../currency-display.component'
describe('CurrencyDisplay Component', () => {
it('should render text with a className', () => {
const wrapper = shallow(<CurrencyDisplay
fromCurrency={'ETH'}
nativeCurrency={'ETH'}
displayValue="$123.45"
className="currency-display"
/>)
Expand All @@ -17,7 +17,7 @@ describe('CurrencyDisplay Component', () => {

it('should render text with a prefix', () => {
const wrapper = shallow(<CurrencyDisplay
fromCurrency={'ETH'}
nativeCurrency={'ETH'}
displayValue="$123.45"
className="currency-display"
prefix="-"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ describe('CurrencyDisplay container', () => {
metamask: {
conversionRate: 280.45,
currentCurrency: 'usd',
fromCurrency: 'ETH',
nativeCurrency: 'ETH',
},
}

assert.deepEqual(mapStateToProps(mockState), {
conversionRate: 280.45,
currentCurrency: 'usd',
fromCurrency: 'ETH',
nativeCurrency: 'ETH',
})
})
})
Expand All @@ -37,7 +37,7 @@ describe('CurrencyDisplay container', () => {
const mockStateProps = {
conversionRate: 280.45,
currentCurrency: 'usd',
fromCurrency: 'ETH',
nativeCurrency: 'ETH',
}

const tests = [
Expand All @@ -46,48 +46,48 @@ describe('CurrencyDisplay container', () => {
value: '0x2386f26fc10000',
numberOfDecimals: 2,
currency: 'usd',
fromCurrency: 'ETH',
nativeCurrency: 'ETH',
},
result: {
displayValue: '$2.80',
suffix: 'USD',
fromCurrency: 'ETH',
nativeCurrency: 'ETH',
},
},
{
props: {
value: '0x2386f26fc10000',
fromCurrency: 'ETH',
nativeCurrency: 'ETH',
},
result: {
displayValue: '$2.80',
suffix: 'USD',
fromCurrency: 'ETH',
nativeCurrency: 'ETH',
},
},
{
props: {
value: '0x1193461d01595930',
currency: 'ETH',
fromCurrency: 'ETH',
nativeCurrency: 'ETH',
numberOfDecimals: 3,
},
result: {
displayValue: '1.266',
suffix: 'ETH',
fromCurrency: 'ETH',
nativeCurrency: 'ETH',
},
},
{
props: {
value: '0x1193461d01595930',
currency: 'ETH',
fromCurrency: 'ETH',
nativeCurrency: 'ETH',
numberOfDecimals: 3,
hideLabel: true,
},
result: {
fromCurrency: 'ETH',
nativeCurrency: 'ETH',
displayValue: '1.266',
suffix: undefined,
},
Expand All @@ -96,12 +96,12 @@ describe('CurrencyDisplay container', () => {
props: {
value: '0x3b9aca00',
currency: 'ETH',
fromCurrency: 'ETH',
nativeCurrency: 'ETH',
denomination: 'GWEI',
hideLabel: true,
},
result: {
fromCurrency: 'ETH',
nativeCurrency: 'ETH',
displayValue: '1',
suffix: undefined,
},
Expand All @@ -110,12 +110,12 @@ describe('CurrencyDisplay container', () => {
props: {
value: '0x3b9aca00',
currency: 'ETH',
fromCurrency: 'ETH',
nativeCurrency: 'ETH',
denomination: 'WEI',
hideLabel: true,
},
result: {
fromCurrency: 'ETH',
nativeCurrency: 'ETH',
displayValue: '1000000000',
suffix: undefined,
},
Expand All @@ -124,12 +124,12 @@ describe('CurrencyDisplay container', () => {
props: {
value: '0x3b9aca00',
currency: 'ETH',
fromCurrency: 'ETH',
nativeCurrency: 'ETH',
numberOfDecimals: 100,
hideLabel: true,
},
result: {
fromCurrency: 'ETH',
nativeCurrency: 'ETH',
displayValue: '1e-9',
suffix: undefined,
},
Expand Down
8 changes: 4 additions & 4 deletions ui/app/components/currency-input/currency-input.container.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ import CurrencyInput from './currency-input.component'
import { ETH } from '../../constants/common'

const mapStateToProps = state => {
const { metamask: { fromCurrency, currentCurrency, conversionRate } } = state
const { metamask: { nativeCurrency, currentCurrency, conversionRate } } = state

return {
fromCurrency,
nativeCurrency,
currentCurrency,
conversionRate,
}
}

const mergeProps = (stateProps, dispatchProps, ownProps) => {
const { fromCurrency, currentCurrency } = stateProps
const { nativeCurrency, currentCurrency } = stateProps
const { useFiat } = ownProps
const suffix = useFiat ? currentCurrency.toUpperCase() : fromCurrency || ETH
const suffix = useFiat ? currentCurrency.toUpperCase() : nativeCurrency || ETH

return {
...stateProps,
Expand Down
Loading

0 comments on commit 47c4508

Please sign in to comment.