blowfish/src/renderer/store/helpers.js

45 lines
1.3 KiB
JavaScript
Raw Normal View History

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'
2020-02-26 21:04:40 +01:00
import { abi } from '@oceanprotocol/keeper-contracts/artifacts/OceanToken.pacific.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
const store = process.env.NODE_ENV === 'test' ? new Store() : global.store
if (store.has('accounts')) {
accountsPref = store.get('accounts')
needsConfig = !accountsPref.length
} else {
accountsPref = []
needsConfig = true
}
return { needsConfig, accountsPref }
}