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

added comments to main Ocean class #79

This commit is contained in:
Pedro Gutiérrez 2019-01-09 16:17:23 +01:00
parent 4074d95f1e
commit 89dbc10532
3 changed files with 118 additions and 5 deletions

View File

@ -5,13 +5,24 @@ import Web3Provider from "../keeper/Web3Provider"
import Balance from "../models/Balance" import Balance from "../models/Balance"
import OceanBase from "./OceanBase" import OceanBase from "./OceanBase"
/**
* Account information.
*/
export default class Account extends OceanBase { export default class Account extends OceanBase {
private balance: Balance private balance: Balance
/**
* Balance of Ocean Token.
* @return {Promise<number>}
*/
public async getOceanBalance(): Promise<number> { public async getOceanBalance(): Promise<number> {
return (await Keeper.getInstance()).token.balanceOf(this.id) return (await Keeper.getInstance()).token.balanceOf(this.id)
} }
/**
* Balance of Ether.
* @return {Promise<number>}
*/
public async getEtherBalance(): Promise<number> { public async getEtherBalance(): Promise<number> {
// Logger.log("getting balance for", account); // Logger.log("getting balance for", account);
return Web3Provider return Web3Provider
@ -24,6 +35,10 @@ export default class Account extends OceanBase {
}) })
} }
/**
* Balances of Ether and Ocean Token.
* @return {Promise<Balance>}
*/
public async getBalance(): Promise<Balance> { public async getBalance(): Promise<Balance> {
if (!this.balance) { if (!this.balance) {
@ -36,6 +51,11 @@ export default class Account extends OceanBase {
return this.balance return this.balance
} }
/**
* Request Ocean Tokens.
* @param {number} amount Tokens to be requested.
* @return {Promise<number>}
*/
public async requestTokens(amount: number): Promise<number> { public async requestTokens(amount: number): Promise<number> {
await (await Keeper.getInstance()) await (await Keeper.getInstance())
.market .market
@ -43,6 +63,10 @@ export default class Account extends OceanBase {
return amount return amount
} }
/**
* Returns the account public key.
* @return {Promise<string>}
*/
public async getPublicKey(): Promise<string> { public async getPublicKey(): Promise<string> {
const web3 = Web3Provider.getWeb3() const web3 = Web3Provider.getWeb3()

View File

@ -2,8 +2,16 @@ import IdGenerator from "./IdGenerator"
const prefix = "did:op:" const prefix = "did:op:"
/**
* Decentralized ID.
*/
export default class DID { export default class DID {
/**
* Parses a DID from a string.
* @param {string} didString DID in string.
* @return {DID}
*/
public static parse(didString: string): DID { public static parse(didString: string): DID {
let did: DID let did: DID
if (didString.startsWith(prefix)) { if (didString.startsWith(prefix)) {
@ -20,21 +28,36 @@ export default class DID {
return did return did
} }
/**
* Returns a new DID.
* @return {DID}
*/
public static generate(): DID { public static generate(): DID {
return new DID(IdGenerator.generateId()) return new DID(IdGenerator.generateId())
} }
/**
* ID.
* @type {string}
*/
private id: string private id: string
private constructor(id: string) { private constructor(id: string) {
this.id = id this.id = id
} }
/**
* Returns the DID.
* @return {string}
*/
public getDid(): string { public getDid(): string {
return `${prefix}${this.id}` return `${prefix}${this.id}`
} }
/**
* Returns the ID.
* @return {string}
*/
public getId(): string { public getId(): string {
return this.id return this.id
} }

View File

@ -27,10 +27,17 @@ import Access from "./ServiceAgreements/Templates/Access"
import EventListener from "../keeper/EventListener" import EventListener from "../keeper/EventListener"
/**
* Main interface for Ocean Protocol.
*/
export default class Ocean { export default class Ocean {
public static async getInstance(config: Config) { /**
* Returns the instance of Ocean.
* @param {Config} config Ocean instance configuration.
* @return {Promise<Ocean>}
*/
public static async getInstance(config: Config): Promise<Ocean> {
if (!Ocean.instance) { if (!Ocean.instance) {
ConfigProvider.setConfig(config) ConfigProvider.setConfig(config)
Ocean.instance = new Ocean() Ocean.instance = new Ocean()
@ -40,13 +47,25 @@ export default class Ocean {
return Ocean.instance return Ocean.instance
} }
private static instance = null /**
* Ocean instance.
* @type {Ocean}
*/
private static instance: Ocean = null
/**
* Keeper instance.
* @type {Keeper}
*/
private keeper: Keeper private keeper: Keeper
private constructor() { private constructor() {
} }
/**
* Returns the list of accounts.
* @return {Promise<Account[]>}
*/
public async getAccounts(): Promise<Account[]> { public async getAccounts(): Promise<Account[]> {
// retrieve eth accounts // retrieve eth accounts
@ -55,12 +74,22 @@ export default class Ocean {
return ethAccounts.map((address: string) => new Account(address)) return ethAccounts.map((address: string) => new Account(address))
} }
/**
* Returns a DDO by DID.
* @param {string} did Decentralized ID.
* @return {Promise<DDO>}
*/
public async resolveDID(did: string): Promise<DDO> { public async resolveDID(did: string): Promise<DDO> {
const d: DID = DID.parse(did) const d: DID = DID.parse(did)
return AquariusProvider.getAquarius().retrieveDDO(d) return AquariusProvider.getAquarius().retrieveDDO(d)
} }
/**
* Registers a new DDO.
* @param {MetaData} metadata DDO metadata.
* @param {Account} publisher Publicher account.
* @return {Promise<DDO>}
*/
public async registerAsset(metadata: MetaData, publisher: Account): Promise<DDO> { public async registerAsset(metadata: MetaData, publisher: Account): Promise<DDO> {
const {didRegistry} = this.keeper const {didRegistry} = this.keeper
@ -153,6 +182,13 @@ export default class Ocean {
return storedDdo return storedDdo
} }
/**
* Signs a service agreement by DID.
* @param {string} did Decentralized ID.
* @param {string} serviceDefinitionId Service definition ID.
* @param {Account} consumer Consumer account.
* @return {Promise<any>}
*/
public async signServiceAgreement(did: string, public async signServiceAgreement(did: string,
serviceDefinitionId: string, serviceDefinitionId: string,
consumer: Account): Promise<any> { consumer: Account): Promise<any> {
@ -201,6 +237,15 @@ export default class Ocean {
} }
} }
/**
* Creates a new service agreement.
* @param {string} did Decentralized ID.
* @param {string} serviceDefinitionId Service definition ID.
* @param {string} serviceAgreementId Service agreement ID.
* @param {string} serviceAgreementSignature Service agreement signature.
* @param {Function} cb Callback executen when the access is granted.
* @param {Account} consumer Consumer account.
*/
public async initializeServiceAgreement(did: string, public async initializeServiceAgreement(did: string,
serviceDefinitionId: string, serviceDefinitionId: string,
serviceAgreementId: string, serviceAgreementId: string,
@ -247,6 +292,16 @@ export default class Ocean {
consumer.getId()) consumer.getId())
} }
/**
* Executes a service agreement.
* @param {string} did Decentralized ID.
* @param {string} serviceDefinitionId Service definition ID.
* @param {string} serviceAgreementId Service agreement ID.
* @param {string} serviceAgreementSignature Service agreement signature.
* @param {Account} consumer Consumer account.
* @param {Account} publisher Publisher account.
* @return {Promise<ServiceAgreement>}
*/
public async executeServiceAgreement(did: string, public async executeServiceAgreement(did: string,
serviceDefinitionId: string, serviceDefinitionId: string,
serviceAgreementId: string, serviceAgreementId: string,
@ -270,10 +325,21 @@ export default class Ocean {
return serviceAgreement return serviceAgreement
} }
/**
* Search over the assets using a query.
* @param {SearchQuery} query Query to filter the assets.
* @return {Promise<DDO[]>}
*/
public async searchAssets(query: SearchQuery): Promise<DDO[]> { public async searchAssets(query: SearchQuery): Promise<DDO[]> {
return AquariusProvider.getAquarius().queryMetadata(query) return AquariusProvider.getAquarius().queryMetadata(query)
} }
/**
* Search over the assets using a keyword.
* @param {SearchQuery} text Text to filter the assets.
* @return {Promise<DDO[]>}
*/
public async searchAssetsByText(text: string): Promise<DDO[]> { public async searchAssetsByText(text: string): Promise<DDO[]> {
return AquariusProvider.getAquarius().queryMetadataByText({ return AquariusProvider.getAquarius().queryMetadataByText({
text, text,