Skip to content

Commit

Permalink
Add fee message on exchange estimate
Browse files Browse the repository at this point in the history
  • Loading branch information
patitonar committed Apr 27, 2020
1 parent ada93ab commit 1a9ccdc
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import BN from 'bn.js'
import { Bridge, ValueTypes } from '@burner-wallet/exchange'
import { waitForEvent } from '../../utils'
import { Bridge, EstimateReturn, ValueTypes } from '@burner-wallet/exchange'
import { waitForEvent, constants } from '../../utils'
import { MEDIATOR_ABI, MEDIATOR_FEE_MANAGER_ABI, ZERO_ADDRESS } from '../../../../../commons'
import { toBN } from 'web3-utils'

Expand Down Expand Up @@ -42,7 +42,8 @@ export default class Mediator extends Bridge {
}
}

async estimateAtoB(value: ValueTypes) {
// @ts-ignore
async estimateAtoB(value: ValueTypes): Promise<EstimateReturn> {
const web3 = this.getExchange()
.getAsset(this.assetB)
.getWeb3()
Expand All @@ -53,10 +54,14 @@ export default class Mediator extends Bridge {
const feeAmount = await this.getFeeAmount(web3, contract, userAmount)
const finalAmount = toBN(userAmount).sub(feeAmount)

return finalAmount.toString()
return {
estimate: finalAmount.toString(),
estimateInfo: feeAmount.isZero() ? null : constants.ESTIMATE_FEE_MESSAGE
}
}

async estimateBtoA(value: ValueTypes) {
// @ts-ignore
async estimateBtoA(value: ValueTypes): Promise<EstimateReturn> {
const web3 = this.getExchange()
.getAsset(this.assetA)
.getWeb3()
Expand All @@ -67,7 +72,10 @@ export default class Mediator extends Bridge {
const feeAmount = await this.getFeeAmount(web3, contract, userAmount)
const finalAmount = toBN(userAmount).sub(feeAmount)

return finalAmount.toString()
return {
estimate: finalAmount.toString(),
estimateInfo: feeAmount.isZero() ? null : constants.ESTIMATE_FEE_MESSAGE
}
}

async getFeeAmount(web3, contract, value): Promise<BN> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ export const wait = (time: number) => new Promise(resolve => setTimeout(resolve,

export const constants = {
EXCHANGE_TIMEOUT: 300000,
MAX_FEE: toWei('1', 'ether')
MAX_FEE: toWei('1', 'ether'),
ESTIMATE_FEE_MESSAGE: 'Estimation takes fee charges into consideration.'
}

export const waitForEvent = async (web3, contract: Contract, event: string, callback: Function) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ export default class WETCBridge extends Mediator {
.div(toBN(constants.MAX_FEE))
const finalAmount = toBN(this._getValue(value)).sub(feeAmount)

return finalAmount.toString()
return {
estimate: finalAmount.toString(),
estimateInfo: feeAmount.isZero() ? null : constants.ESTIMATE_FEE_MESSAGE
}
} else {
return await super.estimateAtoB(value)
}
Expand All @@ -83,7 +86,10 @@ export default class WETCBridge extends Mediator {
.div(toBN(constants.MAX_FEE))
const finalAmount = toBN(this._getValue(value)).sub(feeAmount)

return finalAmount.toString()
return {
estimate: finalAmount.toString(),
estimateInfo: feeAmount.isZero() ? null : constants.ESTIMATE_FEE_MESSAGE
}
} else {
return await super.estimateBtoA(value)
}
Expand Down

0 comments on commit 1a9ccdc

Please sign in to comment.