Skip to content

Commit

Permalink
Hotfix release/v1.5.1 (#205)
Browse files Browse the repository at this point in the history
* 1.5.0

* fix: fetch pending stx transactions and unit test

chore: bump minor version

chore: fix error in package.json
  • Loading branch information
teebszet authored Aug 9, 2023
1 parent d456ea3 commit b76adc3
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 14 deletions.
18 changes: 11 additions & 7 deletions api/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,20 +151,24 @@ export function parseBtcTransactionData(
return parsedTx;
}

export function deDuplicatePendingTx({
export function getUniquePendingTx({
confirmedTransactions,
pendingTransactions,
}: {
confirmedTransactions: StxTransactionData[];
pendingTransactions: StxMempoolTransactionData[];
}): StxMempoolTransactionData[] {
const txArray: StxMempoolTransactionData[] = [];
for (const tx of [...confirmedTransactions, ...pendingTransactions]) {
if (!txArray.find((t) => t.txid === tx.txid)) {
txArray.push(tx as StxMempoolTransactionData);
}
if (!pendingTransactions.length) {
return pendingTransactions;
}
return txArray;
return [
...new Map(
pendingTransactions
.filter((pendingTx) => pendingTx.incoming !== true)
.filter((pendingTx) => !confirmedTransactions.find((confirmedTx) => confirmedTx.txid === pendingTx.txid))
.map((m) => [m.txid, m]),
).values(),
];
}

export function mapTransferTransactionData({
Expand Down
9 changes: 5 additions & 4 deletions api/stacks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ import {
import { API_TIMEOUT_MILLI } from '../constant';
import { AddressToBnsResponse, CoinMetaData, CoreInfo, DelegationInfo } from '../types/api/stacks/assets';
import {
deDuplicatePendingTx,
getNetworkURL,
getUniquePendingTx,
mapTransferTransactionData,
parseMempoolStxTransactionsData,
parseStxTransactionData,
Expand Down Expand Up @@ -151,7 +151,7 @@ export async function fetchStxAddressData(
]);

const confirmedCount = confirmedTransactions.totalCount;
const mempoolCount = deDuplicatePendingTx({
const mempoolCount = getUniquePendingTx({
confirmedTransactions: confirmedTransactions.transactionsList,
pendingTransactions: mempoolTransactions.transactionsList,
}).length;
Expand Down Expand Up @@ -357,6 +357,7 @@ export async function fetchAddressOfBnsName(
}

export async function fetchStxPendingTxData(stxAddress: string, network: StacksNetwork): Promise<StxPendingTxData> {

const [confirmedTransactions, mempoolTransactions] = await Promise.all([
getConfirmedTransactions({
stxAddress,
Expand All @@ -370,10 +371,10 @@ export async function fetchStxPendingTxData(stxAddress: string, network: StacksN
}),
]);

const pendingTransactions = deDuplicatePendingTx({
const pendingTransactions = getUniquePendingTx({
confirmedTransactions: confirmedTransactions.transactionsList,
pendingTransactions: mempoolTransactions.transactionsList,
}).filter((tx) => tx.incoming === false);
});

return {
pendingTransactions,
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@secretkeylabs/xverse-core",
"version": "1.5.0",
"version": "1.5.1",
"description": "",
"main": "dist/index.js",
"dependencies": {
Expand Down
68 changes: 68 additions & 0 deletions tests/api/helper.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { describe, expect, it } from 'vitest';
import { getUniquePendingTx } from 'api/helper';
import { StxMempoolTransactionData, StxTransactionData } from 'types/*';

describe('getUniquePendingTx', () => {
[
{
name: 'returns empty for no pending transactions',
inputs: {
confirmedTransactions: [
{ senderAddress: 'address1', txid: 'tx1' } as StxTransactionData,
{ senderAddress: 'address1', txid: 'tx2' } as StxTransactionData,
],
pendingTransactions: [],
},
expected: [],
},
{
name: 'returns pending transactions not also seen in confirmed transactions',
inputs: {
confirmedTransactions: [
{ senderAddress: 'address1', txid: 'tx1' } as StxTransactionData,
{ senderAddress: 'address1', txid: 'tx2' } as StxTransactionData,
],
pendingTransactions: [
{ senderAddress: 'address1', txid: 'tx2' } as StxMempoolTransactionData,
{ senderAddress: 'address1', txid: 'tx3' } as StxMempoolTransactionData,
],
},
expected: [{ senderAddress: 'address1', txid: 'tx3' } as StxMempoolTransactionData],
},
{
name: 'returns unique pending transactions not also seen in confirmed transactions',
inputs: {
confirmedTransactions: [
{ senderAddress: 'address1', txid: 'tx1' } as StxTransactionData,
{ senderAddress: 'address1', txid: 'tx2' } as StxTransactionData,
],
pendingTransactions: [
{ senderAddress: 'address1', txid: 'tx2' } as StxMempoolTransactionData,
{ senderAddress: 'address1', txid: 'tx3' } as StxMempoolTransactionData,
{ senderAddress: 'address1', txid: 'tx3' } as StxMempoolTransactionData,
],
},
expected: [{ senderAddress: 'address1', txid: 'tx3' } as StxMempoolTransactionData],
},
{
name: 'returns only pending transactions which are not incoming',
inputs: {
confirmedTransactions: [
{ senderAddress: 'address1', txid: 'tx1' } as StxTransactionData,
{ senderAddress: 'address1', txid: 'tx2' } as StxTransactionData,
],
pendingTransactions: [
{ senderAddress: 'address1', txid: 'tx2' } as StxMempoolTransactionData,
{ senderAddress: 'address1', txid: 'tx3' } as StxMempoolTransactionData,
{ senderAddress: 'address1', txid: 'tx3' } as StxMempoolTransactionData,
{ senderAddress: 'address1', txid: 'tx4', incoming: true } as StxMempoolTransactionData,
],
},
expected: [{ senderAddress: 'address1', txid: 'tx3' } as StxMempoolTransactionData],
},
].forEach(({ name, inputs, expected }) => {
it(name, () => {
expect(getUniquePendingTx(inputs)).toEqual(expected);
});
});
});

0 comments on commit b76adc3

Please sign in to comment.