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

chore: use exotics api v2 #298

Merged
merged 5 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
96 changes: 86 additions & 10 deletions api/ordinals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,20 @@
import { INSCRIPTION_REQUESTS_SERVICE_URL, ORDINALS_URL, XVERSE_API_BASE_URL, XVERSE_INSCRIBE_URL } from '../constant';
import {
Account,
AddressBundleResponse,
Brc20HistoryTransactionData,
BtcOrdinal,
Bundle,
BundleSatRange,
FungibleToken,
HiroApiBrc20TxHistoryResponse,
Inscription,
InscriptionRequestResponse,
NetworkType,
RareSatsType,
SatRangeInscription,
UTXO,
UtxoBundleResponse,
UtxoOrdinalBundle,
} from '../types';
import { parseBrc20TransactionData } from './helper';
Expand Down Expand Up @@ -101,8 +107,8 @@
timeout: 30000,
transformResponse: [(data) => parseOrdinalTextContentData(data)],
})
.then((response) => response!.data)

Check warning on line 110 in api/ordinals.ts

View workflow job for this annotation

GitHub Actions / test

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

Check warning on line 111 in api/ordinals.ts

View workflow job for this annotation

GitHub Actions / test

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

Check warning on line 141 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 143 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 @@ -157,7 +163,7 @@
return [];
}
})
.catch((error) => {

Check warning on line 166 in api/ordinals.ts

View workflow job for this annotation

GitHub Actions / test

'error' is defined but never used
return [];
});
}
Expand Down Expand Up @@ -216,12 +222,6 @@
export const isOrdinalOwnedByAccount = (inscription: Inscription, account: Account) =>
inscription.address === account.ordinalsAddress;

export type AddressBundleResponse = {
total: number;
offset: number;
limit: number;
results: UtxoOrdinalBundle[];
};
export const getAddressUtxoOrdinalBundles = async (
network: NetworkType,
address: string,
Expand All @@ -247,7 +247,7 @@
}

const response = await axios.get<AddressBundleResponse>(
`${XVERSE_API_BASE_URL(network)}/v1/address/${address}/ordinal-utxo`,
`${XVERSE_API_BASE_URL(network)}/v2/address/${address}/ordinal-utxo`,
{
params,
},
Expand All @@ -260,9 +260,85 @@
network: NetworkType,
txid: string,
vout: number,
): Promise<UtxoOrdinalBundle> => {
const response = await axios.get<UtxoOrdinalBundle>(
`${XVERSE_API_BASE_URL(network)}/v1/ordinal-utxo/${txid}:${vout}`,
): Promise<UtxoBundleResponse> => {
const response = await axios.get<UtxoBundleResponse>(
`${XVERSE_API_BASE_URL(network)}/v2/ordinal-utxo/${txid}:${vout}`,
);
return response.data;
};

export const mapRareSatsAPIResponseToBundle = (apiBundle: UtxoOrdinalBundle): Bundle => {
const generalBundleInfo = {
txid: apiBundle.txid,
vout: apiBundle.vout,
block_height: apiBundle.block_height,
value: apiBundle.value,
};

const commonUnknownRange: BundleSatRange = {
range: {
start: '0',
end: '0',
},
yearMined: 0,
block: 0,
offset: 0,
satributes: ['COMMON'],
inscriptions: [],
totalSats: apiBundle.value,
};

// if bundle has and empty sat ranges, it means that it's a common/unknown bundle
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit πŸ‘€

Suggested change
// if bundle has and empty sat ranges, it means that it's a common/unknown bundle
// if bundle has empty sat ranges, it means that it's a common/unknown bundle

if (!apiBundle.sat_ranges.length) {
return {
...generalBundleInfo,
satRanges: [commonUnknownRange],
inscriptions: [],
satributes: [['COMMON']],
totalExoticSats: 0,
};
}

const satRanges = apiBundle.sat_ranges.map((satRange) => {
const { year_mined: yearMined, ...satRangeProps } = satRange;
return {
...satRangeProps,
totalSats: Number(BigInt(satRange.range.end) - BigInt(satRange.range.start)),
yearMined,
// we want to common/unknown inscriptions to be shown as a additional row from common/unknown row
satributes:
!satRange.satributes.length && satRange.inscriptions.length
? (['COMMON'] as RareSatsType[])
: satRange.satributes,
};
});

// if totalExotics doesn't match the value of the bundle,
// it means that the bundle is not fully exotic and we need to add a common unknown sat range more
let totalExoticSats = 0;
let totalCommonUnknownInscribedSats = 0;
satRanges.forEach((satRange) => {
if (satRange.satributes.includes('COMMON')) {
totalCommonUnknownInscribedSats += satRange.totalSats;
} else {
totalExoticSats += satRange.totalSats;
}
});
if (totalExoticSats !== apiBundle.value) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (totalExoticSats !== apiBundle.value) {
if (totalExoticSats + totalCommonUnknownInscribedSats !== apiBundle.value) {

satRanges.push({
...commonUnknownRange,
totalSats: apiBundle.value - (totalExoticSats + totalCommonUnknownInscribedSats),
});
}

const inscriptions = satRanges.reduce((acc, curr) => [...acc, ...curr.inscriptions], [] as SatRangeInscription[]);
const satributes = satRanges.reduce((acc, curr) => [...acc, curr.satributes], [] as RareSatsType[][]);

return {
...generalBundleInfo,
satRanges,
inscriptions,
satributes,
totalExoticSats,
};
};
4 changes: 2 additions & 2 deletions api/utxoCache.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NetworkType, UtxoOrdinalBundle } from '../types';
import { AddressBundleResponse, getAddressUtxoOrdinalBundles, getUtxoOrdinalBundle } from './ordinals';
import { AddressBundleResponse, NetworkType, UtxoOrdinalBundle } from '../types';
import { getAddressUtxoOrdinalBundles, getUtxoOrdinalBundle } from './ordinals';

export type UtxoCacheStruct = {
[utxoId: string]: UtxoOrdinalBundle;
Expand Down
Loading
Loading