1
0
mirror of https://github.com/oceanprotocol/ocean.js.git synced 2024-11-26 20:39:05 +01:00

change exchange.create to allow subscribe&approve

This commit is contained in:
alexcos20 2020-10-21 03:29:27 -07:00
parent dec9c57c94
commit 11271a970b
3 changed files with 60 additions and 34 deletions

View File

@ -4,6 +4,8 @@ import { TransactionReceipt } from 'web3-core'
import { Contract, EventData } from 'web3-eth-contract' import { Contract, EventData } from 'web3-eth-contract'
import { AbiItem } from 'web3-utils/types' import { AbiItem } from 'web3-utils/types'
import Web3 from 'web3' import Web3 from 'web3'
import { SubscribablePromise } from '../utils'
import { DataTokens } from '../datatokens/Datatokens'
export interface FixedPriceExchange { export interface FixedPriceExchange {
exchangeID?: string exchangeID?: string
@ -21,6 +23,12 @@ export interface FixedPriceSwap {
baseTokenAmount: string baseTokenAmount: string
dataTokenAmount: string dataTokenAmount: string
} }
export enum FixedRateCreateProgressStep {
CreatingExchange,
ApprovingDatatoken
}
const DEFAULT_GAS_LIMIT = 300000 const DEFAULT_GAS_LIMIT = 300000
export class OceanFixedRateExchange { export class OceanFixedRateExchange {
@ -30,6 +38,7 @@ export class OceanFixedRateExchange {
public fixedRateExchangeABI: AbiItem | AbiItem[] public fixedRateExchangeABI: AbiItem | AbiItem[]
public web3: Web3 public web3: Web3
public contract: Contract = null public contract: Contract = null
public datatokens: DataTokens
/** /**
* Instantiate FixedRateExchange * Instantiate FixedRateExchange
@ -42,13 +51,15 @@ export class OceanFixedRateExchange {
web3: Web3, web3: Web3,
fixedRateExchangeAddress: string = null, fixedRateExchangeAddress: string = null,
fixedRateExchangeABI: AbiItem | AbiItem[] = null, fixedRateExchangeABI: AbiItem | AbiItem[] = null,
oceanAddress: string = null oceanAddress: string = null,
datatokens: DataTokens
) { ) {
this.web3 = web3 this.web3 = web3
this.fixedRateExchangeAddress = fixedRateExchangeAddress this.fixedRateExchangeAddress = fixedRateExchangeAddress
this.fixedRateExchangeABI = this.fixedRateExchangeABI =
fixedRateExchangeABI || (defaultFixedRateExchangeABI.abi as AbiItem[]) fixedRateExchangeABI || (defaultFixedRateExchangeABI.abi as AbiItem[])
this.oceanAddress = oceanAddress this.oceanAddress = oceanAddress
this.datatokens = datatokens
if (web3) if (web3)
this.contract = new this.web3.eth.Contract( this.contract = new this.web3.eth.Contract(
this.fixedRateExchangeABI, this.fixedRateExchangeABI,
@ -61,9 +72,17 @@ export class OceanFixedRateExchange {
* @param {String} dataToken Data Token Contract Address * @param {String} dataToken Data Token Contract Address
* @param {Number} rate exchange rate * @param {Number} rate exchange rate
* @param {String} address User address * @param {String} address User address
* @param {String} amount Optional, amount of datatokens to be approved for the exchange
* @return {Promise<string>} exchangeId * @return {Promise<string>} exchangeId
*/ */
public async create(dataToken: string, rate: string, address: string): Promise<string> { public create(
dataToken: string,
rate: string,
address: string,
amount?: string
): SubscribablePromise<FixedRateCreateProgressStep, string> {
return new SubscribablePromise(async (observer) => {
observer.next(FixedRateCreateProgressStep.CreatingExchange)
let estGas let estGas
try { try {
/* estGas = await this.contract.methods /* estGas = await this.contract.methods
@ -80,10 +99,10 @@ export class OceanFixedRateExchange {
} catch (e) { } catch (e) {
estGas = DEFAULT_GAS_LIMIT estGas = DEFAULT_GAS_LIMIT
} }
let exchangeId = null let exchangeId = null
let trxReceipt = null
try { try {
const trxReceipt = await this.contract.methods trxReceipt = await this.contract.methods
.create(this.oceanAddress, dataToken, this.web3.utils.toWei(rate)) .create(this.oceanAddress, dataToken, this.web3.utils.toWei(rate))
.send({ .send({
from: address, from: address,
@ -93,7 +112,12 @@ export class OceanFixedRateExchange {
} catch (e) { } catch (e) {
console.error(`ERROR: Failed to create new exchange: ${e.message}`) 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 return exchangeId
})
} }
/** /**

View File

@ -63,7 +63,8 @@ export class Ocean extends Instantiable {
instanceConfig.config.web3Provider, instanceConfig.config.web3Provider,
instanceConfig.config.fixedRateExchangeAddress, instanceConfig.config.fixedRateExchangeAddress,
instanceConfig.config.fixedRateExchangeAddressABI, instanceConfig.config.fixedRateExchangeAddressABI,
instanceConfig.config.oceanTokenAddress instanceConfig.config.oceanTokenAddress,
instance.datatokens
) )
instance.OnChainMetadataCache = new OnChainMetadataCache( instance.OnChainMetadataCache = new OnChainMetadataCache(
instanceConfig.config.web3Provider, instanceConfig.config.web3Provider,

View File

@ -107,7 +107,8 @@ describe('FixedRateExchange flow', () => {
web3, web3,
FixedRateExchangeAddress, FixedRateExchangeAddress,
FixedRateExchangeContract.abi as AbiItem[], FixedRateExchangeContract.abi as AbiItem[],
oceanTokenAddress oceanTokenAddress,
datatoken
) )
assert(FixedRateClass !== null) assert(FixedRateClass !== null)
}) })