Skip to content

Commit

Permalink
fix: flaky fee calculating integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
michael1011 committed Apr 22, 2024
1 parent 0c33029 commit c375740
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 39 deletions.
4 changes: 4 additions & 0 deletions lib/rates/Errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ export default {
message: `could not init currency ${symbol} because of missing config value: ${missingValue}`,
code: concatErrorCode(ErrorCodePrefix.Rates, 1),
}),
SYMBOL_LOCKUPS_NOT_BEING_TRACKED: (symbol: string): Error => ({
message: `${symbol} lockup transactions are not being tracked`,
code: concatErrorCode(ErrorCodePrefix.Rates, 2),
}),
};
12 changes: 8 additions & 4 deletions lib/rates/LockupTransactionTracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Swap from '../db/models/Swap';
import PendingLockupTransactionRepository from '../db/repositories/PendingLockupTransactionRepository';
import SwapRepository from '../db/repositories/SwapRepository';
import { Currency } from '../wallet/WalletManager';
import Errors from './Errors';
import RateProvider from './RateProvider';

class LockupTransactionTracker {
Expand All @@ -33,10 +34,13 @@ class LockupTransactionTracker {

public addPendingTransactionToTrack = async (swap: Swap) => {
const { base, quote } = splitPairId(swap.pair);
await PendingLockupTransactionRepository.create(
swap.id,
getChainCurrency(base, quote, swap.orderSide, false),
);
const chainCurrency = getChainCurrency(base, quote, swap.orderSide, false);

if (!this.zeroConfAcceptedMap.has(chainCurrency)) {
throw Errors.SYMBOL_LOCKUPS_NOT_BEING_TRACKED(chainCurrency);
}

await PendingLockupTransactionRepository.create(swap.id, chainCurrency);
};

private listenToBlocks = (chainClient: IChainClient) => {
Expand Down
34 changes: 23 additions & 11 deletions test/integration/Utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ describe('Utils', () => {
await Promise.all([bitcoinClient.generate(1), elementsClient.generate(1)]);
});

afterAll(() => {
bitcoinClient.disconnect();
elementsClient.disconnect();
bitcoinLndClient.disconnect();
});

test('should calculate UTXO transaction fee', async () => {
const satPerVbyte = 2;
const txId = await bitcoinClient.sendToAddress(
Expand All @@ -31,9 +37,15 @@ describe('Utils', () => {
);

const tx = Transaction.fromHex(await bitcoinClient.getRawTransaction(txId));
expect(await calculateUtxoTransactionFee(bitcoinClient, tx)).toEqual(
tx.virtualSize() * satPerVbyte,
);

// Leave some buffer for core not doing *exactly* the sat/vbyte we told it to
const expectedFee = tx.virtualSize() * satPerVbyte;
await expect(
calculateUtxoTransactionFee(bitcoinClient, tx),
).resolves.toBeGreaterThanOrEqual(expectedFee - 5);
await expect(
calculateUtxoTransactionFee(bitcoinClient, tx),
).resolves.toBeLessThanOrEqual(expectedFee + 5);
});

test('should calculate Liquid transaction fee', async () => {
Expand All @@ -47,8 +59,14 @@ describe('Utils', () => {
const tx = TransactionLiquid.fromHex(
await elementsClient.getRawTransaction(txId),
);
expect(calculateLiquidTransactionFee(tx)).toEqual(
tx.virtualSize() * satPerVbyte,

// Leave some buffer for Elements not doing *exactly* the sat/vbyte we told it to
const expectedFee = tx.virtualSize() * satPerVbyte;
expect(calculateLiquidTransactionFee(tx)).toBeGreaterThanOrEqual(
expectedFee - 5,
);
expect(calculateLiquidTransactionFee(tx)).toBeLessThanOrEqual(
expectedFee + 5,
);
});

Expand All @@ -68,10 +86,4 @@ describe('Utils', () => {
expect(decoded.minFinalCltvExpiry).toEqual(cltvExpiry);
expect(getHexBuffer(decoded.paymentHash!)).toEqual(preimageHash);
});

afterAll(() => {
bitcoinClient.disconnect();
elementsClient.disconnect();
bitcoinLndClient.disconnect();
});
});
65 changes: 41 additions & 24 deletions test/integration/rates/LockupTransactionTracker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import { generateId, getHexString } from '../../../lib/Utils';
import ChainClient from '../../../lib/chain/ChainClient';
import { OrderSide } from '../../../lib/consts/Enums';
import { liquidSymbol } from '../../../lib/consts/LiquidTypes';
import Swap from '../../../lib/db/models/Swap';
import PendingLockupTransactionRepository from '../../../lib/db/repositories/PendingLockupTransactionRepository';
import SwapRepository from '../../../lib/db/repositories/SwapRepository';
import Errors from '../../../lib/rates/Errors';
import LockupTransactionTracker from '../../../lib/rates/LockupTransactionTracker';
import RateProvider from '../../../lib/rates/RateProvider';
import { wait } from '../../Utils';
Expand Down Expand Up @@ -72,32 +74,47 @@ describe('LockupTransactionTracker', () => {
expect(tracker.zeroConfAccepted(symbol)).toEqual(result);
});

test.each`
pair | orderSide | chainCurrency
${'BTC/BTC'} | ${OrderSide.BUY} | ${'BTC'}
${'BTC/BTC'} | ${OrderSide.SELL} | ${'BTC'}
${'L-BTC/BTC'} | ${OrderSide.BUY} | ${'BTC'}
${'L-BTC/BTC'} | ${OrderSide.SELL} | ${'L-BTC'}
`(
'should add pending transaction to track',
async ({ pair, orderSide, chainCurrency }) => {
PendingLockupTransactionRepository.create = jest.fn();

const id = generateId();
await tracker.addPendingTransactionToTrack({
id,
pair,
orderSide,
} as any);
describe('addPendingTransactionToTrack', () => {
test.each`
pair | orderSide | chainCurrency
${'BTC/BTC'} | ${OrderSide.BUY} | ${'BTC'}
${'BTC/BTC'} | ${OrderSide.SELL} | ${'BTC'}
${'L-BTC/BTC'} | ${OrderSide.BUY} | ${'BTC'}
${'L-BTC/BTC'} | ${OrderSide.SELL} | ${'L-BTC'}
`(
'should add pending $chainCurrency transaction to track',
async ({ pair, orderSide, chainCurrency }) => {
PendingLockupTransactionRepository.create = jest.fn();

const id = generateId();
await tracker.addPendingTransactionToTrack({
id,
pair,
orderSide,
} as any);
expect(PendingLockupTransactionRepository.create).toHaveBeenCalledTimes(
1,
);
expect(PendingLockupTransactionRepository.create).toHaveBeenCalledWith(
id,
chainCurrency,
);
},
);

test('should throw when transaction of chain currency that is not being tracked is added', async () => {
await expect(
tracker.addPendingTransactionToTrack({
pair: 'NOT/TRACKED',
orderSide: OrderSide.BUY,
} as unknown as Swap),
).rejects.toEqual(Errors.SYMBOL_LOCKUPS_NOT_BEING_TRACKED('TRACKED'));

expect(PendingLockupTransactionRepository.create).toHaveBeenCalledTimes(
1,
);
expect(PendingLockupTransactionRepository.create).toHaveBeenCalledWith(
id,
chainCurrency,
0,
);
},
);
});
});

describe('checkPendingLockupsForChain', () => {
test.each`
Expand Down

0 comments on commit c375740

Please sign in to comment.