Skip to content

Commit 2dfb349

Browse files
authored
Refactor and add rune ticker (#41)
1 parent 92f7600 commit 2dfb349

File tree

6 files changed

+93
-53
lines changed

6 files changed

+93
-53
lines changed

index.ts

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,7 @@ export {
2525
RunestoneStorage,
2626
} from './src/indexer';
2727

28-
export { Edict } from './src/edict';
29-
export { Etching } from './src/etching';
3028
export { Network } from './src/network';
31-
export { Rune } from './src/rune';
32-
export { SpacedRune } from './src/spacedrune';
33-
export { RuneId } from './src/runeid';
34-
export { Runestone } from './src/runestone';
35-
export { Terms } from './src/terms';
3629

3730
export {
3831
BitcoinRpcClient,
@@ -101,9 +94,9 @@ const SPACERS = ['•', '.'];
10194
* @returns encoded runestone bytes
10295
* @throws Error if encoding is detected to be considered a cenotaph
10396
*/
104-
export function encodeRunestoneUnsafe(runestone: RunestoneSpec): {
105-
encodedRune: Buffer;
106-
etchingCommitment: Buffer | undefined;
97+
export function encodeRunestone(runestone: RunestoneSpec): {
98+
encodedRunestone: Buffer;
99+
etchingCommitment?: Buffer;
107100
} {
108101
const mint = runestone.mint
109102
? Some(new RuneId(u64Strict(runestone.mint.block), u32Strict(runestone.mint.tx)))
@@ -121,25 +114,11 @@ export function encodeRunestoneUnsafe(runestone: RunestoneSpec): {
121114
let etchingCommitment: Buffer | undefined = undefined;
122115
if (runestone.etching) {
123116
const etchingSpec = runestone.etching;
124-
let hasSpacers = false;
125-
for (const spacer of SPACERS) {
126-
if (runestone.etching?.rune?.includes(spacer)) {
127-
hasSpacers = true;
128-
break;
129-
}
130-
}
131117

132-
let runeSpacers: number | undefined = undefined;
133-
let parsedRawRune: Rune | undefined = undefined;
134-
if (hasSpacers) {
135-
const spacedRune = etchingSpec.rune ? SpacedRune.fromString(etchingSpec.rune) : undefined;
136-
runeSpacers = spacedRune?.spacers;
137-
parsedRawRune = spacedRune?.rune;
138-
} else {
139-
parsedRawRune = etchingSpec.rune ? Rune.fromString(etchingSpec.rune) : undefined;
140-
}
141-
const rune: Option<Rune> =
142-
parsedRawRune !== undefined ? Some(parsedRawRune).map(() => parsedRawRune!) : None;
118+
const spacedRune = etchingSpec.runeName
119+
? SpacedRune.fromString(etchingSpec.runeName)
120+
: undefined;
121+
const rune = spacedRune?.rune !== undefined ? Some(spacedRune.rune) : None;
143122

144123
if (
145124
etchingSpec.symbol &&
@@ -155,7 +134,10 @@ export function encodeRunestoneUnsafe(runestone: RunestoneSpec): {
155134
etchingSpec.divisibility !== undefined ? Some(etchingSpec.divisibility).map(u8Strict) : None;
156135
const premine =
157136
etchingSpec.premine !== undefined ? Some(etchingSpec.premine).map(u128Strict) : None;
158-
const spacers: Option<u32> = hasSpacers && runeSpacers ? Some(u32Strict(runeSpacers)) : None;
137+
const spacers =
138+
spacedRune?.spacers !== undefined && spacedRune.spacers !== 0
139+
? Some(u32Strict(spacedRune.spacers))
140+
: None;
159141
const symbol = etchingSpec.symbol ? Some(etchingSpec.symbol) : None;
160142

161143
if (divisibility.isSome() && divisibility.unwrap() > MAX_DIVISIBILITY) {
@@ -195,11 +177,11 @@ export function encodeRunestoneUnsafe(runestone: RunestoneSpec): {
195177
const turbo = etchingSpec.turbo ?? false;
196178

197179
etching = Some(new Etching(divisibility, rune, spacers, symbol, terms, premine, turbo));
198-
etchingCommitment = (parsedRawRune as Rune)?.commitment;
180+
etchingCommitment = rune.isSome() ? rune.unwrap().commitment : undefined;
199181
}
200182

201183
return {
202-
encodedRune: new Runestone(mint, pointer, edicts, etching).encipher(),
184+
encodedRunestone: new Runestone(mint, pointer, edicts, etching).encipher(),
203185
etchingCommitment,
204186
};
205187
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@magiceden-oss/runestone-lib",
3-
"version": "0.6.2-alpha",
3+
"version": "0.7.0-alpha",
44
"description": "",
55
"main": "./dist/index.js",
66
"types": "./dist/index.d.ts",

src/indexer/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export class RunestoneIndexer {
4040
if (this._network === Network.MAINNET) {
4141
this._storage.seedEtchings([
4242
{
43+
runeTicker: 'UNCOMMONGOODS',
4344
runeName: 'UNCOMMON•GOODS',
4445
runeId: { block: 1, tx: 0 },
4546
txid: '0000000000000000000000000000000000000000000000000000000000000000',

src/indexer/types.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export interface RunestoneStorage {
5555
*/
5656
getValidMintCount(runeLocation: string, blockhash: string): Promise<number>;
5757

58-
getRuneLocation(rune: string): Promise<RuneLocation | null>;
58+
getRuneLocation(runeTicker: string): Promise<RuneLocation | null>;
5959

6060
/**
6161
* Get the rune balances for the given UTXO.
@@ -112,6 +112,7 @@ export type RuneUtxoBalance = {
112112
scriptPubKey: Buffer;
113113
runeId: RuneLocation;
114114
runeName: string;
115+
runeTicker: string;
115116
amount: bigint;
116117
};
117118

@@ -137,11 +138,12 @@ export type RuneEtchingBase = {
137138
turbo?: boolean;
138139
};
139140

140-
export type RuneEtchingSpec = RuneEtchingBase & { rune?: string };
141+
export type RuneEtchingSpec = RuneEtchingBase & { runeName?: string };
141142

142143
export type RuneEtching = ({ valid: false } | ({ valid: true } & RuneEtchingBase)) & {
143144
runeId: RuneLocation;
144145
runeName: string;
146+
runeTicker: string;
145147
txid: string;
146148
};
147149

src/indexer/updater.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -273,21 +273,21 @@ export class RuneUpdater implements RuneBlockIndex {
273273
continue;
274274
}
275275

276-
const runeNameByRuneId = new Map(
277-
this.etchings.map((etching) => [RuneLocation.toString(etching.runeId), etching.runeName])
276+
const etchingByRuneId = new Map(
277+
this.etchings.map((etching) => [RuneLocation.toString(etching.runeId), etching])
278278
);
279279
for (const balance of balances.values()) {
280280
const runeIdString = RuneLocation.toString(balance.runeId);
281-
const runeName =
282-
runeNameByRuneId.get(runeIdString) ??
283-
(await this._storage.getEtching(runeIdString))?.runeName;
284-
if (runeName === undefined) {
281+
const etching =
282+
etchingByRuneId.get(runeIdString) ?? (await this._storage.getEtching(runeIdString));
283+
if (etching === null) {
285284
throw new Error('Rune should exist at this point');
286285
}
287286

288287
this.utxoBalances.push({
289288
runeId: balance.runeId,
290-
runeName,
289+
runeTicker: etching.runeTicker,
290+
runeName: etching.runeName,
291291
amount: balance.amount,
292292
scriptPubKey: Buffer.from(output.scriptPubKey.hex),
293293
txid: tx.txid,
@@ -512,6 +512,7 @@ export class RuneUpdater implements RuneBlockIndex {
512512
const { divisibility, terms, premine, spacers, symbol } = artifact.etching.unwrap();
513513
this.etchings.push({
514514
valid: true,
515+
runeTicker: rune.toString(),
515516
runeName: new SpacedRune(rune, Number(spacers.map(Number).unwrapOr(0))).toString(),
516517
runeId,
517518
txid,
@@ -563,6 +564,7 @@ export class RuneUpdater implements RuneBlockIndex {
563564
valid: false,
564565
runeId,
565566
txid,
567+
runeTicker: rune.toString(),
566568
runeName: rune.toString(),
567569
});
568570
}

0 commit comments

Comments
 (0)