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 3 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
121 changes: 111 additions & 10 deletions api/ordinals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,24 @@
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,
RoadArmorRareSats,
RoadArmorRareSatsType,
victorkirov marked this conversation as resolved.
Show resolved Hide resolved
SatRangeInscription,
Sattributes,
SattributesType,
UTXO,
UtxoBundleResponse,
UtxoOrdinalBundle,
} from '../types';
import { parseBrc20TransactionData } from './helper';
Expand Down Expand Up @@ -101,8 +111,8 @@
timeout: 30000,
transformResponse: [(data) => parseOrdinalTextContentData(data)],
})
.then((response) => response!.data)

Check warning on line 114 in api/ordinals.ts

View workflow job for this annotation

GitHub Actions / test

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

Check warning on line 115 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 +142,9 @@
})
.then((response) => {
if (response.data) {
const responseTokensList = response!.data;

Check warning on line 145 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 147 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 +167,7 @@
return [];
}
})
.catch((error) => {

Check warning on line 170 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 +226,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 +251,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 +264,106 @@
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,
};
}

let totalExoticSats = 0;
let totalCommonUnknownInscribedSats = 0;
const satRanges: BundleSatRange[] = [];

apiBundle.sat_ranges.forEach((satRange) => {
const { year_mined: yearMined, ...satRangeProps } = satRange;

// filter out unsupported satributes
const supportedSatributes = satRange.satributes.filter(
(satribute) =>
RoadArmorRareSats.includes(satribute as RoadArmorRareSatsType) ||
Sattributes.includes(satribute as SattributesType),
);

const rangeWithUnsupportedSatsAndWithoutInscriptions =
satRange.satributes.length === 1 && !satRange.inscriptions.length && !supportedSatributes.length;
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this be

Suggested change
satRange.satributes.length === 1 && !satRange.inscriptions.length && !supportedSatributes.length;
!satRange.inscriptions.length && !supportedSatributes.length;

// if range has no inscriptions and only unsupported satributes, we skip it
if (rangeWithUnsupportedSatsAndWithoutInscriptions) {
return;
}
victorkirov marked this conversation as resolved.
Show resolved Hide resolved

// if range has inscribed sats of unsupported type or unknown, we map it to a common/unknown sat range
const satributes = !supportedSatributes.length ? (['COMMON'] as RareSatsType[]) : supportedSatributes;

const totalSats = Number(BigInt(satRange.range.end) - BigInt(satRange.range.start));

if (satributes.includes('COMMON')) {
totalCommonUnknownInscribedSats += totalSats;
} else {
totalExoticSats += totalSats;
}

const range = {
...satRangeProps,
totalSats,
yearMined,
satributes,
// only one inscription per range is supported
inscriptions: satRange.inscriptions.length > 1 ? [satRange.inscriptions[0]] : satRange.inscriptions,
Copy link
Member

Choose a reason for hiding this comment

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

Why do we only support one? πŸ‘€

};

satRanges.push(range);
});

// if totalExoticSats 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
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