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

Merge pull request #530 from oceanprotocol/fix/pool-creation

pool creation: check allowance for dt and ocean before aprove is called
This commit is contained in:
Ahmed Ali 2021-01-12 11:37:15 +02:00 committed by GitHub
commit eff37c2869
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 27 deletions

View File

@ -94,52 +94,58 @@ export class OceanPool extends Pool {
): SubscribablePromise<PoolCreateProgressStep, TransactionReceipt> { ): SubscribablePromise<PoolCreateProgressStep, TransactionReceipt> {
if (this.oceanAddress == null) { if (this.oceanAddress == null) {
this.logger.error('ERROR: oceanAddress is not defined') this.logger.error('ERROR: oceanAddress is not defined')
return null throw new Error('ERROR: oceanAddress is not defined')
} }
if (parseFloat(fee) > 0.1) { if (parseFloat(fee) > 0.1) {
this.logger.error('ERROR: Swap fee too high. The maximum allowed swapFee is 10%') this.logger.error('ERROR: Swap fee too high. The maximum allowed swapFee is 10%')
return null throw new Error('ERROR: Swap fee too high. The maximum allowed swapFee is 10%')
} }
if (parseFloat(dtAmount) < 2) { if (parseFloat(dtAmount) < 2) {
this.logger.error('ERROR: Amount of DT is too low') this.logger.error('ERROR: Amount of DT is too low')
return null throw new Error('ERROR: Amount of DT is too low')
} }
if (parseFloat(dtWeight) > 9 || parseFloat(dtWeight) < 1) { if (parseFloat(dtWeight) > 9 || parseFloat(dtWeight) < 1) {
this.logger.error('ERROR: Weight out of bounds (min 1, max9)') this.logger.error('ERROR: Weight out of bounds (min 1, max9)')
return null throw new Error('ERROR: Weight out of bounds (min 1, max9)')
} }
return new SubscribablePromise(async (observer) => { return new SubscribablePromise(async (observer) => {
observer.next(PoolCreateProgressStep.CreatingPool) observer.next(PoolCreateProgressStep.CreatingPool)
const createTxid = await super.createPool(account) const createTxid = await super.createPool(account)
if (!createTxid) { if (!createTxid) {
this.logger.error('ERROR: Failed to call approve DT token') this.logger.error('ERROR: Failed to call create pool')
return null throw new Error('ERROR: Failed to call create pool')
} }
const address = createTxid.events.BPoolRegistered.returnValues[0] const address = createTxid.events.BPoolRegistered.returnValues[0]
const oceanWeight = 10 - parseFloat(dtWeight) const oceanWeight = 10 - parseFloat(dtWeight)
this.dtAddress = dtAddress this.dtAddress = dtAddress
observer.next(PoolCreateProgressStep.ApprovingDatatoken)
let txid let txid
txid = await this.approve( const dtAllowance = await this.allowance(dtAddress, account, address)
account, if (parseFloat(dtAllowance) < parseFloat(dtAmount)) {
dtAddress, observer.next(PoolCreateProgressStep.ApprovingDatatoken)
address, txid = await this.approve(
this.web3.utils.toWei(String(dtAmount)) account,
) dtAddress,
if (!txid) { address,
this.logger.error('ERROR: Failed to call approve DT token') this.web3.utils.toWei(String(dtAmount))
return null )
if (!txid) {
this.logger.error('ERROR: Failed to call approve DT token')
throw new Error('ERROR: Failed to call approve DT token')
}
} }
observer.next(PoolCreateProgressStep.ApprovingOcean) const oceanAllowance = await this.allowance(this.oceanAddress, account, address)
txid = await this.approve( if (parseFloat(oceanAllowance) < parseFloat(oceanAmount)) {
account, observer.next(PoolCreateProgressStep.ApprovingOcean)
this.oceanAddress, txid = await this.approve(
address, account,
this.web3.utils.toWei(String(oceanAmount)) this.oceanAddress,
) address,
if (!txid) { this.web3.utils.toWei(String(oceanAmount))
this.logger.error('ERROR: Failed to call approve OCEAN token') )
return null if (!txid) {
this.logger.error('ERROR: Failed to call approve OCEAN token')
throw new Error('ERROR: Failed to call approve OCEAN token')
}
} }
observer.next(PoolCreateProgressStep.SetupPool) observer.next(PoolCreateProgressStep.SetupPool)
txid = await super.setup( txid = await super.setup(
@ -155,7 +161,7 @@ export class OceanPool extends Pool {
) )
if (!txid) { if (!txid) {
this.logger.error('ERROR: Failed to create a new pool') this.logger.error('ERROR: Failed to create a new pool')
return null throw new Error('ERROR: Failed to create a new pool')
} }
return createTxid return createTxid
}) })

View File

@ -4,6 +4,7 @@ import { TransactionReceipt } from 'web3-core'
import { Logger, getFairGasPrice } from '../utils' import { Logger, getFairGasPrice } from '../utils'
import BigNumber from 'bignumber.js' import BigNumber from 'bignumber.js'
import jsonpoolABI from '@oceanprotocol/contracts/artifacts/BPool.json' import jsonpoolABI from '@oceanprotocol/contracts/artifacts/BPool.json'
import defaultDatatokensABI from '@oceanprotocol/contracts/artifacts/DataTokenTemplate.json'
import { PoolFactory } from './PoolFactory' import { PoolFactory } from './PoolFactory'
const MaxUint256 = const MaxUint256 =
@ -107,6 +108,25 @@ export class Pool extends PoolFactory {
return result return result
} }
/**
* Get Alloance for both DataToken and Ocean
* @param {String } tokenAdress
* @param {String} owner
* @param {String} spender
*/
public async allowance(
tokenAdress: string,
owner: string,
spender: string
): Promise<string> {
const tokenAbi = defaultDatatokensABI.abi as AbiItem[]
const datatoken = new this.web3.eth.Contract(tokenAbi, tokenAdress, {
from: spender
})
const trxReceipt = await datatoken.methods.allowance(owner, spender).call()
return this.web3.utils.fromWei(trxReceipt)
}
/** /**
* Approve spender to spent amount tokens * Approve spender to spent amount tokens
* @param {String} account * @param {String} account