1
0
mirror of https://github.com/oceanprotocol-archive/squid-js.git synced 2024-02-02 15:31:51 +01:00

made getNetworkName and getAccounts async

This commit is contained in:
Sebastian Gerske 2018-09-21 14:43:50 +02:00
parent 68a7823c4b
commit 9a6838a437
3 changed files with 46 additions and 29 deletions

View File

@ -5,7 +5,7 @@ const contracts = []
export default class ContractLoader { export default class ContractLoader {
static async _doLoad(what, web3Helper) { static async _doLoad(what, web3Helper) {
const where = web3Helper.getNetworkName().toLowerCase() const where = (await web3Helper.getNetworkName()).toLowerCase()
Logger.log('Loading', what, 'from', where) Logger.log('Loading', what, 'from', where)
try { try {
/* eslint-disable-next-line security/detect-non-literal-require */ /* eslint-disable-next-line security/detect-non-literal-require */

View File

@ -18,9 +18,9 @@ export default class Ocean {
this.helper = new Web3Helper(this._web3) this.helper = new Web3Helper(this._web3)
this.metadata = new MetaData(this._providerUri) this.metadata = new MetaData(this._providerUri)
this._network = config.network || this.helper.getNetworkName().toLowerCase() || 'development'
return (async () => { return (async () => {
this._network = config.network || (await this.helper.getNetworkName()).toLowerCase() || 'development'
this.market = await new OceanMarket(this.helper) this.market = await new OceanMarket(this.helper)
this.auth = await new OceanAuth(this.helper) this.auth = await new OceanAuth(this.helper)
this.token = await new OceanToken(this.helper) this.token = await new OceanToken(this.helper)
@ -30,7 +30,7 @@ export default class Ocean {
} }
async getAccounts() { async getAccounts() {
return Promise.all(this.helper.getAccounts().map(async (account) => { return Promise.all((await this.helper.getAccounts()).map(async (account) => {
// await ocean.market.requestTokens(account, 1000) // await ocean.market.requestTokens(account, 1000)
return { return {
@ -74,7 +74,7 @@ export default class Ocean {
.map(async (event) => ({ .map(async (event) => ({
...event.args, ...event.args,
timeout: event.args._timeout.toNumber(), timeout: event.args._timeout.toNumber(),
status: await this.market.getOrderStatus(event.args._id).then((status) => status.toNumber()), status: await this.auth.getOrderStatus(event.args._id).then((status) => status.toNumber()),
paid: await this.market.verifyOrderPayment(event.args._id).then((received) => received), paid: await this.market.verifyOrderPayment(event.args._id).then((received) => received),
key: null key: null
})) }))

View File

@ -1,34 +1,51 @@
import Logger from './logger'
export default class Web3Helper { export default class Web3Helper {
constructor(web3) { constructor(web3) {
this.web3 = web3 this.web3 = web3
} }
getAccounts() { async getAccounts() {
return this.web3.eth.accounts return new Promise((resolve, reject) => {
this.web3.eth.getAccounts((err, accounts) => {
if (err) {
throw err
}
resolve(accounts)
})
})
} }
getNetworkName() { async getNetworkName() {
let network = 'unknown' return new Promise((resolve, reject) => {
switch (this.web3.version.network) { let network = 'unknown'
case '1': this.web3.version.getNetwork((err, networkId) => {
network = 'Main' Logger.log('networkId', networkId)
break if (err) {
case '2': throw err
network = 'Morden' }
break switch (networkId) {
case '3': case '1':
network = 'Ropsten' network = 'Main'
break break
case '4': case '2':
network = 'Rinkeby' network = 'Morden'
break break
case '42': case '3':
network = 'Kovan' network = 'Ropsten'
break break
default: case '4':
network = 'development' network = 'Rinkeby'
} break
return network case '42':
network = 'Kovan'
break
default:
network = 'development'
}
resolve(network)
})
})
} }
// web3 wrappers // web3 wrappers