This repository has been archived by the owner on Aug 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0826ad4
commit 7e3350a
Showing
11 changed files
with
182 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import Logger from '../Logger'; | ||
import { PairConfig } from '../consts/Types'; | ||
import BoltzClient from '../boltz/BoltzClient'; | ||
import { mapToObject, getPairId, feeMapToObject, stringify } from '../Utils'; | ||
|
||
class FeeProvider { | ||
// A map between the symbols of the pairs and their percentage fees | ||
private percentageFees = new Map<string, number>(); | ||
|
||
constructor(private logger: Logger, private boltz: BoltzClient) {} | ||
|
||
public init = (pairs: PairConfig[]) => { | ||
pairs.forEach((pair) => { | ||
// Set the configured fee or fallback to 1% if it is not defined | ||
const percentage = pair.fee !== undefined ? pair.fee : 1; | ||
this.percentageFees.set(getPairId(pair), percentage / 100); | ||
}); | ||
|
||
this.logger.debug(`Prepared data for fee estimations: ${stringify(mapToObject(this.percentageFees))}`); | ||
} | ||
|
||
public getFee = async (pair: string, chainCurrency: string, amount: number, isReverse: boolean) => { | ||
const feeReponse = await this.boltz.getFeeEstimation(chainCurrency); | ||
const feeMap = feeMapToObject(feeReponse.feesMap); | ||
|
||
const baseFee = this.getBaseFee(feeMap[chainCurrency], isReverse); | ||
const percentageFee = Math.ceil(this.percentageFees.get(pair)! * amount); | ||
|
||
return baseFee + percentageFee; | ||
} | ||
|
||
private getBaseFee = (satPerVbyte: number, isReverse: boolean) => { | ||
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; | ||
} 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; | ||
} | ||
} | ||
} | ||
|
||
export default FeeProvider; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { expect } from 'chai'; | ||
import { mock, when, instance, anyString } from 'ts-mockito'; | ||
import Logger from '../../../lib/Logger'; | ||
import FeeProvider from '../../../lib/rates/FeeProvider'; | ||
import BoltzClient from '../../../lib/boltz/BoltzClient'; | ||
|
||
describe('FeeProvider', () => { | ||
const btcFee = 36; | ||
const ltcFee = 3; | ||
|
||
const boltzMock = mock(BoltzClient); | ||
when(boltzMock.getFeeEstimation(anyString())).thenResolve({ | ||
feesMap: [ | ||
['BTC', btcFee], | ||
['LTC', ltcFee], | ||
], | ||
}); | ||
|
||
const feeProvider = new FeeProvider(Logger.disabledLogger, instance(boltzMock)); | ||
|
||
it('should init', () => { | ||
feeProvider.init([ | ||
{ | ||
base: 'LTC', | ||
quote: 'BTC', | ||
fee: 0.5, | ||
}, | ||
{ | ||
base: 'BTC', | ||
quote: 'BTC', | ||
fee: 0, | ||
}, | ||
{ | ||
base: 'LTC', | ||
quote: 'LTC', | ||
|
||
// The FeeProvider should set this to 1 | ||
fee: undefined, | ||
}, | ||
]); | ||
|
||
const feeMap = feeProvider['percentageFees']; | ||
expect(feeMap.size).to.be.equal(3); | ||
|
||
expect(feeMap.get('LTC/BTC')).to.be.equal(0.005); | ||
expect(feeMap.get('BTC/BTC')).to.be.equal(0); | ||
expect(feeMap.get('LTC/LTC')).to.be.equal(0.01); | ||
}); | ||
|
||
it('should estimate fees', async () => { | ||
const results = await Promise.all([ | ||
feeProvider.getFee('LTC/BTC', 'BTC', 100000, false), | ||
feeProvider.getFee('LTC/BTC', 'LTC', 532100, false), | ||
|
||
feeProvider.getFee('BTC/BTC', 'BTC', 100000, false), | ||
feeProvider.getFee('BTC/BTC', 'BTC', 100000, true), | ||
|
||
feeProvider.getFee('LTC/LTC', 'LTC', 987654321, false), | ||
feeProvider.getFee('LTC/LTC', 'LTC', 987654321, true), | ||
]); | ||
|
||
const expected = [ | ||
5540, | ||
3081, | ||
|
||
5040, | ||
5508, | ||
|
||
9876964, | ||
9877003, | ||
]; | ||
|
||
results.forEach((result, index) => { | ||
expect(result).to.be.equal(expected[index]); | ||
}); | ||
}); | ||
}); |
8 changes: 4 additions & 4 deletions
8
test/unit/RateProvider.spec.ts → test/unit/rates/RateProvider.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters