-
Notifications
You must be signed in to change notification settings - Fork 339
/
staking.go
92 lines (80 loc) · 2.88 KB
/
staking.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright 2022 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package api
import (
"errors"
"github.com/ethersphere/bee/pkg/bigint"
"math/big"
"net/http"
"github.com/ethersphere/bee/pkg/jsonhttp"
"github.com/ethersphere/bee/pkg/storageincentives/staking"
"github.com/gorilla/mux"
)
func (s *Service) stakingAccessHandler(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if !s.stakingSem.TryAcquire(1) {
s.logger.Debug("staking access: simultaneous on-chain operations not supported")
s.logger.Error(nil, "staking access: simultaneous on-chain operations not supported")
jsonhttp.TooManyRequests(w, "simultaneous on-chain operations not supported")
return
}
defer s.stakingSem.Release(1)
h.ServeHTTP(w, r)
})
}
type getStakeResponse struct {
StakedAmount *bigint.BigInt `json:"stakedAmount"`
}
type stakeDepositResponse struct {
TxHash string `json:"txhash"`
}
func (s *Service) stakingDepositHandler(w http.ResponseWriter, r *http.Request) {
logger := s.logger.WithName("post_stake_deposit").Build()
paths := struct {
Amount *big.Int `map:"amount" validate:"required"`
}{}
if response := s.mapStructure(mux.Vars(r), &paths); response != nil {
response("invalid path params", logger, w)
return
}
txHash, err := s.stakingContract.DepositStake(r.Context(), paths.Amount)
if err != nil {
if errors.Is(err, staking.ErrInsufficientStakeAmount) {
logger.Debug("insufficient stake amount", "minimum_stake", staking.MinimumStakeAmount, "error", err, "txHash", txHash)
logger.Error(nil, "insufficient stake amount")
jsonhttp.BadRequest(w, "insufficient stake amount")
return
}
if errors.Is(err, staking.ErrNotImplemented) {
logger.Debug("not implemented", "error", err, "txHash", txHash)
logger.Error(nil, "not implemented")
jsonhttp.NotImplemented(w, "not implemented")
return
}
if errors.Is(err, staking.ErrInsufficientFunds) {
logger.Debug("out of funds", "error", err, "txHash", txHash)
logger.Error(nil, "out of funds")
jsonhttp.BadRequest(w, "out of funds")
return
}
logger.Debug("deposit failed", "error", err, "txHash", txHash)
logger.Error(nil, "deposit failed")
jsonhttp.InternalServerError(w, "cannot stake")
return
}
jsonhttp.OK(w, stakeDepositResponse{
TxHash: txHash.String(),
})
}
func (s *Service) getStakedAmountHandler(w http.ResponseWriter, r *http.Request) {
logger := s.logger.WithName("get_stake").Build()
stakedAmount, err := s.stakingContract.GetStake(r.Context())
if err != nil {
logger.Debug("get staked amount failed", "overlayAddr", s.overlay, "error", err)
logger.Error(nil, "get staked amount failed")
jsonhttp.InternalServerError(w, "get staked amount failed")
return
}
jsonhttp.OK(w, getStakeResponse{StakedAmount: bigint.Wrap(stakedAmount)})
}