From b76adc3de505cf11862152e2207dd1dffd229857 Mon Sep 17 00:00:00 2001 From: Tim Man Date: Wed, 9 Aug 2023 13:28:33 +0800 Subject: [PATCH] Hotfix release/v1.5.1 (#205) * 1.5.0 * fix: fetch pending stx transactions and unit test chore: bump minor version chore: fix error in package.json --- api/helper.ts | 18 ++++++----- api/stacks.ts | 9 +++--- package-lock.json | 4 +-- package.json | 2 +- tests/api/helper.test.ts | 68 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 87 insertions(+), 14 deletions(-) create mode 100644 tests/api/helper.test.ts diff --git a/api/helper.ts b/api/helper.ts index 3d539066..8c73a96a 100644 --- a/api/helper.ts +++ b/api/helper.ts @@ -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({ diff --git a/api/stacks.ts b/api/stacks.ts index 348deadd..f145a177 100644 --- a/api/stacks.ts +++ b/api/stacks.ts @@ -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, @@ -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; @@ -357,6 +357,7 @@ export async function fetchAddressOfBnsName( } export async function fetchStxPendingTxData(stxAddress: string, network: StacksNetwork): Promise { + const [confirmedTransactions, mempoolTransactions] = await Promise.all([ getConfirmedTransactions({ stxAddress, @@ -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, diff --git a/package-lock.json b/package-lock.json index 8ad3e4af..6afc3cb7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@secretkeylabs/xverse-core", - "version": "1.5.0", + "version": "1.5.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@secretkeylabs/xverse-core", - "version": "1.5.0", + "version": "1.5.1", "license": "ISC", "dependencies": { "@bitcoinerlab/secp256k1": "^1.0.2", diff --git a/package.json b/package.json index 0e70e466..3c690a6a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@secretkeylabs/xverse-core", - "version": "1.5.0", + "version": "1.5.1", "description": "", "main": "dist/index.js", "dependencies": { diff --git a/tests/api/helper.test.ts b/tests/api/helper.test.ts new file mode 100644 index 00000000..b71320c6 --- /dev/null +++ b/tests/api/helper.test.ts @@ -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); + }); + }); +});