Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 1x 1x 1x 1x 1x 1x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 2x 4x 1x 4x 4x 4x | import {
ContractDescriptorType,
ContractInfo,
DigitDecompositionEventDescriptor,
HyperbolaPayoutCurvePiece,
MessageType,
MultiOracleInfo,
NumericalDescriptor,
PayoutCurvePieceType,
PayoutFunction,
SingleContractInfo,
SingleOracleInfo,
} from '@node-dlc/messaging';
import BN from 'bignumber.js';
import { HyperbolaPayoutCurve } from '../HyperbolaPayoutCurve';
import { CoveredCall } from './CoveredCall';
import { ShortPut } from './ShortPut';
export interface OptionInfo {
contractSize: bigint;
strikePrice: bigint;
premium?: bigint;
type?: OptionType;
expiry: Date;
}
export type HasOfferCollateral = {
offerCollateral: bigint;
};
export type HasContractInfo = {
contractInfo: ContractInfo;
};
export type HasType = {
type: MessageType;
};
export type OptionType = 'put' | 'call';
export function getOptionInfoFromContractInfo(
_contractInfo: ContractInfo,
): OptionInfo {
Iif (_contractInfo.type !== MessageType.SingleContractInfo)
throw Error('Only SingleContractInfo currently supported');
const contractInfo = _contractInfo as SingleContractInfo;
Iif (
contractInfo.contractDescriptor.contractDescriptorType !==
ContractDescriptorType.NumericOutcome
)
throw Error('Numeric Descriptor currently supported');
const oracleInfo = contractInfo.oracleInfo;
// Handle both SingleOracleInfo and MultiOracleInfo using proper type discrimination
let eventMaturityEpoch: number;
let eventDescriptor: DigitDecompositionEventDescriptor;
switch (oracleInfo.type) {
case MessageType.SingleOracleInfo: {
const singleOracleInfo = oracleInfo as SingleOracleInfo;
eventMaturityEpoch =
singleOracleInfo.announcement.oracleEvent.eventMaturityEpoch;
eventDescriptor = singleOracleInfo.announcement.oracleEvent
.eventDescriptor as DigitDecompositionEventDescriptor;
Iif (
singleOracleInfo.announcement.oracleEvent.eventDescriptor.type !==
MessageType.DigitDecompositionEventDescriptorV0
) {
throw Error(
'Only DigitDecompositionEventDescriptorV0 currently supported',
);
}
break;
}
case MessageType.MultiOracleInfo: {
const multiOracleInfo = oracleInfo as MultiOracleInfo;
eventMaturityEpoch =
multiOracleInfo.announcements[0].oracleEvent.eventMaturityEpoch;
eventDescriptor = multiOracleInfo.announcements[0].oracleEvent
.eventDescriptor as DigitDecompositionEventDescriptor;
if (
multiOracleInfo.announcements[0].oracleEvent.eventDescriptor.type !==
MessageType.DigitDecompositionEventDescriptorV0
) {
throw Error(
'Only DigitDecompositionEventDescriptorV0 currently supported',
);
}
break;
}
default:
throw Error(`Unknown oracle info type: ${oracleInfo.type}`);
}
const { base: oracleBase, nbDigits: oracleDigits } = eventDescriptor;
const contractDescriptor = contractInfo.contractDescriptor as NumericalDescriptor;
Iif (contractDescriptor.payoutFunction.type !== MessageType.PayoutFunction)
throw Error('Only PayoutFunction currently supported');
const payoutFunction = contractDescriptor.payoutFunction as PayoutFunction;
Iif (payoutFunction.payoutFunctionPieces.length === 0)
throw Error('PayoutFunction must have at least once PayoutFunctionPiece');
Iif (payoutFunction.payoutFunctionPieces.length > 1)
throw Error('More than one PayoutFunctionPiece not supported');
const payoutCurvePiece = payoutFunction.payoutFunctionPieces[0]
.payoutCurvePiece as HyperbolaPayoutCurvePiece;
Iif (payoutCurvePiece.payoutCurvePieceType !== PayoutCurvePieceType.Hyperbola)
throw Error('Must be HyperbolaPayoutCurvePiece');
Iif (!payoutCurvePiece.b.isZero() || !payoutCurvePiece.c.isZero())
throw Error('b and c HyperbolaPayoutCurvePiece values must be 0');
const curve = HyperbolaPayoutCurve.fromPayoutCurvePiece(payoutCurvePiece);
const maxOutcome = BigInt(
new BN(oracleBase).pow(oracleDigits).minus(1).toString(10),
);
// For the new PayoutFunction structure, we need to get the initial payout from the first piece
const initialPayout =
payoutFunction.payoutFunctionPieces[0].endPoint.outcomePayout;
const isAscending = curve.getPayout(maxOutcome).gt(Number(initialPayout));
const expiry = new Date(eventMaturityEpoch * 1000);
const totalCollateral = contractInfo.totalCollateral;
// if curve is ascending, assume it is a put.
const contractSize = isAscending
? BigInt(payoutCurvePiece.translatePayout.toDecimal().toString()) -
totalCollateral
: totalCollateral -
BigInt(payoutCurvePiece.translatePayout.toDecimal().toString());
const strikePrice =
BigInt(payoutCurvePiece.d.toDecimal().toString()) / contractSize;
// rebuild payout curve from option info and perform a sanity check
const { payoutCurve: sanityCurve } = isAscending
? ShortPut.buildCurve(
strikePrice,
contractSize,
totalCollateral,
oracleBase,
oracleDigits,
)
: CoveredCall.buildCurve(
strikePrice,
contractSize,
oracleBase,
oracleDigits,
);
const type = isAscending ? 'put' : 'call';
if (!curve.equals(sanityCurve))
throw new Error(
'Payout curve built from extracted OptionInfo does not match original payout curve',
);
return { contractSize, strikePrice, expiry, type };
}
export function getOptionInfoFromOffer(
offer: HasOfferCollateral & HasContractInfo & HasType,
): OptionInfo {
Iif (
offer.type !== MessageType.DlcOffer &&
offer.type !== MessageType.OrderOffer
)
throw Error('Only DlcOffer and OrderOffer currently supported');
const premium = offer.contractInfo.totalCollateral - offer.offerCollateral;
return {
...getOptionInfoFromContractInfo(offer.contractInfo),
premium,
};
}
|