Skip to content
This repository has been archived by the owner on Aug 12, 2023. It is now read-only.

Commit

Permalink
Implement fixes and improvements related to fees (#409)
Browse files Browse the repository at this point in the history
* Handle edge cases in fees gracefully

* Normalize fees in fill response
  • Loading branch information
cbovis authored Jun 21, 2020
1 parent a1b68d8 commit f3019fc
Show file tree
Hide file tree
Showing 11 changed files with 441 additions and 76 deletions.
90 changes: 62 additions & 28 deletions src/app/routes/v1/util/__snapshots__/transform-fill.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,38 @@ Object {
],
"date": "2018-09-06T10:47:38.000Z",
"feeRecipient": "0xe269e891a2ec8585a378882ffa531141205e92e9",
"fees": undefined,
"fees": Array [
Object {
"amount": Object {
"USD": 9.13,
"token": "15",
},
"token": Object {
"address": "0xe41d2489571d322189246dafa5ebde1f4699f498",
"id": null,
"name": "0x Protocol Token",
"symbol": "ZRX",
"type": "erc-20",
},
"traderType": "maker",
},
Object {
"amount": Object {
"USD": 3.04,
"token": "5",
},
"token": Object {
"address": "0xe41d2489571d322189246dafa5ebde1f4699f498",
"id": null,
"name": "0x Protocol Token",
"symbol": "ZRX",
"type": "erc-20",
},
"traderType": "taker",
},
],
"id": "5b9107e00d05f400042e3494",
"makerAddress": "0x8dd688660ec0babd0b8a2f2de3232645f73cc5eb",
"makerFee": Object {
"USD": 9.13,
"ZRX": "15",
},
"orderHash": "0xd7cbdddb68cfa6216e867227a4cb8ca281e0d82921000b4b977d6038535482f5",
"protocolFee": undefined,
"protocolVersion": 1,
Expand All @@ -90,14 +115,6 @@ Object {
"senderAddress": undefined,
"status": "successful",
"takerAddress": "0xe269e891a2ec8585a378882ffa531141205e92e9",
"takerFee": Object {
"USD": 3.04,
"ZRX": "5",
},
"totalFees": Object {
"USD": 12.170000000000002,
"ZRX": "20",
},
"transactionHash": "0x4c69e925be055eec7bc49cc681fd5267d1aca7e1db21f687ab19ab9d75b7447c",
"value": Object {
"USD": 1601.76195501,
Expand All @@ -119,7 +136,7 @@ Object {
"tokenSymbol": undefined,
"tokenType": undefined,
"traderType": "maker",
"type": undefined,
"type": null,
}
`;

Expand All @@ -137,7 +154,7 @@ Object {
"tokenSymbol": undefined,
"tokenType": undefined,
"traderType": "taker",
"type": undefined,
"type": null,
}
`;

Expand Down Expand Up @@ -177,13 +194,38 @@ Object {
],
"date": "2018-09-06T10:47:38.000Z",
"feeRecipient": "0xe269e891a2ec8585a378882ffa531141205e92e9",
"fees": undefined,
"fees": Array [
Object {
"amount": Object {
"USD": 0.2,
"token": "15",
},
"token": Object {
"address": "0xe41d2489571d322189246dafa5ebde1f4699f498",
"id": null,
"name": "0x Protocol Token",
"symbol": "ZRX",
"type": "erc-20",
},
"traderType": "maker",
},
Object {
"amount": Object {
"USD": 0.3,
"token": "5",
},
"token": Object {
"address": "0xe41d2489571d322189246dafa5ebde1f4699f498",
"id": null,
"name": "0x Protocol Token",
"symbol": "ZRX",
"type": "erc-20",
},
"traderType": "taker",
},
],
"id": "5b9107e00d05f400042e3494",
"makerAddress": "0x8dd688660ec0babd0b8a2f2de3232645f73cc5eb",
"makerFee": Object {
"USD": 0.2,
"ZRX": "15",
},
"orderHash": "0xd7cbdddb68cfa6216e867227a4cb8ca281e0d82921000b4b977d6038535482f5",
"protocolFee": undefined,
"protocolVersion": 2,
Expand All @@ -195,14 +237,6 @@ Object {
"senderAddress": undefined,
"status": "successful",
"takerAddress": "0xe269e891a2ec8585a378882ffa531141205e92e9",
"takerFee": Object {
"USD": 0.3,
"ZRX": "5",
},
"totalFees": Object {
"USD": 0.5,
"ZRX": "20",
},
"transactionHash": "0x4c69e925be055eec7bc49cc681fd5267d1aca7e1db21f687ab19ab9d75b7447c",
"value": Object {
"USD": 37.77675,
Expand Down
35 changes: 1 addition & 34 deletions src/app/routes/v1/util/transform-fill.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
const _ = require('lodash');

const {
ETH_TOKEN_DECIMALS,
ZRX_TOKEN_DECIMALS,
} = require('../../../../constants');
const { ETH_TOKEN_DECIMALS } = require('../../../../constants');
const formatFillStatus = require('../../../../fills/format-fill-status');
const formatTokenAmount = require('../../../../tokens/format-token-amount');
const getAssetsForFill = require('../../../../fills/get-assets-for-fill');
Expand All @@ -17,33 +14,6 @@ const transformFill = fill => {
const fees = getFeesForFill(fill);
const conversions = _.get(fill, `conversions.USD`);

const makerFee =
fill.makerFee !== undefined
? {
USD: _.get(conversions, 'makerFee'),
ZRX: formatTokenAmount(fill.makerFee, ZRX_TOKEN_DECIMALS),
}
: undefined;

const takerFee =
fill.takerFee !== undefined
? {
USD: _.get(conversions, 'takerFee'),
ZRX: formatTokenAmount(fill.takerFee, ZRX_TOKEN_DECIMALS),
}
: undefined;

const totalFees =
takerFee !== undefined || makerFee !== undefined
? {
USD: makerFee.USD + takerFee.USD,
ZRX:
makerFee.ZRX === undefined
? undefined
: makerFee.ZRX.plus(takerFee.ZRX),
}
: undefined;

const protocolFee =
fill.protocolFee !== undefined
? {
Expand All @@ -59,16 +29,13 @@ const transformFill = fill => {
feeRecipient: fill.feeRecipient,
id: fill.id,
makerAddress: fill.maker,
makerFee,
orderHash: fill.orderHash,
protocolFee,
protocolVersion: fill.protocolVersion,
relayer: formatRelayer(fill.relayer),
senderAddress: fill.senderAddress,
status: formatFillStatus(fill.status),
takerAddress: fill.taker,
takerFee,
totalFees,
transactionHash: fill.transactionHash,
value: _.has(conversions, 'amount')
? {
Expand Down
4 changes: 2 additions & 2 deletions src/app/routes/v1/util/transform-fill.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,15 +215,15 @@ describe('transformFill', () => {
amount: { token: new BigNumber('0.005'), USD: 0.3 },
token: {
address: '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
id: undefined,
id: null,
name: 'Wrapped Ether',
symbol: 'WETH',
type: 'erc-20',
},
traderType: 'maker',
},
{
amount: { token: new BigNumber('1'), USD: undefined },
amount: { token: new BigNumber('1'), USD: null },
token: {
address: '0xf5b0a3efb8e8e4c201e2a935f110eaaf3ffecb8d',
id: 58,
Expand Down
4 changes: 4 additions & 0 deletions src/app/routes/v1/util/transform-token.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ it('should only return white-listed fields', () => {
imageUrl: 'http://tmpuri.org/test.jpg',
name: 'DAI Stablecoin',
symbol: 'DAI',
type: 0,
};
const transformed = transformToken(token);

Expand All @@ -48,6 +49,7 @@ it('should only return white-listed fields', () => {
},
symbol: 'DAI',
totalSupply: null,
type: 'erc-20',
});
});

Expand All @@ -56,6 +58,7 @@ it('should transform tokens which dont have an image url', () => {
address: '0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359',
name: 'DAI Stablecoin',
symbol: 'DAI',
type: 0,
};
const transformed = transformToken(token);

Expand All @@ -76,5 +79,6 @@ it('should transform tokens which dont have an image url', () => {
},
symbol: 'DAI',
totalSupply: null,
type: 'erc-20',
});
});
59 changes: 50 additions & 9 deletions src/fills/get-fees-for-fill.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,70 @@
const _ = require('lodash');

const { ZRX_TOKEN_DECIMALS } = require('../constants');
const formatTokenAmount = require('../tokens/format-token-amount');
const formatTokenType = require('../tokens/format-token-type');
const formatTraderType = require('../traders/format-trader-type');

const transformFee = fee => {
const tokenAmount = formatTokenAmount(_.get(fee, 'amount.token'), fee.token);

return {
amount: {
token: formatTokenAmount(_.get(fee, 'amount.token'), fee.token),
USD: _.get(fee, 'amount.USD'),
token: tokenAmount,
USD: _.get(fee, 'amount.USD', null),
},
token: {
address: fee.tokenAddress,
id: fee.tokenId,
name: _.get(fee.token, 'name'),
symbol: _.get(fee.token, 'symbol'),
id: _.get(fee, 'tokenId', null),
name: _.get(fee.token, 'name', null),
symbol: _.get(fee.token, 'symbol', null),
type: formatTokenType(_.get(fee.token, 'type')),
},
traderType: formatTraderType(fee.traderType),
};
};

const getFeesForFill = fill =>
fill.protocolVersion < 3
? undefined
: _.map(fill.fees, fee => transformFee(fee));
const ZRX_TOKEN = {
address: '0xe41d2489571d322189246dafa5ebde1f4699f498',
id: null,
name: '0x Protocol Token',
symbol: 'ZRX',
type: 'erc-20',
};

const getFeesForFill = fill => {
const conversions = _.get(fill, `conversions.USD`, {});
const makerFee = _.get(fill, 'makerFee', 0);
const takerFee = _.get(fill, 'takerFee', 0);

if (makerFee > 0 || takerFee > 0) {
return _.compact([
makerFee === 0
? undefined
: {
amount: {
token: formatTokenAmount(makerFee, ZRX_TOKEN_DECIMALS),
USD: _.get(conversions, 'makerFee'),
},
token: ZRX_TOKEN,
traderType: 'maker',
},
takerFee === 0
? undefined
: {
amount: {
token: formatTokenAmount(takerFee, ZRX_TOKEN_DECIMALS),
USD: _.get(conversions, 'takerFee'),
},
token: ZRX_TOKEN,
traderType: 'taker',
},
]);
}

return _.get(fill, 'fees', [])
.filter(fee => fee.amount.token > 0)
.map(transformFee);
};

module.exports = getFeesForFill;
Loading

0 comments on commit f3019fc

Please sign in to comment.