Skip to content

Commit

Permalink
Add mempool events
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksej-paschenko committed Nov 12, 2024
1 parent 14c9696 commit 4ae5d38
Showing 1 changed file with 40 additions and 4 deletions.
44 changes: 40 additions & 4 deletions examples/webhooks/main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package main

import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"net/http"
"sync"
)

type AccountTxPayload struct {
Expand All @@ -12,20 +15,53 @@ type AccountTxPayload struct {
TxHash string `json:"tx_hash"`
}

type MempoolEvent struct {
EventType string `json:"event_type"`
Boc string `json:"boc"`
}

var (
mu sync.Mutex
hashes = make(map[string]int)
)

func webhook(w http.ResponseWriter, req *http.Request) {
defer req.Body.Close()

var payload AccountTxPayload
fmt.Printf("req: %v\n", req.Method)
for name, header := range req.Header {
fmt.Printf("header: %v -> %#v\n", name, header)

}
var payload MempoolEvent
if err := json.NewDecoder(req.Body).Decode(&payload); err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}

fmt.Printf("%v\n", payload)
hash := sha256.New()
hash.Write([]byte(payload.Boc))
payloadHash := hash.Sum(nil)
payloadHex := hex.EncodeToString(payloadHash)

w.WriteHeader(http.StatusOK)

mu.Lock()
defer mu.Unlock()

hashes[payloadHex] += 1

value := hashes[payloadHex]
if value > 2 {
fmt.Printf("payload hex %v -> %v\n", payloadHex, value)
}

if len(hashes) > 100_000 {
hashes = make(map[string]int)
}
}

func main() {
http.HandleFunc("/", webhook)
http.ListenAndServe(":8092", nil)
http.HandleFunc("/webhook", webhook)
http.ListenAndServe(":8030", nil)
}

0 comments on commit 4ae5d38

Please sign in to comment.