Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: provide a getOrdinalsInUtxos if found function #317

Merged
merged 2 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -112,8 +112,8 @@
timeout: 30000,
transformResponse: [(data) => parseOrdinalTextContentData(data)],
})
.then((response) => response!.data)

Check warning on line 115 in api/ordinals.ts

View workflow job for this annotation

GitHub Actions / test

Forbidden non-null assertion
.catch((error) => {

Check warning on line 116 in api/ordinals.ts

View workflow job for this annotation

GitHub Actions / test

'error' is defined but never used
return '';
});
}
Expand Down Expand Up @@ -144,9 +144,9 @@
})
.then((response) => {
if (response.data) {
const responseTokensList = response!.data;

Check warning on line 147 in api/ordinals.ts

View workflow job for this annotation

GitHub Actions / test

Forbidden non-null assertion
const tokensList: Array<FungibleToken> = [];
responseTokensList.forEach((responseToken: any) => {

Check warning on line 149 in api/ordinals.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected any. Specify a different type
const token: FungibleToken = {
name: responseToken.ticker,
balance: responseToken.overallBalance,
Expand All @@ -169,7 +169,7 @@
return [];
}
})
.catch((error) => {

Check warning on line 172 in api/ordinals.ts

View workflow job for this annotation

GitHub Actions / test

'error' is defined but never used
return [];
});
}
Expand Down Expand Up @@ -273,6 +273,26 @@
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
Loading