diff --git a/package-lock.json b/package-lock.json index 3c7fbd1f..91ea5048 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "simple-staking", - "version": "0.2.24", + "version": "0.2.25", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "simple-staking", - "version": "0.2.24", + "version": "0.2.25", "dependencies": { "@bitcoinerlab/secp256k1": "^1.1.1", "@keystonehq/animated-qr": "^0.8.6", diff --git a/package.json b/package.json index 1ff79930..7bc52e8f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "simple-staking", - "version": "0.2.24", + "version": "0.2.25", "private": true, "scripts": { "dev": "next dev", diff --git a/src/utils/utxo/index.ts b/src/utils/utxo/index.ts index fb72d5a5..7b6cf84a 100644 --- a/src/utils/utxo/index.ts +++ b/src/utils/utxo/index.ts @@ -2,6 +2,8 @@ import { postVerifyUtxoOrdinals, UtxoInfo } from "@/app/api/postFilterOrdinals"; import { InscriptionIdentifier, UTXO } from "../wallet/wallet_provider"; +const LOW_VALUE_UTXO_THRESHOLD = 10000; + /** * Filters out UTXOs that contain ordinals. * This method first attempts to get inscriptions from the wallet. @@ -22,6 +24,9 @@ export const filterOrdinals = async ( if (!utxos.length) { return []; } + // Filter UTXOs that has value less than 10k sats + utxos = filterLowValueUtxos(utxos); + // fallback to Babylon API if the wallet does not support getting inscriptions if (!getInscriptionsFromWalletCb) { return filterFromApi(utxos, address); @@ -70,3 +75,14 @@ const filterFromApi = async ( // helper function to get the identifier of a UTXO const getUTXOIdentifier = (utxo: { txid: string; vout: number }) => `${utxo.txid}:${utxo.vout}`; + +/* + Filter out UTXOs that have value less than 10k sats + Reasons as below: + 1. Most of the original UTXOs are less than 10k sats + 2. 10k sats or less has less economic value which will add more cost to the + transaction due to fees +*/ +const filterLowValueUtxos = (utxos: UTXO[]): UTXO[] => { + return utxos.filter((utxo) => utxo.value > LOW_VALUE_UTXO_THRESHOLD); +}; diff --git a/tests/utils/utox/utxo.test.ts b/tests/utils/utox/utxo.test.ts index 829c534a..04c0f950 100644 --- a/tests/utils/utox/utxo.test.ts +++ b/tests/utils/utox/utxo.test.ts @@ -7,9 +7,9 @@ jest.mock("@/app/api/postFilterOrdinals"); describe("filterOrdinals", () => { const mockUtxos: UTXO[] = [ - { txid: "txid1", vout: 0, value: 1000, scriptPubKey: "scriptPubKey1" }, - { txid: "txid2", vout: 1, value: 2000, scriptPubKey: "scriptPubKey2" }, - { txid: "txid3", vout: 2, value: 3000, scriptPubKey: "scriptPub" }, + { txid: "txid1", vout: 0, value: 100000, scriptPubKey: "scriptPubKey1" }, + { txid: "txid2", vout: 1, value: 200000, scriptPubKey: "scriptPubKey2" }, + { txid: "txid3", vout: 2, value: 300000, scriptPubKey: "scriptPub" }, ]; const address = "testAddress"; @@ -22,6 +22,24 @@ describe("filterOrdinals", () => { expect(result).toEqual([]); }); + it("should filter out UTXOs have less than 10k sats", async () => { + const getInscriptionsFromWalletCb = jest.fn().mockResolvedValue([]); + + const mockedUTXOsWithLowValue = [ + { txid: "txid1", vout: 0, value: 10000, scriptPubKey: "scriptPubKey1" }, + { txid: "txid2", vout: 1, value: 9999, scriptPubKey: "scriptPubKey2" }, + { txid: "txid3", vout: 2, value: 10001, scriptPubKey: "scriptPub" }, + ]; + + const result = await filterOrdinals( + mockedUTXOsWithLowValue, + address, + getInscriptionsFromWalletCb, + ); + + expect(result).toEqual([mockedUTXOsWithLowValue[2]]); + }); + it("should filter out UTXOs that contain ordinals from the wallet", async () => { const mockInscriptions: InscriptionIdentifier[] = [ {