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

Commit ebefd7c

Browse files
authored
Use model population on fills (#337)
* Use model population for fill relayer * Fetch fill tokens using model population * Add mongodb debug option * Fix tests * Tweak some config
1 parent 8723108 commit ebefd7c

15 files changed

Lines changed: 94 additions & 142 deletions

src/app/configure.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
const config = require('config');
2-
const signale = require('signale');
32

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

8-
const logger = signale.scope('application');
9-
107
const configure = () => {
118
errorLogger.configure({
129
appVersion: config.get('appVersion'),
@@ -22,8 +19,6 @@ const configure = () => {
2219
password: config.get('elasticsearch.password'),
2320
},
2421
});
25-
26-
logger.success('application configured');
2722
};
2823

2924
module.exports = configure;

src/app/initialise.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
1-
const config = require('config');
2-
31
const configure = require('./configure');
4-
const tokenCache = require('../tokens/token-cache');
52

6-
const initialise = async () => {
3+
const initialise = () => {
74
configure();
8-
9-
await tokenCache.initialise({
10-
pollingInterval: config.get('tokenCache.pollingInterval'),
11-
});
125
};
136

147
module.exports = initialise;

src/app/routes/v1/fills.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@ const moment = require('moment');
33
const mongoose = require('mongoose');
44
const Router = require('koa-router');
55

6-
const { getTokens } = require('../../../tokens/token-cache');
76
const Fill = require('../../../model/fill');
87
const getRelayerLookupId = require('../../../relayers/get-relayer-lookup-id');
9-
const getRelayers = require('../../../relayers/get-relayers');
108
const InvalidParameterError = require('../../errors/invalid-parameter-error');
119
const pagination = require('../../middleware/pagination');
1210
const reverseMapStatus = require('../../../fills/reverse-map-status');
@@ -166,11 +164,8 @@ const createRouter = () => {
166164
{ limit, page },
167165
);
168166

169-
const tokens = getTokens();
170-
const relayers = await getRelayers();
171-
172167
response.body = {
173-
fills: transformFills(tokens, relayers, docs),
168+
fills: transformFills(docs),
174169
limit,
175170
page,
176171
pageCount: pages,
@@ -184,7 +179,13 @@ const createRouter = () => {
184179
router.get('/:id', async ({ params, response }, next) => {
185180
const fillId = params.id;
186181
const fill = mongoose.Types.ObjectId.isValid(fillId)
187-
? await Fill.findById(fillId)
182+
? await Fill.findById(fillId, undefined, {
183+
populate: [
184+
{ path: 'relayer', select: 'imageUrl name slug' },
185+
{ path: 'assets.token', select: 'decimals name symbol type' },
186+
{ path: 'fees.token', select: 'decimals name symbol type' },
187+
],
188+
})
188189
: null;
189190

190191
if (fill === null) {
@@ -193,10 +194,7 @@ const createRouter = () => {
193194
return;
194195
}
195196

196-
const tokens = getTokens();
197-
const relayers = await getRelayers();
198-
199-
response.body = transformFill(tokens, relayers, fill);
197+
response.body = transformFill(fill);
200198

201199
await next();
202200
});

src/app/routes/v1/util/__snapshots__/transform-fill.test.js.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ Object {
109109
"price": Object {
110110
"USD": 224.42000000000002,
111111
},
112-
"tokenAddress": "0x1234",
112+
"tokenAddress": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
113113
"tokenId": undefined,
114114
"tokenSymbol": undefined,
115115
"tokenType": undefined,
@@ -126,7 +126,7 @@ Object {
126126
"price": Object {
127127
"USD": 0.0053392065167000005,
128128
},
129-
"tokenAddress": "0x9999",
129+
"tokenAddress": "0xd7732e3783b0047aa251928960063f863ad022d8",
130130
"tokenId": undefined,
131131
"tokenSymbol": undefined,
132132
"tokenType": undefined,

src/app/routes/v1/util/transform-fill.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,10 @@ const getFeesForFill = require('../../../../fills/get-fees-for-fill');
1212
const formatRelayer = relayer =>
1313
relayer === undefined ? null : _.pick(relayer, 'slug', 'name', 'imageUrl');
1414

15-
const transformFill = (tokens, relayers, fill) => {
16-
const assets = getAssetsForFill(tokens, fill);
17-
const fees = getFeesForFill(tokens, fill);
15+
const transformFill = fill => {
16+
const assets = getAssetsForFill(fill);
17+
const fees = getFeesForFill(fill);
1818
const conversions = _.get(fill, `conversions.USD`);
19-
const fillRelayer = _.find(relayers, { lookupId: fill.relayerId });
2019

2120
const makerFee =
2221
fill.makerFee !== undefined
@@ -64,7 +63,7 @@ const transformFill = (tokens, relayers, fill) => {
6463
orderHash: fill.orderHash,
6564
protocolFee,
6665
protocolVersion: fill.protocolVersion,
67-
relayer: formatRelayer(fillRelayer),
66+
relayer: formatRelayer(fill.relayer),
6867
senderAddress: fill.senderAddress,
6968
status: formatFillStatus(fill.status),
7069
takerAddress: fill.taker,

src/app/routes/v1/util/transform-fill.test.js

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ const { FILL_ACTOR } = require('../../../../constants');
55
const AXIE_FIXTURE = require('../../../../fixtures/tokens/axie');
66
const BRAHMA_FIXTURE = require('../../../../fixtures/tokens/brahma');
77
const ETHFINEX_FIXTURE = require('../../../../fixtures/relayers/ethfinex');
8-
const Fill = require('../../../../model/fill');
98
const WETH_FIXTURE = require('../../../../fixtures/tokens/weth');
10-
const ZRX_FIXTURE = require('../../../../fixtures/tokens/zrx');
119

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

@@ -19,6 +17,7 @@ const axieMaker = {
1917
},
2018
tokenAddress: '0xf5b0a3efb8e8e4c201e2a935f110eaaf3ffecb8d',
2119
tokenId: 43381,
20+
token: AXIE_FIXTURE,
2221
};
2322

2423
const wethTaker = {
@@ -28,13 +27,15 @@ const wethTaker = {
2827
USD: 137.36999999999998,
2928
},
3029
tokenAddress: '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
30+
token: WETH_FIXTURE,
3131
};
3232

3333
const brahmaTaker = {
3434
actor: FILL_ACTOR.TAKER,
3535
amount: 3e23,
3636
price: { USD: 0.0053392065167000005 },
3737
tokenAddress: '0xd7732e3783b0047aa251928960063f863ad022d8',
38+
token: BRAHMA_FIXTURE,
3839
};
3940

4041
const wethMaker = {
@@ -44,6 +45,7 @@ const wethMaker = {
4445
USD: 224.42000000000002,
4546
},
4647
tokenAddress: '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
48+
token: WETH_FIXTURE,
4749
};
4850

4951
const simpleFill = {
@@ -65,6 +67,7 @@ const simpleFill = {
6567
'0xd7cbdddb68cfa6216e867227a4cb8ca281e0d82921000b4b977d6038535482f5',
6668
protocolVersion: 2,
6769
relayerId: 21,
70+
relayer: ETHFINEX_FIXTURE,
6871
status: 1,
6972
taker: '0xe269e891a2ec8585a378882ffa531141205e92e9',
7073
takerFee: 5000000000000000000,
@@ -86,35 +89,26 @@ const simpleV1Fill = {
8689
protocolVersion: 1,
8790
};
8891

89-
const simpleTokens = {
90-
[AXIE_FIXTURE.address]: AXIE_FIXTURE,
91-
[BRAHMA_FIXTURE.address]: BRAHMA_FIXTURE,
92-
[WETH_FIXTURE.address]: WETH_FIXTURE,
93-
[ZRX_FIXTURE.address]: ZRX_FIXTURE,
94-
};
95-
96-
const relayers = [ETHFINEX_FIXTURE];
97-
9892
describe('transformFill', () => {
9993
it('should transform V1 fill', () => {
100-
const viewModel = transformFill(simpleTokens, relayers, simpleV1Fill);
94+
const viewModel = transformFill(simpleV1Fill);
10195

10296
expect(viewModel).toMatchSnapshot();
10397
});
10498

10599
it('should transform fill without relayer', () => {
106-
const fill = { ...simpleFill, relayerId: undefined };
107-
const viewModel = transformFill(simpleTokens, relayers, fill);
100+
const fill = { ...simpleFill, relayerId: undefined, relayer: undefined };
101+
const viewModel = transformFill(fill);
108102

109103
expect(viewModel.relayer).toBeNull();
110104
});
111105

112106
it('should transform V1 fill with unrecognised maker asset', () => {
113107
const fill = {
114108
...simpleV1Fill,
115-
assets: [{ ...wethMaker, tokenAddress: '0x1234' }, brahmaTaker],
109+
assets: [{ ...wethMaker, token: undefined }, brahmaTaker],
116110
};
117-
const viewModel = transformFill(simpleTokens, relayers, fill);
111+
const viewModel = transformFill(fill);
118112

119113
expect(
120114
viewModel.assets.find(asset => asset.traderType === 'maker'),
@@ -124,64 +118,64 @@ describe('transformFill', () => {
124118
it('should transform V1 fill with unrecognised taker asset', () => {
125119
const fill = {
126120
...simpleV1Fill,
127-
assets: [wethMaker, { ...brahmaTaker, tokenAddress: '0x9999' }],
121+
assets: [wethMaker, { ...brahmaTaker, token: undefined }],
128122
};
129-
const viewModel = transformFill(simpleTokens, relayers, fill);
123+
const viewModel = transformFill(fill);
130124

131125
expect(
132126
viewModel.assets.find(asset => asset.traderType === 'taker'),
133127
).toMatchSnapshot();
134128
});
135129

136130
it('should transform fill with unrecognised relayer', () => {
137-
const fill = { ...simpleFill, relayerId: 999 };
138-
const viewModel = transformFill(simpleTokens, relayers, fill);
131+
const fill = { ...simpleFill, relayer: undefined };
132+
const viewModel = transformFill(fill);
139133

140134
expect(viewModel.relayer).toBeNull();
141135
});
142136

143137
it('should transform pending fill', () => {
144138
const fill = { ...simpleFill, status: 0 };
145-
const viewModel = transformFill(simpleTokens, relayers, fill);
139+
const viewModel = transformFill(fill);
146140

147141
expect(viewModel.status).toBe('pending');
148142
});
149143

150144
it('should transform successful fill', () => {
151-
const viewModel = transformFill(simpleTokens, relayers, simpleFill);
145+
const viewModel = transformFill(simpleFill);
152146

153147
expect(viewModel.status).toBe('successful');
154148
});
155149

156150
it('should transform failed fill', () => {
157151
const fill = { ...simpleV1Fill, status: 2 };
158-
const viewModel = transformFill(simpleTokens, relayers, fill);
152+
const viewModel = transformFill(fill);
159153

160154
expect(viewModel.status).toBe('failed');
161155
});
162156

163157
it('should transform V2 fill', () => {
164-
const viewModel = transformFill(simpleTokens, relayers, simpleFill);
158+
const viewModel = transformFill(simpleFill);
165159

166160
expect(viewModel).toMatchSnapshot();
167161
});
168162

169163
it('should transform ERC721 asset', () => {
170-
const viewModel = transformFill(simpleTokens, relayers, simpleFill);
164+
const viewModel = transformFill(simpleFill);
171165
const asset = _.find(viewModel.assets, { traderType: 'maker' });
172166

173167
expect(asset).toMatchSnapshot();
174168
});
175169

176170
it('should transform ERC20 asset', () => {
177-
const viewModel = transformFill(simpleTokens, relayers, simpleFill);
171+
const viewModel = transformFill(simpleFill);
178172
const asset = _.find(viewModel.assets, { traderType: 'taker' });
179173

180174
expect(asset).toMatchSnapshot();
181175
});
182176

183177
it('should transform V3 fill', () => {
184-
const fill = new Fill({
178+
const fill = {
185179
...simpleFill,
186180
conversions: {
187181
USD: {
@@ -192,11 +186,13 @@ describe('transformFill', () => {
192186
{
193187
amount: { token: 5000000000000000, USD: 0.3 },
194188
tokenAddress: '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
189+
token: WETH_FIXTURE,
195190
traderType: 0,
196191
},
197192
{
198193
amount: { token: 1 },
199194
tokenAddress: '0xf5b0a3efb8e8e4c201e2a935f110eaaf3ffecb8d',
195+
token: AXIE_FIXTURE,
200196
tokenId: 58,
201197
traderType: 1,
202198
},
@@ -205,8 +201,8 @@ describe('transformFill', () => {
205201
protocolVersion: 3,
206202
makerFee: undefined,
207203
takerFee: undefined,
208-
});
209-
const viewModel = transformFill(simpleTokens, relayers, fill);
204+
};
205+
const viewModel = transformFill(fill);
210206

211207
expect(viewModel.makerFee).toBeUndefined();
212208
expect(viewModel.takerFee).toBeUndefined();

src/app/routes/v1/util/transform-fills.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,17 @@ const getAssetsForFill = require('../../../../fills/get-assets-for-fill');
66
const transformRelayer = relayer =>
77
relayer === undefined ? null : _.pick(relayer, 'slug', 'name', 'imageUrl');
88

9-
const transformFill = (tokens, relayers, fill) => {
10-
const assets = getAssetsForFill(tokens, fill);
9+
const transformFill = fill => {
10+
const assets = getAssetsForFill(fill);
1111
const conversions = _.get(fill, `conversions.USD`);
12-
const fillRelayer = _.find(relayers, { lookupId: fill.relayerId });
1312

1413
return {
1514
assets,
1615
date: fill.date,
1716
feeRecipient: fill.feeRecipient,
1817
id: fill.id,
1918
makerAddress: fill.maker,
20-
relayer: transformRelayer(fillRelayer),
19+
relayer: transformRelayer(fill.relayer),
2120
status: formatFillStatus(fill.status),
2221
takerAddress: fill.taker,
2322
value: _.has(conversions, 'amount')
@@ -28,7 +27,6 @@ const transformFill = (tokens, relayers, fill) => {
2827
};
2928
};
3029

31-
const transformFills = (tokens, relayers, fills) =>
32-
fills.map(fill => transformFill(tokens, relayers, fill));
30+
const transformFills = fills => fills.map(fill => transformFill(fill));
3331

3432
module.exports = transformFills;

src/app/start.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ const start = port => {
3232
app.listen(port);
3333

3434
if (process.env.NODE_ENV === 'development') {
35-
logger.start(`serving application at http://localhost:${port}`);
35+
logger.info(`serving application at http://localhost:${port}`);
3636
} else {
37-
logger.start(`serving application on port ${port}`);
37+
logger.info(`serving application on port ${port}`);
3838
}
3939
};
4040

0 commit comments

Comments
 (0)