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:
parent
4074d95f1e
commit
89dbc10532
@ -5,13 +5,24 @@ import Web3Provider from "../keeper/Web3Provider"
|
||||
import Balance from "../models/Balance"
|
||||
import OceanBase from "./OceanBase"
|
||||
|
||||
/**
|
||||
* Account information.
|
||||
*/
|
||||
export default class Account extends OceanBase {
|
||||
private balance: Balance
|
||||
|
||||
/**
|
||||
* Balance of Ocean Token.
|
||||
* @return {Promise<number>}
|
||||
*/
|
||||
public async getOceanBalance(): Promise<number> {
|
||||
return (await Keeper.getInstance()).token.balanceOf(this.id)
|
||||
}
|
||||
|
||||
/**
|
||||
* Balance of Ether.
|
||||
* @return {Promise<number>}
|
||||
*/
|
||||
public async getEtherBalance(): Promise<number> {
|
||||
// Logger.log("getting balance for", account);
|
||||
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> {
|
||||
|
||||
if (!this.balance) {
|
||||
@ -36,6 +51,11 @@ export default class Account extends OceanBase {
|
||||
return this.balance
|
||||
}
|
||||
|
||||
/**
|
||||
* Request Ocean Tokens.
|
||||
* @param {number} amount Tokens to be requested.
|
||||
* @return {Promise<number>}
|
||||
*/
|
||||
public async requestTokens(amount: number): Promise<number> {
|
||||
await (await Keeper.getInstance())
|
||||
.market
|
||||
@ -43,6 +63,10 @@ export default class Account extends OceanBase {
|
||||
return amount
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the account public key.
|
||||
* @return {Promise<string>}
|
||||
*/
|
||||
public async getPublicKey(): Promise<string> {
|
||||
|
||||
const web3 = Web3Provider.getWeb3()
|
||||
|
@ -2,8 +2,16 @@ import IdGenerator from "./IdGenerator"
|
||||
|
||||
const prefix = "did:op:"
|
||||
|
||||
/**
|
||||
* Decentralized ID.
|
||||
*/
|
||||
export default class DID {
|
||||
|
||||
/**
|
||||
* Parses a DID from a string.
|
||||
* @param {string} didString DID in string.
|
||||
* @return {DID}
|
||||
*/
|
||||
public static parse(didString: string): DID {
|
||||
let did: DID
|
||||
if (didString.startsWith(prefix)) {
|
||||
@ -20,21 +28,36 @@ export default class DID {
|
||||
return did
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new DID.
|
||||
* @return {DID}
|
||||
*/
|
||||
public static generate(): DID {
|
||||
return new DID(IdGenerator.generateId())
|
||||
}
|
||||
|
||||
/**
|
||||
* ID.
|
||||
* @type {string}
|
||||
*/
|
||||
private id: string
|
||||
|
||||
private constructor(id: string) {
|
||||
|
||||
this.id = id
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the DID.
|
||||
* @return {string}
|
||||
*/
|
||||
public getDid(): string {
|
||||
return `${prefix}${this.id}`
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the ID.
|
||||
* @return {string}
|
||||
*/
|
||||
public getId(): string {
|
||||
return this.id
|
||||
}
|
||||
|
@ -27,10 +27,17 @@ import Access from "./ServiceAgreements/Templates/Access"
|
||||
|
||||
import EventListener from "../keeper/EventListener"
|
||||
|
||||
/**
|
||||
* Main interface for Ocean Protocol.
|
||||
*/
|
||||
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) {
|
||||
ConfigProvider.setConfig(config)
|
||||
Ocean.instance = new Ocean()
|
||||
@ -40,13 +47,25 @@ export default class Ocean {
|
||||
return Ocean.instance
|
||||
}
|
||||
|
||||
private static instance = null
|
||||
/**
|
||||
* Ocean instance.
|
||||
* @type {Ocean}
|
||||
*/
|
||||
private static instance: Ocean = null
|
||||
|
||||
/**
|
||||
* Keeper instance.
|
||||
* @type {Keeper}
|
||||
*/
|
||||
private keeper: Keeper
|
||||
|
||||
private constructor() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the list of accounts.
|
||||
* @return {Promise<Account[]>}
|
||||
*/
|
||||
public async getAccounts(): Promise<Account[]> {
|
||||
|
||||
// retrieve eth accounts
|
||||
@ -55,12 +74,22 @@ export default class Ocean {
|
||||
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> {
|
||||
|
||||
const d: DID = DID.parse(did)
|
||||
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> {
|
||||
|
||||
const {didRegistry} = this.keeper
|
||||
@ -153,6 +182,13 @@ export default class Ocean {
|
||||
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,
|
||||
serviceDefinitionId: string,
|
||||
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,
|
||||
serviceDefinitionId: string,
|
||||
serviceAgreementId: string,
|
||||
@ -247,6 +292,16 @@ export default class Ocean {
|
||||
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,
|
||||
serviceDefinitionId: string,
|
||||
serviceAgreementId: string,
|
||||
@ -270,10 +325,21 @@ export default class Ocean {
|
||||
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[]> {
|
||||
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[]> {
|
||||
return AquariusProvider.getAquarius().queryMetadataByText({
|
||||
text,
|
||||
|
Loading…
Reference in New Issue
Block a user