fiz: correções da pool
This commit is contained in:
@ -1,53 +1,52 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"github.com/gorilla/mux"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type StakeRequest struct {
|
||||
Address string `json:"address"`
|
||||
Amount uint64 `json:"amount"`
|
||||
Duration int64 `json:"duration"`
|
||||
}
|
||||
|
||||
type UnstakeRequest struct {
|
||||
Address string `json:"address"`
|
||||
}
|
||||
|
||||
func (h *Handler) StakeHandler(w http.ResponseWriter, r *http.Request) {
|
||||
var req StakeRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
WriteError(w, http.StatusBadRequest, "Invalid request")
|
||||
return
|
||||
}
|
||||
if err := h.StakingStore.Stake(req.Address, req.Amount, uint64(req.Duration)); err != nil {
|
||||
WriteError(w, http.StatusInternalServerError, "Failed to stake")
|
||||
return
|
||||
}
|
||||
WriteJSON(w, map[string]string{"message": "Stake registered successfully"})
|
||||
}
|
||||
|
||||
func (h *Handler) UnstakeHandler(w http.ResponseWriter, r *http.Request) {
|
||||
var req UnstakeRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
WriteError(w, http.StatusBadRequest, "Invalid request")
|
||||
return
|
||||
}
|
||||
if err := h.StakingStore.Unstake(req.Address); err != nil {
|
||||
WriteError(w, http.StatusInternalServerError, "Failed to unstake")
|
||||
return
|
||||
}
|
||||
WriteJSON(w, map[string]string{"message": "Unstake successful"})
|
||||
}
|
||||
|
||||
func (h *Handler) GetStakeInfo(w http.ResponseWriter, r *http.Request) {
|
||||
address := mux.Vars(r)["address"]
|
||||
info, ok := h.StakingStore.GetStakeInfo(address)
|
||||
if !ok {
|
||||
WriteError(w, http.StatusNotFound, "Stake info not found")
|
||||
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, info)
|
||||
}
|
||||
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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user