This repository has been archived by the owner on Oct 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
auctions.ts
223 lines (215 loc) · 5.63 KB
/
auctions.ts
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
import { AUCTION_SETTINGS, SECONDS_IN_A_YEAR } from './constants';
import { calculateRegistrationFee } from './pricing';
import {
isActiveReservedName,
isExistingActiveRecord,
isShortNameRestricted,
} from './records';
import {
ArNSAuctionData,
ArNSNameData,
AuctionSettings,
BlockHeight,
BlockTimestamp,
DeepReadonly,
DemandFactoringData,
Fees,
RegistrationType,
ReservedNameData,
mIOToken,
} from './types';
export function calculateAuctionPriceForBlock({
startHeight,
startPrice,
floorPrice,
currentBlockHeight,
auctionSettings = AUCTION_SETTINGS,
}: {
startHeight: BlockHeight;
startPrice: mIOToken;
floorPrice: mIOToken;
currentBlockHeight: BlockHeight;
auctionSettings: AuctionSettings;
}): mIOToken {
const blocksSinceStart = currentBlockHeight.valueOf() - startHeight.valueOf();
const decaySinceStart =
auctionSettings.exponentialDecayRate * blocksSinceStart;
const dutchAuctionBid = startPrice.multiply(
Math.pow(1 - decaySinceStart, auctionSettings.scalingExponent),
);
const defaultMinimumBid = floorPrice.isGreaterThan(dutchAuctionBid)
? floorPrice
: dutchAuctionBid;
return startPrice.isLessThan(defaultMinimumBid)
? startPrice
: defaultMinimumBid;
}
export function getAuctionPricesForInterval({
startHeight,
startPrice,
floorPrice,
blocksPerInterval,
auctionSettings = AUCTION_SETTINGS,
}: {
startHeight: BlockHeight;
startPrice: mIOToken;
floorPrice: mIOToken;
blocksPerInterval: number;
auctionSettings: AuctionSettings;
}): Record<number, number> {
const prices: Record<number, number> = {};
for (
let intervalBlockHeight = 0;
intervalBlockHeight <= auctionSettings.auctionDuration;
intervalBlockHeight += blocksPerInterval
) {
const blockHeightForInterval = startHeight.valueOf() + intervalBlockHeight;
const price = calculateAuctionPriceForBlock({
startHeight,
startPrice,
floorPrice,
currentBlockHeight: new BlockHeight(blockHeightForInterval),
auctionSettings,
});
prices[blockHeightForInterval] = price.valueOf();
}
return prices;
}
export function createAuctionObject({
fees,
contractTxId,
currentBlockHeight,
currentBlockTimestamp,
type,
initiator,
demandFactoring,
name,
}: {
name: string;
fees: Fees;
contractTxId: string;
currentBlockHeight: BlockHeight;
currentBlockTimestamp: BlockTimestamp;
type: RegistrationType;
initiator: string;
demandFactoring: DeepReadonly<DemandFactoringData>;
}): {
startPrice: mIOToken;
floorPrice: mIOToken;
startHeight: BlockHeight;
endHeight: BlockHeight;
type: RegistrationType;
years?: number;
initiator: string;
contractTxId: string;
} {
const initialRegistrationFee = calculateRegistrationFee({
name,
fees,
type,
years: 1,
currentBlockTimestamp,
demandFactoring,
});
const calculatedFloorPrice = initialRegistrationFee.multiply(
AUCTION_SETTINGS.floorPriceMultiplier,
);
const startPrice = calculatedFloorPrice.multiply(
AUCTION_SETTINGS.startPriceMultiplier,
);
const endHeight = currentBlockHeight.plus(
new BlockHeight(AUCTION_SETTINGS.auctionDuration),
);
const baseAuctionData = {
initiator, // the balance that the floor price is decremented from
contractTxId,
startPrice: startPrice,
floorPrice: calculatedFloorPrice, // this is decremented from the initiators wallet, and could be higher than the precalculated floor
startHeight: currentBlockHeight, // auction starts right away
endHeight: endHeight, // auction ends after the set duration
type,
};
switch (type) {
case 'permabuy':
return {
...baseAuctionData,
type: 'permabuy',
};
case 'lease':
return {
...baseAuctionData,
years: 1,
type: 'lease',
};
default:
throw new ContractError('Invalid auction type');
}
}
export function getEndTimestampForAuction({
auction,
currentBlockTimestamp,
}: {
auction: ArNSAuctionData;
currentBlockTimestamp: BlockTimestamp;
}): BlockTimestamp | undefined {
switch (auction.type) {
case 'permabuy':
return undefined;
case 'lease':
return new BlockTimestamp(
currentBlockTimestamp.valueOf() + SECONDS_IN_A_YEAR * auction.years,
);
default:
throw new ContractError('Invalid auction type');
}
}
export function calculateExistingAuctionBidForCaller({
caller,
auction,
submittedBid,
requiredMinimumBid,
}: {
caller: string;
auction: ArNSAuctionData;
submittedBid: mIOToken | undefined;
requiredMinimumBid: mIOToken;
}): mIOToken {
if (submittedBid && submittedBid.isLessThan(requiredMinimumBid)) {
throw new ContractError(
`The bid (${submittedBid.valueOf()} mIO) is less than the current required minimum bid of ${requiredMinimumBid.valueOf()} mIO.`,
);
}
if (caller === auction.initiator) {
const floorPrice = new mIOToken(auction.floorPrice);
return requiredMinimumBid.minus(floorPrice);
}
return requiredMinimumBid;
}
export function isNameAvailableForAuction({
name,
record,
reservedName,
caller,
currentBlockTimestamp,
}: {
name: string;
record: ArNSNameData | undefined;
caller: string;
reservedName: ReservedNameData | undefined;
currentBlockTimestamp: BlockTimestamp;
}): boolean {
return (
!isExistingActiveRecord({ record, currentBlockTimestamp }) &&
!isActiveReservedName({ reservedName, caller, currentBlockTimestamp }) &&
!isShortNameRestricted({ name, currentBlockTimestamp })
);
}
export function isNameRequiredToBeAuction({
name,
type,
}: {
name: string;
type: RegistrationType;
}): boolean {
return type === 'permabuy' && name.length < 12;
}