Skip to content

Commit

Permalink
added a websocket message for NFT mints on the indexer backend and to…
Browse files Browse the repository at this point in the history
…ken_id request on the frontend
  • Loading branch information
raizo07 committed Apr 30, 2024
1 parent c271dc6 commit ea4e778
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
24 changes: 21 additions & 3 deletions backend/routes/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ func consumeIndexerMsg(w http.ResponseWriter, r *http.Request) {
if eventKey == pixelPlacedEvent {
processPixelPlacedEvent(event, w)
} else if eventKey == nftMintedEvent {
processNFTMintedEvent(event, w)
var tokenID int64
processNFTMintedEvent(event, w, tokenID)
} else if eventKey == templateAddedEvent {
processTemplateAddedEvent(event, w)
} else if eventKey == newDay {
Expand Down Expand Up @@ -160,7 +161,7 @@ func processPixelPlacedEvent(event IndexerEvent, w http.ResponseWriter) {
WriteResultJson(w, "Pixel placement indexed successfully")
}

func processNFTMintedEvent(event IndexerEvent, w http.ResponseWriter) {
func processNFTMintedEvent(event IndexerEvent, w http.ResponseWriter, tokenID int64) {
// TODO: combine high and low token ids
tokenIdLowHex := event.Event.Keys[1]
// TODO: tokenIdHighHex := event.Event.Keys[2]
Expand Down Expand Up @@ -277,7 +278,9 @@ func processNFTMintedEvent(event IndexerEvent, w http.ResponseWriter) {

// TODO: Ws message to all clients

WriteResultJson(w, "NFT mint indexed successfully")
WriteResultJson(w, "NFT mint indexed successfully");

sendNFTWebSocketMessage(tokenID) //**
}

func processTemplateAddedEvent(event IndexerEvent, w http.ResponseWriter) {
Expand Down Expand Up @@ -384,3 +387,18 @@ func processNewDayEvent(event IndexerEvent, w http.ResponseWriter) {
}
}
}

func sendNFTWebSocketMessage(tokenID int64) {
message := map[string]int64{"token_id": tokenID} // Message to be sent
messageBytes, _ := json.Marshal(message) // Convert message to bytes
for idx, conn := range core.ArtPeaceBackend.WSConnections {
if err := conn.WriteMessage(websocket.TextMessage, messageBytes); err != nil {
fmt.Println(err)
conn.Close()
core.ArtPeaceBackend.WSConnections = append(core.ArtPeaceBackend.WSConnections[:idx], core.ArtPeaceBackend.WSConnections[idx+1:]...)
}
}
}



6 changes: 6 additions & 0 deletions frontend/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ function App() {
}
}, [readyState]);

useEffect(() => {
if (lastJsonMessage) {
colorPixel(lastJsonMessage.position, lastJsonMessage.color);
}
}, [lastJsonMessage]);

// Colors
const staticColors = canvasConfig.colors;
const [colors, setColors] = useState([]);
Expand Down

0 comments on commit ea4e778

Please sign in to comment.