mirror of
https://github.com/oceanprotocol/ocean.js.git
synced 2024-11-26 20:39:05 +01:00
Fix provider fee approval in order helper (#1795)
* fix provider fee approval in order helper * fix lint * undo hardcode
This commit is contained in:
parent
da262f7498
commit
a0964d5096
@ -10,7 +10,9 @@ import {
|
|||||||
approve,
|
approve,
|
||||||
FixedRateExchange,
|
FixedRateExchange,
|
||||||
ConsumeMarketFee,
|
ConsumeMarketFee,
|
||||||
ProviderFees
|
ProviderFees,
|
||||||
|
ZERO_ADDRESS,
|
||||||
|
approveWei
|
||||||
} from '../index'
|
} from '../index'
|
||||||
import Decimal from 'decimal.js'
|
import Decimal from 'decimal.js'
|
||||||
|
|
||||||
@ -88,13 +90,32 @@ export async function orderAsset(
|
|||||||
)
|
)
|
||||||
).providerFee
|
).providerFee
|
||||||
|
|
||||||
|
if (
|
||||||
|
fees &&
|
||||||
|
fees.providerFeeAddress !== ZERO_ADDRESS &&
|
||||||
|
fees.providerFeeAmount &&
|
||||||
|
parseInt(fees.providerFeeAmount) > 0
|
||||||
|
) {
|
||||||
|
try {
|
||||||
|
await approveWei(
|
||||||
|
consumerAccount,
|
||||||
|
config,
|
||||||
|
await consumerAccount.getAddress(),
|
||||||
|
fees.providerFeeToken,
|
||||||
|
asset.services[0].datatokenAddress,
|
||||||
|
fees.providerFeeAmount
|
||||||
|
)
|
||||||
|
} catch (error) {
|
||||||
|
throw new Error(`Failed to approve provider fee token ${fees.providerFeeToken}`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const orderParams = {
|
const orderParams = {
|
||||||
consumer: consumerAddress || (await consumerAccount.getAddress()),
|
consumer: consumerAddress || (await consumerAccount.getAddress()),
|
||||||
serviceIndex,
|
serviceIndex,
|
||||||
_providerFee: fees,
|
_providerFee: fees,
|
||||||
_consumeMarketFee: consumeMarketOrderFee
|
_consumeMarketFee: consumeMarketOrderFee
|
||||||
} as OrderParams
|
} as OrderParams
|
||||||
|
|
||||||
switch (pricingType) {
|
switch (pricingType) {
|
||||||
case 'free': {
|
case 'free': {
|
||||||
if (templateIndex === 1) {
|
if (templateIndex === 1) {
|
||||||
@ -105,8 +126,9 @@ export async function orderAsset(
|
|||||||
await consumerAccount.getAddress()
|
await consumerAccount.getAddress()
|
||||||
)
|
)
|
||||||
if (!dispenserTx) {
|
if (!dispenserTx) {
|
||||||
return
|
throw new Error(`Failed to dispense !`)
|
||||||
}
|
}
|
||||||
|
await dispenserTx.wait()
|
||||||
return await datatoken.startOrder(
|
return await datatoken.startOrder(
|
||||||
asset.datatokens[datatokenIndex].address,
|
asset.datatokens[datatokenIndex].address,
|
||||||
orderParams.consumer,
|
orderParams.consumer,
|
||||||
@ -133,6 +155,7 @@ export async function orderAsset(
|
|||||||
)
|
)
|
||||||
const fees = await fre.getFeesInfo(fixedRates[fixedRateIndex].id)
|
const fees = await fre.getFeesInfo(fixedRates[fixedRateIndex].id)
|
||||||
const exchange = await fre.getExchange(fixedRates[fixedRateIndex].id)
|
const exchange = await fre.getExchange(fixedRates[fixedRateIndex].id)
|
||||||
|
|
||||||
const { baseTokenAmount } = await fre.calcBaseInGivenDatatokensOut(
|
const { baseTokenAmount } = await fre.calcBaseInGivenDatatokensOut(
|
||||||
fees.exchangeId,
|
fees.exchangeId,
|
||||||
'1',
|
'1',
|
||||||
@ -166,7 +189,7 @@ export async function orderAsset(
|
|||||||
)
|
)
|
||||||
const txApprove = typeof tx !== 'number' ? await tx.wait() : tx
|
const txApprove = typeof tx !== 'number' ? await tx.wait() : tx
|
||||||
if (!txApprove) {
|
if (!txApprove) {
|
||||||
return
|
throw new Error(`Failed to appove ${exchange.baseToken} !`)
|
||||||
}
|
}
|
||||||
const freTx = await fre.buyDatatokens(
|
const freTx = await fre.buyDatatokens(
|
||||||
exchange.exchangeId,
|
exchange.exchangeId,
|
||||||
@ -177,7 +200,7 @@ export async function orderAsset(
|
|||||||
)
|
)
|
||||||
const buyDtTx = await freTx.wait()
|
const buyDtTx = await freTx.wait()
|
||||||
if (!buyDtTx) {
|
if (!buyDtTx) {
|
||||||
return
|
throw new Error(`Failed to buy datatoken from fixed rate!`)
|
||||||
}
|
}
|
||||||
return await datatoken.startOrder(
|
return await datatoken.startOrder(
|
||||||
asset.datatokens[datatokenIndex].address,
|
asset.datatokens[datatokenIndex].address,
|
||||||
@ -202,11 +225,12 @@ export async function orderAsset(
|
|||||||
if (!txApprove) {
|
if (!txApprove) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
return await datatoken.buyFromFreAndOrder(
|
const txBuy = await datatoken.buyFromFreAndOrder(
|
||||||
asset.datatokens[datatokenIndex].address,
|
asset.datatokens[datatokenIndex].address,
|
||||||
orderParams,
|
orderParams,
|
||||||
freParams
|
freParams
|
||||||
)
|
)
|
||||||
|
return txBuy
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user