@covenance/dlc
Version:
Crypto and Bitcoin functions for Covenance DLC implementation
101 lines (88 loc) • 3.33 kB
text/typescript
import { expect } from 'chai';
import { Address, Networks, PublicKey, PrivateKey, Script } from '../../src/btc';
import { Point, utils } from '../../src/crypto/secp256k1';
import { createMaturityCets, createDlcInitTx } from '../../src/cet/transactions';
import { OracleEvent, LoanConfig, UTXO } from '../../src/cet/types';
import { PubKey, PrivKey, EventOutcomeHash } from '../../src/crypto/types';
import { sha256 } from '../../src';
describe('createMaturityCet', () => {
let borrowerKey: PrivateKey;
let lenderKey: PrivateKey;
let borrowerPubKey: PublicKey;
let lenderPubKey: PublicKey;
let borrowerAddress: Address;
let lenderAddress: Address;
let borrowerDlcPrivateKey: PrivKey;
let borrowerDlcPubKey: PubKey;
let lenderDlcPrivateKey: PrivKey;
let lenderDlcPubKey: PubKey;
let dlcUtxo: UTXO;
let config: LoanConfig;
let mockEvent: OracleEvent;
before(async () => {
// Create test keys and addresses
borrowerKey = new PrivateKey();
lenderKey = new PrivateKey();
borrowerPubKey = borrowerKey.toPublicKey();
lenderPubKey = lenderKey.toPublicKey();
borrowerAddress = new Address(borrowerPubKey, Networks.testnet, 'witnesspubkeyhash');
lenderAddress = new Address(lenderPubKey, Networks.testnet, 'witnesspubkeyhash');
borrowerDlcPrivateKey = utils.randomPrivateKey();
borrowerDlcPubKey = Point.fromPrivateKey(borrowerDlcPrivateKey);
lenderDlcPrivateKey = utils.randomPrivateKey();
lenderDlcPubKey = Point.fromPrivateKey(lenderDlcPrivateKey);
// Create DLC init transaction
const borrowedAmountUsd = 15000; // $15,000 USD
const liquidationThreshold = 0.8;
const annualInterestRate = 0.1;
const initalBtcPrice = 100000; // 100,000 USD/BTC
const collateralAmount = (borrowedAmountUsd / initalBtcPrice) * 2;
const inputAmount = collateralAmount * 2;
const collateralUtxos = [{
txId: 'a'.repeat(64),
outputIndex: 0,
satoshis: inputAmount * 100000000,
script: (Script as any).buildWitnessV0Out(borrowerAddress)
}];
const dlcInitTx = createDlcInitTx(
collateralUtxos,
collateralAmount * 100000000,
borrowerDlcPubKey,
lenderDlcPubKey,
borrowerAddress
);
dlcUtxo = dlcInitTx.dlcUtxo;
config = {
collateralAmount,
annualInterestRate,
liquidationThreshold,
borrowedAmount: borrowedAmountUsd,
penaltyPercentage: 0.1
};
// Create mock event with valid secp256k1 points
const outcomeHashes = new Array<EventOutcomeHash>(100 * 10);
for (let i = 0; i < 100 * 10; i++) {
outcomeHashes[i] = await sha256(new Uint8Array([i]));
}
mockEvent = {
id: 'maturity-event',
timestamp: Date.now(),
outcomeSignaturePoints: Array.from({ length: 100 }, () =>
Point.fromPrivateKey(utils.randomPrivateKey())
),
outcomePrices: Array.from({ length: 100 * 10 }, (_, i) => 100000 + i * 100),
outcomeHashes: outcomeHashes
};
});
it('should create correct number of CETs based on price grid', () => {
const cets = createMaturityCets(
mockEvent,
mockEvent.timestamp - 60 * 60 * 24 * 90,
config,
dlcUtxo,
borrowerAddress,
lenderAddress
);
expect(cets.length).to.equal(mockEvent.outcomePrices.length);
});
});