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

Commit

Permalink
Use model population on fills (#337)
Browse files Browse the repository at this point in the history
* Use model population for fill relayer

* Fetch fill tokens using model population

* Add mongodb debug option

* Fix tests

* Tweak some config
  • Loading branch information
cbovis authored Feb 21, 2020
1 parent 8723108 commit ebefd7c
Show file tree
Hide file tree
Showing 15 changed files with 94 additions and 142 deletions.
5 changes: 0 additions & 5 deletions src/app/configure.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
const config = require('config');
const signale = require('signale');

const db = require('../util/db');
const elasticsearch = require('../util/elasticsearch');
const errorLogger = require('../util/error-logger');

const logger = signale.scope('application');

const configure = () => {
errorLogger.configure({
appVersion: config.get('appVersion'),
Expand All @@ -22,8 +19,6 @@ const configure = () => {
password: config.get('elasticsearch.password'),
},
});

logger.success('application configured');
};

module.exports = configure;
9 changes: 1 addition & 8 deletions src/app/initialise.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
const config = require('config');

const configure = require('./configure');
const tokenCache = require('../tokens/token-cache');

const initialise = async () => {
const initialise = () => {
configure();

await tokenCache.initialise({
pollingInterval: config.get('tokenCache.pollingInterval'),
});
};

module.exports = initialise;
20 changes: 9 additions & 11 deletions src/app/routes/v1/fills.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ const moment = require('moment');
const mongoose = require('mongoose');
const Router = require('koa-router');

const { getTokens } = require('../../../tokens/token-cache');
const Fill = require('../../../model/fill');
const getRelayerLookupId = require('../../../relayers/get-relayer-lookup-id');
const getRelayers = require('../../../relayers/get-relayers');
const InvalidParameterError = require('../../errors/invalid-parameter-error');
const pagination = require('../../middleware/pagination');
const reverseMapStatus = require('../../../fills/reverse-map-status');
Expand Down Expand Up @@ -166,11 +164,8 @@ const createRouter = () => {
{ limit, page },
);

const tokens = getTokens();
const relayers = await getRelayers();

response.body = {
fills: transformFills(tokens, relayers, docs),
fills: transformFills(docs),
limit,
page,
pageCount: pages,
Expand All @@ -184,7 +179,13 @@ const createRouter = () => {
router.get('/:id', async ({ params, response }, next) => {
const fillId = params.id;
const fill = mongoose.Types.ObjectId.isValid(fillId)
? await Fill.findById(fillId)
? await Fill.findById(fillId, undefined, {
populate: [
{ path: 'relayer', select: 'imageUrl name slug' },
{ path: 'assets.token', select: 'decimals name symbol type' },
{ path: 'fees.token', select: 'decimals name symbol type' },
],
})
: null;

if (fill === null) {
Expand All @@ -193,10 +194,7 @@ const createRouter = () => {
return;
}

const tokens = getTokens();
const relayers = await getRelayers();

response.body = transformFill(tokens, relayers, fill);
response.body = transformFill(fill);

await next();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ Object {
"price": Object {
"USD": 224.42000000000002,
},
"tokenAddress": "0x1234",
"tokenAddress": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
"tokenId": undefined,
"tokenSymbol": undefined,
"tokenType": undefined,
Expand All @@ -126,7 +126,7 @@ Object {
"price": Object {
"USD": 0.0053392065167000005,
},
"tokenAddress": "0x9999",
"tokenAddress": "0xd7732e3783b0047aa251928960063f863ad022d8",
"tokenId": undefined,
"tokenSymbol": undefined,
"tokenType": undefined,
Expand Down
9 changes: 4 additions & 5 deletions src/app/routes/v1/util/transform-fill.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ const getFeesForFill = require('../../../../fills/get-fees-for-fill');
const formatRelayer = relayer =>
relayer === undefined ? null : _.pick(relayer, 'slug', 'name', 'imageUrl');

const transformFill = (tokens, relayers, fill) => {
const assets = getAssetsForFill(tokens, fill);
const fees = getFeesForFill(tokens, fill);
const transformFill = fill => {
const assets = getAssetsForFill(fill);
const fees = getFeesForFill(fill);
const conversions = _.get(fill, `conversions.USD`);
const fillRelayer = _.find(relayers, { lookupId: fill.relayerId });

const makerFee =
fill.makerFee !== undefined
Expand Down Expand Up @@ -64,7 +63,7 @@ const transformFill = (tokens, relayers, fill) => {
orderHash: fill.orderHash,
protocolFee,
protocolVersion: fill.protocolVersion,
relayer: formatRelayer(fillRelayer),
relayer: formatRelayer(fill.relayer),
senderAddress: fill.senderAddress,
status: formatFillStatus(fill.status),
takerAddress: fill.taker,
Expand Down
54 changes: 25 additions & 29 deletions src/app/routes/v1/util/transform-fill.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ const { FILL_ACTOR } = require('../../../../constants');
const AXIE_FIXTURE = require('../../../../fixtures/tokens/axie');
const BRAHMA_FIXTURE = require('../../../../fixtures/tokens/brahma');
const ETHFINEX_FIXTURE = require('../../../../fixtures/relayers/ethfinex');
const Fill = require('../../../../model/fill');
const WETH_FIXTURE = require('../../../../fixtures/tokens/weth');
const ZRX_FIXTURE = require('../../../../fixtures/tokens/zrx');

const transformFill = require('./transform-fill');

Expand All @@ -19,6 +17,7 @@ const axieMaker = {
},
tokenAddress: '0xf5b0a3efb8e8e4c201e2a935f110eaaf3ffecb8d',
tokenId: 43381,
token: AXIE_FIXTURE,
};

const wethTaker = {
Expand All @@ -28,13 +27,15 @@ const wethTaker = {
USD: 137.36999999999998,
},
tokenAddress: '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
token: WETH_FIXTURE,
};

const brahmaTaker = {
actor: FILL_ACTOR.TAKER,
amount: 3e23,
price: { USD: 0.0053392065167000005 },
tokenAddress: '0xd7732e3783b0047aa251928960063f863ad022d8',
token: BRAHMA_FIXTURE,
};

const wethMaker = {
Expand All @@ -44,6 +45,7 @@ const wethMaker = {
USD: 224.42000000000002,
},
tokenAddress: '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
token: WETH_FIXTURE,
};

const simpleFill = {
Expand All @@ -65,6 +67,7 @@ const simpleFill = {
'0xd7cbdddb68cfa6216e867227a4cb8ca281e0d82921000b4b977d6038535482f5',
protocolVersion: 2,
relayerId: 21,
relayer: ETHFINEX_FIXTURE,
status: 1,
taker: '0xe269e891a2ec8585a378882ffa531141205e92e9',
takerFee: 5000000000000000000,
Expand All @@ -86,35 +89,26 @@ const simpleV1Fill = {
protocolVersion: 1,
};

const simpleTokens = {
[AXIE_FIXTURE.address]: AXIE_FIXTURE,
[BRAHMA_FIXTURE.address]: BRAHMA_FIXTURE,
[WETH_FIXTURE.address]: WETH_FIXTURE,
[ZRX_FIXTURE.address]: ZRX_FIXTURE,
};

const relayers = [ETHFINEX_FIXTURE];

describe('transformFill', () => {
it('should transform V1 fill', () => {
const viewModel = transformFill(simpleTokens, relayers, simpleV1Fill);
const viewModel = transformFill(simpleV1Fill);

expect(viewModel).toMatchSnapshot();
});

it('should transform fill without relayer', () => {
const fill = { ...simpleFill, relayerId: undefined };
const viewModel = transformFill(simpleTokens, relayers, fill);
const fill = { ...simpleFill, relayerId: undefined, relayer: undefined };
const viewModel = transformFill(fill);

expect(viewModel.relayer).toBeNull();
});

it('should transform V1 fill with unrecognised maker asset', () => {
const fill = {
...simpleV1Fill,
assets: [{ ...wethMaker, tokenAddress: '0x1234' }, brahmaTaker],
assets: [{ ...wethMaker, token: undefined }, brahmaTaker],
};
const viewModel = transformFill(simpleTokens, relayers, fill);
const viewModel = transformFill(fill);

expect(
viewModel.assets.find(asset => asset.traderType === 'maker'),
Expand All @@ -124,64 +118,64 @@ describe('transformFill', () => {
it('should transform V1 fill with unrecognised taker asset', () => {
const fill = {
...simpleV1Fill,
assets: [wethMaker, { ...brahmaTaker, tokenAddress: '0x9999' }],
assets: [wethMaker, { ...brahmaTaker, token: undefined }],
};
const viewModel = transformFill(simpleTokens, relayers, fill);
const viewModel = transformFill(fill);

expect(
viewModel.assets.find(asset => asset.traderType === 'taker'),
).toMatchSnapshot();
});

it('should transform fill with unrecognised relayer', () => {
const fill = { ...simpleFill, relayerId: 999 };
const viewModel = transformFill(simpleTokens, relayers, fill);
const fill = { ...simpleFill, relayer: undefined };
const viewModel = transformFill(fill);

expect(viewModel.relayer).toBeNull();
});

it('should transform pending fill', () => {
const fill = { ...simpleFill, status: 0 };
const viewModel = transformFill(simpleTokens, relayers, fill);
const viewModel = transformFill(fill);

expect(viewModel.status).toBe('pending');
});

it('should transform successful fill', () => {
const viewModel = transformFill(simpleTokens, relayers, simpleFill);
const viewModel = transformFill(simpleFill);

expect(viewModel.status).toBe('successful');
});

it('should transform failed fill', () => {
const fill = { ...simpleV1Fill, status: 2 };
const viewModel = transformFill(simpleTokens, relayers, fill);
const viewModel = transformFill(fill);

expect(viewModel.status).toBe('failed');
});

it('should transform V2 fill', () => {
const viewModel = transformFill(simpleTokens, relayers, simpleFill);
const viewModel = transformFill(simpleFill);

expect(viewModel).toMatchSnapshot();
});

it('should transform ERC721 asset', () => {
const viewModel = transformFill(simpleTokens, relayers, simpleFill);
const viewModel = transformFill(simpleFill);
const asset = _.find(viewModel.assets, { traderType: 'maker' });

expect(asset).toMatchSnapshot();
});

it('should transform ERC20 asset', () => {
const viewModel = transformFill(simpleTokens, relayers, simpleFill);
const viewModel = transformFill(simpleFill);
const asset = _.find(viewModel.assets, { traderType: 'taker' });

expect(asset).toMatchSnapshot();
});

it('should transform V3 fill', () => {
const fill = new Fill({
const fill = {
...simpleFill,
conversions: {
USD: {
Expand All @@ -192,11 +186,13 @@ describe('transformFill', () => {
{
amount: { token: 5000000000000000, USD: 0.3 },
tokenAddress: '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
token: WETH_FIXTURE,
traderType: 0,
},
{
amount: { token: 1 },
tokenAddress: '0xf5b0a3efb8e8e4c201e2a935f110eaaf3ffecb8d',
token: AXIE_FIXTURE,
tokenId: 58,
traderType: 1,
},
Expand All @@ -205,8 +201,8 @@ describe('transformFill', () => {
protocolVersion: 3,
makerFee: undefined,
takerFee: undefined,
});
const viewModel = transformFill(simpleTokens, relayers, fill);
};
const viewModel = transformFill(fill);

expect(viewModel.makerFee).toBeUndefined();
expect(viewModel.takerFee).toBeUndefined();
Expand Down
10 changes: 4 additions & 6 deletions src/app/routes/v1/util/transform-fills.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,17 @@ const getAssetsForFill = require('../../../../fills/get-assets-for-fill');
const transformRelayer = relayer =>
relayer === undefined ? null : _.pick(relayer, 'slug', 'name', 'imageUrl');

const transformFill = (tokens, relayers, fill) => {
const assets = getAssetsForFill(tokens, fill);
const transformFill = fill => {
const assets = getAssetsForFill(fill);
const conversions = _.get(fill, `conversions.USD`);
const fillRelayer = _.find(relayers, { lookupId: fill.relayerId });

return {
assets,
date: fill.date,
feeRecipient: fill.feeRecipient,
id: fill.id,
makerAddress: fill.maker,
relayer: transformRelayer(fillRelayer),
relayer: transformRelayer(fill.relayer),
status: formatFillStatus(fill.status),
takerAddress: fill.taker,
value: _.has(conversions, 'amount')
Expand All @@ -28,7 +27,6 @@ const transformFill = (tokens, relayers, fill) => {
};
};

const transformFills = (tokens, relayers, fills) =>
fills.map(fill => transformFill(tokens, relayers, fill));
const transformFills = fills => fills.map(fill => transformFill(fill));

module.exports = transformFills;
4 changes: 2 additions & 2 deletions src/app/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ const start = port => {
app.listen(port);

if (process.env.NODE_ENV === 'development') {
logger.start(`serving application at http://localhost:${port}`);
logger.info(`serving application at http://localhost:${port}`);
} else {
logger.start(`serving application on port ${port}`);
logger.info(`serving application on port ${port}`);
}
};

Expand Down
Loading

0 comments on commit ebefd7c

Please sign in to comment.