Skip to content

Commit

Permalink
fix: high leverage collateral csoinfo tests
Browse files Browse the repository at this point in the history
These tests were affected by the DualFundingTxFinalizer future fees
fix.
  • Loading branch information
matthewjablack committed Mar 15, 2024
1 parent 795f23b commit f2628df
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 23 deletions.
60 changes: 44 additions & 16 deletions packages/core/__tests__/dlc/finance/CsoInfo.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
dustThreshold,
getFinalizerByCount,
LinearPayout,
roundDownToNearestMultiplier,
roundUpToNearestMultiplier,
} from '../../../lib';
import {
Expand Down Expand Up @@ -631,10 +632,19 @@ describe('CsoInfo', () => {
} = getCsoInfoFromOffer(csoOrderOffer, 'v1');

// Fees are very high, so use dust threshold for max gain
const expectedMaxGainForContractSize = Value.fromSats(
const offerFees = Value.fromSats(
getFinalizerByCount(feePerByte, numOfferInputs, 3, numContracts)
.offerFees,
).addn(Value.fromSats(dustThreshold(feePerByte)));
);
const expectedMaxGainForContractSize_ = offerFees.addn(
Value.fromSats(dustThreshold(BigInt(feePerByte))),
);
const expectedMaxGainForContractSize = Value.fromSats(
roundUpToNearestMultiplier(
expectedMaxGainForContractSize_.sats,
BigInt(100),
),
);

const expectedMaxLossForContractSize = Value.fromSats(
roundUpToNearestMultiplier(
Expand All @@ -644,24 +654,24 @@ describe('CsoInfo', () => {
);

const expectedNormalizedMaxGain = Value.fromSats(
roundUpToNearestMultiplier(
roundDownToNearestMultiplier(
(expectedMaxGainForContractSize.sats * BigInt(1e8)) /
contractSize.sats,
BigInt(100),
),
);

expect(actualNormalizedMaxGain.sats).to.equal(
expectedNormalizedMaxGain.sats,
);
expect(actualNormalizedMaxLoss.sats).to.equal(normalizedMaxLoss.sats);
expect(actualMaxGainForContractSize.sats).to.equal(
expectedMaxGainForContractSize.sats,
);
expect(actualMaxLossForContractSize.sats).to.equal(
expectedMaxLossForContractSize.sats,
);
expect(minPayout).to.equal(BigInt(119900));
expect(actualNormalizedMaxLoss.sats).to.equal(normalizedMaxLoss.sats);
expect(actualNormalizedMaxGain.sats).to.equal(
expectedNormalizedMaxGain.sats,
); // TODO: Fix issue with this line
expect(minPayout).to.equal(BigInt(121200));
expect(maxPayout).to.equal(collateral.sats);
expect(actualContractSize.sats).to.equal(contractSize.sats);
expect(actualOfferCollateral.sats).to.equal(
Expand Down Expand Up @@ -706,10 +716,19 @@ describe('CsoInfo', () => {
} = getCsoInfoFromOffer(csoOrderOffer, 'v1');

// Fees are very high, so use dust threshold for max gain
const expectedMaxGainForContractSize = Value.fromSats(
const offerFees = Value.fromSats(
getFinalizerByCount(feePerByte, numOfferInputs, 3, numContracts)
.offerFees,
).addn(Value.fromSats(dustThreshold(feePerByte)));
);
const expectedMaxGainForContractSize_ = offerFees.addn(
Value.fromSats(dustThreshold(BigInt(feePerByte))),
);
const expectedMaxGainForContractSize = Value.fromSats(
roundUpToNearestMultiplier(
expectedMaxGainForContractSize_.sats,
BigInt(100),
),
);

const expectedMaxLossForContractSize = Value.fromSats(
roundUpToNearestMultiplier(
Expand All @@ -719,7 +738,7 @@ describe('CsoInfo', () => {
);

const expectedNormalizedMaxGain = Value.fromSats(
roundUpToNearestMultiplier(
roundDownToNearestMultiplier(
(expectedMaxGainForContractSize.sats * BigInt(1e8)) /
contractSize.sats,
BigInt(100),
Expand All @@ -736,7 +755,7 @@ describe('CsoInfo', () => {
expect(actualMaxLossForContractSize.sats).to.equal(
expectedMaxLossForContractSize.sats,
);
expect(minPayout).to.equal(BigInt(119900));
expect(minPayout).to.equal(BigInt(121200));
expect(maxPayout).to.equal(collateral.sats);
expect(actualContractSize.sats).to.equal(contractSize.sats);
expect(actualOfferCollateral.sats).to.equal(
Expand Down Expand Up @@ -781,10 +800,19 @@ describe('CsoInfo', () => {
} = getCsoInfoFromOffer(csoOrderOffer, 'v1');

// Fees are very high, so use dust threshold for max gain
const expectedMaxGainForContractSize = Value.fromSats(
const offerFees = Value.fromSats(
getFinalizerByCount(feePerByte, numOfferInputs, 3, numContracts)
.offerFees,
).addn(Value.fromSats(dustThreshold(feePerByte)));
);
const expectedMaxGainForContractSize_ = offerFees.addn(
Value.fromSats(dustThreshold(BigInt(feePerByte))),
);
const expectedMaxGainForContractSize = Value.fromSats(
roundUpToNearestMultiplier(
expectedMaxGainForContractSize_.sats,
BigInt(100),
),
);

const expectedMaxLossForContractSize = Value.fromSats(
roundUpToNearestMultiplier(
Expand All @@ -794,7 +822,7 @@ describe('CsoInfo', () => {
);

const expectedNormalizedMaxGain = Value.fromSats(
roundUpToNearestMultiplier(
roundDownToNearestMultiplier(
(expectedMaxGainForContractSize.sats * BigInt(1e8)) /
contractSize.sats,
BigInt(100),
Expand All @@ -811,7 +839,7 @@ describe('CsoInfo', () => {
expect(actualMaxLossForContractSize.sats).to.equal(
expectedMaxLossForContractSize.sats,
);
expect(minPayout).to.equal(BigInt(119900));
expect(minPayout).to.equal(BigInt(121200));
expect(maxPayout).to.equal(collateral.sats);
expect(actualContractSize.sats).to.equal(contractSize.sats);
expect(actualOfferCollateral.sats).to.equal(
Expand Down
5 changes: 5 additions & 0 deletions packages/core/lib/dlc/finance/Builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ export const roundUpToNearestMultiplier = (
multiplier: bigint,
): bigint => ((num + multiplier - BigInt(1)) / multiplier) * multiplier;

export const roundDownToNearestMultiplier = (
num: bigint,
multiplier: bigint,
): bigint => num - (num % multiplier);

export type DlcParty = 'offeror' | 'acceptor' | 'neither';

/**
Expand Down
12 changes: 5 additions & 7 deletions packages/core/lib/dlc/finance/CsoInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,11 @@ export const getCsoInfoParamsFromContractInfoV1 = (
const normalizedMaxLoss = ONE_BTC_CONTRACT.clone();
normalizedMaxLoss.sub(startOutcomeValue);

const maxGainForContractSize = Value.fromBitcoin(
new Decimal(normalizedMaxGain.bitcoin)
.times(contractSize.bitcoin)
.toDecimalPlaces(
8 - Math.log10(Number(UNIT_MULTIPLIER[unit.toLowerCase()])),
)
.toNumber(),
const maxGainForContractSize = Value.fromSats(
roundUpToNearestMultiplier(
(normalizedMaxGain.sats * contractSize.sats) / BigInt(1e8),
BigInt(UNIT_MULTIPLIER[unit.toLowerCase()]),
),
);

const maxLossForContractSize = Value.fromSats(
Expand Down

0 comments on commit f2628df

Please sign in to comment.