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

Commit e02e59f

Browse files
authored
Add apps to fill endpoints (#433)
* Add apps to fills endpoint * Add apps to fill endpoint
1 parent 9791995 commit e02e59f

File tree

6 files changed

+107
-10
lines changed

6 files changed

+107
-10
lines changed

src/app/routes/v1/fills.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,12 @@ const createRouter = () => {
151151
const fill = mongoose.Types.ObjectId.isValid(fillId)
152152
? await Fill.findById(fillId, undefined, {
153153
populate: [
154-
{ path: 'relayer', select: 'imageUrl name slug' },
155154
{ path: 'assets.token', select: 'decimals name symbol type' },
156155
{ path: 'fees.token', select: 'decimals name symbol type' },
157156
{ path: 'affiliate', select: 'name imageUrl' },
158157
{ path: 'takerMetadata', select: 'isContract' },
159158
{ path: 'transaction', select: 'from' },
159+
{ path: 'attributions.entity', select: 'id logoUrl name urlSlug' },
160160
],
161161
})
162162
: null;

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

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,30 @@
11
const _ = require('lodash');
22

3-
const { ETH_TOKEN_DECIMALS } = require('../../../../constants');
3+
const {
4+
ETH_TOKEN_DECIMALS,
5+
FILL_ATTRIBUTION_TYPE,
6+
} = require('../../../../constants');
7+
const formatFillAttributionType = require('../../../../fills/format-fill-attribution-type');
48
const formatFillStatus = require('../../../../fills/format-fill-status');
59
const formatTokenAmount = require('../../../../tokens/format-token-amount');
610
const getAssetsForFill = require('../../../../fills/get-assets-for-fill');
711
const getFeesForFill = require('../../../../fills/get-fees-for-fill');
812

9-
const formatRelayer = relayer =>
10-
relayer === null ? null : _.pick(relayer, 'slug', 'name', 'imageUrl');
13+
const getRelayer = fill => {
14+
const relayerAttribution = fill.attributions.find(
15+
a => a.type === FILL_ATTRIBUTION_TYPE.RELAYER,
16+
);
17+
18+
if (relayerAttribution === undefined) {
19+
return null;
20+
}
21+
22+
return {
23+
imageUrl: relayerAttribution.entity.logoUrl,
24+
name: relayerAttribution.entity.name,
25+
slug: relayerAttribution.entity.urlSlug,
26+
};
27+
};
1128

1229
const transformFill = fill => {
1330
const assets = getAssetsForFill(fill);
@@ -33,6 +50,20 @@ const transformFill = fill => {
3350
imageUrl: _.get(fill, 'affiliate.imageUrl', null),
3451
name: _.get(fill, 'affiliate.name', null),
3552
},
53+
apps: fill.attributions
54+
.filter(a =>
55+
[
56+
FILL_ATTRIBUTION_TYPE.CONSUMER,
57+
FILL_ATTRIBUTION_TYPE.RELAYER,
58+
].includes(a.type),
59+
)
60+
.map(attribution => ({
61+
id: attribution.entityId,
62+
logoUrl: attribution.entity.logoUrl,
63+
name: attribution.entity.name,
64+
type: formatFillAttributionType(attribution.type),
65+
urlSlug: attribution.entity.urlSlug,
66+
})),
3667
assets,
3768
date: fill.date,
3869
fees,
@@ -42,7 +73,7 @@ const transformFill = fill => {
4273
orderHash: fill.orderHash,
4374
protocolFee,
4475
protocolVersion: fill.protocolVersion,
45-
relayer: formatRelayer(fill.relayer),
76+
relayer: getRelayer(fill),
4677
senderAddress: fill.senderAddress,
4778
status: formatFillStatus(fill.status),
4879
takerAddress: taker,

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

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,29 @@
11
const _ = require('lodash');
22

3-
const { ETH_TOKEN_DECIMALS } = require('../../../../constants');
3+
const {
4+
ETH_TOKEN_DECIMALS,
5+
FILL_ATTRIBUTION_TYPE,
6+
} = require('../../../../constants');
7+
const formatFillAttributionType = require('../../../../fills/format-fill-attribution-type');
48
const formatFillStatus = require('../../../../fills/format-fill-status');
59
const formatTokenAmount = require('../../../../tokens/format-token-amount');
610
const getAssetsForFill = require('../../../../fills/get-assets-for-fill');
711

8-
const transformRelayer = relayer =>
9-
relayer === null ? null : _.pick(relayer, 'slug', 'name', 'imageUrl');
12+
const getRelayer = fill => {
13+
const relayerAttribution = fill.attributions.find(
14+
a => a.type === FILL_ATTRIBUTION_TYPE.RELAYER,
15+
);
16+
17+
if (relayerAttribution === undefined) {
18+
return null;
19+
}
20+
21+
return {
22+
imageUrl: relayerAttribution.entity.logoUrl,
23+
name: relayerAttribution.entity.name,
24+
slug: relayerAttribution.entity.urlSlug,
25+
};
26+
};
1027

1128
const transformFill = fill => {
1229
const assets = getAssetsForFill(fill);
@@ -24,14 +41,28 @@ const transformFill = fill => {
2441
: undefined;
2542

2643
return {
44+
apps: fill.attributions
45+
.filter(a =>
46+
[
47+
FILL_ATTRIBUTION_TYPE.CONSUMER,
48+
FILL_ATTRIBUTION_TYPE.RELAYER,
49+
].includes(a.type),
50+
)
51+
.map(a => ({
52+
id: a.entity.id,
53+
logoUrl: a.entity.logoUrl,
54+
name: a.entity.name,
55+
type: formatFillAttributionType(a.type),
56+
urlSlug: a.entity.urlSlug,
57+
})),
2758
assets,
2859
date: fill.date,
2960
feeRecipient: fill.feeRecipient,
3061
id: fill.id,
3162
makerAddress: fill.maker,
3263
protocolFee,
3364
protocolVersion: fill.protocolVersion,
34-
relayer: transformRelayer(fill.relayer),
65+
relayer: getRelayer(fill),
3566
status: formatFillStatus(fill.status),
3667
takerAddress: taker,
3768
value: _.has(conversions, 'amount')
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const _ = require('lodash');
2+
3+
const { FILL_ATTRIBUTION_TYPE } = require('../constants');
4+
5+
const formatFillAttributionType = type => {
6+
if (type === undefined) {
7+
return undefined;
8+
}
9+
10+
const matchingKey = _.findKey(FILL_ATTRIBUTION_TYPE, value => type === value);
11+
12+
if (matchingKey === undefined) {
13+
throw new Error(`Unrecognised fill attribution type: ${type}`);
14+
}
15+
16+
return matchingKey.toLowerCase();
17+
};
18+
19+
module.exports = formatFillAttributionType;

src/fills/search-fills.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ const searchFills = async (params, options) => {
1919
const fillIds = results.body.hits.hits.map(hit => hit._id);
2020
const fills = await Fill.find({ _id: { $in: fillIds } })
2121
.populate([
22-
{ path: 'relayer', select: 'imageUrl name slug' },
2322
{ path: 'assets.token', select: 'decimals name symbol type imageUrl' },
2423
{ path: 'transaction', select: 'from' },
2524
{ path: 'takerMetadata', select: 'isContract' },
25+
{ path: 'attributions.entity', select: 'logoUrl name urlSlug' },
2626
])
2727
.sort({ date: -1 });
2828

src/model/fill.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const { FILL_STATUS } = require('../constants');
44

55
const { Schema } = mongoose;
66

7+
require('./attribution-entity');
78
require('./transaction');
89

910
const schema = Schema({
@@ -20,6 +21,14 @@ const schema = Schema({
2021
tokenId: Number,
2122
},
2223
],
24+
attributions: [
25+
{
26+
entityId: String,
27+
type: {
28+
type: Number,
29+
},
30+
},
31+
],
2332
conversions: {
2433
USD: {
2534
amount: Number,
@@ -93,6 +102,13 @@ schema.virtual('transaction', {
93102
justOne: true,
94103
});
95104

105+
schema.virtual('attributions.entity', {
106+
ref: 'AttributionEntity',
107+
localField: 'attributions.entityId',
108+
foreignField: '_id',
109+
justOne: true,
110+
});
111+
96112
const Model = mongoose.model('Fill', schema);
97113

98114
module.exports = Model;

0 commit comments

Comments
 (0)