diff --git a/CHANGELOG.md b/CHANGELOG.md index 0a80e53f..d31691b9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,12 +4,30 @@ All notable changes to this project will be documented in this file. Dates are d Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog). +#### [v0.7.0](https://github.com/oceanprotocol/ocean.js/compare/v0.6.7...v0.7.0) + +- make create methods subscribable [`#389`](https://github.com/oceanprotocol/ocean.js/pull/389) +- Bump @types/node from 14.11.10 to 14.14.0 [`#383`](https://github.com/oceanprotocol/ocean.js/pull/383) +- make pool.create subscribable [`dec9c57`](https://github.com/oceanprotocol/ocean.js/commit/dec9c57c94f777201355c1356c881989002931c1) +- change exchange.create to allow subscribe&approve [`11271a9`](https://github.com/oceanprotocol/ocean.js/commit/11271a970b6fd692e26cf398973244ae1e153b9b) +- pool.create returns txid [`4db0a0d`](https://github.com/oceanprotocol/ocean.js/commit/4db0a0d43d25aaefba121985dc00f5dad570db11) + +#### [v0.6.7](https://github.com/oceanprotocol/ocean.js/compare/v0.6.6...v0.6.7) + +> 21 October 2020 + +- Update .travis.yml [`#384`](https://github.com/oceanprotocol/ocean.js/pull/384) +- Release 0.6.7 [`f5f9e33`](https://github.com/oceanprotocol/ocean.js/commit/f5f9e3308def5daea18cb4f2d2b9a2742c602fdb) + #### [v0.6.6](https://github.com/oceanprotocol/ocean.js/compare/v0.6.5...v0.6.6) +> 20 October 2020 + - add datatokeninfo to ddo creation [`#382`](https://github.com/oceanprotocol/ocean.js/pull/382) - fix getOrderHistory [`#381`](https://github.com/oceanprotocol/ocean.js/pull/381) - Bump @typescript-eslint/parser from 4.4.1 to 4.5.0 [`#378`](https://github.com/oceanprotocol/ocean.js/pull/378) - Bump @typescript-eslint/eslint-plugin from 4.4.1 to 4.5.0 [`#379`](https://github.com/oceanprotocol/ocean.js/pull/379) +- Release 0.6.6 [`461ea5c`](https://github.com/oceanprotocol/ocean.js/commit/461ea5c46ab8f419f5d95a59a7237052b1192edd) - fix tests [`efaa3f6`](https://github.com/oceanprotocol/ocean.js/commit/efaa3f6d1d6b425cb9faa1a4d113d9272909cf7e) #### [v0.6.5](https://github.com/oceanprotocol/ocean.js/compare/v0.6.4...v0.6.5) diff --git a/package-lock.json b/package-lock.json index 2ebf6fa8..b6249933 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@oceanprotocol/lib", - "version": "0.6.6", + "version": "0.7.0", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -1465,9 +1465,9 @@ "dev": true }, "@types/node": { - "version": "14.11.10", - "resolved": "https://registry.npmjs.org/@types/node/-/node-14.11.10.tgz", - "integrity": "sha512-yV1nWZPlMFpoXyoknm4S56y2nlTAuFYaJuQtYRAOU7xA/FJ9RY0Xm7QOkaYMMmr8ESdHIuUb6oQgR/0+2NqlyA==" + "version": "14.14.0", + "resolved": "https://registry.npmjs.org/@types/node/-/node-14.14.0.tgz", + "integrity": "sha512-BfbIHP9IapdupGhq/hc+jT5dyiBVZ2DdeC5WwJWQWDb0GijQlzUFAeIQn/2GtvZcd2HVUU7An8felIICFTC2qg==" }, "@types/node-fetch": { "version": "2.5.7", @@ -1772,79 +1772,6 @@ } } }, - "@typescript-eslint/scope-manager": { - "version": "4.4.1", - "resolved": "https://registry.npmjs.org/@typescript-eslint/scope-manager/-/scope-manager-4.4.1.tgz", - "integrity": "sha512-2oD/ZqD4Gj41UdFeWZxegH3cVEEH/Z6Bhr/XvwTtGv66737XkR4C9IqEkebCuqArqBJQSj4AgNHHiN1okzD/wQ==", - "dev": true, - "requires": { - "@typescript-eslint/types": "4.4.1", - "@typescript-eslint/visitor-keys": "4.4.1" - } - }, - "@typescript-eslint/types": { - "version": "4.4.1", - "resolved": "https://registry.npmjs.org/@typescript-eslint/types/-/types-4.4.1.tgz", - "integrity": "sha512-KNDfH2bCyax5db+KKIZT4rfA8rEk5N0EJ8P0T5AJjo5xrV26UAzaiqoJCxeaibqc0c/IvZxp7v2g3difn2Pn3w==", - "dev": true - }, - "@typescript-eslint/typescript-estree": { - "version": "4.4.1", - "resolved": "https://registry.npmjs.org/@typescript-eslint/typescript-estree/-/typescript-estree-4.4.1.tgz", - "integrity": "sha512-wP/V7ScKzgSdtcY1a0pZYBoCxrCstLrgRQ2O9MmCUZDtmgxCO/TCqOTGRVwpP4/2hVfqMz/Vw1ZYrG8cVxvN3g==", - "dev": true, - "requires": { - "@typescript-eslint/types": "4.4.1", - "@typescript-eslint/visitor-keys": "4.4.1", - "debug": "^4.1.1", - "globby": "^11.0.1", - "is-glob": "^4.0.1", - "lodash": "^4.17.15", - "semver": "^7.3.2", - "tsutils": "^3.17.1" - }, - "dependencies": { - "debug": { - "version": "4.2.0", - "resolved": "https://registry.npmjs.org/debug/-/debug-4.2.0.tgz", - "integrity": "sha512-IX2ncY78vDTjZMFUdmsvIRFY2Cf4FnD0wRs+nQwJU8Lu99/tPFdb0VybiiMTPe3I6rQmwsqQqRBvxU+bZ/I8sg==", - "dev": true, - "requires": { - "ms": "2.1.2" - } - }, - "ms": { - "version": "2.1.2", - "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", - "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==", - "dev": true - }, - "semver": { - "version": "7.3.2", - "resolved": "https://registry.npmjs.org/semver/-/semver-7.3.2.tgz", - "integrity": "sha512-OrOb32TeeambH6UrhtShmF7CRDqhL6/5XpPNp2DuRH6+9QLw/orhp72j87v8Qa1ScDkvrrBNpZcDejAirJmfXQ==", - "dev": true - } - } - }, - "@typescript-eslint/visitor-keys": { - "version": "4.4.1", - "resolved": "https://registry.npmjs.org/@typescript-eslint/visitor-keys/-/visitor-keys-4.4.1.tgz", - "integrity": "sha512-H2JMWhLaJNeaylSnMSQFEhT/S/FsJbebQALmoJxMPMxLtlVAMy2uJP/Z543n9IizhjRayLSqoInehCeNW9rWcw==", - "dev": true, - "requires": { - "@typescript-eslint/types": "4.4.1", - "eslint-visitor-keys": "^2.0.0" - }, - "dependencies": { - "eslint-visitor-keys": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/eslint-visitor-keys/-/eslint-visitor-keys-2.0.0.tgz", - "integrity": "sha512-QudtT6av5WXels9WjIM7qz1XD1cWGvX4gGXvp/zBn9nXG02D0utdU3Em2m/QjTnrsk6bBjmCygl3rmj118msQQ==", - "dev": true - } - } - }, "abstract-leveldown": { "version": "2.6.3", "resolved": "https://registry.npmjs.org/abstract-leveldown/-/abstract-leveldown-2.6.3.tgz", diff --git a/package.json b/package.json index a18ae99a..11876a9b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@oceanprotocol/lib", - "version": "0.6.6", + "version": "0.7.0", "description": "JavaScript client library for Ocean Protocol", "main": "./dist/node/lib.js", "typings": "./dist/node/lib.d.ts", diff --git a/src/balancer/OceanPool.ts b/src/balancer/OceanPool.ts index 6a0a1ea3..d09bcbd4 100644 --- a/src/balancer/OceanPool.ts +++ b/src/balancer/OceanPool.ts @@ -5,6 +5,7 @@ import { Pool } from './Pool' import { EventData, Filter } from 'web3-eth-contract' import BigNumber from 'bignumber.js' import { Logger } from '../utils' +import { SubscribablePromise } from '../utils' declare type PoolTransactionType = 'swap' | 'join' | 'exit' @@ -35,6 +36,13 @@ export interface PoolTransaction { type: PoolTransactionType } +export enum PoolCreateProgressStep { + CreatingPool, + ApprovingDatatoken, + ApprovingOcean, + SetupPool +} + /** * Ocean Pools submodule exposed under ocean.pool */ @@ -66,13 +74,13 @@ export class OceanPool extends Pool { * @param {String} fee Swap fee. E.g. to get a 0.1% swapFee use `0.001`. The maximum allowed swapFee is `0.1` (10%). * @return {String} */ - public async createDTPool( + public create( account: string, token: string, amount: string, weight: string, fee: string - ): Promise { + ): SubscribablePromise { if (this.oceanAddress == null) { this.logger.error('ERROR: oceanAddress is not defined') return null @@ -85,47 +93,58 @@ export class OceanPool extends Pool { this.logger.error('ERROR: Weight out of bounds (min 1, max9)') return null } - const address = await super.createPool(account) - const oceanWeight = 10 - parseFloat(weight) - const oceanAmount = (parseFloat(amount) * oceanWeight) / parseFloat(weight) - this.dtAddress = token - let txid - txid = await this.approve( - account, - token, - address, - this.web3.utils.toWei(String(amount)) - ) - if (!txid) { - this.logger.error('ERROR: Failed to call approve DT token') - return null - } - txid = await this.approve( - account, - this.oceanAddress, - address, - this.web3.utils.toWei(String(oceanAmount)) - ) - if (!txid) { - this.logger.error('ERROR: Failed to call approve OCEAN token') - return null - } - txid = await super.setup( - account, - address, - token, - this.web3.utils.toWei(String(amount)), - this.web3.utils.toWei(String(weight)), - this.oceanAddress, - this.web3.utils.toWei(String(oceanAmount)), - this.web3.utils.toWei(String(oceanWeight)), - this.web3.utils.toWei(fee) - ) - if (!txid) { - this.logger.error('ERROR: Failed to create a new pool') - return null - } - return address + return new SubscribablePromise(async (observer) => { + observer.next(PoolCreateProgressStep.CreatingPool) + const createTxid = await super.createPool(account) + if (!createTxid) { + this.logger.error('ERROR: Failed to call approve DT token') + return null + } + const address = createTxid.events.BPoolRegistered.returnValues[0] + const oceanWeight = 10 - parseFloat(weight) + const oceanAmount = (parseFloat(amount) * oceanWeight) / parseFloat(weight) + this.dtAddress = token + observer.next(PoolCreateProgressStep.ApprovingDatatoken) + let txid + txid = await this.approve( + account, + token, + address, + this.web3.utils.toWei(String(amount)) + ) + if (!txid) { + this.logger.error('ERROR: Failed to call approve DT token') + return null + } + observer.next(PoolCreateProgressStep.ApprovingOcean) + txid = await this.approve( + account, + this.oceanAddress, + address, + this.web3.utils.toWei(String(oceanAmount)) + ) + if (!txid) { + this.logger.error('ERROR: Failed to call approve OCEAN token') + return null + } + observer.next(PoolCreateProgressStep.SetupPool) + txid = await super.setup( + account, + address, + token, + this.web3.utils.toWei(String(amount)), + this.web3.utils.toWei(String(weight)), + this.oceanAddress, + this.web3.utils.toWei(String(oceanAmount)), + this.web3.utils.toWei(String(oceanWeight)), + this.web3.utils.toWei(fee) + ) + if (!txid) { + this.logger.error('ERROR: Failed to create a new pool') + return null + } + return createTxid + }) } /** diff --git a/src/balancer/Pool.ts b/src/balancer/Pool.ts index 3f84d08b..ea6c3f3d 100644 --- a/src/balancer/Pool.ts +++ b/src/balancer/Pool.ts @@ -37,7 +37,7 @@ export class Pool extends PoolFactory { /** * Creates a new pool */ - async createPool(account: string): Promise { + async createPool(account: string): Promise { return await super.createPool(account) } diff --git a/src/balancer/PoolFactory.ts b/src/balancer/PoolFactory.ts index 77ba5d99..886bd596 100644 --- a/src/balancer/PoolFactory.ts +++ b/src/balancer/PoolFactory.ts @@ -2,6 +2,7 @@ import Web3 from 'web3' import { AbiItem } from 'web3-utils/types' import { Logger } from '../utils' import jsonFactoryABI from '@oceanprotocol/contracts/artifacts/BFactory.json' +import { TransactionReceipt } from 'web3-core' export class PoolFactory { public GASLIMIT_DEFAULT = 5000000 @@ -31,7 +32,7 @@ export class PoolFactory { /** * Creates a new pool */ - async createPool(account: string): Promise { + async createPool(account: string): Promise { if (this.web3 === null) { this.logger.error('ERROR: Web3 object is null') return null @@ -45,18 +46,15 @@ export class PoolFactory { const factory = new this.web3.eth.Contract(this.factoryABI, this.factoryAddress, { from: account }) - - const transactiondata = await factory.methods - .newBPool() - .send({ from: account, gas: this.GASLIMIT_DEFAULT }) - - let pooladdress: string - + let txid = null try { - pooladdress = transactiondata.events.BPoolRegistered.returnValues[0] + txid = await factory.methods + .newBPool() + .send({ from: account, gas: this.GASLIMIT_DEFAULT }) + // pooladdress = transactiondata.events.BPoolRegistered.returnValues[0] } catch (e) { this.logger.error(`ERROR: Failed to create new pool: ${e.message}`) } - return pooladdress + return txid } } diff --git a/src/exchange/FixedRateExchange.ts b/src/exchange/FixedRateExchange.ts index 5c7f9f2a..2e6e004e 100644 --- a/src/exchange/FixedRateExchange.ts +++ b/src/exchange/FixedRateExchange.ts @@ -5,6 +5,8 @@ import { TransactionReceipt } from 'web3-core' import { Contract, EventData } from 'web3-eth-contract' import { AbiItem } from 'web3-utils/types' import Web3 from 'web3' +import { SubscribablePromise } from '../utils' +import { DataTokens } from '../datatokens/Datatokens' export interface FixedPriceExchange { exchangeID?: string @@ -22,6 +24,12 @@ export interface FixedPriceSwap { baseTokenAmount: string dataTokenAmount: string } + +export enum FixedRateCreateProgressStep { + CreatingExchange, + ApprovingDatatoken +} + const DEFAULT_GAS_LIMIT = 300000 export class OceanFixedRateExchange { @@ -32,6 +40,7 @@ export class OceanFixedRateExchange { public web3: Web3 public contract: Contract = null private logger: Logger + public datatokens: DataTokens /** * Instantiate FixedRateExchange @@ -45,13 +54,15 @@ export class OceanFixedRateExchange { logger: Logger, fixedRateExchangeAddress: string = null, fixedRateExchangeABI: AbiItem | AbiItem[] = null, - oceanAddress: string = null + oceanAddress: string = null, + datatokens: DataTokens ) { this.web3 = web3 this.fixedRateExchangeAddress = fixedRateExchangeAddress this.fixedRateExchangeABI = fixedRateExchangeABI || (defaultFixedRateExchangeABI.abi as AbiItem[]) this.oceanAddress = oceanAddress + this.datatokens = datatokens if (web3) this.contract = new this.web3.eth.Contract( this.fixedRateExchangeABI, @@ -65,39 +76,52 @@ export class OceanFixedRateExchange { * @param {String} dataToken Data Token Contract Address * @param {Number} rate exchange rate * @param {String} address User address - * @return {Promise} exchangeId + * @param {String} amount Optional, amount of datatokens to be approved for the exchange + * @return {Promise} TransactionReceipt */ - public async create(dataToken: string, rate: string, address: string): Promise { - let estGas - try { - /* estGas = await this.contract.methods - .create(this.oceanAddress, dataToken, this.web3.utils.toWei(rate)) - .estimateGas(function (err, g) { - if (err) { - return DEFAULT_GAS_LIMIT - } else { - return g - } - }) - */ - estGas = DEFAULT_GAS_LIMIT - } catch (e) { - estGas = DEFAULT_GAS_LIMIT - } - - let exchangeId = null - try { - const trxReceipt = await this.contract.methods - .create(this.oceanAddress, dataToken, this.web3.utils.toWei(rate)) - .send({ - from: address, - gas: estGas + 1 - }) - exchangeId = trxReceipt.events.ExchangeCreated.returnValues[0] - } catch (e) { - this.logger.error(`ERROR: Failed to create new exchange: ${e.message}`) - } - return exchangeId + public create( + dataToken: string, + rate: string, + address: string, + amount?: string + ): SubscribablePromise { + return new SubscribablePromise(async (observer) => { + observer.next(FixedRateCreateProgressStep.CreatingExchange) + let estGas + try { + /* estGas = await this.contract.methods + .create(this.oceanAddress, dataToken, this.web3.utils.toWei(rate)) + .estimateGas(function (err, g) { + if (err) { + return DEFAULT_GAS_LIMIT + } else { + return g + } + }) + */ + estGas = DEFAULT_GAS_LIMIT + } catch (e) { + estGas = DEFAULT_GAS_LIMIT + } + let exchangeId = null + let trxReceipt = null + try { + trxReceipt = await this.contract.methods + .create(this.oceanAddress, dataToken, this.web3.utils.toWei(rate)) + .send({ + from: address, + gas: estGas + 1 + }) + exchangeId = trxReceipt.events.ExchangeCreated.returnValues[0] + } catch (e) { + this.logger.error(`ERROR: Failed to create new exchange: ${e.message}`) + } + if (amount && exchangeId) { + observer.next(FixedRateCreateProgressStep.ApprovingDatatoken) + this.datatokens.approve(dataToken, this.fixedRateExchangeAddress, amount, address) + } + return trxReceipt + }) } /** diff --git a/src/ocean/Ocean.ts b/src/ocean/Ocean.ts index a9f4a732..4318e0db 100644 --- a/src/ocean/Ocean.ts +++ b/src/ocean/Ocean.ts @@ -66,7 +66,8 @@ export class Ocean extends Instantiable { instanceConfig.logger, instanceConfig.config.fixedRateExchangeAddress, instanceConfig.config.fixedRateExchangeAddressABI, - instanceConfig.config.oceanTokenAddress + instanceConfig.config.oceanTokenAddress, + instance.datatokens ) instance.OnChainMetadataCache = new OnChainMetadataCache( instanceConfig.config.web3Provider, diff --git a/test/unit/balancer/Balancer.test.ts b/test/unit/balancer/Balancer.test.ts index 4bcc2592..2f7388f8 100644 --- a/test/unit/balancer/Balancer.test.ts +++ b/test/unit/balancer/Balancer.test.ts @@ -125,7 +125,8 @@ describe('Balancer flow', () => { }) it('Alice creates a new OceanPool pool', async () => { /// new pool with total DT = 45 , dt weight=90% with swap fee 2% - alicePoolAddress = await Pool.createDTPool(alice, tokenAddress, '45', '9', '0.02') + const createTx = await Pool.create(alice, tokenAddress, '45', '9', '0.02') + alicePoolAddress = createTx.events.BPoolRegistered.returnValues[0] const s = await Pool.getPoolSharesTotalSupply(alicePoolAddress) assert(String(s) === '100', 'totalSupply does not match: ' + s) const n = await Pool.getNumTokens(alicePoolAddress) diff --git a/test/unit/exchanges/FixedPriceExchange.test.ts b/test/unit/exchanges/FixedPriceExchange.test.ts index eae10258..a675f6c1 100644 --- a/test/unit/exchanges/FixedPriceExchange.test.ts +++ b/test/unit/exchanges/FixedPriceExchange.test.ts @@ -110,7 +110,8 @@ describe('FixedRateExchange flow', () => { LoggerInstance, FixedRateExchangeAddress, FixedRateExchangeContract.abi as AbiItem[], - oceanTokenAddress + oceanTokenAddress, + datatoken ) assert(FixedRateClass !== null) }) @@ -170,7 +171,8 @@ describe('FixedRateExchange flow', () => { if (consoleDebug) console.log('Bob ocean allowance:' + balance) }) it('Alice creates a new FixedRate Exchange with a rate of 0.5', async () => { - aliceExchangeId = await FixedRateClass.create(tokenAddress, fixedPriceRate, alice) + const trxReceipt = await FixedRateClass.create(tokenAddress, fixedPriceRate, alice) + aliceExchangeId = trxReceipt.events.ExchangeCreated.returnValues[0] if (consoleDebug) console.log('aliceExchangeId:' + aliceExchangeId) }) it('Bob should find the exchange', async () => {