-
Notifications
You must be signed in to change notification settings - Fork 1
/
wallet_deleg.go
291 lines (258 loc) · 8.01 KB
/
wallet_deleg.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package main
import (
"errors"
"fmt"
"net/http"
"strconv"
"github.com/go-macaron/session"
"gopkg.in/macaron.v1"
ms "github.com/ValidatorCenter/minter-go-sdk"
s "github.com/ValidatorCenter/prs3r/strc"
)
// Функция делегирования
func Delegat(sess session.Store, delegCoin string, validator string, valueDeleg float32, feeCoin string) (string, error) {
sdk.AccPrivateKey = sess.Get("priv_k").(string)
//sdk.AccAddress,_ = ms.GetAddressPrivateKey(sdk.AccPrivateKey) // другой вариант ниже
sdk.AccAddress = sess.Get("login").(string)
delegDt := ms.TxDelegateData{
Coin: delegCoin,
PubKey: validator,
Stake: valueDeleg,
// Gas
GasCoin: feeCoin,
GasPrice: 1,
}
resHash, err := sdk.TxDelegate(&delegDt)
if err != nil {
return "", err
}
return resHash, nil
}
// Функция отзыва делегированных
func UnDelegat(sess session.Store, delegCoin string, validator string, valueDeleg int64, feeCoin string) (string, error) {
sdk.AccPrivateKey = sess.Get("priv_k").(string)
//sdk.AccAddress,_ = ms.GetAddressPrivateKey(sdk.AccPrivateKey) // другой вариант ниже
sdk.AccAddress = sess.Get("login").(string)
unbDt := ms.TxUnbondData{
Coin: delegCoin,
PubKey: validator,
Value: valueDeleg,
// Gas
GasCoin: feeCoin,
GasPrice: 1,
}
resHash, err := sdk.TxUnbond(&unbDt)
if err != nil {
return "", err
}
return resHash, nil
}
// Добавление нового правила делегирования
func AddAutodeleg(xPubKey, xAddress, xCoin string, xAmntPrc int) error {
var err error
vN := []s.AutodelegCfg{}
vN = srchAutodeleg(dbSQL, xPubKey, xAddress, xCoin)
if len(vN) > 0 {
return errors.New("Dubl")
} else {
// добавляем
newNUX := s.AutodelegCfg{}
newNUX.Address = xAddress
newNUX.PubKey = xPubKey
newNUX.Coin = xCoin
newNUX.WalletPrc = xAmntPrc
err = addAutodelegSql(dbSQL, newNUX)
if err != nil {
return err
}
}
return nil
}
// Сохранение изменений правила делегирования
func SavAutodeleg(xPubKey, xAddress, xCoin string, xAmntPrc int) error {
var err error
vN := []s.AutodelegCfg{}
vN = srchAutodeleg(dbSQL, xPubKey, xAddress, xCoin)
if len(vN) > 0 {
// Изменение
err = updAutodelegSql(dbSQL, s.AutodelegCfg{
// Ищем:
PubKey: xPubKey,
Address: xAddress,
Coin: xCoin,
// Обновляем:
WalletPrc: xAmntPrc,
})
if err != nil {
return err
}
} else {
return errors.New("NoData")
}
return nil
}
// Удаление правила делегирования
func DelAutodeleg(xPubKey, xAddress, xCoin string) error {
var err error
vN := []s.AutodelegCfg{}
vN = srchAutodeleg(dbSQL, xPubKey, xAddress, xCoin)
if len(vN) > 0 {
// Изменение
err = delAutodelegSql(dbSQL, xPubKey, xAddress, xCoin)
if err != nil {
return err
}
} else {
return errors.New("NoData")
}
return nil
}
// СТРАНИЦА: делегирования монет из личного кабинета
func hndWalletTxDelegation(ctx *macaron.Context, sess session.Store) {
var alertMsg, alertType, alertAct string
var auth bool = false
var usrName, idUsr string
if sess.Get("login") != nil {
auth = true
idUsr = sess.Get("login").(string)
usrName = fmt.Sprintf("%s...%s", idUsr[:6], idUsr[len(idUsr)-4:len(idUsr)])
} else {
// Редирект на главную или страницу авторизации
ctx.Redirect("/coins")
return
}
// если был POST!
ctx.Req.ParseForm()
ctx.Resp.WriteHeader(http.StatusOK)
inputPubKey := ctx.Req.PostFormValue("inputPubKey")
inputAmnt := ctx.Req.PostFormValue("inputAmnt")
inputCoin := ctx.Req.PostFormValue("inputCoin")
//inputMsg := ctx.Req.PostFormValue("inputMsg")
inputFeeCoin := ctx.Req.PostFormValue("inputFeeCoin")
typeAct := ctx.Req.PostFormValue("typeAct")
inputPrcDeleg := ctx.Req.PostFormValue("inputPrcDeleg")
if typeAct != "" {
if typeAct == "DELEG" {
inputAmnt_f32, err := strconv.ParseFloat(inputAmnt, 32)
if err != nil {
alertAct = "Delegation"
alertMsg = fmt.Sprintf("%s", err)
alertType = "danger" //danger warning
} else {
fmt.Println("DELEG", inputPubKey, inputAmnt, inputCoin)
hashTr, err := Delegat(sess, inputCoin, inputPubKey, float32(inputAmnt_f32), inputFeeCoin)
if err != nil {
alertAct = "Delegation"
alertMsg = fmt.Sprintf("%s-%s", err, hashTr)
alertType = "danger" //danger warning
} else {
fmt.Println("....DELEG:", hashTr)
alertAct = "Delegation"
alertMsg = hashTr
alertType = "success" //danger warning
}
}
} else if typeAct == "UNDELEG" {
inputAmnt_i, err := strconv.Atoi(inputAmnt)
if err != nil {
alertAct = "UnDelegation"
alertMsg = fmt.Sprintf("%s", err)
alertType = "danger" //danger warning
} else {
fmt.Println("UNDELEG", inputPubKey, inputAmnt, inputCoin)
hashTr, err := UnDelegat(sess, inputCoin, inputPubKey, int64(inputAmnt_i), inputFeeCoin)
if err != nil {
alertAct = "UnDelegation"
alertMsg = fmt.Sprintf("%s-%s", err, hashTr)
alertType = "danger" //danger warning
} else {
fmt.Println("....UNDELEG:", hashTr)
alertAct = "UnDelegation"
alertMsg = hashTr
alertType = "success" //danger warning
}
}
} else if typeAct == "ADELEG-ADD" {
inputPrcDeleg_i, err := strconv.Atoi(inputPrcDeleg)
if err != nil {
alertAct = "AutoDelegation add"
alertMsg = fmt.Sprintf("%s", err)
alertType = "danger" //danger warning
} else {
err = AddAutodeleg(inputPubKey, idUsr, inputCoin, inputPrcDeleg_i)
if err != nil {
alertAct = "AutoDelegation add"
alertMsg = fmt.Sprintf("%s", err)
alertType = "danger" //danger warning
} else {
alertAct = "AutoDelegation add"
alertMsg = ""
alertType = "success" //danger warning
}
}
} else if typeAct == "ADELEG-SAV" {
inputPrcDeleg_i, err := strconv.Atoi(inputPrcDeleg)
if err != nil {
alertAct = "AutoDelegation save"
alertMsg = fmt.Sprintf("%s", err)
alertType = "danger" //danger warning
} else {
err = SavAutodeleg(inputPubKey, idUsr, inputCoin, inputPrcDeleg_i)
if err != nil {
alertAct = "AutoDelegation sav"
alertMsg = fmt.Sprintf("%s", err)
alertType = "danger" //danger warning
} else {
alertAct = "AutoDelegation sav"
alertMsg = ""
alertType = "success" //danger warning
}
}
} else if typeAct == "ADELEG-DEL" {
err := DelAutodeleg(inputPubKey, idUsr, inputCoin)
if err != nil {
alertAct = "AutoDelegation del"
alertMsg = fmt.Sprintf("%s", err)
alertType = "danger" //danger warning
} else {
alertAct = "AutoDelegation del"
alertMsg = ""
alertType = "success" //danger warning
}
}
}
vN := []s.AutodelegCfg{}
vN = srchAutodelegAddress(dbSQL, idUsr)
// Последний синхронизированный блок
ResultNetwork, _ := sdk.GetStatus()
statusMDB := srchSysSql(dbSys)
// Инф. о синхронизации БД с БлокЧейном:
ctx.Data["LastSync"] = statusMDB.LatestBlockSave
ctx.Data["Current"] = ResultNetwork.LatestBlockHeight
if sdk.ChainMainnet {
ctx.Data["ChainNet"] = "mainnet"
} else {
ctx.Data["ChainNet"] = "testnet"
}
// получаем список
coinsAddrs, totalAmnt, _ := sdk.GetAddress(idUsr)
ctx.Data["AllCoins"] = coinsAddrs
ctx.Data["TotalAmntInBaseCoin"] = totalAmnt
ctx.Data["AllAutoDeleg"] = vN
ctx.Data["Title"] = "Delegation"
ctx.Data["UsrAuth"] = auth
ctx.Data["UsrName"] = usrName
ctx.Data["UsrAddress"] = idUsr
ctx.Data["AlertAct"] = alertAct
ctx.Data["AlertMsg"] = alertMsg
ctx.Data["AlertType"] = alertType
ctx.HTML(200, "tx_deleg")
}
// API: получить JSON конфигурацию автоделегирования по адресу
func hndAPIAutoDelegAddress(ctx *macaron.Context) {
nmbrAddrs := ctx.Params(":number")
retDt := []s.AutodelegCfg{}
retDt = srchAutodelegAddress(dbSQL, nmbrAddrs)
// возврат JSON данных, если нет, то пустой массив
ctx.JSON(200, &retDt)
}