diff --git a/src/Ocean.ts b/src/Ocean.ts deleted file mode 100644 index 2d089cc..0000000 --- a/src/Ocean.ts +++ /dev/null @@ -1,54 +0,0 @@ -import Keeper from "./keeper/Keeper" -import Web3Helper from "./keeper/Web3Helper" -import MetaData from "./metadata" -import Config from "./models/Config" -import Asset from "./ocean/Asset" -import Order from "./ocean/Order" - -export default class Ocean { - - public static async getInstance(config) { - const ocean = new Ocean(config) - ocean.keeper = await Keeper.getInstance(config, ocean.helper) - ocean.order = new Order(ocean.keeper) - ocean.asset = new Asset(ocean.keeper) - return ocean - } - - public order: Order - public asset: Asset - public helper: Web3Helper - public metadata: MetaData - - private keeper: Keeper - private config: Config - - private constructor(config: Config) { - - this.config = config - - this.helper = new Web3Helper(config) - this.metadata = new MetaData(config) - } - - // Transactions with gas cost - public async requestTokens(amount: number, receiver: string): Promise { - return this.keeper.market.requestTokens(amount, receiver) - } - - public async getAccounts() { - const {token} = this.keeper - const {helper} = this - - return Promise.all((await helper.getAccounts()).map(async (account: string) => { - // await ocean.market.requestTokens(account, 1000) - return { - name: account, - balance: { - eth: await token.getEthBalance(account), - ocn: await token.getTokenBalance(account), - }, - } - })) - } -} diff --git a/src/keeper/Auth.ts b/src/keeper/Auth.ts index f504ef6..aa49dac 100644 --- a/src/keeper/Auth.ts +++ b/src/keeper/Auth.ts @@ -1,4 +1,5 @@ import BigNumber from "bignumber.js" +import {Receipt} from "web3-utils" import Asset from "../models/Asset" import Config from "../models/Config" import ContractBaseWrapper from "./ContractWrapperBase" @@ -13,26 +14,31 @@ export default class OceanAuth extends ContractBaseWrapper { } public async getOrderStatus(orderId: string): Promise { - return this.contract.statusOfAccessRequest.call(orderId) + return this.contract.statusOfAccessRequest(orderId) + .call() .then((status: BigNumber) => status.toNumber()) } - public async cancelAccessRequest(orderId: string, senderAddress: string) { - return this.contract.cancelAccessRequest.send(orderId, { - from: senderAddress, - }) + public async cancelAccessRequest(orderId: string, senderAddress: string): Promise { + return this.contract.cancelAccessRequest(orderId) + .send({ + from: senderAddress, + }) } - public async getEncryptedAccessToken(orderId: string, senderAddress: string) { - return this.contract.getEncryptedAccessToken.send(orderId, { - from: senderAddress, - }) + public async getEncryptedAccessToken(orderId: string, senderAddress: string): Promise { + return this.contract.getEncryptedAccessToken(orderId) + .send({ + from: senderAddress, + }) } - public async initiateAccessRequest(asset: Asset, publicKey: string, timeout, buyerAddress: string) { - return this.contract.initiateAccessRequest.send( - asset.assetId, asset.publisherId, publicKey, timeout, { - from: buyerAddress, gas: this.config.defaultGas, + public async initiateAccessRequest(asset: Asset, publicKey: string, + timeout, buyerAddress: string): Promise { + return this.contract.initiateAccessRequest(asset.assetId, asset.publisherId, publicKey, timeout) + .send({ + from: buyerAddress, + gas: this.config.defaultGas, }) } } diff --git a/src/keeper/ContractHandler.ts b/src/keeper/ContractHandler.ts index d9278a3..9357833 100644 --- a/src/keeper/ContractHandler.ts +++ b/src/keeper/ContractHandler.ts @@ -1,12 +1,12 @@ -import {Contract} from "web3-utils" +import Contract from "web3-eth-contract" import Logger from "../utils/Logger" import Web3Helper from "./Web3Helper" -const contracts: Map = new Map() +const contracts: Map = new Map() export default class ContractHandler { - public static async get(what: string, web3Helper: Web3Helper) { + public static async get(what: string, web3Helper: Web3Helper): Contract { return contracts.get(what) || await ContractHandler.load(what, web3Helper) } @@ -47,7 +47,7 @@ export default class ContractHandler { }) } - private static async load(what: string, web3Helper: Web3Helper): Promise { + private static async load(what: string, web3Helper: Web3Helper): Promise { const where = (await web3Helper.getNetworkName()).toLowerCase() Logger.log("Loading", what, "from", where) try { @@ -79,7 +79,12 @@ export default class ContractHandler { private static async deployContract(web3, name, from, params?): Promise { - let contractInstance + // dont redeploy if there is already something loaded + if (contracts.has(name)) { + return contracts.get(name) + } + + let contractInstance: Contract try { Logger.log("Deploying", name) diff --git a/src/keeper/ContractWrapperBase.ts b/src/keeper/ContractWrapperBase.ts index a25772a..e7aa9d4 100644 --- a/src/keeper/ContractWrapperBase.ts +++ b/src/keeper/ContractWrapperBase.ts @@ -1,3 +1,4 @@ +import Contract from "web3-eth-contract" import Config from "../models/Config" import Logger from "../utils/Logger" import ContractHandler from "./ContractHandler" @@ -5,7 +6,7 @@ import Web3Helper from "./Web3Helper" export default class ContractWrapperBase { - protected contract: any = null + protected contract: Contract = null protected config: Config protected web3Helper: Web3Helper @@ -53,7 +54,7 @@ export default class ContractWrapperBase { } public getAddress() { - return this.contract.address + return this.contract.options.address } } diff --git a/src/keeper/Market.ts b/src/keeper/Market.ts index 476c9bb..6564efe 100644 --- a/src/keeper/Market.ts +++ b/src/keeper/Market.ts @@ -1,3 +1,5 @@ +import BigNumber from "bignumber.js" +import {Receipt} from "web3-utils" import Asset from "../models/Asset" import Config from "../models/Config" import Order from "../models/Order" @@ -16,41 +18,43 @@ export default class OceanMarket extends ContractWrapperBase { // call functions (costs no gas) public async isAssetActive(assetId: string): Promise { - return this.contract.methods.checkAsset(assetId).call + return this.contract.methods.checkAsset(assetId).call() } public async verifyOrderPayment(orderId: string): Promise { - return this.contract.methods.verifyPaymentReceived(orderId).call + return this.contract.methods.verifyPaymentReceived(orderId).call() } public async getAssetPrice(assetId: string): Promise { - return this.contract.methods.getAssetPrice(assetId).call().then((result) => result.toNumber()) + return this.contract.methods.getAssetPrice(assetId) + .call() + .then((price: string) => new BigNumber(price).toNumber()) } - public async requestTokens(amount: number, receiverAddress: string): Promise { - return this.contract.methods.requestTokens(amount).send({ - from: receiverAddress, - }) + public async requestTokens(amount: number, receiverAddress: string): Promise { + return this.contract.methods.requestTokens(amount) + .send({ + from: receiverAddress, + }) } - public async registerAsset(name: string, description: string, - price: number, publisherAddress: string): Promise { - const assetId = await this.contract.methods.generateId(name + description).call() - Logger.log("Registering: ", assetId) - const result = await this.contract.methods.register(assetId, price).send({ + public async generateId(input: string): Promise { + return await this.contract.methods.generateId(input).call() + } + + public async register(assetId: string, price: number, publisherAddress: string): Promise { + return await this.contract.methods.register(assetId, price) + .send({ from: publisherAddress, gas: this.config.defaultGas, - }, - ) - Logger.log("Registered: ", result) - return assetId + }) } - public async payAsset(asset: Asset, order: Order, buyerAddress: string): Promise { - Logger.log("Sending payment") - return this.contract.methods.sendPayment(order.id, asset.publisherId, asset.price, order.timeout).send({ - from: buyerAddress, - gas: this.config.defaultGas, - }) + public async payAsset(asset: Asset, order: Order, buyerAddress: string): Promise { + return this.contract.methods.sendPayment(order.id, asset.publisherId, asset.price, order.timeout) + .send({ + from: buyerAddress, + gas: this.config.defaultGas, + }) } } diff --git a/src/keeper/Token.ts b/src/keeper/Token.ts index f254026..a25c20e 100644 --- a/src/keeper/Token.ts +++ b/src/keeper/Token.ts @@ -1,3 +1,5 @@ +import BigNumber from "bignumber.js" +import {Receipt} from "web3-utils" import Config from "../models/Config" import ContractBaseWrapper from "./ContractWrapperBase" import Web3Helper from "./Web3Helper" @@ -10,27 +12,17 @@ export default class OceanToken extends ContractBaseWrapper { return token } - public async getTokenBalance(accountAddress: string) { - return this.contract.methods.balanceOf(accountAddress).call() - } - - public async getEthBalance(account: string): Promise { - return new Promise((resolve, reject) => { - // Logger.log("getting balance for", account); - this.web3Helper.getWeb3().eth.getBalance(account, "latest", (err: any, balance: number) => { - if (err) { - return reject(err) - } - // Logger.log("balance", balance); - resolve(balance) + public async approve(marketAddress: string, price: number, buyerAddress: string): Promise { + return this.contract.methods.approve(marketAddress, price) + .send({ + from: buyerAddress, + gas: this.config.defaultGas, }) - }) } - public async approve(marketAddress: string, price: number, buyerAddress: string) { - return this.contract.methods.approve(marketAddress, price).send({ - from: buyerAddress, - gas: this.config.defaultGas, - }) + public async balanceOf(address: string): Promise { + return this.contract.methods.balanceOf(address) + .call() + .then((balance: string) => new BigNumber(balance).toNumber()) } } diff --git a/src/ocean/Account.ts b/src/ocean/Account.ts new file mode 100644 index 0000000..3b6b289 --- /dev/null +++ b/src/ocean/Account.ts @@ -0,0 +1,46 @@ +import BigNumber from "bignumber.js" +import Keeper from "../keeper/Keeper" +import Web3Helper from "../keeper/Web3Helper" + +export default class Account { + + private keeper: Keeper + + constructor(keeper: Keeper) { + this.keeper = keeper + } + + public async getTokenBalance(accountAddress: string): Promise { + return this.keeper.token.balanceOf(accountAddress) + } + + public async getEthBalance(account: string): Promise { + const {web3Helper} = this.keeper + // Logger.log("getting balance for", account); + return web3Helper.getWeb3().eth.getBalance(account, "latest") + .then((balance: string) => { + // Logger.log("balance", balance); + return new BigNumber(balance).toNumber() + }) + } + + public async list() { + const {web3Helper} = this.keeper + + return Promise.all((await web3Helper.getAccounts()).map(async (account: string) => { + // await ocean.market.requestTokens(account, 1000) + return { + name: account, + balance: { + eth: await this.getEthBalance(account), + ocn: await this.getTokenBalance(account), + }, + } + })) + } + + // Transactions with gas cost + public async requestTokens(amount: number, receiver: string): Promise { + return this.keeper.market.requestTokens(amount, receiver) + } +} diff --git a/src/ocean/Asset.ts b/src/ocean/Asset.ts index 9f5a076..9cead00 100644 --- a/src/ocean/Asset.ts +++ b/src/ocean/Asset.ts @@ -1,4 +1,5 @@ import Keeper from "../keeper/Keeper" +import Logger from "../utils/Logger" export default class Asset { @@ -8,9 +9,24 @@ export default class Asset { this.keeper = keeper } - public isAssetActive(assetId: string): Promise { + public async isAssetActive(assetId: string): Promise { const {market} = this.keeper return market.isAssetActive(assetId) } + public async registerAsset(name: string, description: string, + price: number, publisherAddress: string): Promise { + const {market} = this.keeper + + // generate an id + const assetId = await market.generateId(name + description) + Logger.log("Registering: ", assetId) + + // register asset in the market + const result = await market.register(assetId, price, publisherAddress) + Logger.log("Registered: ", assetId, "in block", result.blockNumber) + + return assetId + } + } diff --git a/src/metadata.ts b/src/ocean/MetaData.ts similarity index 94% rename from src/metadata.ts rename to src/ocean/MetaData.ts index 1acee86..2794ac3 100644 --- a/src/metadata.ts +++ b/src/ocean/MetaData.ts @@ -1,5 +1,5 @@ -import Config from "./models/Config" -import Logger from "./utils/Logger" +import Config from "../models/Config" +import Logger from "../utils/Logger" declare var fetch diff --git a/src/ocean/Ocean.ts b/src/ocean/Ocean.ts new file mode 100644 index 0000000..5761514 --- /dev/null +++ b/src/ocean/Ocean.ts @@ -0,0 +1,39 @@ +import Keeper from "../keeper/Keeper" +import Web3Helper from "../keeper/Web3Helper" +import Config from "../models/Config" +import Account from "./Account" +import Asset from "./Asset" +import MetaData from "./metadata" +import Order from "./Order" +import Tribe from "./Tribe" + +export default class Ocean { + + public static async getInstance(config) { + const ocean = new Ocean(config) + ocean.keeper = await Keeper.getInstance(config, ocean.helper) + ocean.tribe = await Tribe.getInstance(ocean.helper) + ocean.order = new Order(ocean.keeper) + ocean.account = new Account(ocean.keeper) + ocean.asset = new Asset(ocean.keeper) + return ocean + } + + public account: Account + public order: Order + public tribe: Tribe + public asset: Asset + public helper: Web3Helper + public metadata: MetaData + + private keeper: Keeper + private config: Config + + private constructor(config: Config) { + + this.config = config + + this.helper = new Web3Helper(config) + this.metadata = new MetaData(config) + } +} diff --git a/src/Tribe.ts b/src/ocean/Tribe.ts similarity index 91% rename from src/Tribe.ts rename to src/ocean/Tribe.ts index 9060c4a..552b918 100644 --- a/src/Tribe.ts +++ b/src/ocean/Tribe.ts @@ -1,4 +1,4 @@ -import Web3Helper from "./keeper/Web3Helper" +import Web3Helper from "../keeper/Web3Helper" export default class Tribe { diff --git a/src/squid.ts b/src/squid.ts index 1c6b895..5d371a8 100644 --- a/src/squid.ts +++ b/src/squid.ts @@ -1,4 +1,4 @@ -import Ocean from "./Ocean" +import Ocean from "./ocean/Ocean" import Logger from "./utils/Logger" export {