From dec9c57c94f777201355c1356c881989002931c1 Mon Sep 17 00:00:00 2001 From: alexcos20 Date: Wed, 21 Oct 2020 02:27:58 -0700 Subject: [PATCH 1/4] make pool.create subscribable --- src/balancer/OceanPool.ts | 101 ++++++++++++++++------------ test/unit/balancer/Balancer.test.ts | 2 +- 2 files changed, 58 insertions(+), 45 deletions(-) diff --git a/src/balancer/OceanPool.ts b/src/balancer/OceanPool.ts index 93a21fc79..3753df515 100644 --- a/src/balancer/OceanPool.ts +++ b/src/balancer/OceanPool.ts @@ -4,7 +4,7 @@ import { TransactionReceipt } from 'web3-core' import { Pool } from './Pool' import { EventData, Filter } from 'web3-eth-contract' import BigNumber from 'bignumber.js' - +import { SubscribablePromise } from '../utils' declare type PoolTransactionType = 'swap' | 'join' | 'exit' const POOL_MAX_AMOUNT_IN_LIMIT = 0.25 // maximum 1/4 of the pool reserve @@ -34,6 +34,13 @@ export interface PoolTransaction { type: PoolTransactionType } +export enum PoolCreateProgressStep { + CreatingPool, + ApprovingDatatoken, + ApprovingOcean, + SetupPool +} + /** * Ocean Pools submodule exposed under ocean.pool */ @@ -64,13 +71,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) { console.error('ERROR: oceanAddress is not defined') return null @@ -83,47 +90,53 @@ export class OceanPool extends Pool { console.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) { - console.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) { - console.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) { - console.error('ERROR: Failed to create a new pool') - return null - } - return address + return new SubscribablePromise(async (observer) => { + observer.next(PoolCreateProgressStep.CreatingPool) + const address = await super.createPool(account) + 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) { + console.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) { + console.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) { + console.error('ERROR: Failed to create a new pool') + return null + } + return address + }) } /** diff --git a/test/unit/balancer/Balancer.test.ts b/test/unit/balancer/Balancer.test.ts index 85aa330c6..5d4a509eb 100644 --- a/test/unit/balancer/Balancer.test.ts +++ b/test/unit/balancer/Balancer.test.ts @@ -121,7 +121,7 @@ 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') + alicePoolAddress = await Pool.create(alice, tokenAddress, '45', '9', '0.02') const s = await Pool.getPoolSharesTotalSupply(alicePoolAddress) assert(String(s) === '100', 'totalSupply does not match: ' + s) const n = await Pool.getNumTokens(alicePoolAddress) From 11271a970b6fd692e26cf398973244ae1e153b9b Mon Sep 17 00:00:00 2001 From: alexcos20 Date: Wed, 21 Oct 2020 03:29:27 -0700 Subject: [PATCH 2/4] change exchange.create to allow subscribe&approve --- src/exchange/FixedRateExchange.ts | 88 ++++++++++++------- src/ocean/Ocean.ts | 3 +- .../unit/exchanges/FixedPriceExchange.test.ts | 3 +- 3 files changed, 60 insertions(+), 34 deletions(-) diff --git a/src/exchange/FixedRateExchange.ts b/src/exchange/FixedRateExchange.ts index 2e3343e34..f179c5236 100644 --- a/src/exchange/FixedRateExchange.ts +++ b/src/exchange/FixedRateExchange.ts @@ -4,6 +4,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 @@ -21,6 +23,12 @@ export interface FixedPriceSwap { baseTokenAmount: string dataTokenAmount: string } + +export enum FixedRateCreateProgressStep { + CreatingExchange, + ApprovingDatatoken +} + const DEFAULT_GAS_LIMIT = 300000 export class OceanFixedRateExchange { @@ -30,6 +38,7 @@ export class OceanFixedRateExchange { public fixedRateExchangeABI: AbiItem | AbiItem[] public web3: Web3 public contract: Contract = null + public datatokens: DataTokens /** * Instantiate FixedRateExchange @@ -42,13 +51,15 @@ export class OceanFixedRateExchange { web3: Web3, 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, @@ -61,39 +72,52 @@ export class OceanFixedRateExchange { * @param {String} dataToken Data Token Contract Address * @param {Number} rate exchange rate * @param {String} address User address + * @param {String} amount Optional, amount of datatokens to be approved for the exchange * @return {Promise} exchangeId */ - 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) { - console.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) { + console.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 exchangeId + }) } /** diff --git a/src/ocean/Ocean.ts b/src/ocean/Ocean.ts index 1c4d43779..03fd0f022 100644 --- a/src/ocean/Ocean.ts +++ b/src/ocean/Ocean.ts @@ -63,7 +63,8 @@ export class Ocean extends Instantiable { instanceConfig.config.web3Provider, 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/exchanges/FixedPriceExchange.test.ts b/test/unit/exchanges/FixedPriceExchange.test.ts index ea4cc2058..7e150b153 100644 --- a/test/unit/exchanges/FixedPriceExchange.test.ts +++ b/test/unit/exchanges/FixedPriceExchange.test.ts @@ -107,7 +107,8 @@ describe('FixedRateExchange flow', () => { web3, FixedRateExchangeAddress, FixedRateExchangeContract.abi as AbiItem[], - oceanTokenAddress + oceanTokenAddress, + datatoken ) assert(FixedRateClass !== null) }) From 93f0d53c5a10e153f30d6a76601a5cba36a27173 Mon Sep 17 00:00:00 2001 From: alexcos20 Date: Wed, 21 Oct 2020 03:38:03 -0700 Subject: [PATCH 3/4] exchange.create returns TransactionReceipt --- src/exchange/FixedRateExchange.ts | 6 +++--- test/unit/exchanges/FixedPriceExchange.test.ts | 3 ++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/exchange/FixedRateExchange.ts b/src/exchange/FixedRateExchange.ts index f179c5236..d77f471b1 100644 --- a/src/exchange/FixedRateExchange.ts +++ b/src/exchange/FixedRateExchange.ts @@ -73,14 +73,14 @@ export class OceanFixedRateExchange { * @param {Number} rate exchange rate * @param {String} address User address * @param {String} amount Optional, amount of datatokens to be approved for the exchange - * @return {Promise} exchangeId + * @return {Promise} TransactionReceipt */ public create( dataToken: string, rate: string, address: string, amount?: string - ): SubscribablePromise { + ): SubscribablePromise { return new SubscribablePromise(async (observer) => { observer.next(FixedRateCreateProgressStep.CreatingExchange) let estGas @@ -116,7 +116,7 @@ export class OceanFixedRateExchange { observer.next(FixedRateCreateProgressStep.ApprovingDatatoken) this.datatokens.approve(dataToken, this.fixedRateExchangeAddress, amount, address) } - return exchangeId + return trxReceipt }) } diff --git a/test/unit/exchanges/FixedPriceExchange.test.ts b/test/unit/exchanges/FixedPriceExchange.test.ts index 7e150b153..d945c7dcd 100644 --- a/test/unit/exchanges/FixedPriceExchange.test.ts +++ b/test/unit/exchanges/FixedPriceExchange.test.ts @@ -168,7 +168,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 () => { From 4db0a0d43d25aaefba121985dc00f5dad570db11 Mon Sep 17 00:00:00 2001 From: alexcos20 Date: Wed, 21 Oct 2020 04:38:19 -0700 Subject: [PATCH 4/4] pool.create returns txid --- src/balancer/OceanPool.ts | 11 ++++++++--- src/balancer/Pool.ts | 2 +- src/balancer/PoolFactory.ts | 18 ++++++++---------- test/unit/balancer/Balancer.test.ts | 3 ++- 4 files changed, 19 insertions(+), 15 deletions(-) diff --git a/src/balancer/OceanPool.ts b/src/balancer/OceanPool.ts index 3753df515..a5e60dfdc 100644 --- a/src/balancer/OceanPool.ts +++ b/src/balancer/OceanPool.ts @@ -77,7 +77,7 @@ export class OceanPool extends Pool { amount: string, weight: string, fee: string - ): SubscribablePromise { + ): SubscribablePromise { if (this.oceanAddress == null) { console.error('ERROR: oceanAddress is not defined') return null @@ -92,7 +92,12 @@ export class OceanPool extends Pool { } return new SubscribablePromise(async (observer) => { observer.next(PoolCreateProgressStep.CreatingPool) - const address = await super.createPool(account) + const createTxid = await super.createPool(account) + if (!createTxid) { + console.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 @@ -135,7 +140,7 @@ export class OceanPool extends Pool { console.error('ERROR: Failed to create a new pool') return null } - return address + return createTxid }) } diff --git a/src/balancer/Pool.ts b/src/balancer/Pool.ts index 540eb8c5c..4c743dc35 100644 --- a/src/balancer/Pool.ts +++ b/src/balancer/Pool.ts @@ -36,7 +36,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 19c614d9e..791c5b509 100644 --- a/src/balancer/PoolFactory.ts +++ b/src/balancer/PoolFactory.ts @@ -1,6 +1,7 @@ import Web3 from 'web3' import { AbiItem } from 'web3-utils/types' import jsonFactoryABI from '@oceanprotocol/contracts/artifacts/BFactory.json' +import { TransactionReceipt } from 'web3-core' export class PoolFactory { public GASLIMIT_DEFAULT = 5000000 @@ -27,7 +28,7 @@ export class PoolFactory { /** * Creates a new pool */ - async createPool(account: string): Promise { + async createPool(account: string): Promise { if (this.web3 === null) { console.error('ERROR: Web3 object is null') return null @@ -41,18 +42,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) { console.error(`ERROR: Failed to create new pool: ${e.message}`) } - return pooladdress + return txid } } diff --git a/test/unit/balancer/Balancer.test.ts b/test/unit/balancer/Balancer.test.ts index 5d4a509eb..d001f5bb4 100644 --- a/test/unit/balancer/Balancer.test.ts +++ b/test/unit/balancer/Balancer.test.ts @@ -121,7 +121,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.create(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)