commit inicial do projeto
This commit is contained in:
53
internal/api/staking_handlers.go
Normal file
53
internal/api/staking_handlers.go
Normal file
@ -0,0 +1,53 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
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")
|
||||
return
|
||||
}
|
||||
WriteJSON(w, info)
|
||||
}
|
||||
Reference in New Issue
Block a user