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 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 | 1x 1x 1x 1x 1x 288x 287x 287x 287x 287x 36x 36x 36x 36x 58x 58x 58x 58x 58x 58x 271x 271x 271x 542x 542x 542x 542x 271x 2x 4x 4x 7x 14x 14x 14x 7x 4x 1x 3x 7x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 4x 4x 4x 2x 2x 2x 2x 2x | import {
MessageType,
PayoutCurvePieceType,
PayoutFunction,
PolynomialPayoutCurvePiece,
RoundingIntervals,
} from '@node-dlc/messaging';
import BigNumber from 'bignumber.js';
import { fromPrecision, getPrecision } from '../utils/Precision';
import { CETPayout, mergePayouts, splitIntoRanges } from './CETCalculator';
interface DlcPoint {
outcome: BigNumber;
payout: BigNumber;
}
export class PolynomialPayoutCurve {
private readonly points: DlcPoint[];
private readonly slope: BigNumber;
private readonly left: DlcPoint;
private readonly right: DlcPoint;
constructor(points: DlcPoint[]) {
if (points.length !== 2) throw new Error('Must have two points');
this.points = points;
this.left = points[0];
this.right = points[points.length - 1];
// m = (y2 - y1) / (x2 - x1)
this.slope = this.right.payout
.minus(this.left.payout)
.dividedBy(this.right.outcome.minus(this.left.outcome));
}
/**
* Get the payout for a given outcome
* @param outcome The outcome to get the payout for
* @returns The payout for the outcome
*/
getPayout(outcome: bigint): BigNumber {
const { left, slope } = this;
const x = new BigNumber(Number(outcome));
// y = mx + b
const payout = slope.times(x.minus(left.outcome)).plus(left.payout);
return payout;
}
/**
* Get the outcome for a given payout
* @param payout The payout to get the outcome for
* @returns The outcome for the payout
*/
getOutcomeForPayout(payout: BigNumber): bigint {
const { left, right, slope } = this;
const y = new BigNumber(Number(payout));
// Handle constant payout curve (slope = 0)
Iif (slope.isZero()) {
// For constant curves, if the payout matches, return any outcome in the range
// If it doesn't match, this is an invalid request
if (y.eq(left.payout)) {
return BigInt(left.outcome.toString());
} else {
// For constant curves, if payout doesn't match, return left outcome
// This prevents Infinity errors in calculations
return BigInt(left.outcome.toString());
}
}
// Find the x value for the given y
// slope = (y2 - y1) / (x2 - x1)
// x1 = (y2 - y1) / slope + x2
const outcome = y
.minus(left.payout)
.dividedBy(slope)
.plus(left.outcome)
.integerValue();
// Clamp the outcome to the valid range
const clampedOutcome = BigNumber.max(
left.outcome,
BigNumber.min(outcome, right.outcome),
);
return BigInt(clampedOutcome.toString());
}
/**
* Serializes PolynomialPayoutCurve to a PolynomialPayoutCurvePiece (for transport)
* @returns A PolynomialPayoutCurvePiece
*/
toPayoutCurvePiece(): PolynomialPayoutCurvePiece {
const { points } = this;
const piece = new PolynomialPayoutCurvePiece();
piece.points = points.map((point) => {
const eventOutcome = BigInt(point.outcome.toString());
const outcomePayout = BigInt(point.payout.toString());
const extraPrecision = getPrecision(point.payout);
return { eventOutcome, outcomePayout, extraPrecision };
});
return piece;
}
/**
* Determine if the payout curve is equal to another
* @param curve A PolynomialPayoutCurve
* @returns True if the curves are the same
*/
equals(curve: PolynomialPayoutCurve): boolean {
return this.points.every((point, i) => {
const otherPoint = curve.points[i];
return (
point.outcome.eq(otherPoint.outcome) &&
point.payout.eq(otherPoint.payout)
);
});
}
/**
* Creates a PolynomialPayoutCurve from a PolynomialPayoutCurvePiece
* @param piece
* @returns A PolynomialPayoutCurve
*/
static fromPayoutCurvePiece(
piece: PolynomialPayoutCurvePiece,
): PolynomialPayoutCurve {
const points = piece.points.map((point) => {
const outcome = new BigNumber(point.eventOutcome.toString());
const payout = new BigNumber(point.outcomePayout.toString()).plus(
fromPrecision(point.extraPrecision),
);
return { outcome, payout };
});
return new PolynomialPayoutCurve(points);
}
/**
* Computes all CETs for a given payout curve
* @param payoutFunction The payout function
* @param totalCollateral The total collateral
* @param roundingIntervals The rounding intervals
* @returns A list of CETs
*/
static computePayouts(
payoutFunction: PayoutFunction,
totalCollateral: bigint,
roundingIntervals: RoundingIntervals,
): CETPayout[] {
if (payoutFunction.payoutFunctionPieces.length < 1)
throw new Error('Must have at least one piece');
payoutFunction.payoutFunctionPieces.forEach((piece) => {
if (
piece.payoutCurvePiece.payoutCurvePieceType !==
PayoutCurvePieceType.Polynomial &&
piece.payoutCurvePiece.type !== MessageType.PolynomialPayoutCurvePiece
)
throw new Error('Payout curve piece must be a polynomial');
});
const CETS: CETPayout[] = [];
// 1. Add the first piece to the list
const { payoutCurvePiece } = payoutFunction.payoutFunctionPieces[0];
const curve = this.fromPayoutCurvePiece(
payoutCurvePiece as PolynomialPayoutCurvePiece,
);
// For the first piece, start from 0 and go to the first endpoint
const firstPiece = payoutFunction.payoutFunctionPieces[0];
// Calculate the start payout by evaluating the curve at outcome 0
const startPayout = curve.getPayout(BigInt(0));
const startPayoutBigInt = BigInt(startPayout.integerValue().toString());
// Only add ranges if there's actually a range to cover
Eif (firstPiece.endPoint.eventOutcome > 0) {
CETS.push(
...splitIntoRanges(
BigInt(0), // Start from 0
firstPiece.endPoint.eventOutcome,
startPayoutBigInt, // Start payout calculated from curve
firstPiece.endPoint.outcomePayout, // End payout from endpoint
totalCollateral,
curve,
roundingIntervals.intervals,
),
);
}
// 2. If there are subsequent pieces, add them to the list
for (let i = 1; i < payoutFunction.payoutFunctionPieces.length; i++) {
const { payoutCurvePiece } = payoutFunction.payoutFunctionPieces[i];
const curve = this.fromPayoutCurvePiece(
payoutCurvePiece as PolynomialPayoutCurvePiece,
);
CETS.push(
...splitIntoRanges(
payoutFunction.payoutFunctionPieces[i - 1].endPoint.eventOutcome,
payoutFunction.payoutFunctionPieces[i].endPoint.eventOutcome,
payoutFunction.payoutFunctionPieces[i - 1].endPoint.outcomePayout,
payoutFunction.payoutFunctionPieces[i].endPoint.outcomePayout,
totalCollateral,
curve,
roundingIntervals.intervals,
),
);
}
// 3. Handle the final range from the last piece to lastEndpoint if it exists
Eif (
payoutFunction.lastEndpoint &&
payoutFunction.payoutFunctionPieces.length > 0
) {
const lastPieceIndex = payoutFunction.payoutFunctionPieces.length - 1;
const lastPiece = payoutFunction.payoutFunctionPieces[lastPieceIndex];
// Check if there's a range to cover
Iif (
payoutFunction.lastEndpoint.eventOutcome >
lastPiece.endPoint.eventOutcome
) {
// For the final range, we'll assume a constant payout from the last piece endpoint to lastEndpoint
// This is a common pattern for DLC payout functions
const finalCurve = new PolynomialPayoutCurve([
{
outcome: new BigNumber(lastPiece.endPoint.eventOutcome.toString()),
payout: new BigNumber(lastPiece.endPoint.outcomePayout.toString()),
},
{
outcome: new BigNumber(
payoutFunction.lastEndpoint.eventOutcome.toString(),
),
payout: new BigNumber(
payoutFunction.lastEndpoint.outcomePayout.toString(),
),
},
]);
CETS.push(
...splitIntoRanges(
lastPiece.endPoint.eventOutcome,
payoutFunction.lastEndpoint.eventOutcome,
lastPiece.endPoint.outcomePayout,
payoutFunction.lastEndpoint.outcomePayout,
totalCollateral,
finalCurve,
roundingIntervals.intervals,
),
);
}
}
return mergePayouts(CETS);
}
}
|