Skip to content

Commit cdb1bf9

Browse files
committed
Fiat rates
1 parent c516de2 commit cdb1bf9

File tree

18 files changed

+295
-32
lines changed

18 files changed

+295
-32
lines changed

package-lock.json

Lines changed: 34 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
"pbkdf2": "^3.1.1",
4949
"qrcode": "^1.4.4",
5050
"qs": "^6.9.4",
51+
"svg-url-loader": "^6.0.0",
5152
"uuid": "^8.1.0",
5253
"v-tooltip": "^2.0.3",
5354
"vue": "^2.6.11",

src/assets/bg/asset_list.svg

Lines changed: 102 additions & 0 deletions
Loading

src/assets/scss/style.scss

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@ svg {
7676
font-size: $font-size-sm;
7777
font-weight: bold;
7878
text-transform: uppercase;
79+
width: 100%;
80+
81+
.label-append {
82+
float: right;
83+
color: $text-muted;
84+
font-weight: normal;
85+
}
7986
}
8087
label.btn {
8188
text-transform: none;
@@ -89,6 +96,8 @@ svg {
8996
}
9097
.confirm-value {
9198
font-size: $input-font-size;
99+
line-height: $input-font-size + 4px;
100+
margin-bottom: 0;
92101
}
93102
.form-control-address {
94103
font-size: $font-size-lg;

src/background.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,32 @@
1+
import { random } from 'lodash-es'
12
import store from './store'
23

34
let balanceInterval
4-
const BALANCE_UPDATE_INTERVAL = 30000
5+
let fiatRatesInterval
56

67
store.subscribe(({ type, payload }, state) => {
78
switch (type) {
89
case 'CHANGE_ACTIVE_NETWORK':
910
store.dispatch('updateBalances', { network: state.activeNetwork, walletId: state.activeWalletId })
1011
store.dispatch('updateMarketData', { network: state.activeNetwork })
11-
1212
break
1313

1414
case 'UNLOCK_WALLET':
1515
store.dispatch('updateBalances', { network: state.activeNetwork, walletId: state.activeWalletId })
16+
store.dispatch('updateFiatRates')
1617
store.dispatch('updateMarketData', { network: state.activeNetwork })
1718
store.dispatch('checkPendingSwaps', { walletId: state.activeWalletId })
1819

1920
if (!balanceInterval) {
2021
balanceInterval = setInterval(() => {
2122
store.dispatch('updateBalances', { network: state.activeNetwork, walletId: state.activeWalletId })
22-
}, BALANCE_UPDATE_INTERVAL)
23+
}, random(20000, 30000))
24+
}
25+
26+
if (!fiatRatesInterval) {
27+
fiatRatesInterval = setInterval(() => {
28+
store.dispatch('updateFiatRates')
29+
}, random(20000, 30000))
2330
}
2431

2532
break

src/store/actions/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { unlockWallet } from './unlockWallet'
2222
import { lockWallet } from './lockWallet'
2323
import { updateBalances } from './updateBalances'
2424
import { updateFees } from './updateFees'
25+
import { updateFiatRates } from './updateFiatRates'
2526
import { updateMarketData } from './updateMarketData'
2627

2728
export {
@@ -49,5 +50,6 @@ export {
4950
lockWallet,
5051
updateBalances,
5152
updateFees,
53+
updateFiatRates,
5254
updateMarketData
5355
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { getPrices } from '../utils'
2+
import { NetworkAssets } from '../factory/client'
3+
4+
export const updateFiatRates = async ({ commit }) => {
5+
const assets = NetworkAssets.mainnet.concat(NetworkAssets.testnet)
6+
const fiatRates = await getPrices(assets, 'usd')
7+
8+
commit('UPDATE_FIAT_RATES', { fiatRates })
9+
10+
return fiatRates
11+
}

src/store/mutations.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ export default {
7373

7474
Vue.set(state.fees[network][walletId], asset, fees)
7575
},
76+
UPDATE_FIAT_RATES (state, { fiatRates }) {
77+
state.fiatRates = fiatRates
78+
},
7679
UPDATE_MARKET_DATA (state, { network, marketData }) {
7780
Vue.set(state.marketData, network, marketData)
7881
}

src/store/state.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export default {
1515
encryptedWallets: null,
1616
addresses: {},
1717
balances: {},
18+
fiatRates: {},
1819
fees: {},
1920
history: {},
2021
marketData: {},

src/store/utils.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,29 @@ export const getMarketData = agent => {
7575
}
7676
}).then(res => res.data)
7777
}
78+
79+
const COIN_GECKO_CACHE = {}
80+
const COIN_GECKO_API = 'https://api.coingecko.com/api/v3'
81+
82+
async function getCoins () {
83+
if ('coins' in COIN_GECKO_CACHE) {
84+
return COIN_GECKO_CACHE.coins
85+
}
86+
87+
const response = await axios.get(`${COIN_GECKO_API}/coins/list`)
88+
const coins = response.data
89+
COIN_GECKO_CACHE.coins = coins
90+
return coins
91+
}
92+
93+
export async function getPrices (baseCurrencies, toCurrency) {
94+
const coins = await getCoins()
95+
const coindIds = baseCurrencies.map(currency => coins.find(coin => coin.symbol === currency.toLowerCase()).id)
96+
const response = await axios.get(`${COIN_GECKO_API}/simple/price?ids=${coindIds.join(',')}&vs_currencies=${toCurrency}`)
97+
const prices = response.data
98+
const symbolPrices = Object.entries(prices).reduce((curr, [id, toPrices]) => {
99+
const currencySymbol = coins.find(coin => coin.id === id).symbol
100+
return Object.assign(curr, { [currencySymbol.toUpperCase()]: toPrices[toCurrency] })
101+
}, {})
102+
return symbolPrices
103+
}

0 commit comments

Comments
 (0)