Skip to content

Commit a7073da

Browse files
authored
feat(api): return inscription charms in responses (#435)
* add to schema * test: values
1 parent 4291eab commit a7073da

File tree

9 files changed

+125
-0
lines changed

9 files changed

+125
-0
lines changed

api/ordinals/src/api/schemas.ts

+1
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ export const InscriptionResponse = Type.Object(
342342
examples: ['brc20'],
343343
})
344344
),
345+
charms: Type.Array(Type.String({ examples: ['coin', 'palindrome', 'vindicated'] })),
345346
},
346347
{ title: 'Inscription Response' }
347348
);

api/ordinals/src/api/util/helpers.ts

+32
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,37 @@ function parseTimestamp(timestamp: number): number {
3131
return timestamp * 1000;
3232
}
3333

34+
enum Charm {
35+
coin = 0,
36+
cursed = 1,
37+
epic = 2,
38+
legendary = 3,
39+
lost = 4,
40+
nineball = 5,
41+
rare = 6,
42+
reinscription = 7,
43+
unbound = 8,
44+
uncommon = 9,
45+
vindicated = 10,
46+
mythic = 11,
47+
burned = 12,
48+
palindrome = 13,
49+
}
50+
51+
function parseCharms(charms: string): string[] {
52+
const charmsVal = parseInt(charms);
53+
const result: Charm[] = [];
54+
for (const charm in Charm) {
55+
if (!isNaN(Number(charm))) {
56+
const charmValue = Number(charm);
57+
if (charmsVal & (1 << charmValue)) {
58+
result.push(charmValue as Charm);
59+
}
60+
}
61+
}
62+
return result.map(charm => Charm[charm]);
63+
}
64+
3465
export function parseDbInscriptions(
3566
items: DbFullyLocatedInscriptionResult[]
3667
): InscriptionResponseType[] {
@@ -63,6 +94,7 @@ export function parseDbInscriptions(
6394
metadata: i.metadata ? JSON.parse(i.metadata) : null,
6495
delegate: i.delegate ?? null,
6596
meta_protocol: i.metaprotocol ?? null,
97+
charms: parseCharms(i.charms),
6698
}));
6799
}
68100
export function parseDbInscription(item: DbFullyLocatedInscriptionResult): InscriptionResponseType {

api/ordinals/src/pg/pg-store.ts

+1
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ export class PgStore extends BasePgStore {
190190
i.tx_index AS genesis_tx_index,
191191
i.timestamp AS genesis_timestamp,
192192
i.address AS genesis_address,
193+
i.charms,
193194
cur.address,
194195
cur.tx_index,
195196
cur.block_height,

api/ordinals/src/pg/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export type DbFullyLocatedInscriptionResult = {
3737
pointer: number | null;
3838
metaprotocol: string | null;
3939
delegate: string | null;
40+
charms: string;
4041
};
4142

4243
export type DbLocation = {

api/ordinals/tests/api/cache.test.ts

+7
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ describe('ETag cache', () => {
6565
transfer_type: 'transferred',
6666
rarity: 'common',
6767
coinbase_height: '9000',
68+
charms: 0,
6869
});
6970
const response = await fastify.inject({
7071
method: 'GET',
@@ -165,6 +166,7 @@ describe('ETag cache', () => {
165166
prev_offset: null,
166167
transfer_type: 'transferred',
167168
rarity: 'common',
169+
charms: 0,
168170
});
169171
await inscriptionReveal(db.sql, {
170172
content: '0x48656C6C6F',
@@ -197,6 +199,7 @@ describe('ETag cache', () => {
197199
prev_offset: null,
198200
transfer_type: 'transferred',
199201
rarity: 'common',
202+
charms: 0,
200203
});
201204

202205
// ETag response
@@ -277,6 +280,7 @@ describe('ETag cache', () => {
277280
prev_offset: null,
278281
transfer_type: 'transferred',
279282
rarity: 'common',
283+
charms: 0,
280284
});
281285

282286
// ETag response
@@ -328,6 +332,7 @@ describe('ETag cache', () => {
328332
prev_offset: null,
329333
transfer_type: 'transferred',
330334
rarity: 'common',
335+
charms: 0,
331336
});
332337

333338
// Cache busted
@@ -371,6 +376,7 @@ describe('ETag cache', () => {
371376
prev_offset: null,
372377
transfer_type: 'transferred',
373378
rarity: 'common',
379+
charms: 0,
374380
});
375381

376382
// ETag response
@@ -422,6 +428,7 @@ describe('ETag cache', () => {
422428
prev_offset: null,
423429
transfer_type: 'transferred',
424430
rarity: 'common',
431+
charms: 0,
425432
});
426433

427434
// Cache busted

0 commit comments

Comments
 (0)