2022-04-22 06:05:56 +03:00
|
|
|
/* eslint-disable no-console */
|
2023-08-21 20:22:17 +03:00
|
|
|
import { TokenPriceOracle } from '@tornado/tornado-oracles'
|
2022-04-22 06:05:56 +03:00
|
|
|
|
|
|
|
export const state = () => {
|
|
|
|
return {
|
|
|
|
prices: {
|
2023-08-21 20:22:17 +03:00
|
|
|
torn: '1607657994944809',
|
|
|
|
dai: '604623353553108',
|
|
|
|
cdai: '13524059864784',
|
|
|
|
usdc: '600920027684692',
|
|
|
|
usdt: '600764414690498',
|
|
|
|
wbtc: '15636492243151834302'
|
2022-04-22 06:05:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const getters = {
|
2023-08-21 20:22:17 +03:00
|
|
|
priceOracle: (state, getters, rootState, rootGetters) => {
|
|
|
|
const netId = Number(rootGetters['metamask/netId'])
|
|
|
|
const { url: rpcUrl } = rootState.settings[`netId${netId}`].rpc
|
2024-05-12 23:58:20 +03:00
|
|
|
const config = rootGetters['metamask/networkConfig']
|
2023-08-21 20:22:17 +03:00
|
|
|
|
2024-05-12 23:58:20 +03:00
|
|
|
return new TokenPriceOracle(netId, rpcUrl, config)
|
|
|
|
},
|
|
|
|
tokens: (state, getters, rootStater, rootGetters) => {
|
|
|
|
const config = rootGetters['metamask/networkConfig']
|
|
|
|
const { 'torn.contract.tornadocash.eth': tornContract, tokens } = config
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
tokenAddress: tornContract,
|
|
|
|
symbol: 'TORN',
|
|
|
|
decimals: 18
|
|
|
|
},
|
|
|
|
...Object.values(tokens)
|
|
|
|
.map(({ tokenAddress, symbol, decimals }) =>
|
|
|
|
tokenAddress ? { tokenAddress, symbol, decimals } : undefined
|
|
|
|
)
|
|
|
|
.filter((t) => t)
|
|
|
|
]
|
2022-04-22 06:05:56 +03:00
|
|
|
},
|
2023-08-21 20:22:17 +03:00
|
|
|
tokenRate: (state, getters, rootState) => {
|
|
|
|
return state.prices[rootState.application.selectedStatistic.currency]
|
2022-04-22 06:05:56 +03:00
|
|
|
},
|
2023-08-21 20:22:17 +03:00
|
|
|
getTokenPrice: (state) => (currency) => {
|
2022-04-22 06:05:56 +03:00
|
|
|
return state.prices[currency]
|
|
|
|
},
|
|
|
|
isPriceWatcherDisabled: (state, getters, rootState, rootGetters) => {
|
|
|
|
const nativeCurrency = rootGetters['metamask/nativeCurrency']
|
|
|
|
const tokens = Object.keys(rootGetters['metamask/networkConfig'].tokens)
|
|
|
|
return tokens.includes(nativeCurrency) && tokens.length === 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const mutations = {
|
|
|
|
SAVE_TOKEN_PRICES(state, prices) {
|
|
|
|
state.prices = {
|
|
|
|
...state.prices,
|
|
|
|
...prices
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const actions = {
|
|
|
|
async fetchTokenPrice({ getters, commit, dispatch, rootState }) {
|
|
|
|
if (getters.isPriceWatcherDisabled) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-05-12 23:58:20 +03:00
|
|
|
const tokens = getters.tokens
|
|
|
|
|
2022-04-22 06:05:56 +03:00
|
|
|
try {
|
2024-05-12 23:58:20 +03:00
|
|
|
const prices = await getters.priceOracle.fetchPrices(tokens)
|
2022-04-22 06:05:56 +03:00
|
|
|
console.log('prices', prices)
|
|
|
|
commit('SAVE_TOKEN_PRICES', prices)
|
|
|
|
|
|
|
|
setTimeout(() => dispatch('fetchTokenPrice'), 1000 * 30)
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e)
|
|
|
|
setTimeout(() => dispatch('fetchTokenPrice'), 1000 * 30)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|