mirror of
https://github.com/oceanprotocol-archive/squid-js.git
synced 2024-02-02 15:31:51 +01:00
Work In Progress: Refactor the ocean interface.
This commit is contained in:
parent
5f80fdfd88
commit
82a149ec48
@ -6,6 +6,12 @@ export default class MetaData {
|
||||
this.assetsUrl = providerUri + '/assets'
|
||||
}
|
||||
|
||||
getAssetDDO(assetDID) {
|
||||
return fetch(this.assetsUrl + `/metadata/${assetDID}`, { method: 'GET' })
|
||||
.then(res => res.json())
|
||||
.then(data => JSON.parse(data))
|
||||
}
|
||||
|
||||
getAssetsMetadata() {
|
||||
return fetch(this.assetsUrl + '/metadata', { method: 'GET' })
|
||||
.then(res => res.json())
|
||||
|
92
src/ocean.js
92
src/ocean.js
@ -1,12 +1,11 @@
|
||||
import Web3 from 'web3'
|
||||
import OceanMarket from './keeper/market'
|
||||
import OceanAuth from './keeper/auth'
|
||||
import OceanToken from './keeper/token'
|
||||
import Logger from './utils/logger'
|
||||
import Web3Helper from './utils/Web3Helper'
|
||||
import MetaData from './metadata'
|
||||
import ContractLoader from './keeper/contractLoader'
|
||||
|
||||
const DEFAULT_GAS = 300000
|
||||
const contractsToLoad = { market: 'OceanMarket', token: 'OceanToken', auth: 'OceanAuth' }
|
||||
|
||||
export default class Ocean {
|
||||
constructor(config) {
|
||||
@ -17,13 +16,12 @@ export default class Ocean {
|
||||
|
||||
this.helper = new Web3Helper(this._web3)
|
||||
this.metadata = new MetaData(this._providerUri)
|
||||
|
||||
this.contracts = {}
|
||||
return (async () => {
|
||||
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)
|
||||
for (const key of contractsToLoad) {
|
||||
this.contracts[key] = await ContractLoader.load(contractsToLoad[key], this.helper)
|
||||
}
|
||||
|
||||
return this
|
||||
})()
|
||||
@ -36,15 +34,15 @@ export default class Ocean {
|
||||
return {
|
||||
name: account,
|
||||
balance: {
|
||||
ocn: await this.token.getTokenBalance(account),
|
||||
eth: await this.token.getEthBalance(account)
|
||||
ocn: await this.contracts.token.getTokenBalance(account),
|
||||
eth: await this.contracts.token.getEthBalance(account)
|
||||
}
|
||||
}
|
||||
}))
|
||||
}
|
||||
|
||||
async getOrdersByConsumer(consumerAddress) {
|
||||
let accessConsentEvent = this.auth.contract.AccessConsentRequested({ _consumer: consumerAddress }, {
|
||||
let accessConsentEvent = this.contracts.auth.AccessConsentRequested({ _consumer: consumerAddress }, {
|
||||
fromBlock: 0,
|
||||
toBlock: 'latest'
|
||||
})
|
||||
@ -85,17 +83,17 @@ export default class Ocean {
|
||||
purchaseAsset(
|
||||
assetId, publisherId, price, privateKey, publicKey, timeout, senderAddress,
|
||||
initialRequestEventHandler, accessCommittedEventHandler, tokenPublishedEventHandler) {
|
||||
const { token, market, auth } = this
|
||||
const { token, market, auth } = this.contracts
|
||||
// Allow market contract to transfer funds on the consumer's behalf
|
||||
token.contract.approve(market.address, price, { from: senderAddress, gas: 2000000 })
|
||||
token.approve(market.address, price, { from: senderAddress, gas: 2000000 })
|
||||
// Submit the access request
|
||||
auth.contract.initiateAccessRequest(
|
||||
auth.initiateAccessRequest(
|
||||
assetId, publisherId, publicKey,
|
||||
timeout, { from: senderAddress, gas: 1000000 }
|
||||
)
|
||||
|
||||
const resourceFilter = { _resourceId: assetId, _consumer: senderAddress }
|
||||
const initRequestEvent = auth.contract.AccessConsentRequested(resourceFilter)
|
||||
const initRequestEvent = auth.AccessConsentRequested(resourceFilter)
|
||||
let order = {}
|
||||
this._listenOnce(
|
||||
initRequestEvent,
|
||||
@ -103,8 +101,8 @@ export default class Ocean {
|
||||
(result, error) => {
|
||||
order = initialRequestEventHandler(result, error)
|
||||
const requestIdFilter = { _id: order.id }
|
||||
const accessCommittedEvent = auth.contract.AccessRequestCommitted(requestIdFilter)
|
||||
const tokenPublishedEvent = auth.contract.EncryptedTokenPublished(requestIdFilter)
|
||||
const accessCommittedEvent = auth.AccessRequestCommitted(requestIdFilter)
|
||||
const tokenPublishedEvent = auth.EncryptedTokenPublished(requestIdFilter)
|
||||
this._listenOnce(
|
||||
accessCommittedEvent,
|
||||
'AccessRequestCommitted',
|
||||
@ -134,4 +132,64 @@ export default class Ocean {
|
||||
callback(result, error)
|
||||
})
|
||||
}
|
||||
|
||||
// The new interface
|
||||
async publishDataAsset(assetMetadata, price) {
|
||||
// Register on-chain (in the keeper)
|
||||
const { market } = this.contracts
|
||||
const assetDID = await this.generateDID(assetMetadata)
|
||||
const result = await market.register(
|
||||
assetDID,
|
||||
price,
|
||||
{ from: this.getCurrentAccount(), gas: this.defaultGas }
|
||||
)
|
||||
if (!result) {
|
||||
throw Error('Register asset in ocean keeper failed.')
|
||||
}
|
||||
// Register in oceandb
|
||||
const assetDDO = this.createAssetDDO(assetDID, assetMetadata)
|
||||
this.metadata.publishDataAsset(assetDID, assetDDO)
|
||||
return assetDDO
|
||||
}
|
||||
|
||||
getCurrentAccount() {
|
||||
return this.helper.getCurrentAccount()
|
||||
}
|
||||
|
||||
getTokenBalance() {
|
||||
return this.contracts.token.getTokenBalance()
|
||||
}
|
||||
|
||||
getEthBalance() {
|
||||
return this.contracts.token.getEthBalance()
|
||||
}
|
||||
|
||||
requestTokens(numTokens) {
|
||||
return this.contracts.market.requestTokens(numTokens, { from: this.getCurrentAccount() })
|
||||
}
|
||||
|
||||
getMessageHash(message) {
|
||||
return this._web3.utils.sha3(message)
|
||||
}
|
||||
|
||||
async generateDID(content) {
|
||||
return 'did:ocn:' + (await this.contracts.market.generateId(content)).toString()
|
||||
}
|
||||
|
||||
createAssetDDO(assetDID, assetMetadata) {
|
||||
return {
|
||||
'@context': 'https://w3id.org/did/v1',
|
||||
id: assetDID,
|
||||
publicKey: [],
|
||||
authentication: [],
|
||||
service: [],
|
||||
metadata: assetMetadata
|
||||
}
|
||||
}
|
||||
|
||||
resolveDID(did) {
|
||||
const providerURL = this.contracts.market.resolveAssetDID(did)
|
||||
const metadataGuy = new MetaData(providerURL)
|
||||
return metadataGuy.getAssetDDO(did)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user