This repository was 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
Expand file tree
/
Copy pathrecords.ts
More file actions
194 lines (175 loc) · 4.86 KB
/
Copy pathrecords.ts
File metadata and controls
194 lines (175 loc) · 4.86 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
import { isNameRequiredToBeAuction } from './auctions';
import {
ARNS_INVALID_SHORT_NAME,
ARNS_LEASE_LENGTH_MAX_YEARS,
ARNS_NAME_MUST_BE_AUCTIONED_MESSAGE,
ARNS_NAME_RESERVED_MESSAGE,
ARNS_NON_EXPIRED_NAME_MESSAGE,
MINIMUM_ALLOWED_NAME_LENGTH,
SECONDS_IN_A_YEAR,
SECONDS_IN_GRACE_PERIOD,
SHORT_NAME_RESERVATION_UNLOCK_TIMESTAMP,
} from './constants';
import {
ArNSLeaseData,
ArNSNameData,
BlockTimestamp,
DeepReadonly,
Records,
ReservedNameData,
ReservedNames,
} from './types';
export function isNameInGracePeriod({
record,
currentBlockTimestamp,
}: {
currentBlockTimestamp: BlockTimestamp;
record: ArNSLeaseData;
}): boolean {
if (!record.endTimestamp) return false;
const recordIsExpired = currentBlockTimestamp.valueOf() > record.endTimestamp;
return (
recordIsExpired &&
record.endTimestamp + SECONDS_IN_GRACE_PERIOD >
currentBlockTimestamp.valueOf()
);
}
export function getMaxAllowedYearsExtensionForRecord({
currentBlockTimestamp,
record,
}: {
currentBlockTimestamp: BlockTimestamp;
record: ArNSLeaseData;
}): number {
if (!record.endTimestamp) {
return 0;
}
// if expired return 0 because it cannot be extended and must be re-bought
if (
currentBlockTimestamp.valueOf() >
record.endTimestamp + SECONDS_IN_GRACE_PERIOD
) {
return 0;
}
if (isNameInGracePeriod({ currentBlockTimestamp, record })) {
return ARNS_LEASE_LENGTH_MAX_YEARS;
}
// TODO: should we put this as the ceiling? or should we allow people to extend as soon as it is purchased
const yearsRemainingOnLease = Math.ceil(
(record.endTimestamp.valueOf() - currentBlockTimestamp.valueOf()) /
SECONDS_IN_A_YEAR,
);
// a number between 0 and 5 (MAX_YEARS)
return ARNS_LEASE_LENGTH_MAX_YEARS - yearsRemainingOnLease;
}
export function isExistingActiveRecord({
record,
currentBlockTimestamp,
}: {
record: ArNSNameData | undefined;
currentBlockTimestamp: BlockTimestamp;
}): boolean {
if (!record) return false;
if (!isLeaseRecord(record)) {
return true;
}
return (
record.endTimestamp > currentBlockTimestamp.valueOf() ||
isNameInGracePeriod({ currentBlockTimestamp, record })
);
}
export function isShortNameRestricted({
name,
currentBlockTimestamp,
}: {
name: string;
currentBlockTimestamp: BlockTimestamp;
}): boolean {
return (
name.length < MINIMUM_ALLOWED_NAME_LENGTH &&
currentBlockTimestamp.valueOf() < SHORT_NAME_RESERVATION_UNLOCK_TIMESTAMP
);
}
export function isActiveReservedName({
caller,
reservedName,
currentBlockTimestamp,
}: {
caller: string | undefined;
reservedName: ReservedNameData | undefined;
currentBlockTimestamp: BlockTimestamp;
}): boolean {
if (!reservedName) return false;
const target = reservedName.target;
const endTimestamp = reservedName.endTimestamp;
const permanentlyReserved = !target && !endTimestamp;
if (permanentlyReserved) {
return true;
}
const isCallerTarget = caller !== undefined && target === caller;
const isActiveReservation =
endTimestamp === undefined ||
(endTimestamp !== undefined &&
endTimestamp > currentBlockTimestamp.valueOf());
// if the caller is not the target, and it's still active - the name is considered reserved
if (!isCallerTarget && isActiveReservation) {
return true;
}
return false;
}
export function assertAvailableRecord({
caller,
name,
records,
reserved,
currentBlockTimestamp,
type,
auction,
}: {
caller: string | undefined; // TODO: type for this
name: DeepReadonly<string>;
records: DeepReadonly<Records>;
reserved: DeepReadonly<ReservedNames>;
currentBlockTimestamp: BlockTimestamp;
type: 'permabuy' | 'lease';
auction: boolean;
}): void {
const isActiveRecord = isExistingActiveRecord({
record: records[name],
currentBlockTimestamp,
});
const isReserved = isActiveReservedName({
caller,
reservedName: reserved[name],
currentBlockTimestamp,
});
const isShortName = isShortNameRestricted({
name,
currentBlockTimestamp,
});
const isAuctionRequired = isNameRequiredToBeAuction({ name, type });
if (isActiveRecord) {
throw new ContractError(ARNS_NON_EXPIRED_NAME_MESSAGE);
}
if (reserved[name]?.target === caller) {
// if the caller is the target of the reserved name, they can buy it
return;
}
// if caller is not provided, assume a read interaction and do not throw
if (!caller) {
return;
}
if (isReserved) {
throw new ContractError(ARNS_NAME_RESERVED_MESSAGE);
}
if (isShortName) {
throw new ContractError(ARNS_INVALID_SHORT_NAME);
}
// TODO: we may want to move this up if we want to force permabuys for short names on reserved names
if (isAuctionRequired && !auction) {
throw new ContractError(ARNS_NAME_MUST_BE_AUCTIONED_MESSAGE);
}
}
export function isLeaseRecord(record: ArNSNameData): record is ArNSLeaseData {
return record.type === 'lease';
}