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

Merge pull request #430 from oceanprotocol/bug/fix_fre_wei_conversion

fix fromwei conversion
This commit is contained in:
Alex Coseru 2020-10-30 15:45:09 +02:00 committed by GitHub
commit 80bb373f42
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -4,7 +4,7 @@ import { TransactionReceipt } from 'web3-core'
import { Contract, EventData } from 'web3-eth-contract'
import { AbiItem } from 'web3-utils/types'
import Web3 from 'web3'
import { SubscribablePromise, Logger } from '../utils'
import { SubscribablePromise, Logger, getFairGasPrice } from '../utils'
import { DataTokens } from '../datatokens/Datatokens'
export interface FixedPriceExchange {
@ -29,9 +29,8 @@ export enum FixedRateCreateProgressStep {
ApprovingDatatoken
}
const DEFAULT_GAS_LIMIT = 1000000
export class OceanFixedRateExchange {
public GASLIMIT_DEFAULT = 1000000
/** Ocean related functions */
public oceanAddress: string = null
public fixedRateExchangeAddress: string
@ -87,20 +86,13 @@ export class OceanFixedRateExchange {
return new SubscribablePromise(async (observer) => {
observer.next(FixedRateCreateProgressStep.CreatingExchange)
let estGas
const gasLimitDefault = this.GASLIMIT_DEFAULT
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
estGas = await this.contract.methods
.create(this.oceanAddress, dataToken, this.web3.utils.toWei(rate))
.estimateGas((err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = DEFAULT_GAS_LIMIT
estGas = gasLimitDefault
}
let exchangeId = null
let trxReceipt = null
@ -109,7 +101,8 @@ export class OceanFixedRateExchange {
.create(this.oceanAddress, dataToken, this.web3.utils.toWei(rate))
.send({
from: address,
gas: estGas + 1
gas: estGas + 1,
gasPrice: await getFairGasPrice(this.web3)
})
exchangeId = trxReceipt.events.ExchangeCreated.returnValues[0]
} catch (e) {
@ -148,26 +141,22 @@ export class OceanFixedRateExchange {
dataTokenAmount: string,
address: string
): Promise<TransactionReceipt> {
const gasLimitDefault = this.GASLIMIT_DEFAULT
let estGas
try {
estGas = await this.contract.methods
.swap(exchangeId, this.web3.utils.toWei(String(dataTokenAmount)))
.estimateGas(function (err, g) {
if (err) {
return DEFAULT_GAS_LIMIT
} else {
return g
}
})
.estimateGas((err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = DEFAULT_GAS_LIMIT
estGas = gasLimitDefault
}
try {
const trxReceipt = await this.contract.methods
.swap(exchangeId, this.web3.utils.toWei(String(dataTokenAmount)))
.send({
from: address,
gas: estGas + 1
gas: estGas + 1,
gasPrice: await getFairGasPrice(this.web3)
})
return trxReceipt
} catch (e) {
@ -199,25 +188,21 @@ export class OceanFixedRateExchange {
newRate: number,
address: string
): Promise<TransactionReceipt> {
const gasLimitDefault = this.GASLIMIT_DEFAULT
let estGas
try {
estGas = await this.contract.methods
.setRate(exchangeId, this.web3.utils.toWei(String(newRate)))
.estimateGas(function (err, estGas) {
if (err) {
console.error(`ERROR: FixedPriceExchange: ${err.message}`)
return DEFAULT_GAS_LIMIT
}
return estGas
})
.estimateGas((err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = DEFAULT_GAS_LIMIT
estGas = gasLimitDefault
}
const trxReceipt = await this.contract.methods
.setRate(exchangeId, this.web3.utils.toWei(String(newRate)))
.send({
from: address,
gas: estGas + 1
gas: estGas + 1,
gasPrice: await getFairGasPrice(this.web3)
})
return trxReceipt
}
@ -235,24 +220,19 @@ export class OceanFixedRateExchange {
const exchange = await this.getExchange(exchangeId)
if (!exchange) return null
if (exchange.active === true) return null
const gasLimitDefault = this.GASLIMIT_DEFAULT
let estGas
try {
estGas = await this.contract.methods
.toggleExchangeState(exchangeId)
.estimateGas(function (err, estGas) {
if (err) {
console.error(`ERROR: FixedPriceExchange: ${err.message}`)
estGas = DEFAULT_GAS_LIMIT
}
return estGas
})
.estimateGas((err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
this.logger.error(`ERROR: FixedPriceExchange: ${e.message}`)
estGas = DEFAULT_GAS_LIMIT
estGas = gasLimitDefault
}
const trxReceipt = await this.contract.methods.toggleExchangeState(exchangeId).send({
from: address,
gas: estGas + 1
gas: estGas + 1,
gasPrice: await getFairGasPrice(this.web3)
})
return trxReceipt
}
@ -270,24 +250,19 @@ export class OceanFixedRateExchange {
const exchange = await this.getExchange(exchangeId)
if (!exchange) return null
if (exchange.active === false) return null
const gasLimitDefault = this.GASLIMIT_DEFAULT
let estGas
try {
estGas = await this.contract.methods
.toggleExchangeState(exchangeId)
.estimateGas(function (err, estGas) {
if (err) {
console.error(`ERROR: FixedPriceExchange: ${err.message}`)
estGas = DEFAULT_GAS_LIMIT
}
return estGas
})
.estimateGas((err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
this.logger.error(`ERROR: FixedPriceExchange: ${e.message}`)
estGas = DEFAULT_GAS_LIMIT
estGas = gasLimitDefault
}
const trxReceipt = await this.contract.methods.toggleExchangeState(exchangeId).send({
from: address,
gas: estGas + 1
gas: estGas + 1,
gasPrice: await getFairGasPrice(this.web3)
})
return trxReceipt
}
@ -337,6 +312,8 @@ export class OceanFixedRateExchange {
const result: FixedPriceExchange = await this.contract.methods
.getExchange(exchangeId)
.call()
result.fixedRate = this.web3.utils.fromWei(result.fixedRate)
result.supply = this.web3.utils.fromWei(result.supply)
return result
}
@ -392,7 +369,7 @@ export class OceanFixedRateExchange {
constituents.active === true &&
constituents.dataToken.toLowerCase() === dataTokenAddress.toLowerCase()
) {
const supply = new BigNumber(await this.getSupply(constituents.exchangeID))
const supply = new BigNumber(constituents.supply)
const required = new BigNumber(minSupply)
if (supply.gte(required)) {
result.push(constituents)
@ -472,8 +449,8 @@ export class OceanFixedRateExchange {
const result: FixedPriceSwap = {
exchangeID: data.returnValues[0],
caller: data.returnValues[1],
baseTokenAmount: data.returnValues[2],
dataTokenAmount: data.returnValues[3]
baseTokenAmount: this.web3.utils.fromWei(data.returnValues[2]),
dataTokenAmount: this.web3.utils.fromWei(data.returnValues[3])
}
return result
}