mirror of
https://github.com/oceanprotocol-archive/squid-js.git
synced 2024-02-02 15:31:51 +01:00
Merge pull request #14 from oceanprotocol/feature/network_discovery
added automatic network discovery to squid
This commit is contained in:
commit
a0ef3caa73
2
package-lock.json
generated
2
package-lock.json
generated
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@oceanprotocol/squid",
|
||||
"version": "0.0.6",
|
||||
"version": "0.0.9",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@oceanprotocol/squid",
|
||||
"version": "0.0.6",
|
||||
"version": "0.0.9",
|
||||
"description": "JavaScript client library for Ocean Protocol",
|
||||
"main": "dist/squid.js",
|
||||
"scripts": {
|
||||
|
@ -2,11 +2,11 @@ import ContractLoader from './contractLoader'
|
||||
import KeeperBase from './keeper-base'
|
||||
|
||||
export default class OceanAuth extends KeeperBase {
|
||||
constructor(web3, network) {
|
||||
super(web3, network)
|
||||
constructor(web3Helper) {
|
||||
super(web3Helper)
|
||||
|
||||
return (async () => {
|
||||
this.contract = await ContractLoader.load('OceanAuth', this._network, this._web3)
|
||||
this.contract = await ContractLoader.load('OceanAuth', this._web3Helper)
|
||||
return this
|
||||
})()
|
||||
}
|
||||
|
@ -4,17 +4,22 @@ import Logger from '../utils/logger'
|
||||
const contracts = []
|
||||
|
||||
export default class ContractLoader {
|
||||
static async _doLoad(what, where, web3) {
|
||||
static async _doLoad(what, web3Helper) {
|
||||
const where = (await web3Helper.getNetworkName()).toLowerCase()
|
||||
Logger.log('Loading', what, 'from', where)
|
||||
/* eslint-disable-next-line security/detect-non-literal-require */
|
||||
const artifact = require(`@oceanprotocol/keeper-contracts/artifacts/${what}.${where}`)
|
||||
Logger.log('Loaded artifact', artifact)
|
||||
try {
|
||||
/* eslint-disable-next-line security/detect-non-literal-require */
|
||||
const artifact = require(`@oceanprotocol/keeper-contracts/artifacts/${what}.${where}`)
|
||||
// Logger.log('Loaded artifact', artifact)
|
||||
|
||||
const contract = TruffleContract(artifact)
|
||||
Logger.log('Getting instance of', what, 'from', where, 'at', artifact.address)
|
||||
contract.setProvider(web3.currentProvider)
|
||||
contracts[what] = await contract.at(artifact.address)
|
||||
return contracts[what]
|
||||
const contract = TruffleContract(artifact)
|
||||
Logger.log('Getting instance of', what, 'from', where, 'at', artifact.address)
|
||||
contract.setProvider(web3Helper.web3.currentProvider)
|
||||
contracts[what] = await contract.at(artifact.address)
|
||||
return contracts[what]
|
||||
} catch (err) {
|
||||
Logger.error('Failed to load', what, 'from', where)
|
||||
}
|
||||
}
|
||||
|
||||
static async load(what, where, provider) {
|
||||
|
@ -1,7 +1,6 @@
|
||||
export default class KeeperBase {
|
||||
constructor(web3, network) {
|
||||
this._web3 = web3
|
||||
this._network = network
|
||||
constructor(web3Helper) {
|
||||
this._web3Helper = web3Helper
|
||||
this.contract = null
|
||||
}
|
||||
}
|
||||
|
@ -4,11 +4,11 @@ import KeeperBase from './keeper-base'
|
||||
import Logger from '../utils/logger'
|
||||
|
||||
export default class OceanMarket extends KeeperBase {
|
||||
constructor(web3, network) {
|
||||
super(web3, network)
|
||||
constructor(web3Helper) {
|
||||
super(web3Helper)
|
||||
|
||||
return (async () => {
|
||||
this.contract = await ContractLoader.load('OceanMarket', this._network, this._web3)
|
||||
this.contract = await ContractLoader.load('OceanMarket', this._web3Helper)
|
||||
return this
|
||||
})()
|
||||
}
|
||||
|
@ -3,11 +3,11 @@ import KeeperBase from './keeper-base'
|
||||
import Logger from '../utils/logger'
|
||||
|
||||
export default class OceanToken extends KeeperBase {
|
||||
constructor(web3, network) {
|
||||
super(web3, network)
|
||||
constructor(web3Helper) {
|
||||
super(web3Helper)
|
||||
|
||||
return (async () => {
|
||||
this.contract = await ContractLoader.load('OceanToken', this._network, this._web3)
|
||||
this.contract = await ContractLoader.load('OceanToken', this._web3Helper)
|
||||
|
||||
return this
|
||||
})()
|
||||
@ -20,7 +20,7 @@ export default class OceanToken extends KeeperBase {
|
||||
async getEthBalance(account) {
|
||||
return new Promise((resolve, reject) => {
|
||||
Logger.log('getting balance for', account)
|
||||
this._web3.eth.getBalance(account, 'latest', (err, balance) => {
|
||||
this._web3Helper.web3.eth.getBalance(account, 'latest', (err, balance) => {
|
||||
if (err) return reject(err)
|
||||
Logger.log('balance', balance)
|
||||
resolve(balance)
|
||||
|
17
src/ocean.js
17
src/ocean.js
@ -13,23 +13,24 @@ export default class Ocean {
|
||||
const web3Provider = config.web3Provider || new Web3.providers.HttpProvider(config.nodeUri)
|
||||
this._web3 = new Web3(web3Provider)
|
||||
this._defaultGas = config.gas || DEFAULT_GAS
|
||||
this._network = config.network || 'development'
|
||||
this._providerUri = config.providerUri || null
|
||||
|
||||
this.helper = new Web3Helper(this._web3)
|
||||
this.metadata = new MetaData(this._providerUri)
|
||||
|
||||
return (async () => {
|
||||
this.market = await new OceanMarket(this._web3, this._network)
|
||||
this.auth = await new OceanAuth(this._web3, this._network)
|
||||
this.token = await new OceanToken(this._web3, this._network)
|
||||
this._network = config.network || (await this.helper.getNetworkName()).toLowerCase() || 'development'
|
||||
|
||||
this.market = await new OceanMarket(this.helper)
|
||||
this.auth = await new OceanAuth(this.helper)
|
||||
this.token = await new OceanToken(this.helper)
|
||||
|
||||
return this
|
||||
})()
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
return {
|
||||
@ -73,8 +74,8 @@ export default class Ocean {
|
||||
.map(async (event) => ({
|
||||
...event.args,
|
||||
timeout: event.args._timeout.toNumber(),
|
||||
status: await this.getOrderStatus(event.args._id).then((status) => status.toNumber()),
|
||||
paid: await this.verifyOrderPayment(event.args._id).then((received) => received),
|
||||
status: await this.auth.getOrderStatus(event.args._id).then((status) => status.toNumber()),
|
||||
paid: await this.market.verifyOrderPayment(event.args._id).then((received) => received),
|
||||
key: null
|
||||
}))
|
||||
Logger.debug('got orders: ', orders)
|
||||
@ -86,7 +87,7 @@ export default class Ocean {
|
||||
initialRequestEventHandler, accessCommittedEventHandler, tokenPublishedEventHandler) {
|
||||
const { token, market, auth } = this
|
||||
// Allow market contract to transfer funds on the consumer's behalf
|
||||
token.approve(market.address, price, { from: senderAddress, gas: 2000000 })
|
||||
token.contract.approve(market.address, price, { from: senderAddress, gas: 2000000 })
|
||||
// Submit the access request
|
||||
auth.initiateAccessRequest(
|
||||
assetId, publisherId, publicKey,
|
||||
|
@ -1,17 +1,52 @@
|
||||
import Logger from './logger'
|
||||
|
||||
export default class Web3Helper {
|
||||
constructor(web3) {
|
||||
this._web3 = web3
|
||||
this.web3 = web3
|
||||
}
|
||||
|
||||
getAccounts() {
|
||||
Logger.log(this._web3)
|
||||
return this._web3.eth.accounts
|
||||
async getAccounts() {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.web3.eth.getAccounts((err, accounts) => {
|
||||
if (err) {
|
||||
throw err
|
||||
}
|
||||
resolve(accounts)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
async getNetworkName() {
|
||||
return new Promise((resolve, reject) => {
|
||||
let network = 'unknown'
|
||||
this.web3.version.getNetwork((err, networkId) => {
|
||||
if (err) {
|
||||
throw err
|
||||
}
|
||||
switch (networkId) {
|
||||
case '1':
|
||||
network = 'Main'
|
||||
break
|
||||
case '2':
|
||||
network = 'Morden'
|
||||
break
|
||||
case '3':
|
||||
network = 'Ropsten'
|
||||
break
|
||||
case '4':
|
||||
network = 'Rinkeby'
|
||||
break
|
||||
case '42':
|
||||
network = 'Kovan'
|
||||
break
|
||||
default:
|
||||
network = 'development'
|
||||
}
|
||||
resolve(network)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
// web3 wrappers
|
||||
sign(accountAddress, message) {
|
||||
return this._web3.eth.sign(accountAddress, message)
|
||||
return this.web3.eth.sign(accountAddress, message)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user