-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOptionsEngine.sol
More file actions
407 lines (337 loc) · 16.8 KB
/
OptionsEngine.sol
File metadata and controls
407 lines (337 loc) · 16.8 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
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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import {Errors} from "./libraries/Errors.sol";
import {Events} from "./libraries/Events.sol";
import {CoreDexRegistry} from "./CoreDexRegistry.sol";
import {CoretimeLedger} from "./CoretimeLedger.sol";
import {ICoretimeNFT} from "./interfaces/ICoretimeNFT.sol";
import {IAssetsPrecompile} from "./interfaces/IAssetsPrecompile.sol";
/// @title OptionsEngine
/// @notice Issues and manages European-style coretime options. A call option
/// gives the buyer the right (not obligation) to purchase a specific
/// coretime region at the strike price at expiry. A put option gives
/// the right to sell. Options require premium payment upfront and
/// collateral from the option writer.
///
/// @dev EUROPEAN-STYLE:
/// Options are exercisable only at the expiry block, not before.
/// This simplifies settlement mechanics and removes the complexity
/// of early-exercise premium adjustments.
///
/// PRICING:
/// Premium is fetched from PricingModule (PVM) before issuance —
/// non-negotiable, not user-set. The PricingModule implements
/// Black-Scholes with inputs from CoretimeOracle.
///
/// COLLATERAL:
/// - Call writer: must escrow the coretime NFT (physical delivery)
/// - Put writer: must escrow DOT equal to strikePriceDOT (cash settlement)
/// - Collateral locked for full option lifetime — no partial withdrawal
contract OptionsEngine {
// -------------------------------------------------------------------------
// Constants
// -------------------------------------------------------------------------
address public constant ASSETS_PRECOMPILE =
0xc82e04234549D48b961d8Cb3F3c60609dDF3F006; // MockAssets PVM contract
uint8 public constant OPTION_CALL = 0;
uint8 public constant OPTION_PUT = 1;
// -------------------------------------------------------------------------
// Registry keys
// -------------------------------------------------------------------------
bytes32 public constant KEY_PRICING_MODULE = keccak256("PricingModule");
bytes32 public constant KEY_CORETIME_ORACLE = keccak256("CoretimeOracle");
bytes32 public constant KEY_SETTLEMENT = keccak256("SettlementExecutor");
bytes32 public constant KEY_LEDGER = keccak256("CoretimeLedger");
// -------------------------------------------------------------------------
// Structs
// -------------------------------------------------------------------------
struct Option {
uint256 optionId;
address writer; // sold the option, posted collateral
address holder; // paid the premium, holds the right
uint128 coretimeRegion; // underlying coretime NFT token ID
uint128 strikePriceDOT; // strike in DOT planck (18 decimals)
uint128 premiumDOT; // premium paid at issuance (from PricingModule)
uint32 expiryBlock; // relay chain block
uint8 optionType; // 0 = call, 1 = put
uint8 status; // 0=active, 1=exercised, 2=expired
}
// -------------------------------------------------------------------------
// State
// -------------------------------------------------------------------------
CoreDexRegistry public immutable registry;
ICoretimeNFT public immutable coretimeNFT;
mapping(uint256 => Option) public options;
mapping(address => uint256[]) public writerOptions;
mapping(address => uint256[]) public holderOptions;
uint256 public nextOptionId;
// -------------------------------------------------------------------------
// Constructor
// -------------------------------------------------------------------------
constructor(address _registry, address _coretimeNFT) {
if (_registry == address(0)) revert Errors.ZeroAddress();
if (_coretimeNFT == address(0)) revert Errors.ZeroAddress();
registry = CoreDexRegistry(_registry);
coretimeNFT = ICoretimeNFT(_coretimeNFT);
nextOptionId = 1;
}
// -------------------------------------------------------------------------
// Modifiers
// -------------------------------------------------------------------------
modifier whenNotPaused() {
if (registry.paused()) revert Errors.ProtocolPaused();
_;
}
// -------------------------------------------------------------------------
// Write Options
// -------------------------------------------------------------------------
/// @notice Write a call option — writer escrows their coretime NFT.
/// The option is created but not yet active until a holder buys it.
/// @param regionId The coretime region NFT token ID.
/// @param strike Strike price in DOT planck (18 decimals).
/// @param expiryBlock Relay chain block at which the option can be exercised.
/// @return optionId The newly created option ID.
function writeCall(
uint128 regionId,
uint128 strike,
uint32 expiryBlock
)
external
whenNotPaused
returns (uint256 optionId)
{
// --- CHECKS ---
if (expiryBlock <= uint32(block.number)) {
revert Errors.DeliveryBlockInPast(expiryBlock);
}
if (strike == 0) revert Errors.ZeroAmount();
if (coretimeNFT.ownerOf(uint256(regionId)) != msg.sender) {
revert Errors.Unauthorised(msg.sender);
}
// Get premium from PricingModule
uint128 premium = _getPremium(strike, expiryBlock, OPTION_CALL);
// Lock region in ledger
CoretimeLedger ledger = CoretimeLedger(registry.resolve(KEY_LEDGER));
ledger.lockRegion(regionId, ledger.OPTION_POSITION());
// --- EFFECTS ---
optionId = nextOptionId++;
options[optionId] = Option({
optionId: optionId,
writer: msg.sender,
holder: address(0),
coretimeRegion: regionId,
strikePriceDOT: strike,
premiumDOT: premium,
expiryBlock: expiryBlock,
optionType: OPTION_CALL,
status: 0 // active (awaiting holder)
});
writerOptions[msg.sender].push(optionId);
// --- INTERACT ---
// Escrow the NFT
coretimeNFT.transferFrom(msg.sender, address(this), uint256(regionId));
ledger.incrementPositionCount(msg.sender);
emit Events.OptionWritten(optionId, msg.sender, OPTION_CALL, premium);
}
/// @notice Write a put option — writer escrows DOT equal to strike price.
/// @param regionId The coretime region NFT token ID (underlying).
/// @param strike Strike price in DOT planck (18 decimals).
/// @param expiryBlock Relay chain block at which the option can be exercised.
/// @return optionId The newly created option ID.
function writePut(
uint128 regionId,
uint128 strike,
uint32 expiryBlock
)
external
whenNotPaused
returns (uint256 optionId)
{
if (expiryBlock <= uint32(block.number)) {
revert Errors.DeliveryBlockInPast(expiryBlock);
}
if (strike == 0) revert Errors.ZeroAmount();
uint128 premium = _getPremium(strike, expiryBlock, OPTION_PUT);
// --- EFFECTS ---
optionId = nextOptionId++;
options[optionId] = Option({
optionId: optionId,
writer: msg.sender,
holder: address(0),
coretimeRegion: regionId,
strikePriceDOT: strike,
premiumDOT: premium,
expiryBlock: expiryBlock,
optionType: OPTION_PUT,
status: 0
});
writerOptions[msg.sender].push(optionId);
// --- INTERACT ---
// Escrow DOT equal to strike price from writer (using transferFrom since contract is calling)
bool transferred = IAssetsPrecompile(ASSETS_PRECOMPILE)
.transferFrom(msg.sender, address(this), uint256(strike));
if (!transferred) revert Errors.DOTTransferFailed(uint256(strike));
CoretimeLedger ledger = CoretimeLedger(registry.resolve(KEY_LEDGER));
ledger.addMargin(msg.sender, uint256(strike));
ledger.incrementPositionCount(msg.sender);
emit Events.OptionWritten(optionId, msg.sender, OPTION_PUT, premium);
}
// -------------------------------------------------------------------------
// Buy Option
// -------------------------------------------------------------------------
/// @notice Buy an option — holder pays the premium to the writer.
/// The option becomes active.
/// @param optionId The option ID to purchase.
function buyOption(uint256 optionId) external whenNotPaused {
Option storage opt = options[optionId];
// --- CHECKS ---
if (opt.optionId == 0) revert Errors.OptionNotActive(optionId);
if (opt.status != 0) revert Errors.OptionNotActive(optionId);
if (opt.holder != address(0)) revert Errors.OptionNotActive(optionId);
if (opt.writer == msg.sender) revert Errors.Unauthorised(msg.sender);
// --- EFFECTS ---
opt.holder = msg.sender;
holderOptions[msg.sender].push(optionId);
// --- INTERACT ---
// Transfer premium from holder directly to writer (using transferFrom)
bool premiumSent = IAssetsPrecompile(ASSETS_PRECOMPILE)
.transferFrom(msg.sender, opt.writer, uint256(opt.premiumDOT));
if (!premiumSent) revert Errors.DOTTransferFailed(uint256(opt.premiumDOT));
CoretimeLedger ledger = CoretimeLedger(registry.resolve(KEY_LEDGER));
ledger.incrementPositionCount(msg.sender);
emit Events.OptionPurchased(optionId, msg.sender);
}
// -------------------------------------------------------------------------
// Exercise Option (European-style: only at expiry block)
// -------------------------------------------------------------------------
/// @notice Exercise an option at the expiry block.
/// For calls: holder pays strike, receives NFT.
/// For puts: holder delivers NFT, receives strike DOT.
/// @param optionId The option ID to exercise.
function exercise(uint256 optionId) external whenNotPaused {
Option storage opt = options[optionId];
// --- CHECKS ---
if (opt.optionId == 0) revert Errors.OptionNotActive(optionId);
if (opt.status != 0) revert Errors.OptionNotActive(optionId);
if (opt.holder == address(0)) revert Errors.OptionNotActive(optionId);
if (opt.holder != msg.sender) revert Errors.NotOptionHolder(msg.sender, optionId);
if (uint32(block.number) != opt.expiryBlock) {
revert Errors.NotAtExpiryBlock(optionId, opt.expiryBlock);
}
// --- EFFECTS ---
opt.status = 1; // exercised
CoretimeLedger ledger = CoretimeLedger(registry.resolve(KEY_LEDGER));
if (opt.optionType == OPTION_CALL) {
// --- INTERACT: Call exercise ---
// Holder pays strike price in DOT (using transferFrom since contract is calling)
bool dotPaid = IAssetsPrecompile(ASSETS_PRECOMPILE)
.transferFrom(msg.sender, address(this), uint256(opt.strikePriceDOT));
if (!dotPaid) revert Errors.DOTTransferFailed(uint256(opt.strikePriceDOT));
// Transfer DOT to writer
bool dotToWriter = IAssetsPrecompile(ASSETS_PRECOMPILE)
.transfer(opt.writer, uint256(opt.strikePriceDOT));
if (!dotToWriter) revert Errors.DOTTransferFailed(uint256(opt.strikePriceDOT));
// Transfer NFT to holder via SettlementExecutor
address settlementAddr = registry.resolve(KEY_SETTLEMENT);
// solhint-disable-next-line avoid-low-level-calls
(bool settled, ) = settlementAddr.call(
abi.encodeWithSignature("settleOption(uint256)", optionId)
);
require(settled, "Settlement call failed");
// Unlock region
ledger.unlockRegion(opt.coretimeRegion);
} else {
// --- INTERACT: Put exercise ---
// Holder delivers NFT
coretimeNFT.transferFrom(msg.sender, opt.writer, uint256(opt.coretimeRegion));
// Release escrowed DOT (strike) to holder
bool dotToHolder = IAssetsPrecompile(ASSETS_PRECOMPILE)
.transfer(opt.holder, uint256(opt.strikePriceDOT));
if (!dotToHolder) revert Errors.DOTTransferFailed(uint256(opt.strikePriceDOT));
ledger.releaseMargin(opt.writer, uint256(opt.strikePriceDOT));
}
ledger.decrementPositionCount(opt.writer);
ledger.decrementPositionCount(opt.holder);
emit Events.OptionExercised(optionId, msg.sender);
}
/// @notice Expire an unexercised option after the expiry block.
/// Releases collateral back to the writer.
/// @param optionId The option ID to expire.
function expireOption(uint256 optionId) external whenNotPaused {
Option storage opt = options[optionId];
// --- CHECKS ---
if (opt.optionId == 0) revert Errors.OptionNotActive(optionId);
if (opt.status != 0) revert Errors.OptionNotActive(optionId);
if (uint32(block.number) <= opt.expiryBlock) {
revert Errors.OptionNotExpired(optionId);
}
// --- EFFECTS ---
opt.status = 2; // expired
CoretimeLedger ledger = CoretimeLedger(registry.resolve(KEY_LEDGER));
// --- INTERACT ---
if (opt.optionType == OPTION_CALL) {
// Return NFT to writer
coretimeNFT.transferFrom(address(this), opt.writer, uint256(opt.coretimeRegion));
ledger.unlockRegion(opt.coretimeRegion);
} else {
// Return DOT to writer
bool dotReturned = IAssetsPrecompile(ASSETS_PRECOMPILE)
.transfer(opt.writer, uint256(opt.strikePriceDOT));
if (!dotReturned) revert Errors.DOTTransferFailed(uint256(opt.strikePriceDOT));
ledger.releaseMargin(opt.writer, uint256(opt.strikePriceDOT));
}
ledger.decrementPositionCount(opt.writer);
if (opt.holder != address(0)) {
ledger.decrementPositionCount(opt.holder);
}
emit Events.OptionExpired(optionId);
}
// -------------------------------------------------------------------------
// Internal — PricingModule Integration
// -------------------------------------------------------------------------
/// @notice Get the premium for an option from the PricingModule (PVM).
/// @param strike Strike price in DOT planck.
/// @param expiryBlock Expiry block number.
/// @param optionType 0 = call, 1 = put.
/// @return premium Premium in DOT planck.
function _getPremium(
uint128 strike,
uint32 expiryBlock,
uint8 optionType
) internal view returns (uint128 premium) {
// Get spot price and volatility from CoretimeOracle
address oracleAddr = registry.resolve(KEY_CORETIME_ORACLE);
(bool spotSuccess, bytes memory spotResult) = oracleAddr.staticcall(
abi.encodeWithSignature("spotPrice()")
);
if (!spotSuccess || spotResult.length < 32) revert Errors.PricingModuleCallFailed();
uint128 spotPrice = abi.decode(spotResult, (uint128));
(bool volSuccess, bytes memory volResult) = oracleAddr.staticcall(
abi.encodeWithSignature("impliedVolatility()")
);
if (!volSuccess || volResult.length < 32) revert Errors.PricingModuleCallFailed();
uint64 volatility = abi.decode(volResult, (uint64));
// Call PricingModule to get premium via Black-Scholes
address pricingAddr = registry.resolve(KEY_PRICING_MODULE);
bytes memory calldata_ = abi.encodeWithSignature(
"price_option(uint128,uint128,uint32,uint64,uint8)",
spotPrice,
strike,
expiryBlock - uint32(block.number),
volatility,
optionType
);
(bool success, bytes memory result) = pricingAddr.staticcall(calldata_);
if (!success || result.length < 64) revert Errors.PricingModuleCallFailed();
(premium, ) = abi.decode(result, (uint128, uint128));
}
// -------------------------------------------------------------------------
// View Functions
// -------------------------------------------------------------------------
function getWriterOptions(address writer) external view returns (uint256[] memory) {
return writerOptions[writer];
}
function getHolderOptions(address holder) external view returns (uint256[] memory) {
return holderOptions[holder];
}
}