From 11271a970b6fd692e26cf398973244ae1e153b9b Mon Sep 17 00:00:00 2001 From: alexcos20 Date: Wed, 21 Oct 2020 03:29:27 -0700 Subject: [PATCH] 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 2e3343e3..f179c523 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 1c4d4377..03fd0f02 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 ea4cc205..7e150b15 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) })