This repository was archived by the owner on Oct 4, 2024. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathpricing.ts
More file actions
340 lines (314 loc) · 9.21 KB
/
Copy pathpricing.ts
File metadata and controls
340 lines (314 loc) · 9.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import {
ANNUAL_PERCENTAGE_FEE,
DEMAND_FACTORING_SETTINGS,
PERMABUY_LEASE_FEE_LENGTH,
UNDERNAME_LEASE_FEE_PERCENTAGE,
UNDERNAME_PERMABUY_FEE_PERCENTAGE,
} from './constants';
import {
ArNSName,
BlockHeight,
BlockTimestamp,
DeepReadonly,
DemandFactoringCriteria,
DemandFactoringData,
Fees,
IOState,
RegistrationType,
mIOToken,
} from './types';
export function tallyNamePurchase(
dfData: DeepReadonly<DemandFactoringData>,
revenue: mIOToken,
): DemandFactoringData {
const newDfData = cloneDemandFactoringData(dfData);
newDfData.purchasesThisPeriod++;
newDfData.revenueThisPeriod += revenue.valueOf();
return newDfData;
}
export function updateDemandFactor(
currentHeight: BlockHeight,
dfData: DeepReadonly<DemandFactoringData>,
fees: DeepReadonly<Fees>,
): Pick<IOState, 'demandFactoring' | 'fees'> {
if (!shouldUpdateDemandFactor(currentHeight, dfData)) {
return {
demandFactoring: dfData as DemandFactoringData,
fees: fees as Fees,
};
}
const newDemandFactoringData = cloneDemandFactoringData(dfData);
let updatedFees: Fees | undefined;
const numNamesPurchasedInLastPeriod = dfData.purchasesThisPeriod;
const mvgAvgOfTrailingNamePurchases = mvgAvgTrailingPurchaseCounts(dfData);
const revenueInLastPeriod = dfData.revenueThisPeriod;
const mvgAvgOfTrailingRevenue = mvgAvgTrailingRevenues(dfData);
if (
demandIsIncreasing({
numNamesPurchasedInLastPeriod,
mvgAvgOfTrailingNamePurchases,
revenueInLastPeriod,
mvgAvgOfTrailingRevenue,
demandFactoringCriteria: DEMAND_FACTORING_SETTINGS.criteria,
})
) {
newDemandFactoringData.demandFactor *=
1 + DEMAND_FACTORING_SETTINGS.demandFactorUpAdjustment;
} else if (dfData.demandFactor > DEMAND_FACTORING_SETTINGS.demandFactorMin) {
newDemandFactoringData.demandFactor *=
1 - DEMAND_FACTORING_SETTINGS.demandFactorDownAdjustment;
}
// If necessary, reset the demand factor after enough consecutive periods at the minimum
if (
newDemandFactoringData.demandFactor ===
DEMAND_FACTORING_SETTINGS.demandFactorMin
) {
if (
++newDemandFactoringData.consecutivePeriodsWithMinDemandFactor >=
DEMAND_FACTORING_SETTINGS.stepDownThreshold
) {
newDemandFactoringData.consecutivePeriodsWithMinDemandFactor = 0;
newDemandFactoringData.demandFactor =
DEMAND_FACTORING_SETTINGS.demandFactorBaseValue;
// Rebase fees on their values at the minimum demand factor
updatedFees = Object.keys(fees).reduce(
(acc: Fees, nameLength: string) => {
const updatedFee = new mIOToken(fees[nameLength]).multiply(
DEMAND_FACTORING_SETTINGS.demandFactorMin,
);
// set the minimum value to 1 MIO
acc[nameLength] = Math.max(
updatedFee.valueOf(),
new mIOToken(1).valueOf(),
);
return acc;
},
{},
);
}
} else {
newDemandFactoringData.consecutivePeriodsWithMinDemandFactor = 0;
}
// Stash the number of purchases and revenue for this period in the trailing metrics
const trailingPeriodIndex = demandFactorPeriodIndex(
newDemandFactoringData.currentPeriod,
);
newDemandFactoringData.trailingPeriodPurchases[trailingPeriodIndex] =
numNamesPurchasedInLastPeriod;
newDemandFactoringData.trailingPeriodRevenues[trailingPeriodIndex] =
revenueInLastPeriod;
// Increment the current period and reset the purchase count and revenue amount
newDemandFactoringData.currentPeriod++;
newDemandFactoringData.purchasesThisPeriod = 0;
newDemandFactoringData.revenueThisPeriod = 0;
return {
demandFactoring: newDemandFactoringData,
fees: updatedFees || fees,
};
}
export function shouldUpdateDemandFactor(
currentHeight: BlockHeight,
dfData: DeepReadonly<DemandFactoringData>,
): boolean {
// Don't update the demand factor if we're still in the first-ever period
if (currentHeight.valueOf() === dfData.periodZeroBlockHeight) {
return false;
}
const currentPeriod = periodAtHeight(
currentHeight,
new BlockHeight(dfData.periodZeroBlockHeight),
);
return currentPeriod > dfData.currentPeriod;
}
export function demandIsIncreasing({
numNamesPurchasedInLastPeriod,
mvgAvgOfTrailingNamePurchases: mvgAvgOfTailingNamePurchases,
revenueInLastPeriod,
mvgAvgOfTrailingRevenue,
demandFactoringCriteria,
}: {
numNamesPurchasedInLastPeriod: number;
mvgAvgOfTrailingNamePurchases: number;
revenueInLastPeriod: number;
mvgAvgOfTrailingRevenue: number;
demandFactoringCriteria: DemandFactoringCriteria;
}): boolean {
switch (demandFactoringCriteria) {
case 'purchases':
return (
numNamesPurchasedInLastPeriod >= mvgAvgOfTailingNamePurchases &&
numNamesPurchasedInLastPeriod !== 0
);
case 'revenue':
return (
revenueInLastPeriod >= mvgAvgOfTrailingRevenue &&
revenueInLastPeriod !== 0
);
}
}
export function periodAtHeight(
height: BlockHeight,
periodZeroHeight: BlockHeight,
): number {
return Math.floor(
(height.valueOf() - periodZeroHeight.valueOf()) /
DEMAND_FACTORING_SETTINGS.periodBlockCount,
);
}
export function demandFactorPeriodIndex(period: number): number {
return period % DEMAND_FACTORING_SETTINGS.movingAvgPeriodCount;
}
export function mvgAvgTrailingPurchaseCounts(
dfData: DeepReadonly<DemandFactoringData>,
): number {
return (
dfData.trailingPeriodPurchases.reduce(
(acc, periodPurchaseCount) => acc + periodPurchaseCount,
0,
) / DEMAND_FACTORING_SETTINGS.movingAvgPeriodCount
);
}
export function mvgAvgTrailingRevenues(
dfData: DeepReadonly<DemandFactoringData>,
): number {
return (
dfData.trailingPeriodRevenues.reduce(
(acc, periodRevenue) => acc + periodRevenue,
0,
) / DEMAND_FACTORING_SETTINGS.movingAvgPeriodCount
);
}
export function cloneDemandFactoringData(
dfData: DeepReadonly<DemandFactoringData>,
): DemandFactoringData {
return {
...dfData,
trailingPeriodPurchases: dfData.trailingPeriodPurchases.slice(),
trailingPeriodRevenues: dfData.trailingPeriodRevenues.slice(),
};
}
export function calculateLeaseFee({
name,
fees,
years,
demandFactoring,
}: {
name: string;
fees: Fees;
years: number;
currentBlockTimestamp: BlockTimestamp;
demandFactoring: DeepReadonly<DemandFactoringData>;
}): mIOToken {
// Initial cost to register a name
// TODO: Harden the types here to make fees[name.length] an error
const initialNamePurchaseFee = new mIOToken(fees[name.length.toString()]);
const annualRenewalFees = calculateAnnualRenewalFee({
name,
fees,
years,
});
const initialFeeWithAnnualRenewal =
initialNamePurchaseFee.plus(annualRenewalFees);
// total cost to purchase name
return initialFeeWithAnnualRenewal.multiply(demandFactoring.demandFactor);
}
export function calculateAnnualRenewalFee({
name,
fees,
years,
}: {
name: string;
fees: Fees;
years: number;
}): mIOToken {
// Determine annual registration price of name
const initialNamePurchaseFee = new mIOToken(fees[name.length.toString()]);
// Annual fee is specific % of initial purchase cost
return initialNamePurchaseFee.multiply(ANNUAL_PERCENTAGE_FEE).multiply(years);
}
export function calculatePermabuyFee({
name,
fees,
demandFactoring,
}: {
name: string;
fees: Fees;
currentBlockTimestamp: BlockTimestamp;
demandFactoring: DeepReadonly<DemandFactoringData>;
}): mIOToken {
// genesis price
const initialNamePurchaseFee = new mIOToken(fees[name.length.toString()]);
const annualRenewalFeeForPermabuy = calculateAnnualRenewalFee({
name,
fees,
years: PERMABUY_LEASE_FEE_LENGTH,
});
const initialFeeWithAnnualRenewal = initialNamePurchaseFee.plus(
annualRenewalFeeForPermabuy,
);
return initialFeeWithAnnualRenewal.multiply(demandFactoring.demandFactor);
}
export function calculateRegistrationFee({
type,
name,
fees,
years,
currentBlockTimestamp,
demandFactoring,
}: {
type: RegistrationType;
name: string;
fees: Fees;
years: number;
currentBlockTimestamp: BlockTimestamp;
demandFactoring: DeepReadonly<DemandFactoringData>;
}): mIOToken {
switch (type) {
case 'lease':
return calculateLeaseFee({
name,
fees,
years,
currentBlockTimestamp,
demandFactoring,
});
case 'permabuy':
return calculatePermabuyFee({
name,
fees,
currentBlockTimestamp,
demandFactoring,
});
}
}
export function calculateUndernameCost({
name,
fees,
increaseQty,
type,
demandFactoring,
years,
}: {
name: ArNSName;
fees: Fees;
increaseQty: number;
type: RegistrationType;
years: number;
demandFactoring: DeepReadonly<DemandFactoringData>;
}): mIOToken {
const initialNameFee = new mIOToken(fees[name.length.toString()]);
const getUndernameFeePercentage = () => {
switch (type) {
case 'lease':
return UNDERNAME_LEASE_FEE_PERCENTAGE;
case 'permabuy':
return UNDERNAME_PERMABUY_FEE_PERCENTAGE;
}
};
const undernamePercentageFee = getUndernameFeePercentage();
const totalFeeForQtyAndYears = initialNameFee
.multiply(undernamePercentageFee)
.multiply(increaseQty)
.multiply(years);
return totalFeeForQtyAndYears.multiply(demandFactoring.demandFactor);
}