/
perpetuals.go
411 lines (391 loc) · 11.6 KB
/
perpetuals.go
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
package constants
import (
"github.com/furyanprotocol/v4-chain/protocol/dtypes"
perptypes "github.com/furyanprotocol/v4-chain/protocol/x/perpetuals/types"
)
func init() {
_ = TestTxBuilder.SetMsgs(EmptyMsgAddPremiumVotes)
EmptyMsgAddPremiumVotesTxBytes, _ = TestEncodingCfg.TxConfig.TxEncoder()(TestTxBuilder.GetTx())
_ = TestTxBuilder.SetMsgs(ValidMsgAddPremiumVotes)
ValidMsgAddPremiumVotesTxBytes, _ = TestEncodingCfg.TxConfig.TxEncoder()(TestTxBuilder.GetTx())
_ = TestTxBuilder.SetMsgs(InvalidMsgAddPremiumVotes)
InvalidMsgAddPremiumVotesTxBytes, _ = TestEncodingCfg.TxConfig.TxEncoder()(TestTxBuilder.GetTx())
}
// LiquidityTier objects.
var LiquidityTiers = []perptypes.LiquidityTier{
{
Id: 0,
Name: "0",
InitialMarginPpm: 1_000_000,
MaintenanceFractionPpm: 1_000_000,
ImpactNotional: 500_000_000,
},
{
Id: 1,
Name: "1",
InitialMarginPpm: 1_000_000,
MaintenanceFractionPpm: 750_000,
ImpactNotional: 500_000_000,
},
{
Id: 2,
Name: "2",
InitialMarginPpm: 1_000_000,
MaintenanceFractionPpm: 0,
ImpactNotional: 500_000_000,
},
{
Id: 3,
Name: "3",
InitialMarginPpm: 200_000,
MaintenanceFractionPpm: 500_000,
ImpactNotional: 2_500_000_000,
},
{
Id: 4,
Name: "4",
InitialMarginPpm: 500_000,
MaintenanceFractionPpm: 800_000,
ImpactNotional: 1_000_000_000,
},
{
Id: 5,
Name: "5",
InitialMarginPpm: 500_000,
MaintenanceFractionPpm: 600_000,
ImpactNotional: 1_000_000_000,
},
{
Id: 6,
Name: "6",
InitialMarginPpm: 200_000,
MaintenanceFractionPpm: 900_000,
ImpactNotional: 2_500_000_000,
},
{
Id: 7,
Name: "7",
InitialMarginPpm: 0,
MaintenanceFractionPpm: 0,
ImpactNotional: 1_000_000_000,
},
{
Id: 8,
Name: "8",
InitialMarginPpm: 9_910, // 0.9910%
MaintenanceFractionPpm: 1_000_000,
ImpactNotional: 50_454_000_000,
},
{
Id: 101,
Name: "101",
InitialMarginPpm: 200_000,
MaintenanceFractionPpm: 500_000,
ImpactNotional: 2_500_000_000,
},
}
// Perpetual genesis parameters.
const TestFundingRateClampFactorPpm = 6_000_000
const TestPremiumVoteClampFactorPpm = 60_000_000
const TestMinNumVotesPerSample = 15
var PerpetualsGenesisParams = perptypes.Params{
FundingRateClampFactorPpm: TestFundingRateClampFactorPpm,
PremiumVoteClampFactorPpm: TestPremiumVoteClampFactorPpm,
MinNumVotesPerSample: TestMinNumVotesPerSample,
}
var Perpetuals_GenesisState_ParamsOnly = perptypes.GenesisState{
Params: PerpetualsGenesisParams,
}
// Perpetual objects.
var (
BtcUsd_InvalidMarketId = perptypes.Perpetual{
Params: perptypes.PerpetualParams{
Id: 0,
Ticker: "BTC-USD invalid market Id",
MarketId: uint32(9999),
AtomicResolution: int32(-10),
DefaultFundingPpm: int32(0),
LiquidityTier: uint32(0),
},
FundingIndex: dtypes.ZeroInt(),
}
BtcUsd_0DefaultFunding_0AtomicResolution = perptypes.Perpetual{
Params: perptypes.PerpetualParams{
Id: 0,
Ticker: "BTC-USD 0 percent default funding, 0 atomic resolution",
MarketId: uint32(0),
AtomicResolution: int32(0),
DefaultFundingPpm: int32(0),
LiquidityTier: uint32(2),
},
FundingIndex: dtypes.ZeroInt(),
}
BtcUsd_NegativeDefaultFunding_10AtomicResolution = perptypes.Perpetual{
Params: perptypes.PerpetualParams{
Id: 0,
Ticker: "BTC-USD -0.001 percent percent default funding",
MarketId: uint32(0),
AtomicResolution: int32(-10),
DefaultFundingPpm: int32(-1_000),
LiquidityTier: uint32(1),
},
FundingIndex: dtypes.ZeroInt(),
}
BtcUsd_0DefaultFunding_10AtomicResolution = perptypes.Perpetual{
Params: perptypes.PerpetualParams{
Id: 0,
Ticker: "BTC-USD 0 percent default funding",
MarketId: uint32(0),
AtomicResolution: int32(-10),
DefaultFundingPpm: int32(0),
LiquidityTier: uint32(1),
},
FundingIndex: dtypes.ZeroInt(),
}
BtcUsd_0DefaultFunding_10AtomicResolution_20IM_18MM = perptypes.Perpetual{
Params: perptypes.PerpetualParams{
Id: 0,
Ticker: "BTC-USD 0 percent default funding, 20% IM, 18% MM",
MarketId: uint32(0),
AtomicResolution: int32(-10),
DefaultFundingPpm: int32(0),
LiquidityTier: uint32(6),
},
FundingIndex: dtypes.ZeroInt(),
}
BtcUsd_0_001Percent_DefaultFunding_10AtomicResolution = perptypes.Perpetual{
Params: perptypes.PerpetualParams{
Id: 10000,
Ticker: "BTC-USD 0.001 percent default funding",
MarketId: uint32(0),
AtomicResolution: int32(-10),
DefaultFundingPpm: int32(1000), // 0.001%
LiquidityTier: uint32(1),
},
FundingIndex: dtypes.ZeroInt(),
}
BtcUsd_SmallMarginRequirement = perptypes.Perpetual{
Params: perptypes.PerpetualParams{
Id: 0,
Ticker: "BTC-USD small margin requirement",
MarketId: uint32(0),
AtomicResolution: int32(-8),
DefaultFundingPpm: int32(0),
LiquidityTier: uint32(8),
},
FundingIndex: dtypes.ZeroInt(),
}
BtcUsd_100PercentMarginRequirement = perptypes.Perpetual{
Params: perptypes.PerpetualParams{
Id: 0,
Ticker: "BTC-USD 100% margin requirement",
MarketId: uint32(0),
AtomicResolution: int32(-8),
DefaultFundingPpm: int32(0),
LiquidityTier: uint32(0),
},
FundingIndex: dtypes.ZeroInt(),
}
BtcUsd_50PercentInitial_40PercentMaintenance = perptypes.Perpetual{
Params: perptypes.PerpetualParams{
Id: 0,
Ticker: "BTC-USD 50/40 margin requirements",
MarketId: uint32(0),
AtomicResolution: int32(-8),
DefaultFundingPpm: int32(0),
LiquidityTier: uint32(4),
},
FundingIndex: dtypes.ZeroInt(),
}
BtcUsd_20PercentInitial_10PercentMaintenance = perptypes.Perpetual{
Params: perptypes.PerpetualParams{
Id: 0,
Ticker: "BTC-USD 20/10 margin requirements",
MarketId: uint32(0),
AtomicResolution: int32(-8),
DefaultFundingPpm: int32(0),
LiquidityTier: uint32(3),
},
FundingIndex: dtypes.ZeroInt(),
}
BtcUsd_NoMarginRequirement = perptypes.Perpetual{
Params: perptypes.PerpetualParams{
Id: 0,
Ticker: "BTC-USD no margin requirement",
MarketId: uint32(0),
AtomicResolution: int32(-8),
DefaultFundingPpm: int32(0),
LiquidityTier: uint32(7),
},
FundingIndex: dtypes.ZeroInt(),
}
EthUsd_0DefaultFunding_9AtomicResolution = perptypes.Perpetual{
Params: perptypes.PerpetualParams{
Id: 1,
Ticker: "ETH-USD default fundingm, -9 atomic resolution",
MarketId: uint32(1),
AtomicResolution: int32(-9),
DefaultFundingPpm: int32(0),
LiquidityTier: uint32(5),
},
FundingIndex: dtypes.ZeroInt(),
}
EthUsd_NoMarginRequirement = perptypes.Perpetual{
Params: perptypes.PerpetualParams{
Id: 1,
Ticker: "ETH-USD no margin requirement",
MarketId: uint32(1),
AtomicResolution: int32(-9),
DefaultFundingPpm: int32(0),
LiquidityTier: uint32(7),
},
FundingIndex: dtypes.ZeroInt(),
}
EthUsd_20PercentInitial_10PercentMaintenance = perptypes.Perpetual{
Params: perptypes.PerpetualParams{
Id: 1,
Ticker: "ETH-USD 20/10 margin requirements",
MarketId: uint32(1),
AtomicResolution: int32(-9),
DefaultFundingPpm: int32(0),
LiquidityTier: uint32(3),
},
FundingIndex: dtypes.ZeroInt(),
}
EthUsd_100PercentMarginRequirement = perptypes.Perpetual{
Params: perptypes.PerpetualParams{
Id: 1,
Ticker: "ETH-USD 100/100 margin requirements",
MarketId: uint32(1),
AtomicResolution: int32(-9),
DefaultFundingPpm: int32(0),
LiquidityTier: uint32(0),
},
FundingIndex: dtypes.ZeroInt(),
}
SolUsd_20PercentInitial_10PercentMaintenance = perptypes.Perpetual{
Params: perptypes.PerpetualParams{
Id: 2,
Ticker: "SOL-USD 20/10 margin requirements",
MarketId: uint32(2),
AtomicResolution: int32(-9),
DefaultFundingPpm: int32(0),
LiquidityTier: uint32(3),
},
FundingIndex: dtypes.ZeroInt(),
}
)
// AddPremiumVotes messages.
var (
TestAddPremiumVotesMsg = &perptypes.MsgAddPremiumVotes{
Votes: []perptypes.FundingPremium{
{
PerpetualId: 0,
PremiumPpm: 1000,
},
},
}
EmptyMsgAddPremiumVotes = &perptypes.MsgAddPremiumVotes{}
EmptyMsgAddPremiumVotesTxBytes []byte
ValidMsgAddPremiumVotes = &perptypes.MsgAddPremiumVotes{
Votes: []perptypes.FundingPremium{
{PerpetualId: 1, PremiumPpm: 1_000},
{PerpetualId: 2, PremiumPpm: 2_000},
},
}
ValidMsgAddPremiumVotesTxBytes []byte
InvalidMsgAddPremiumVotes = &perptypes.MsgAddPremiumVotes{
Votes: []perptypes.FundingPremium{
{PerpetualId: 3, PremiumPpm: 3_000}, // descending order is incorrect.
{PerpetualId: 2, PremiumPpm: 2_000},
},
}
InvalidMsgAddPremiumVotesTxBytes []byte
Perpetuals_DefaultGenesisState = perptypes.GenesisState{
LiquidityTiers: []perptypes.LiquidityTier{
{
Id: uint32(0),
Name: "Large-Cap",
InitialMarginPpm: 200_000,
MaintenanceFractionPpm: 500_000,
ImpactNotional: 2_500_000_000,
},
{
Id: uint32(1),
Name: "Mid-Cap",
InitialMarginPpm: 300_000,
MaintenanceFractionPpm: 600_000,
ImpactNotional: 1_667_000_000,
},
{
Id: uint32(2),
Name: "Small-Cap",
InitialMarginPpm: 400_000,
MaintenanceFractionPpm: 700_000,
ImpactNotional: 1_250_000_000,
},
},
Params: PerpetualsGenesisParams,
Perpetuals: []perptypes.Perpetual{
{
Params: perptypes.PerpetualParams{
Id: uint32(0),
Ticker: "genesis_test_ticker_0",
LiquidityTier: 0,
},
FundingIndex: dtypes.ZeroInt(),
},
{
Params: perptypes.PerpetualParams{
Id: uint32(1),
Ticker: "genesis_test_ticker_1",
LiquidityTier: 1,
},
FundingIndex: dtypes.ZeroInt(),
},
},
}
)
// Return a list of `count` constant funding premiums equal to `value`.
func GenerateConstantFundingPremiums(
value int32,
count uint32,
) (
result []int32,
) {
result = make([]int32, count)
for i := uint32(0); i < count; i += 1 {
result[i] = value
}
return result
}
// Returns a funding sample list of length `n = sum(counts)`, where
// each `values[i]` appears `counts[i]` times.
func GenerateFundingSamplesWithValues(
values []int32,
counts []uint32,
) (
result []int32,
) {
for i, count := range counts {
result = append(result, GenerateConstantFundingPremiums(values[i], count)...)
}
return result
}
// Return a list of 60 funding premium samples, with 30 equal to
// 0.001% or 1000 in ppm, 15 equal to -0.1% and 15 equal to 0.1%.
func FundingSamples_Constant_0_001_Percent_Length_60_With_Noises() (
result []int32,
) {
result = make([]int32, 60)
for i := 0; i < 30; i += 1 {
result[i] = 1000
}
for i := 30; i < 45; i += 1 {
result[i] = 100000
}
for i := 45; i < 60; i += 1 {
result[i] = -100000
}
return result
}