-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
AIP #6 impl #267
AIP #6 impl #267
Changes from 6 commits
ee8a7f6
8919cde
61b7442
55a481c
8d1a6db
7c5a2ab
90b984a
b261949
7e89bb0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,9 +6,9 @@ function newAggr(channelId) { | |
return { channelId, created: new Date(), events: {}, totals: {}, earners: [] } | ||
} | ||
|
||
function reduce(channel, initialAggr, ev) { | ||
function reduce(channel, session, initialAggr, event) { | ||
let aggr = { ...initialAggr } | ||
|
||
const ev = { ...event, ...session } // add session details (i.e country, device type etc) to event | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why don't you just pass the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please answer this @samparsky There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i have changed it to pass |
||
const payout = getPayout(channel, ev) | ||
if (payout) { | ||
aggr.events[ev.type] = mergeEv(initialAggr.events[ev.type], payout) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,84 @@ | ||
/* eslint-disable no-nested-ternary */ | ||
// const BN = require('bignumber.js') // allows | ||
samparsky marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const BN = require('bn.js') | ||
const toBalancesKey = require('../toBalancesKey') | ||
|
||
function getPayout(channel, ev) { | ||
function getPayout(channel, ev, session) { | ||
if (ev.type === 'IMPRESSION' && ev.publisher) { | ||
// add the minimum price for the event to the current amount | ||
return [toBalancesKey(ev.publisher), new BN(channel.spec.minPerImpression || 1)] | ||
const [minPrice, maxPrice] = getPriceBounds(channel.spec, ev.type) | ||
samparsky marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const price = channel.spec.priceMultiplicationRules | ||
? payout(channel.spec.priceMultiplicationRules, ev, session, maxPrice, minPrice) | ||
: minPrice | ||
return [toBalancesKey(ev.publisher), new BN(price.toString())] | ||
} | ||
if (ev.type === 'CLICK' && ev.publisher) { | ||
return [ | ||
toBalancesKey(ev.publisher), | ||
new BN((channel.spec.pricingBounds && channel.spec.pricingBounds.CLICK.min) || 0) | ||
] | ||
const [minPrice, maxPrice] = getPriceBounds(channel.spec, ev.type) | ||
const price = channel.spec.priceMultiplicationRules | ||
? payout(channel.spec.priceMultiplicationRules, ev, session, maxPrice, minPrice) | ||
: minPrice | ||
return [toBalancesKey(ev.publisher), new BN(price.toString())] | ||
} | ||
return null | ||
} | ||
|
||
function payout(rules, ev, session, maxPrice, startPrice) { | ||
const match = isRuleMatching.bind(null, ev, session) | ||
const matchingRules = rules.filter(match) | ||
let finalPrice = startPrice | ||
|
||
if (matchingRules.length > 0) { | ||
const divisionExponent = new BN(10).pow(new BN(18, 10)) | ||
const firstFixed = matchingRules.find(x => x.amount) | ||
const priceByRules = firstFixed | ||
? new BN(firstFixed.amount) | ||
: startPrice | ||
.mul( | ||
new BN( | ||
( | ||
matchingRules | ||
.filter(x => x.multiplier) | ||
.map(x => x.multiplier) | ||
.reduce((a, b) => a * b, 1) * 1e18 | ||
).toString() | ||
) | ||
) | ||
.div(divisionExponent) | ||
finalPrice = BN.min(maxPrice, priceByRules) | ||
} | ||
|
||
return finalPrice | ||
} | ||
|
||
function isRuleMatching(ev, session, rule) { | ||
return rule.eventType | ||
? rule.eventType.includes(ev.type.toLowerCase()) | ||
: true && rule.publisher | ||
? rule.publisher.includes(ev.publisher) | ||
: true && rule.osType | ||
? rule.osType.includes(session.os && session.os.toLowerCase()) | ||
: true && rule.country | ||
? rule.country.includes(session.country && session.country.toLowerCase()) | ||
: true | ||
} | ||
|
||
function getPriceBounds(spec, evType) { | ||
const { pricingBounds, minPerImpression, maxPerImpression } = spec | ||
if (evType === 'IMPRESSION') { | ||
return ( | ||
(pricingBounds && | ||
samparsky marked this conversation as resolved.
Show resolved
Hide resolved
|
||
pricingBounds[evType] && [ | ||
new BN(pricingBounds[evType].min), | ||
new BN(pricingBounds[evType].max) | ||
]) || [new BN(minPerImpression || 1), new BN(maxPerImpression || 1)] | ||
) | ||
} | ||
return ( | ||
(pricingBounds && | ||
pricingBounds[evType] && [ | ||
new BN(pricingBounds[evType].min), | ||
new BN(pricingBounds[evType].max) | ||
]) || [new BN(0), new BN(0)] | ||
) | ||
} | ||
module.exports = getPayout |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why does this no longer have a default?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This because if it has default '0', then the behavior becomes unreliable if an advertiser specifies one property and omits the next. e.g. specifies
min
omitsmax