Files
dejo-node/internal/api/staking_handlers.go
2025-06-17 18:26:14 -03:00

53 lines
1.3 KiB
Go

package api
import (
"net/http"
"strconv"
)
func (h *Handler) GetStakeInfo(w http.ResponseWriter, r *http.Request) {
address := r.URL.Query().Get("address")
info, exists := h.StakingStore.GetStakeInfo(address)
if !exists {
WriteError(w, http.StatusNotFound, "Endereço não encontrado")
return
}
WriteJSON(w, http.StatusOK, info)
}
func (h *Handler) Stake(w http.ResponseWriter, r *http.Request) {
address := r.URL.Query().Get("address")
amountStr := r.URL.Query().Get("amount")
durationStr := r.URL.Query().Get("duration")
amount, err := strconv.ParseUint(amountStr, 10, 64)
if err != nil {
WriteError(w, http.StatusBadRequest, "Valor inválido")
return
}
duration, err := strconv.ParseUint(durationStr, 10, 64)
if err != nil {
WriteError(w, http.StatusBadRequest, "Duração inválida")
return
}
err = h.StakingStore.Stake(address, amount, duration)
if err != nil {
WriteError(w, http.StatusInternalServerError, "Erro ao realizar stake")
return
}
w.WriteHeader(http.StatusOK)
}
func (h *Handler) Unstake(w http.ResponseWriter, r *http.Request) {
address := r.URL.Query().Get("address")
err := h.StakingStore.Unstake(address)
if err != nil {
WriteError(w, http.StatusInternalServerError, "Erro ao remover stake")
return
}
w.WriteHeader(http.StatusOK)
}