Skip to content

Commit

Permalink
Initialize inscriber and indexing for lock request
Browse files Browse the repository at this point in the history
  • Loading branch information
b-j-roberts committed Jan 13, 2025
1 parent 085ba3b commit 5f71429
Show file tree
Hide file tree
Showing 29 changed files with 493 additions and 78 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ dist-ssr
server/dist
public/dist
TODO
inscriptions/
*.key
account/
30 changes: 30 additions & 0 deletions apps/backend/Dockerfile.inscriber
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
FROM golang:1.23.1-alpine

RUN apk add --no-cache bash curl git jq yq ncurses

SHELL ["/bin/bash", "-c"]
# RUN curl --proto '=https' --tlsv1.2 -sSf https://docs.swmansion.com/scarb/install.sh | bash -s -- -v 2.9.1
RUN curl https://get.starkli.sh | sh && \
source /root/.starkli/env && \
starkliup
ENV PATH="$PATH:/root/.local/bin:/root/.starkli/bin"

# Copy over the configs
WORKDIR /configs
COPY ./apps/backend/configs/docker.config.yaml /configs/config.yaml
COPY ./apps/backend/configs/docker.script-config.yaml /configs/script-config.yaml

# Copy over the scripts
WORKDIR /scripts
COPY ./packages/scripts /scripts

# Copy over the app
WORKDIR /app
COPY ./apps/backend/go.mod ./apps/backend/go.sum ./
RUN go mod download
COPY ./apps/backend .

# Build the app & run it
RUN go build -o inscriber ./cmd/inscriber/main.go

CMD ["./inscriber"]
58 changes: 58 additions & 0 deletions apps/backend/cmd/inscriber/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package main

import (
"fmt"
"net/http"
"strconv"
"time"

"github.com/keep-starknet-strange/broly/backend/internal/config"
"github.com/keep-starknet-strange/broly/backend/internal/scripts"
"github.com/keep-starknet-strange/broly/backend/routes"
routeutils "github.com/keep-starknet-strange/broly/backend/routes/utils"
)

func InscriberLockingService() {
for {
sleepTime := 10
time.Sleep(time.Duration(sleepTime) * time.Second)

// Query the backend for open inscription requests
backendUrl := "http://" + config.Conf.Api.Host + ":" + strconv.Itoa(config.Conf.Api.Port) + "/inscriptions/get-open-requests"
response, err := http.Get(backendUrl) // TODO: Use pagination
if err != nil {
fmt.Println("Error while querying the backend for open inscription requests")
continue
}

// Parse the response as Json
responseJson, err := routeutils.ReadJsonDataResponse[[]routes.InscriptionRequest](response)
if err != nil {
fmt.Println("Error while parsing the response as Json")
continue
}

if len(responseJson.Data) == 0 {
fmt.Println("No open inscription requests")
continue
}

// TODO: Determine which requests to use
// Lock the inscription request
fmt.Println("Locking inscription request: ", responseJson.Data[0])
txHash := "0x1234567890" // TODO
err = scripts.LockInscriptionInvokeScript(responseJson.Data[0].InscriptionId, txHash)
if err != nil {
fmt.Println("Error while invoking the lockInscription script")
continue
}
}
}

func main() {
config.InitConfig()
scripts.InitScriptConfig()

// TODO: To go routine and run in parallel with InscriberSubmitService
InscriberLockingService()
}
2 changes: 2 additions & 0 deletions apps/backend/configs/config.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Api:
Host: localhost
Port: 8080
AllowOrigins:
- '*'
Expand All @@ -13,6 +14,7 @@ Api:
Production: false
Admin: true
Consumer:
Host: localhost
Port: 8081
Postgres:
Host: localhost
Expand Down
2 changes: 2 additions & 0 deletions apps/backend/configs/docker.config.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Api:
Host: broly-backend-1
Port: 8080
AllowOrigins:
- '*'
Expand All @@ -13,6 +14,7 @@ Api:
Production: false
Admin: true
Consumer:
Host: broly-consumer-1
Port: 8081
Postgres:
Host: broly-postgres-1
Expand Down
1 change: 1 addition & 0 deletions apps/backend/configs/docker.script-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
LockInscriptionScript: /scripts/lock_request.sh
2 changes: 2 additions & 0 deletions apps/backend/configs/local.config.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Api:
Host: localhost
Port: 8080
AllowOrigins:
- '*'
Expand All @@ -13,6 +14,7 @@ Api:
Production: false
Admin: true
Consumer:
Host: localhost
Port: 8081
Postgres:
Host: localhost
Expand Down
1 change: 1 addition & 0 deletions apps/backend/configs/local.script-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
LockInscriptionScript: ../../packages/scripts/lock_request.sh
1 change: 1 addition & 0 deletions apps/backend/configs/script-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
LockInscriptionScript: ../../packages/scripts/lock_request.sh
4 changes: 4 additions & 0 deletions apps/backend/indexer/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,22 @@ var FinalizedMessageLock = &sync.Mutex{}

const (
requestCreatedEvent = "0x02206f1373fa5f0c53a9546d291d8e7389cdbee50a22dca64f02545611a91cc2"
requestLockedEvent = "0x00cb8cf3a8b98da361712b27e7be452a22ec254dfa7c0b59a74dd7d111bcbe9d"
)

var eventProcessors = map[string](func(IndexerEvent)){
requestCreatedEvent: processRequestCreatedEvent,
requestLockedEvent: processRequestLockedEvent,
}

var eventReverters = map[string](func(IndexerEvent)){
requestCreatedEvent: revertRequestCreatedEvent,
requestLockedEvent: revertRequestLockedEvent,
}

var eventRequiresOrdering = map[string]bool{
requestCreatedEvent: true,
requestLockedEvent: true,
}

const (
Expand Down
48 changes: 47 additions & 1 deletion apps/backend/indexer/orderbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,13 @@ func processRequestCreatedEvent(event IndexerEvent) {
}

// Insert into Postgres
_, err = db.Db.Postgres.Exec(context.Background(), "INSERT INTO InscriptionRequests (inscription_id, requester, type, inscription_data, bitcoin_address, fee_token, fee_amount) VALUES ($1, $2, $3, $4, $5, $6, $7)", inscriptionId, caller, inscriptionType, inscriptionData, bitcoinAddress, feeToken, feeAmount)
_, err = db.Db.Postgres.Exec(context.Background(), "INSERT INTO InscriptionRequests (inscription_id, requester, bitcoin_address, fee_token, fee_amount) VALUES ($1, $2, $3, $4, $5)", inscriptionId, caller, bitcoinAddress, feeToken, feeAmount)
if err != nil {
PrintIndexerEventError("processRequestCreatedEvent", event, err)
return
}

_, err = db.Db.Postgres.Exec(context.Background(), "INSERT INTO InscriptionRequestsData (inscription_id, type, inscription_data) VALUES ($1, $2, $3)", inscriptionId, inscriptionType, inscriptionData)
if err != nil {
PrintIndexerEventError("processRequestCreatedEvent", event, err)
return
Expand Down Expand Up @@ -133,9 +139,49 @@ func revertRequestCreatedEvent(event IndexerEvent) {
return
}

_, err = db.Db.Postgres.Exec(context.Background(), "DELETE FROM InscriptionRequestsData WHERE inscription_id = $1", inscriptionId)
if err != nil {
PrintIndexerEventError("revertRequestCreatedEvent", event, err)
return
}

_, err = db.Db.Postgres.Exec(context.Background(), "DELETE FROM InscriptionRequestsStatus WHERE inscription_id = $1", inscriptionId)
if err != nil {
PrintIndexerEventError("revertRequestCreatedEvent", event, err)
return
}
}

func processRequestLockedEvent(event IndexerEvent) {
inscriptionIdHex := event.Event.Keys[1]
inscriptionId, err := strconv.ParseInt(inscriptionIdHex, 0, 64)
if err != nil {
PrintIndexerEventError("processRequestLockedEvent", event, err)
return
}

// TODO: Interpret tx_hash data

// Insert into Postgres
_, err = db.Db.Postgres.Exec(context.Background(), "UPDATE InscriptionRequestsStatus SET status = 1 WHERE inscription_id = $1", inscriptionId)
if err != nil {
PrintIndexerEventError("processRequestLockedEvent", event, err)
return
}
}

func revertRequestLockedEvent(event IndexerEvent) {
inscriptionIdHex := event.Event.Keys[1]
inscriptionId, err := strconv.ParseInt(inscriptionIdHex, 0, 64)
if err != nil {
PrintIndexerEventError("revertRequestLockedEvent", event, err)
return
}

// Insert into Postgres
_, err = db.Db.Postgres.Exec(context.Background(), "UPDATE InscriptionRequestsStatus SET status = 0 WHERE inscription_id = $1", inscriptionId)
if err != nil {
PrintIndexerEventError("revertRequestLockedEvent", event, err)
return
}
}
2 changes: 2 additions & 0 deletions apps/backend/internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
)

type ApiConfig struct {
Host string `yaml:"Host"`
Port int `yaml:"Port"`
AllowOrigins []string `yaml:"AllowOrigins"`
AllowMethods []string `yaml:"AllowMethods"`
Expand All @@ -17,6 +18,7 @@ type ApiConfig struct {
}

type ConsumerConfig struct {
Host string `yaml:"Host"`
Port int `yaml:"Port"`
}

Expand Down
48 changes: 48 additions & 0 deletions apps/backend/internal/scripts/scripts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package scripts

import (
"fmt"
"os"
"os/exec"
"strconv"

"gopkg.in/yaml.v3"
)

type ScriptConfig struct {
LockInscriptionScript string `yaml:"LockInscriptionScript"`
}

var Conf *ScriptConfig

func InitScriptConfig() {
configPath, ok := os.LookupEnv("SCRIPT_CONFIG_PATH")
if !ok {
configPath = "configs/script-config.yaml"
fmt.Println("SCRIPT_CONFIG_PATH not set, using default script-config.yaml")
}

yamlFile, err := os.ReadFile(configPath)
if err != nil {
fmt.Println("Error reading config file: ", err)
os.Exit(1)
}

err = yaml.Unmarshal(yamlFile, &Conf)
if err != nil {
fmt.Println("Error parsing config file: ", err)
os.Exit(1)
}
}

func LockInscriptionInvokeScript(inscriptionId int, txHash string) error {
shellCmd := Conf.LockInscriptionScript

cmd := exec.Command(shellCmd, strconv.Itoa(inscriptionId), txHash)
_, err := cmd.Output()
if err != nil {
return err
}

return nil
}
10 changes: 7 additions & 3 deletions apps/backend/postgres/init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,22 @@ CREATE INDEX IF NOT EXISTS Inscriptions_minted_block ON Inscriptions(minted_bloc
CREATE TABLE IF NOT EXISTS InscriptionRequests (
inscription_id integer NOT NULL PRIMARY KEY,
requester char(64) NOT NULL,
type string NOT NULL,
inscription_data text NOT NULL,
bitcoin_address text NOT NULL,
fee_token text NOT NULL,
fee_amount integer NOT NULL
);
CREATE INDEX IF NOT EXISTS InscriptionRequests_requester ON InscriptionRequests(requester);
CREATE INDEX IF NOT EXISTS InscriptionRequests_inscription_id ON InscriptionRequests(inscription_id);
CREATE INDEX IF NOT EXISTS InscriptionRequests_type ON InscriptionRequests(type);
CREATE INDEX IF NOT EXISTS InscriptionRequests_bitcoin_address ON InscriptionRequests(bitcoin_address);
CREATE INDEX IF NOT EXISTS InscriptionRequests_fee_token ON InscriptionRequests(fee_token);

CREATE TABLE IF NOT EXISTS InscriptionRequestsData (
inscription_id integer NOT NULL PRIMARY KEY,
type text NOT NULL,
inscription_data text NOT NULL
);
CREATE INDEX IF NOT EXISTS InscriptionRequestsData_type ON InscriptionRequestsData(type);

CREATE TABLE IF NOT EXISTS InscriptionRequestsStatus (
inscription_id integer NOT NULL PRIMARY KEY,
status integer NOT NULL
Expand Down
Loading

0 comments on commit 5f71429

Please sign in to comment.