-
Notifications
You must be signed in to change notification settings - Fork 2
/
admin_buy_deso.go
160 lines (138 loc) · 5.54 KB
/
admin_buy_deso.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package routes
import (
"encoding/json"
"fmt"
"github.com/deso-smart/deso-core/v2/lib"
"github.com/golang/glog"
"io"
"net/http"
)
type SetUSDCentsToDeSoExchangeRateRequest struct {
USDCentsPerDeSo uint64
AdminPublicKey string
}
type SetUSDCentsToDeSoExchangeRateResponse struct {
USDCentsPerDeSo uint64
}
// SetUSDCentsToDeSoReserveExchangeRate sets the minimum price to buy DeSo from this node.
func (fes *APIServer) SetUSDCentsToDeSoReserveExchangeRate(ww http.ResponseWriter, req *http.Request) {
decoder := json.NewDecoder(io.LimitReader(req.Body, MaxRequestBodySizeBytes))
requestData := SetUSDCentsToDeSoExchangeRateRequest{}
if err := decoder.Decode(&requestData); err != nil {
_AddBadRequestError(ww, fmt.Sprintf("SetUSDCentsToDeSoReserveExchangeRate: Problem parsing request body: %v", err))
return
}
// Put the new value in global state
if err := fes.GlobalState.Put(
GlobalStateKeyForUSDCentsToDeSoReserveExchangeRate(),
lib.UintToBuf(requestData.USDCentsPerDeSo)); err != nil {
_AddBadRequestError(ww, fmt.Sprintf("SetUSDCentsToDeSoReserveExchangeRate: Problem putting exchange rate in global state: %v", err))
return
}
fes.USDCentsToDESOReserveExchangeRate = requestData.USDCentsPerDeSo
// Force refresh the USD Cent to DeSo exchange rate
fes.UpdateUSDCentsToDeSoExchangeRate()
res := SetUSDCentsToDeSoExchangeRateResponse{
USDCentsPerDeSo: requestData.USDCentsPerDeSo,
}
if err := json.NewEncoder(ww).Encode(res); err != nil {
_AddBadRequestError(ww, fmt.Sprintf("SetUSDCentsToDeSoReserveExchangeRate: Problem encoding response as JSON: %v", err))
return
}
}
type GetUSDCentsToDeSoExchangeRateResponse struct {
USDCentsPerDeSo uint64
}
// GetUSDCentsToDeSoReserveExchangeRate get the current reserve exchange rate
func (fes *APIServer) GetUSDCentsToDeSoReserveExchangeRate(ww http.ResponseWriter, req *http.Request) {
res := GetUSDCentsToDeSoExchangeRateResponse{
USDCentsPerDeSo: fes.USDCentsToDESOReserveExchangeRate,
}
if err := json.NewEncoder(ww).Encode(res); err != nil {
_AddBadRequestError(ww, fmt.Sprintf("GetUSDCentsToDeSoExchangeRate: Problem encoding response as JSON: %v", err))
return
}
}
// SetUSDCentsToDeSoReserveExchangeRateFromGlobalState is a helper function to set the cached value of the current USD
// cents to DeSo exchange rate
func (fes *APIServer) SetUSDCentsToDeSoReserveExchangeRateFromGlobalState() {
val, err := fes.GlobalState.Get(GlobalStateKeyForUSDCentsToDeSoReserveExchangeRate())
if err != nil {
glog.Errorf("SetUSDCentsToDeSoReserveExchangeRateFromGlobalState: Error getting Reserve exchange rate "+
"from global state: %v", err)
return
}
// If there was no value found, this node has not set the Fee Basis points yet so we return.
if val == nil {
return
}
usdCentsPerDeSo, bytesRead := lib.Uvarint(val)
if bytesRead <= 0 {
glog.Errorf("SetUSDCentsToDeSoReserveExchangeRateFromGlobalState: invalid bytes read: %v", bytesRead)
return
}
fes.USDCentsToDESOReserveExchangeRate = usdCentsPerDeSo
}
type SetBuyDeSoFeeBasisPointsRequest struct {
BuyDeSoFeeBasisPoints uint64
AdminPublicKey string
}
type SetBuyDeSoFeeBasisPointsResponse struct {
BuyDeSoFeeBasisPoints uint64
}
// SetBuyDeSoFeeBasisPoints sets the percentage fee applied to all DeSo buys on this node.
func (fes *APIServer) SetBuyDeSoFeeBasisPoints(ww http.ResponseWriter, req *http.Request) {
decoder := json.NewDecoder(io.LimitReader(req.Body, MaxRequestBodySizeBytes))
requestData := SetBuyDeSoFeeBasisPointsRequest{}
if err := decoder.Decode(&requestData); err != nil {
_AddBadRequestError(ww, fmt.Sprintf("SetBuyDeSoFeeBasisPoints: Problem parsing request body: %v", err))
return
}
if err := fes.GlobalState.Put(
GlobalStateKeyForBuyDeSoFeeBasisPoints(),
lib.UintToBuf(requestData.BuyDeSoFeeBasisPoints)); err != nil {
_AddBadRequestError(ww, fmt.Sprintf("SetBuyDeSoFeeBasisPoints: Problem putting premium basis points in global state: %v", err))
return
}
fes.BuyDESOFeeBasisPoints = requestData.BuyDeSoFeeBasisPoints
res := SetBuyDeSoFeeBasisPointsResponse{
BuyDeSoFeeBasisPoints: requestData.BuyDeSoFeeBasisPoints,
}
if err := json.NewEncoder(ww).Encode(res); err != nil {
_AddBadRequestError(ww, fmt.Sprintf("SetBuyDeSoFeeBasisPoints: Problem encoding response as JSON: %v", err))
return
}
}
type GetBuyDeSoFeeBasisPointsResponse struct {
BuyDeSoFeeBasisPoints uint64
}
// GetBuyDeSoFeeBasisPoints gets the current value of the buy DeSo fee.
func (fes *APIServer) GetBuyDeSoFeeBasisPoints(ww http.ResponseWriter, req *http.Request) {
res := GetBuyDeSoFeeBasisPointsResponse{
BuyDeSoFeeBasisPoints: fes.BuyDESOFeeBasisPoints,
}
if err := json.NewEncoder(ww).Encode(res); err != nil {
_AddBadRequestError(ww, fmt.Sprintf("GetBuyDeSoFeeBasisPoints: Problem encoding response as JSON: %v", err))
return
}
}
// SetBuyDeSoFeeBasisPointsResponseFromGlobalState is a utility to set the cached value of the current buy DeSo fee
// from global state.
func (fes *APIServer) SetBuyDeSoFeeBasisPointsResponseFromGlobalState() {
val, err := fes.GlobalState.Get(GlobalStateKeyForBuyDeSoFeeBasisPoints())
if err != nil {
glog.Errorf("SetBuyDeSoFeeBasisPointsResponseFromGlobalState: Error getting Buy DESO Fee Basis Points "+
"from global state: %v", err)
return
}
// If there was no value found, this node has not set the Fee Basis points yet so we return 0.
if val == nil {
return
}
feeBasisPoints, bytesRead := lib.Uvarint(val)
if bytesRead <= 0 {
glog.Errorf("SetBuyDeSoFeeBasisPointsResponseFromGlobalState: invalid bytes read: %v", bytesRead)
return
}
fes.BuyDESOFeeBasisPoints = feeBasisPoints
}