From 35ce962899ae9d5648586e7825f301fdb1ddf273 Mon Sep 17 00:00:00 2001 From: Xaber Date: Mon, 13 Jan 2020 15:41:31 +0800 Subject: [PATCH 1/4] feat: support fee engine --- src/router/newOrder.ts | 11 ++++++----- src/utils/order.ts | 25 +++++++++++++++++++++---- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/router/newOrder.ts b/src/router/newOrder.ts index dbc67d6..506542e 100644 --- a/src/router/newOrder.ts +++ b/src/router/newOrder.ts @@ -37,19 +37,20 @@ export const newOrder = async (ctx) => { } else { const { rate, minAmount, maxAmount, quoteId } = rateBody - const { userAddr } = query + const { userAddr, feefactor } = query const config = updaterStack.markerMakerConfigUpdater.cacheResult const tokenConfigs = updaterStack.tokenConfigsFromImtokenUpdater.cacheResult const tokenList = getSupportedTokens() try { - const orderFormated = getFormatedSignedOrder( - ctx.query, + const orderFormated = getFormatedSignedOrder({ + simpleOrder: ctx.query, rate, - userAddr.toLowerCase(), + userAddr: userAddr.toLowerCase(), tokenList, tokenConfigs, config, - ) + queryFeeFactor: feefactor, + }) ctx.body = { result: true, exchangeable: true, diff --git a/src/utils/order.ts b/src/utils/order.ts index ca4a840..3085008 100644 --- a/src/utils/order.ts +++ b/src/utils/order.ts @@ -14,7 +14,21 @@ const getFixPrecision = (decimal) => { return decimal < 8 ? decimal : 9 } -const getOrderAndFeeFactor = (simpleOrder, rate, tokenList: Token[], tokenConfigs: TokenConfig[], config: MarketMakerConfig) => { +interface GetOrderAndFeeFactorParams { + simpleOrder: any + rate: number | string + tokenList: Token[] + tokenConfigs: TokenConfig[] + config: MarketMakerConfig + queryFeeFactor?: number +} + +interface GetFormatedSignedOrderParams extends GetOrderAndFeeFactorParams { + userAddr: string +} + +const getOrderAndFeeFactor = (params: GetOrderAndFeeFactorParams) => { + const {simpleOrder, rate, tokenList, tokenConfigs, config, queryFeeFactor } = params const { side, amount } = simpleOrder const baseToken = getTokenBySymbol(tokenList, simpleOrder.base) const quoteToken = getTokenBySymbol(tokenList, simpleOrder.quote) @@ -75,7 +89,9 @@ const getOrderAndFeeFactor = (simpleOrder, rate, tokenList: Token[], tokenConfig } const foundTokenConfig = tokenConfigs.find(t => t.symbol === takerToken.symbol) - const feeFactor = foundTokenConfig && foundTokenConfig.feeFactor ? foundTokenConfig.feeFactor : (config.feeFactor ? config.feeFactor : 0) + const feeFactor = queryFeeFactor && +queryFeeFactor ? +queryFeeFactor : ( + foundTokenConfig && foundTokenConfig.feeFactor ? foundTokenConfig.feeFactor : (config.feeFactor ? config.feeFactor : 0) + ) return { order, @@ -83,8 +99,9 @@ const getOrderAndFeeFactor = (simpleOrder, rate, tokenList: Token[], tokenConfig } } -export const getFormatedSignedOrder = (simpleOrder, rate, userAddr, tokenList: Token[], tokenConfigs: TokenConfig[], config: MarketMakerConfig) => { - const { order, feeFactor } = getOrderAndFeeFactor(simpleOrder, rate, tokenList, tokenConfigs, config) +export const getFormatedSignedOrder = (params: GetFormatedSignedOrderParams) => { + const { userAddr } = params + const { order, feeFactor } = getOrderAndFeeFactor(params) const wallet = getWallet() const o = { From b153dd4a10c042bb74fc7cdb6f1fa6a3a19556bf Mon Sep 17 00:00:00 2001 From: Xaber Date: Mon, 13 Jan 2020 15:41:49 +0800 Subject: [PATCH 2/4] chore: update version --- package.json | 2 +- src/router/version.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 2b378f9..9d3cd4b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "tokenlon-mmsk", - "version": "0.3.0", + "version": "0.4.0", "description": "", "main": "lib/index.js", "types": "src/globals.d.ts", diff --git a/src/router/version.ts b/src/router/version.ts index bb9e923..7224a49 100644 --- a/src/router/version.ts +++ b/src/router/version.ts @@ -1,6 +1,6 @@ export const version = (ctx) => { ctx.body = { result: true, - version: '0.3.0', + version: '0.4.0', } } \ No newline at end of file From 6dd24bf02a852cadf08692c6a0051a627a33bd3a Mon Sep 17 00:00:00 2001 From: Xaber Date: Mon, 13 Jan 2020 16:08:25 +0800 Subject: [PATCH 3/4] chore: optimize code --- src/utils/order.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/order.ts b/src/utils/order.ts index 3085008..c360377 100644 --- a/src/utils/order.ts +++ b/src/utils/order.ts @@ -89,7 +89,7 @@ const getOrderAndFeeFactor = (params: GetOrderAndFeeFactorParams) => { } const foundTokenConfig = tokenConfigs.find(t => t.symbol === takerToken.symbol) - const feeFactor = queryFeeFactor && +queryFeeFactor ? +queryFeeFactor : ( + const feeFactor = queryFeeFactor && +queryFeeFactor > 0 ? +queryFeeFactor : ( foundTokenConfig && foundTokenConfig.feeFactor ? foundTokenConfig.feeFactor : (config.feeFactor ? config.feeFactor : 0) ) From 0eda2925bfa377abfa4e9abcf93e1348462cdf61 Mon Sep 17 00:00:00 2001 From: Xaber Date: Wed, 15 Jan 2020 14:17:17 +0800 Subject: [PATCH 4/4] fix: queryFeeFactor 0 situation --- src/utils/order.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/utils/order.ts b/src/utils/order.ts index c360377..509b62d 100644 --- a/src/utils/order.ts +++ b/src/utils/order.ts @@ -1,5 +1,6 @@ import { MarketMakerConfig, Token, TokenConfig } from '../types' import { assetDataUtils, generatePseudoRandomSalt, orderHashUtils, signatureUtils, SignerType, SignatureType } from '0x.js' +import * as _ from 'lodash' import * as ethUtils from 'ethereumjs-util' import { toBN } from './math' import { getTokenBySymbol } from './token' @@ -89,7 +90,7 @@ const getOrderAndFeeFactor = (params: GetOrderAndFeeFactorParams) => { } const foundTokenConfig = tokenConfigs.find(t => t.symbol === takerToken.symbol) - const feeFactor = queryFeeFactor && +queryFeeFactor > 0 ? +queryFeeFactor : ( + const feeFactor = !_.isUndefined(queryFeeFactor) && !_.isNaN(+queryFeeFactor) && +queryFeeFactor >= 0 ? +queryFeeFactor : ( foundTokenConfig && foundTokenConfig.feeFactor ? foundTokenConfig.feeFactor : (config.feeFactor ? config.feeFactor : 0) )