Skip to content

Commit

Permalink
feat: filter out utxos less or equal to 10k sats (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
jrwbabylonlab authored Aug 4, 2024
2 parents c974e8a + 17ff815 commit d66e05a
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 6 deletions.
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": "simple-staking",
"version": "0.2.24",
"version": "0.2.25",
"private": true,
"scripts": {
"dev": "next dev",
Expand Down
16 changes: 16 additions & 0 deletions src/utils/utxo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
Expand Down Expand Up @@ -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);
};
24 changes: 21 additions & 3 deletions tests/utils/utox/utxo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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[] = [
{
Expand Down

0 comments on commit d66e05a

Please sign in to comment.