feat: add rune number to rune entry

This commit is contained in:
Gaze
2024-04-15 21:57:05 +07:00
parent 184c3d8ab5
commit 72a5578c29
10 changed files with 46 additions and 5 deletions

View File

@@ -29,6 +29,7 @@ CREATE TABLE IF NOT EXISTS "runes_indexed_blocks" (
CREATE TABLE IF NOT EXISTS "runes_entries" (
"rune_id" TEXT NOT NULL PRIMARY KEY,
"number" BIGINT NOT NULL, -- sequential number of the rune starting from 0
"rune" TEXT NOT NULL,
"spacers" INT NOT NULL,
"premine" DECIMAL NOT NULL,

View File

@@ -27,9 +27,12 @@ SELECT * FROM runes_transactions
LEFT JOIN runes_runestones ON runes_transactions.hash = runes_runestones.tx_hash
WHERE runes_transactions.block_height = $1;
-- name: CountRuneEntries :one
SELECT COUNT(*) FROM runes_entries;
-- name: CreateRuneEntry :exec
INSERT INTO runes_entries (rune_id, rune, spacers, premine, symbol, divisibility, terms, terms_amount, terms_cap, terms_height_start, terms_height_end, terms_offset_start, terms_offset_end, turbo, etching_block, etching_tx_hash)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16);
INSERT INTO runes_entries (rune_id, rune, number, spacers, premine, symbol, divisibility, terms, terms_amount, terms_cap, terms_height_start, terms_height_end, terms_offset_start, terms_offset_end, turbo, etching_block, etching_tx_hash)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17);
-- name: CreateRuneEntryState :exec
INSERT INTO runes_entry_states (rune_id, block_height, mints, burned_amount, completed_at, completed_at_height) VALUES ($1, $2, $3, $4, $5, $6);

View File

@@ -28,6 +28,8 @@ type RunesReaderDataGateway interface {
GetRuneEntryByRuneId(ctx context.Context, runeId runes.RuneId) (*runes.RuneEntry, error)
// GetRuneEntryByRuneId returns the RuneEntry for the given runeId. Returns errs.NotFound if the rune entry is not found.
GetRuneEntryByRuneIdBatch(ctx context.Context, runeIds []runes.RuneId) (map[runes.RuneId]*runes.RuneEntry, error)
// CountRuneEntries returns the number of existing rune entries.
CountRuneEntries(ctx context.Context) (uint64, error)
// GetBalancesByPkScript returns the balances for the given pkScript at the given blockHeight.
GetBalancesByPkScript(ctx context.Context, pkScript []byte, blockHeight uint64) (map[runes.RuneId]*entity.Balance, error)

View File

@@ -11,6 +11,7 @@ import (
type RuneEntry struct {
RuneId RuneId
Number uint64
Divisibility uint8
// Premine is the amount of the rune that was premined.
Premine uint128.Uint128

View File

@@ -108,6 +108,7 @@ func (p *Processor) ensureGenesisRune(ctx context.Context) error {
if errors.Is(err, errs.NotFound) {
runeEntry := &runes.RuneEntry{
RuneId: genesisRuneId,
Number: 0,
Divisibility: 0,
Premine: uint128.Zero,
SpacedRune: runes.NewSpacedRune(runes.NewRune(2055900680524219742), 0b10000000),

View File

@@ -550,10 +550,16 @@ func removeAnnexFromWitness(witness [][]byte) [][]byte {
}
func (p *Processor) createRuneEntry(ctx context.Context, runestone *runes.Runestone, runeId runes.RuneId, rune runes.Rune, tx *types.Transaction) error {
count, err := p.runesDg.CountRuneEntries(ctx)
if err != nil {
return errors.Wrap(err, "failed to count rune entries")
}
var runeEntry *runes.RuneEntry
if runestone.Cenotaph {
runeEntry = &runes.RuneEntry{
RuneId: runeId,
Number: count,
SpacedRune: runes.NewSpacedRune(rune, 0),
Mints: uint128.Zero,
BurnedAmount: uint128.Zero,
@@ -571,6 +577,7 @@ func (p *Processor) createRuneEntry(ctx context.Context, runestone *runes.Runest
etching := runestone.Etching
runeEntry = &runes.RuneEntry{
RuneId: runeId,
Number: count,
SpacedRune: runes.NewSpacedRune(rune, lo.FromPtr(etching.Spacers)),
Mints: uint128.Zero,
BurnedAmount: uint128.Zero,

View File

@@ -11,6 +11,17 @@ import (
"github.com/jackc/pgx/v5/pgtype"
)
const countRuneEntries = `-- name: CountRuneEntries :one
SELECT COUNT(*) FROM runes_entries
`
func (q *Queries) CountRuneEntries(ctx context.Context) (int64, error) {
row := q.db.QueryRow(ctx, countRuneEntries)
var count int64
err := row.Scan(&count)
return count, err
}
const createIndexedBlock = `-- name: CreateIndexedBlock :exec
INSERT INTO runes_indexed_blocks (hash, height, prev_hash, event_hash, cumulative_event_hash) VALUES ($1, $2, $3, $4, $5)
`
@@ -35,13 +46,14 @@ func (q *Queries) CreateIndexedBlock(ctx context.Context, arg CreateIndexedBlock
}
const createRuneEntry = `-- name: CreateRuneEntry :exec
INSERT INTO runes_entries (rune_id, rune, spacers, premine, symbol, divisibility, terms, terms_amount, terms_cap, terms_height_start, terms_height_end, terms_offset_start, terms_offset_end, turbo, etching_block, etching_tx_hash)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16)
INSERT INTO runes_entries (rune_id, rune, number, spacers, premine, symbol, divisibility, terms, terms_amount, terms_cap, terms_height_start, terms_height_end, terms_offset_start, terms_offset_end, turbo, etching_block, etching_tx_hash)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17)
`
type CreateRuneEntryParams struct {
RuneID string
Rune string
Number int64
Spacers int32
Premine pgtype.Numeric
Symbol int32
@@ -62,6 +74,7 @@ func (q *Queries) CreateRuneEntry(ctx context.Context, arg CreateRuneEntryParams
_, err := q.db.Exec(ctx, createRuneEntry,
arg.RuneID,
arg.Rune,
arg.Number,
arg.Spacers,
arg.Premine,
arg.Symbol,
@@ -418,13 +431,14 @@ WITH states AS (
-- select latest state
SELECT DISTINCT ON (rune_id) rune_id, block_height, mints, burned_amount, completed_at, completed_at_height FROM runes_entry_states WHERE rune_id = ANY($1::text[]) ORDER BY rune_id, block_height DESC
)
SELECT runes_entries.rune_id, rune, spacers, premine, symbol, divisibility, terms, terms_amount, terms_cap, terms_height_start, terms_height_end, terms_offset_start, terms_offset_end, turbo, etching_block, etching_tx_hash, states.rune_id, block_height, mints, burned_amount, completed_at, completed_at_height FROM runes_entries
SELECT runes_entries.rune_id, number, rune, spacers, premine, symbol, divisibility, terms, terms_amount, terms_cap, terms_height_start, terms_height_end, terms_offset_start, terms_offset_end, turbo, etching_block, etching_tx_hash, states.rune_id, block_height, mints, burned_amount, completed_at, completed_at_height FROM runes_entries
LEFT JOIN states ON runes_entries.rune_id = states.rune_id
WHERE runes_entries.rune_id = ANY($1::text[])
`
type GetRuneEntriesByRuneIdsRow struct {
RuneID string
Number int64
Rune string
Spacers int32
Premine pgtype.Numeric
@@ -459,6 +473,7 @@ func (q *Queries) GetRuneEntriesByRuneIds(ctx context.Context, runeIds []string)
var i GetRuneEntriesByRuneIdsRow
if err := rows.Scan(
&i.RuneID,
&i.Number,
&i.Rune,
&i.Spacers,
&i.Premine,

View File

@@ -17,6 +17,7 @@ type RunesBalance struct {
type RunesEntry struct {
RuneID string
Number int64
Rune string
Spacers int32
Premine pgtype.Numeric

View File

@@ -131,6 +131,7 @@ func mapRuneEntryModelToType(src gen.GetRuneEntriesByRuneIdsRow) (runes.RuneEntr
}
return runes.RuneEntry{
RuneId: runeId,
Number: uint64(src.Number),
Divisibility: uint8(src.Divisibility),
Premine: lo.FromPtr(premine),
SpacedRune: runes.NewSpacedRune(rune, uint32(src.Spacers)),
@@ -218,6 +219,7 @@ func mapRuneEntryTypeToParams(src runes.RuneEntry, blockHeight uint64) (gen.Crea
return gen.CreateRuneEntryParams{
RuneID: runeId,
Rune: rune,
Number: int64(src.Number),
Spacers: spacers,
Premine: premine,
Symbol: src.Symbol,

View File

@@ -179,6 +179,14 @@ func (r *Repository) GetRuneEntryByRuneIdBatch(ctx context.Context, runeIds []ru
return runeEntries, nil
}
func (r *Repository) CountRuneEntries(ctx context.Context) (uint64, error) {
count, err := r.getQueries().CountRuneEntries(ctx)
if err != nil {
return 0, errors.Wrap(err, "error during query")
}
return uint64(count), nil
}
func (r *Repository) GetBalancesByPkScript(ctx context.Context, pkScript []byte, blockHeight uint64) (map[runes.RuneId]*entity.Balance, error) {
balances, err := r.getQueries().GetBalancesByPkScript(ctx, gen.GetBalancesByPkScriptParams{
Pkscript: hex.EncodeToString(pkScript),