Skip to content

Commit

Permalink
Refactor and add rune ticker (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
summraznboi authored Apr 15, 2024
1 parent 92f7600 commit 2dfb349
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 53 deletions.
44 changes: 13 additions & 31 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,7 @@ export {
RunestoneStorage,
} from './src/indexer';

export { Edict } from './src/edict';
export { Etching } from './src/etching';
export { Network } from './src/network';
export { Rune } from './src/rune';
export { SpacedRune } from './src/spacedrune';
export { RuneId } from './src/runeid';
export { Runestone } from './src/runestone';
export { Terms } from './src/terms';

export {
BitcoinRpcClient,
Expand Down Expand Up @@ -101,9 +94,9 @@ const SPACERS = ['•', '.'];
* @returns encoded runestone bytes
* @throws Error if encoding is detected to be considered a cenotaph
*/
export function encodeRunestoneUnsafe(runestone: RunestoneSpec): {
encodedRune: Buffer;
etchingCommitment: Buffer | undefined;
export function encodeRunestone(runestone: RunestoneSpec): {
encodedRunestone: Buffer;
etchingCommitment?: Buffer;
} {
const mint = runestone.mint
? Some(new RuneId(u64Strict(runestone.mint.block), u32Strict(runestone.mint.tx)))
Expand All @@ -121,25 +114,11 @@ export function encodeRunestoneUnsafe(runestone: RunestoneSpec): {
let etchingCommitment: Buffer | undefined = undefined;
if (runestone.etching) {
const etchingSpec = runestone.etching;
let hasSpacers = false;
for (const spacer of SPACERS) {
if (runestone.etching?.rune?.includes(spacer)) {
hasSpacers = true;
break;
}
}

let runeSpacers: number | undefined = undefined;
let parsedRawRune: Rune | undefined = undefined;
if (hasSpacers) {
const spacedRune = etchingSpec.rune ? SpacedRune.fromString(etchingSpec.rune) : undefined;
runeSpacers = spacedRune?.spacers;
parsedRawRune = spacedRune?.rune;
} else {
parsedRawRune = etchingSpec.rune ? Rune.fromString(etchingSpec.rune) : undefined;
}
const rune: Option<Rune> =
parsedRawRune !== undefined ? Some(parsedRawRune).map(() => parsedRawRune!) : None;
const spacedRune = etchingSpec.runeName
? SpacedRune.fromString(etchingSpec.runeName)
: undefined;
const rune = spacedRune?.rune !== undefined ? Some(spacedRune.rune) : None;

if (
etchingSpec.symbol &&
Expand All @@ -155,7 +134,10 @@ export function encodeRunestoneUnsafe(runestone: RunestoneSpec): {
etchingSpec.divisibility !== undefined ? Some(etchingSpec.divisibility).map(u8Strict) : None;
const premine =
etchingSpec.premine !== undefined ? Some(etchingSpec.premine).map(u128Strict) : None;
const spacers: Option<u32> = hasSpacers && runeSpacers ? Some(u32Strict(runeSpacers)) : None;
const spacers =
spacedRune?.spacers !== undefined && spacedRune.spacers !== 0
? Some(u32Strict(spacedRune.spacers))
: None;
const symbol = etchingSpec.symbol ? Some(etchingSpec.symbol) : None;

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

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

return {
encodedRune: new Runestone(mint, pointer, edicts, etching).encipher(),
encodedRunestone: new Runestone(mint, pointer, edicts, etching).encipher(),
etchingCommitment,
};
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@magiceden-oss/runestone-lib",
"version": "0.6.2-alpha",
"version": "0.7.0-alpha",
"description": "",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand Down
1 change: 1 addition & 0 deletions src/indexer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export class RunestoneIndexer {
if (this._network === Network.MAINNET) {
this._storage.seedEtchings([
{
runeTicker: 'UNCOMMONGOODS',
runeName: 'UNCOMMON•GOODS',
runeId: { block: 1, tx: 0 },
txid: '0000000000000000000000000000000000000000000000000000000000000000',
Expand Down
6 changes: 4 additions & 2 deletions src/indexer/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export interface RunestoneStorage {
*/
getValidMintCount(runeLocation: string, blockhash: string): Promise<number>;

getRuneLocation(rune: string): Promise<RuneLocation | null>;
getRuneLocation(runeTicker: string): Promise<RuneLocation | null>;

/**
* Get the rune balances for the given UTXO.
Expand Down Expand Up @@ -112,6 +112,7 @@ export type RuneUtxoBalance = {
scriptPubKey: Buffer;
runeId: RuneLocation;
runeName: string;
runeTicker: string;
amount: bigint;
};

Expand All @@ -137,11 +138,12 @@ export type RuneEtchingBase = {
turbo?: boolean;
};

export type RuneEtchingSpec = RuneEtchingBase & { rune?: string };
export type RuneEtchingSpec = RuneEtchingBase & { runeName?: string };

export type RuneEtching = ({ valid: false } | ({ valid: true } & RuneEtchingBase)) & {
runeId: RuneLocation;
runeName: string;
runeTicker: string;
txid: string;
};

Expand Down
16 changes: 9 additions & 7 deletions src/indexer/updater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,21 +273,21 @@ export class RuneUpdater implements RuneBlockIndex {
continue;
}

const runeNameByRuneId = new Map(
this.etchings.map((etching) => [RuneLocation.toString(etching.runeId), etching.runeName])
const etchingByRuneId = new Map(
this.etchings.map((etching) => [RuneLocation.toString(etching.runeId), etching])
);
for (const balance of balances.values()) {
const runeIdString = RuneLocation.toString(balance.runeId);
const runeName =
runeNameByRuneId.get(runeIdString) ??
(await this._storage.getEtching(runeIdString))?.runeName;
if (runeName === undefined) {
const etching =
etchingByRuneId.get(runeIdString) ?? (await this._storage.getEtching(runeIdString));
if (etching === null) {
throw new Error('Rune should exist at this point');
}

this.utxoBalances.push({
runeId: balance.runeId,
runeName,
runeTicker: etching.runeTicker,
runeName: etching.runeName,
amount: balance.amount,
scriptPubKey: Buffer.from(output.scriptPubKey.hex),
txid: tx.txid,
Expand Down Expand Up @@ -512,6 +512,7 @@ export class RuneUpdater implements RuneBlockIndex {
const { divisibility, terms, premine, spacers, symbol } = artifact.etching.unwrap();
this.etchings.push({
valid: true,
runeTicker: rune.toString(),
runeName: new SpacedRune(rune, Number(spacers.map(Number).unwrapOr(0))).toString(),
runeId,
txid,
Expand Down Expand Up @@ -563,6 +564,7 @@ export class RuneUpdater implements RuneBlockIndex {
valid: false,
runeId,
txid,
runeTicker: rune.toString(),
runeName: rune.toString(),
});
}
Expand Down
Loading

0 comments on commit 2dfb349

Please sign in to comment.