Skip to content

Commit

Permalink
fix: provide a getOrdinalsInUtxos if found function (#317)
Browse files Browse the repository at this point in the history
* fix: provide a getOrdinalsInUtxos if found function

* test: add tests for getUtxoOrdinalBundleIfFound
  • Loading branch information
teebszet committed Dec 12, 2023
1 parent 8d13683 commit 86c7bc9
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 11 deletions.
22 changes: 21 additions & 1 deletion api/ordinals.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import axios from 'axios';
import axios, { isAxiosError } from 'axios';
import EsploraApiProvider from '../api/esplora/esploraAPiProvider';
import XordApiProvider from '../api/ordinals/provider';
import { INSCRIPTION_REQUESTS_SERVICE_URL, ORDINALS_URL, XVERSE_API_BASE_URL, XVERSE_INSCRIBE_URL } from '../constant';
Expand Down Expand Up @@ -273,6 +273,26 @@ export const getUtxoOrdinalBundle = async (
return response.data;
};

export const getUtxoOrdinalBundleIfFound = async (
network: NetworkType,
txid: string,
vout: number,
): Promise<UtxoBundleResponse | undefined> => {
try {
const data = await getUtxoOrdinalBundle(network, txid, vout);
return data;
} catch (e) {
// we don't reject on 404s because if the UTXO is not found,
// it is likely this is a UTXO from an unpublished txn.
// this is required for gamma.io purchase flow
if (!isAxiosError(e) || e.response?.status !== 404) {
// rethrow error if response was not 404
throw e;
}
return undefined;
}
};

export const mapRareSatsAPIResponseToBundle = (apiBundle: UtxoOrdinalBundle): Bundle => {
const generalBundleInfo = {
txid: apiBundle.txid,
Expand Down
18 changes: 12 additions & 6 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
Expand Up @@ -18,7 +18,7 @@
"@stacks/transactions": "4.3.5",
"@stacks/wallet-sdk": "^5.0.2",
"@zondax/ledger-stacks": "^1.0.4",
"axios": "0.27.2",
"axios": "1.4.0",
"base64url": "^3.0.1",
"bip32": "^4.0.0",
"bip39": "3.0.3",
Expand Down
41 changes: 39 additions & 2 deletions tests/api/ordinals.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,43 @@
import { mapRareSatsAPIResponseToBundle } from '../../api/ordinals';
import { AxiosError, AxiosResponse } from 'axios';
import { getUtxoOrdinalBundleIfFound, mapRareSatsAPIResponseToBundle } from '../../api/ordinals';
import { Bundle, UtxoOrdinalBundle } from '../../types/api/xverse/ordinals';
import { describe, expect, it } from 'vitest';
import { describe, expect, it, vi, afterEach } from 'vitest';

const mocked = vi.hoisted(() => ({
get: vi.fn(),
}));
vi.mock('axios', async () => ({
...(await vi.importActual<any>('axios')),
default: {
get: mocked.get,
},
}));

describe('getUtxoOrdinalBundleIfFound', () => {
afterEach(() => {
vi.resetAllMocks();
});

it('rejects if API returns 500', () => {
mocked.get.mockRejectedValueOnce(
new AxiosError('server error', undefined, undefined, undefined, { status: 500 } as AxiosResponse),
);

expect(getUtxoOrdinalBundleIfFound('Testnet', '', 0)).rejects.toEqual(
expect.objectContaining({ message: 'server error' }),
);
expect(mocked.get).toHaveBeenCalledOnce();
});

it('resolves undefined if API returns 404', () => {
mocked.get.mockRejectedValueOnce(
new AxiosError('not found', undefined, undefined, undefined, { status: 404 } as AxiosResponse),
);

expect(getUtxoOrdinalBundleIfFound('Testnet', '', 0)).resolves.toEqual(undefined);
expect(mocked.get).toHaveBeenCalledOnce();
});
});

describe('rareSats', () => {
describe('mapRareSatsAPIResponseToRareSats', () => {
Expand Down
2 changes: 1 addition & 1 deletion types/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class ResponseError extends Error {
export type ApiResponseErrorParams = {
status: number;
data: unknown;
headers: Record<string, string>;
headers: unknown;
};
export class ApiResponseError extends Error implements ApiResponseErrorParams {
public status;
Expand Down

0 comments on commit 86c7bc9

Please sign in to comment.