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:
parent
dec9c57c94
commit
11271a970b
@ -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,39 +72,52 @@ 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(
|
||||||
let estGas
|
dataToken: string,
|
||||||
try {
|
rate: string,
|
||||||
/* estGas = await this.contract.methods
|
address: string,
|
||||||
.create(this.oceanAddress, dataToken, this.web3.utils.toWei(rate))
|
amount?: string
|
||||||
.estimateGas(function (err, g) {
|
): SubscribablePromise<FixedRateCreateProgressStep, string> {
|
||||||
if (err) {
|
return new SubscribablePromise(async (observer) => {
|
||||||
return DEFAULT_GAS_LIMIT
|
observer.next(FixedRateCreateProgressStep.CreatingExchange)
|
||||||
} else {
|
let estGas
|
||||||
return g
|
try {
|
||||||
}
|
/* estGas = await this.contract.methods
|
||||||
})
|
.create(this.oceanAddress, dataToken, this.web3.utils.toWei(rate))
|
||||||
*/
|
.estimateGas(function (err, g) {
|
||||||
estGas = DEFAULT_GAS_LIMIT
|
if (err) {
|
||||||
} catch (e) {
|
return DEFAULT_GAS_LIMIT
|
||||||
estGas = DEFAULT_GAS_LIMIT
|
} else {
|
||||||
}
|
return g
|
||||||
|
}
|
||||||
let exchangeId = null
|
})
|
||||||
try {
|
*/
|
||||||
const trxReceipt = await this.contract.methods
|
estGas = DEFAULT_GAS_LIMIT
|
||||||
.create(this.oceanAddress, dataToken, this.web3.utils.toWei(rate))
|
} catch (e) {
|
||||||
.send({
|
estGas = DEFAULT_GAS_LIMIT
|
||||||
from: address,
|
}
|
||||||
gas: estGas + 1
|
let exchangeId = null
|
||||||
})
|
let trxReceipt = null
|
||||||
exchangeId = trxReceipt.events.ExchangeCreated.returnValues[0]
|
try {
|
||||||
} catch (e) {
|
trxReceipt = await this.contract.methods
|
||||||
console.error(`ERROR: Failed to create new exchange: ${e.message}`)
|
.create(this.oceanAddress, dataToken, this.web3.utils.toWei(rate))
|
||||||
}
|
.send({
|
||||||
return exchangeId
|
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
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -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,
|
||||||
|
@ -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)
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user