-
Notifications
You must be signed in to change notification settings - Fork 17
/
auctions.test.ts
191 lines (182 loc) · 5.6 KB
/
auctions.test.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
import {
calculateAuctionPriceForBlock,
calculateExistingAuctionBidForCaller,
getAuctionPricesForInterval,
getEndTimestampForAuction,
} from './auctions';
import { AUCTION_SETTINGS, SECONDS_IN_A_YEAR } from './constants';
import {
ArNSAuctionData,
ArNSBaseAuctionData,
ArNSLeaseAuctionData,
BlockTimestamp,
mIOToken,
} from './types';
import { BlockHeight } from './types';
describe('calculateAuctionPriceForBlock', () => {
describe('validate AUCTION_SETTINGS used in the contract', () => {
const basePrice = new mIOToken(30);
const allowedThreshold = 0.05; // prices must be within 5% of the expected value
const startPrice = basePrice.multiply(
AUCTION_SETTINGS.startPriceMultiplier,
);
const floorPrice = basePrice.multiply(
AUCTION_SETTINGS.floorPriceMultiplier,
);
const startHeight = new BlockHeight(0);
it.each([
[
'should never be larger than the start price',
new BlockHeight(0),
startPrice,
],
[
'should be half the start price after ~2.5 days (1800 blocks)',
new BlockHeight(1800),
startPrice.divide(2),
],
[
'should twice the floor price after ~11.5 days (8300 blocks)',
new BlockHeight(8300),
floorPrice.multiply(2),
],
[
'should end at a price larger than the floor price',
new BlockHeight(AUCTION_SETTINGS.auctionDuration),
floorPrice,
],
])('%s', (_: string, currentBlockHeight: BlockHeight, expectedPrice) => {
const priceAtBlock = calculateAuctionPriceForBlock({
startHeight,
startPrice,
floorPrice,
currentBlockHeight,
auctionSettings: AUCTION_SETTINGS,
});
const percentDifference = Math.abs(
1 - expectedPrice.valueOf() / priceAtBlock.valueOf(),
);
expect(priceAtBlock.valueOf()).toBeGreaterThanOrEqual(
expectedPrice.valueOf(),
);
expect(percentDifference).toBeLessThanOrEqual(allowedThreshold);
});
it.each([
[[0, 0], 100],
[[0, 1], 99],
[[0, 2], 99],
[[0, 3], 99],
[[0, 1], 99],
[[0, 2], 99],
[[0, 3], 99],
// block heights before the start should just return the start price
[[10, 9], 100],
[[10, 0], 100],
])(
'given [current block height, moving average purchase count] of %j, should return %d',
([startHeight, currentHeight], expectedPrice) => {
const calculatedMinimumBid = calculateAuctionPriceForBlock({
startHeight: new BlockHeight(startHeight),
startPrice: new mIOToken(100),
floorPrice: new mIOToken(10),
currentBlockHeight: new BlockHeight(currentHeight),
auctionSettings: AUCTION_SETTINGS,
});
expect(calculatedMinimumBid.valueOf()).toEqual(expectedPrice);
},
);
});
describe('getAuctionPricesForInterval function', () => {
it('should return the correct prices for all block heights', () => {
const prices = getAuctionPricesForInterval({
startHeight: new BlockHeight(0),
startPrice: new mIOToken(100),
floorPrice: new mIOToken(10),
blocksPerInterval: 1,
auctionSettings: AUCTION_SETTINGS,
});
expect(Object.keys(prices).length).toEqual(10081);
expect(prices[0]).toEqual(100);
expect(prices[1000]).toEqual(68);
expect(prices[2000]).toEqual(46);
expect(prices[3000]).toEqual(31);
expect(prices[4000]).toEqual(21);
expect(prices[5000]).toEqual(14);
expect(prices[6000]).toEqual(10);
expect(prices[7000]).toEqual(10);
expect(prices[8000]).toEqual(10);
expect(prices[9000]).toEqual(10);
expect(prices[10080]).toEqual(10);
});
});
describe('getEndTimestampForAuction function', () => {
const baselineAuctionData: ArNSBaseAuctionData = {
startHeight: 1,
endHeight: 4,
type: 'lease',
startPrice: 100,
floorPrice: 10,
initiator: 'initiator',
contractTxId: 'atomic',
};
it.each([
[
'should return the correct endTimestamp for a lease',
{
...baselineAuctionData,
type: 'lease',
years: 1,
},
1,
new BlockTimestamp(SECONDS_IN_A_YEAR + 1),
],
[
'should return undefined for a permabuy',
{
...baselineAuctionData,
type: 'permabuy',
},
1,
undefined,
],
])(
'%s',
(
_,
auctionData: ArNSAuctionData,
currentTimestamp: number,
expectedEndTimestamp: BlockTimestamp | undefined,
) => {
const endTimestamp = getEndTimestampForAuction({
auction: auctionData,
currentBlockTimestamp: new BlockTimestamp(currentTimestamp),
});
expect(endTimestamp).toEqual(expectedEndTimestamp);
},
);
});
});
describe('calculateExistingAuctionBidForCaller function', () => {
const nihilisticAuction: ArNSLeaseAuctionData = {
startPrice: Number.NEGATIVE_INFINITY,
floorPrice: Number.NEGATIVE_INFINITY,
startHeight: Number.NEGATIVE_INFINITY,
endHeight: Number.NEGATIVE_INFINITY,
type: 'lease',
initiator: '',
contractTxId: '',
years: 1,
};
it('should throw if submitted bid is less than the required minimum bid', () => {
expect(() => {
calculateExistingAuctionBidForCaller({
caller: '',
auction: nihilisticAuction,
submittedBid: new mIOToken(1),
requiredMinimumBid: new mIOToken(2),
});
}).toThrowError(
'The bid (1 mIO) is less than the current required minimum bid of 2 mIO.',
);
});
});