blowfish/src/renderer/store/helpers.js

47 lines
1.3 KiB
JavaScript
Raw Normal View History

2020-10-10 15:37:05 +02:00
import electron from 'electron'
2020-02-25 16:46:04 +01:00
import Store from 'electron-store'
2020-02-26 21:04:40 +01:00
import Eth from 'ethjs'
2020-02-25 16:46:04 +01:00
import { oceanTokenContract, conversions } from '../../config'
import { abi } from '@oceanprotocol/contracts/artifacts/ERC20.json'
2020-02-25 16:46:04 +01:00
2020-03-22 23:51:54 +01:00
export async function convertPrices(data, prices) {
2020-02-25 16:46:04 +01:00
let newPrices = new Map(prices) // make a shallow copy of the Map
2020-03-22 23:51:54 +01:00
conversions.map((key) => newPrices.set(key, data['ocean-protocol'][key])) // modify the copy
2020-02-25 16:46:04 +01:00
const newPriceChanges = await Object.assign(
2020-03-22 00:49:10 +01:00
...conversions.map((key) => ({
2020-03-22 23:51:54 +01:00
[key]: data['ocean-protocol'][key + '_24h_change']
2020-02-25 16:46:04 +01:00
}))
)
return { newPrices, newPriceChanges }
}
export async function getBalance(account) {
2020-02-26 21:04:40 +01:00
const provider = new Eth.HttpProvider(
`https://mainnet.infura.io/v3/${process.env.INFURA_PROJECT_ID}`
2020-02-25 16:46:04 +01:00
)
2020-02-26 21:04:40 +01:00
const eth = new Eth(provider)
const token = eth.contract(abi).at(oceanTokenContract)
const balance = await token.balanceOf(account)
2020-02-25 16:46:04 +01:00
2020-02-26 21:04:40 +01:00
return Eth.fromWei(balance[0], 'ether')
2020-02-25 16:46:04 +01:00
}
export async function getAccounts() {
let needsConfig
let accountsPref
2020-10-10 15:37:05 +02:00
const store = (electron.remote && new Store()) || false
if (store && store.has('accounts')) {
2020-02-25 16:46:04 +01:00
accountsPref = store.get('accounts')
needsConfig = !accountsPref.length
} else {
accountsPref = []
needsConfig = true
}
return { needsConfig, accountsPref }
}