Skip to content
This repository has been archived by the owner on Aug 28, 2019. It is now read-only.

Commit

Permalink
feat: include reverse claim fee in miner fees
Browse files Browse the repository at this point in the history
  • Loading branch information
michael1011 committed Mar 30, 2019
1 parent 38646c3 commit f7d76bd
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 13 deletions.
13 changes: 10 additions & 3 deletions lib/rates/FeeProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ class FeeProvider {
// A map between the symbols of the pairs and their percentage fees
public percentageFees = new Map<string, number>();

public static transactionSizes = {
normalClaim: 140,

reverseLockup: 153,
reverseClaim: 138,
};

constructor(private logger: Logger, private boltz: BoltzClient) {}

public init = (pairs: PairConfig[]) => {
Expand Down Expand Up @@ -35,11 +42,11 @@ class FeeProvider {
if (isReverse) {
// The lockup transaction which spends a P2WPKH output (possibly more but we assume a best case scenario here),
// locks up funds in a P2WSH swap and sends the change back to a P2WKH output has about 153 vbytes
return satPerVbyte * 153;
return satPerVbyte * FeeProvider.transactionSizes.reverseLockup;
} else {
// The claim transaction which spends a nested SegWit swap output and
// sends it to a bech32 P2WPKH address has about 140 vbytes
return satPerVbyte * 140;
// sends it to a P2WPKH address has about 140 vbytes
return satPerVbyte * FeeProvider.transactionSizes.normalClaim;
}
}
}
Expand Down
19 changes: 16 additions & 3 deletions lib/rates/RateProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@ type Limits = {
maximal: number;
};

type ReverseMinerFees = {
lockup: number;
claim: number;
};

type MinerFees = {
normal: number;
reverse: number;
reverse: ReverseMinerFees;
};

type Pair = {
Expand Down Expand Up @@ -210,14 +215,22 @@ class RateProvider {

for (const [symbol] of this.limits) {
// The pair and amount can be emtpy because we just want the miner fee
const [normal, reverse] = await Promise.all([
const [normal, reverseLockup] = await Promise.all([
this.feeProvider.getFee('', symbol, 0, false),
this.feeProvider.getFee('', symbol, 0, true),
]);

minerFees.set(symbol, {
normal,
reverse,
reverse: {
lockup: reverseLockup,

// We cannot know what kind of address the user will claim to so we just assume the worst case: P2PKH
//
// Claiming a P2WSH to a P2PKH address is about 138 bytes and to get the sats per vbyte we divide the
// reverse fee by the size of the reverse lockup transaction (153 vbyte)
claim: FeeProvider.transactionSizes.reverseClaim * (reverseLockup / FeeProvider.transactionSizes.reverseLockup),
},
});
}

Expand Down
1 change: 0 additions & 1 deletion lib/service/Service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ interface Service {
emit(event: 'swap.successful', swap: SwapInstance | ReverseSwapInstance): boolean;
}

// TODO: do not override invoice settled status with transaction confirmed and invoice paid with with transaction confirmed
class Service extends EventEmitter {
public swapRepository: SwapRepository;
public reverseSwapRepository: ReverseSwapRepository;
Expand Down
18 changes: 12 additions & 6 deletions test/unit/rates/RateProvider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,29 @@ describe('RateProvider', () => {

const minerFees = {
BTC: {
normal: 280,
reverse: 306,
normal: FeeProvider.transactionSizes.normalClaim * 2,
reverse: {
lockup: FeeProvider.transactionSizes.reverseLockup * 2,
claim: FeeProvider.transactionSizes.reverseClaim * 2,
},
},
LTC: {
normal: 140,
reverse: 153,
normal: FeeProvider.transactionSizes.normalClaim ,
reverse: {
lockup: FeeProvider.transactionSizes.reverseLockup,
claim: FeeProvider.transactionSizes.reverseClaim,
},
},
};

const feeProviderMock = mock(FeeProvider);
when(feeProviderMock.percentageFees).thenReturn(percentageFees);

when(feeProviderMock.getFee('', 'BTC', 0, false)).thenResolve(minerFees.BTC.normal);
when(feeProviderMock.getFee('', 'BTC', 0, true)).thenResolve(minerFees.BTC.reverse);
when(feeProviderMock.getFee('', 'BTC', 0, true)).thenResolve(minerFees.BTC.reverse.lockup);

when(feeProviderMock.getFee('', 'LTC', 0, false)).thenResolve(minerFees.LTC.normal);
when(feeProviderMock.getFee('', 'LTC', 0, true)).thenResolve(minerFees.LTC.reverse);
when(feeProviderMock.getFee('', 'LTC', 0, true)).thenResolve(minerFees.LTC.reverse.lockup);

const rateProvider = new RateProvider(Logger.disabledLogger, instance(feeProviderMock), 0.1, [
{
Expand Down

0 comments on commit f7d76bd

Please sign in to comment.