Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix provider fee approval in order helper #1795

Merged
merged 3 commits into from
Dec 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 30 additions & 6 deletions src/utils/OrderUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import {
approve,
FixedRateExchange,
ConsumeMarketFee,
ProviderFees
ProviderFees,
ZERO_ADDRESS,
approveWei
} from '../index'
import Decimal from 'decimal.js'

Expand Down Expand Up @@ -88,13 +90,32 @@ export async function orderAsset(
)
).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 = {
consumer: consumerAddress || (await consumerAccount.getAddress()),
serviceIndex,
_providerFee: fees,
_consumeMarketFee: consumeMarketOrderFee
} as OrderParams

switch (pricingType) {
case 'free': {
if (templateIndex === 1) {
Expand All @@ -105,8 +126,9 @@ export async function orderAsset(
await consumerAccount.getAddress()
)
if (!dispenserTx) {
return
throw new Error(`Failed to dispense !`)
}
await dispenserTx.wait()
return await datatoken.startOrder(
asset.datatokens[datatokenIndex].address,
orderParams.consumer,
Expand All @@ -133,6 +155,7 @@ export async function orderAsset(
)
const fees = await fre.getFeesInfo(fixedRates[fixedRateIndex].id)
const exchange = await fre.getExchange(fixedRates[fixedRateIndex].id)

const { baseTokenAmount } = await fre.calcBaseInGivenDatatokensOut(
fees.exchangeId,
'1',
Expand Down Expand Up @@ -166,7 +189,7 @@ export async function orderAsset(
)
const txApprove = typeof tx !== 'number' ? await tx.wait() : tx
if (!txApprove) {
return
throw new Error(`Failed to appove ${exchange.baseToken} !`)
}
const freTx = await fre.buyDatatokens(
exchange.exchangeId,
Expand All @@ -177,7 +200,7 @@ export async function orderAsset(
)
const buyDtTx = await freTx.wait()
if (!buyDtTx) {
return
throw new Error(`Failed to buy datatoken from fixed rate!`)
}
return await datatoken.startOrder(
asset.datatokens[datatokenIndex].address,
Expand All @@ -202,11 +225,12 @@ export async function orderAsset(
if (!txApprove) {
return
}
return await datatoken.buyFromFreAndOrder(
const txBuy = await datatoken.buyFromFreAndOrder(
asset.datatokens[datatokenIndex].address,
orderParams,
freParams
)
return txBuy
}
break
}
Expand Down
Loading