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

Commit

Permalink
Add apps to fill endpoints (#433)
Browse files Browse the repository at this point in the history
* Add apps to fills endpoint

* Add apps to fill endpoint
  • Loading branch information
cbovis authored Oct 23, 2020
1 parent 9791995 commit e02e59f
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/app/routes/v1/fills.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,12 @@ const createRouter = () => {
const fill = mongoose.Types.ObjectId.isValid(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' },
{ path: 'affiliate', select: 'name imageUrl' },
{ path: 'takerMetadata', select: 'isContract' },
{ path: 'transaction', select: 'from' },
{ path: 'attributions.entity', select: 'id logoUrl name urlSlug' },
],
})
: null;
Expand Down
39 changes: 35 additions & 4 deletions src/app/routes/v1/util/transform-fill.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,30 @@
const _ = require('lodash');

const { ETH_TOKEN_DECIMALS } = require('../../../../constants');
const {
ETH_TOKEN_DECIMALS,
FILL_ATTRIBUTION_TYPE,
} = require('../../../../constants');
const formatFillAttributionType = require('../../../../fills/format-fill-attribution-type');
const formatFillStatus = require('../../../../fills/format-fill-status');
const formatTokenAmount = require('../../../../tokens/format-token-amount');
const getAssetsForFill = require('../../../../fills/get-assets-for-fill');
const getFeesForFill = require('../../../../fills/get-fees-for-fill');

const formatRelayer = relayer =>
relayer === null ? null : _.pick(relayer, 'slug', 'name', 'imageUrl');
const getRelayer = fill => {
const relayerAttribution = fill.attributions.find(
a => a.type === FILL_ATTRIBUTION_TYPE.RELAYER,
);

if (relayerAttribution === undefined) {
return null;
}

return {
imageUrl: relayerAttribution.entity.logoUrl,
name: relayerAttribution.entity.name,
slug: relayerAttribution.entity.urlSlug,
};
};

const transformFill = fill => {
const assets = getAssetsForFill(fill);
Expand All @@ -33,6 +50,20 @@ const transformFill = fill => {
imageUrl: _.get(fill, 'affiliate.imageUrl', null),
name: _.get(fill, 'affiliate.name', null),
},
apps: fill.attributions
.filter(a =>
[
FILL_ATTRIBUTION_TYPE.CONSUMER,
FILL_ATTRIBUTION_TYPE.RELAYER,
].includes(a.type),
)
.map(attribution => ({
id: attribution.entityId,
logoUrl: attribution.entity.logoUrl,
name: attribution.entity.name,
type: formatFillAttributionType(attribution.type),
urlSlug: attribution.entity.urlSlug,
})),
assets,
date: fill.date,
fees,
Expand All @@ -42,7 +73,7 @@ const transformFill = fill => {
orderHash: fill.orderHash,
protocolFee,
protocolVersion: fill.protocolVersion,
relayer: formatRelayer(fill.relayer),
relayer: getRelayer(fill),
senderAddress: fill.senderAddress,
status: formatFillStatus(fill.status),
takerAddress: taker,
Expand Down
39 changes: 35 additions & 4 deletions src/app/routes/v1/util/transform-fills.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
const _ = require('lodash');

const { ETH_TOKEN_DECIMALS } = require('../../../../constants');
const {
ETH_TOKEN_DECIMALS,
FILL_ATTRIBUTION_TYPE,
} = require('../../../../constants');
const formatFillAttributionType = require('../../../../fills/format-fill-attribution-type');
const formatFillStatus = require('../../../../fills/format-fill-status');
const formatTokenAmount = require('../../../../tokens/format-token-amount');
const getAssetsForFill = require('../../../../fills/get-assets-for-fill');

const transformRelayer = relayer =>
relayer === null ? null : _.pick(relayer, 'slug', 'name', 'imageUrl');
const getRelayer = fill => {
const relayerAttribution = fill.attributions.find(
a => a.type === FILL_ATTRIBUTION_TYPE.RELAYER,
);

if (relayerAttribution === undefined) {
return null;
}

return {
imageUrl: relayerAttribution.entity.logoUrl,
name: relayerAttribution.entity.name,
slug: relayerAttribution.entity.urlSlug,
};
};

const transformFill = fill => {
const assets = getAssetsForFill(fill);
Expand All @@ -24,14 +41,28 @@ const transformFill = fill => {
: undefined;

return {
apps: fill.attributions
.filter(a =>
[
FILL_ATTRIBUTION_TYPE.CONSUMER,
FILL_ATTRIBUTION_TYPE.RELAYER,
].includes(a.type),
)
.map(a => ({
id: a.entity.id,
logoUrl: a.entity.logoUrl,
name: a.entity.name,
type: formatFillAttributionType(a.type),
urlSlug: a.entity.urlSlug,
})),
assets,
date: fill.date,
feeRecipient: fill.feeRecipient,
id: fill.id,
makerAddress: fill.maker,
protocolFee,
protocolVersion: fill.protocolVersion,
relayer: transformRelayer(fill.relayer),
relayer: getRelayer(fill),
status: formatFillStatus(fill.status),
takerAddress: taker,
value: _.has(conversions, 'amount')
Expand Down
19 changes: 19 additions & 0 deletions src/fills/format-fill-attribution-type.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const _ = require('lodash');

const { FILL_ATTRIBUTION_TYPE } = require('../constants');

const formatFillAttributionType = type => {
if (type === undefined) {
return undefined;
}

const matchingKey = _.findKey(FILL_ATTRIBUTION_TYPE, value => type === value);

if (matchingKey === undefined) {
throw new Error(`Unrecognised fill attribution type: ${type}`);
}

return matchingKey.toLowerCase();
};

module.exports = formatFillAttributionType;
2 changes: 1 addition & 1 deletion src/fills/search-fills.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ const searchFills = async (params, options) => {
const fillIds = results.body.hits.hits.map(hit => hit._id);
const fills = await Fill.find({ _id: { $in: fillIds } })
.populate([
{ path: 'relayer', select: 'imageUrl name slug' },
{ path: 'assets.token', select: 'decimals name symbol type imageUrl' },
{ path: 'transaction', select: 'from' },
{ path: 'takerMetadata', select: 'isContract' },
{ path: 'attributions.entity', select: 'logoUrl name urlSlug' },
])
.sort({ date: -1 });

Expand Down
16 changes: 16 additions & 0 deletions src/model/fill.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const { FILL_STATUS } = require('../constants');

const { Schema } = mongoose;

require('./attribution-entity');
require('./transaction');

const schema = Schema({
Expand All @@ -20,6 +21,14 @@ const schema = Schema({
tokenId: Number,
},
],
attributions: [
{
entityId: String,
type: {
type: Number,
},
},
],
conversions: {
USD: {
amount: Number,
Expand Down Expand Up @@ -93,6 +102,13 @@ schema.virtual('transaction', {
justOne: true,
});

schema.virtual('attributions.entity', {
ref: 'AttributionEntity',
localField: 'attributions.entityId',
foreignField: '_id',
justOne: true,
});

const Model = mongoose.model('Fill', schema);

module.exports = Model;

0 comments on commit e02e59f

Please sign in to comment.