-
Notifications
You must be signed in to change notification settings - Fork 19
/
staking.go
290 lines (262 loc) · 7.84 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
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
// Copyright (C) 2023 Gobalsky Labs Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package types
import (
"encoding/hex"
"errors"
"fmt"
"strconv"
"code.vegaprotocol.io/vega/libs/crypto"
"code.vegaprotocol.io/vega/libs/num"
"code.vegaprotocol.io/vega/libs/stringer"
vgproto "code.vegaprotocol.io/vega/protos/vega"
eventspb "code.vegaprotocol.io/vega/protos/vega/events/v1"
)
type StakeLinkingType = eventspb.StakeLinking_Type
const (
StakeLinkingTypeUnspecified StakeLinkingType = eventspb.StakeLinking_TYPE_UNSPECIFIED
StakeLinkingTypeDeposited = eventspb.StakeLinking_TYPE_LINK
StakeLinkingTypeRemoved = eventspb.StakeLinking_TYPE_UNLINK
)
type StakeLinkingStatus = eventspb.StakeLinking_Status
const (
StakeLinkingStatusUnspecified StakeLinkingStatus = eventspb.StakeLinking_STATUS_UNSPECIFIED
StakeLinkingStatusPending = eventspb.StakeLinking_STATUS_PENDING
StakeLinkingStatusAccepted = eventspb.StakeLinking_STATUS_ACCEPTED
StakeLinkingStatusRejected = eventspb.StakeLinking_STATUS_REJECTED
)
type StakeTotalSupply struct {
TokenAddress string
TotalSupply *num.Uint
}
func (s *StakeTotalSupply) IntoProto() *vgproto.StakeTotalSupply {
return &vgproto.StakeTotalSupply{
TokenAddress: crypto.EthereumChecksumAddress(s.TokenAddress),
TotalSupply: s.TotalSupply.String(),
}
}
func (s *StakeTotalSupply) String() string {
return fmt.Sprintf(
"tokenAddress(%s) totalSupply(%s)",
s.TokenAddress,
stringer.UintPointerToString(s.TotalSupply),
)
}
func StakeTotalSupplyFromProto(s *vgproto.StakeTotalSupply) (*StakeTotalSupply, error) {
totalSupply := num.UintZero()
if len(s.TotalSupply) > 0 {
var overflowed bool
totalSupply, overflowed = num.UintFromString(s.TotalSupply, 10)
if overflowed {
return nil, errors.New("invalid amount (not a base 10 uint)")
}
}
return &StakeTotalSupply{
TokenAddress: crypto.EthereumChecksumAddress(s.TokenAddress),
TotalSupply: totalSupply,
}, nil
}
type StakeLinking struct {
ID string
Type StakeLinkingType
TS int64
Party string
Amount *num.Uint
Status StakeLinkingStatus
FinalizedAt int64
TxHash string
BlockHeight uint64
BlockTime int64
LogIndex uint64
EthereumAddress string
}
func (s StakeLinking) Hash() string {
bn, li := strconv.FormatUint(s.BlockHeight, 10), strconv.FormatUint(s.LogIndex, 10)
return hex.EncodeToString(
crypto.Hash(
[]byte(bn + li + s.TxHash + s.Party + s.EthereumAddress + s.Amount.String() + s.Type.String()),
),
)
}
func (s *StakeLinking) String() string {
return fmt.Sprintf(
"ID(%s) type(%s) ts(%v) party(%s) amount(%s) status(%s) finalizedAt(%v) txHash(%s) blockHeight(%v) blockTime(%v) logIndex(%v) ethereumAddress(%s)",
s.ID,
s.Type.String(),
s.TS,
s.Party,
stringer.UintPointerToString(s.Amount),
s.Status.String(),
s.FinalizedAt,
s.TxHash,
s.BlockHeight,
s.BlockTime,
s.LogIndex,
s.EthereumAddress,
)
}
func (s *StakeLinking) IntoProto() *eventspb.StakeLinking {
return &eventspb.StakeLinking{
Id: s.ID,
Type: s.Type,
Ts: s.TS,
Party: s.Party,
Amount: num.UintToString(s.Amount),
Status: s.Status,
FinalizedAt: s.FinalizedAt,
TxHash: s.TxHash,
BlockHeight: s.BlockHeight,
BlockTime: s.BlockTime,
LogIndex: s.LogIndex,
EthereumAddress: s.EthereumAddress,
}
}
func StakeLinkingFromProto(sl *eventspb.StakeLinking) *StakeLinking {
amt, _ := num.UintFromString(sl.Amount, 10)
return &StakeLinking{
ID: sl.Id,
Type: sl.Type,
TS: sl.Ts,
Party: sl.Party,
Amount: amt,
Status: sl.Status,
FinalizedAt: sl.FinalizedAt,
TxHash: sl.TxHash,
BlockHeight: sl.BlockHeight,
BlockTime: sl.BlockTime,
LogIndex: sl.LogIndex,
EthereumAddress: crypto.EthereumChecksumAddress(sl.EthereumAddress),
}
}
type StakeDeposited struct {
BlockNumber, LogIndex uint64
TxID string // hash
ID string
VegaPubKey string
EthereumAddress string
Amount *num.Uint
BlockTime int64
}
func StakeDepositedFromProto(
s *vgproto.StakeDeposited,
blockNumber, logIndex uint64,
txID, id string,
) (*StakeDeposited, error) {
amount := num.UintZero()
if len(s.Amount) > 0 {
var overflowed bool
amount, overflowed = num.UintFromString(s.Amount, 10)
if overflowed {
return nil, errors.New("invalid amount (not a base 10 uint)")
}
}
return &StakeDeposited{
ID: id,
BlockNumber: blockNumber,
LogIndex: logIndex,
TxID: txID,
VegaPubKey: s.VegaPublicKey,
EthereumAddress: crypto.EthereumChecksumAddress(s.EthereumAddress),
Amount: amount,
BlockTime: s.BlockTime,
}, nil
}
func (s *StakeDeposited) IntoStakeLinking() *StakeLinking {
return &StakeLinking{
ID: s.ID,
Type: StakeLinkingTypeDeposited,
TS: s.BlockTime,
Party: s.VegaPubKey,
Amount: s.Amount.Clone(),
TxHash: s.TxID,
BlockHeight: s.BlockNumber,
BlockTime: s.BlockTime,
LogIndex: s.LogIndex,
EthereumAddress: s.EthereumAddress,
}
}
func (s StakeDeposited) String() string {
return fmt.Sprintf(
"ID(%s) txID(%s) blockNumber(%v) logIndex(%v) vegaPubKey(%s) ethereumAddress(%s) amount(%s) blockTime(%v)",
s.ID,
s.TxID,
s.BlockNumber,
s.LogIndex,
s.VegaPubKey,
s.EthereumAddress,
stringer.UintPointerToString(s.Amount),
s.BlockTime,
)
}
type StakeRemoved struct {
BlockNumber, LogIndex uint64
TxID string // hash
ID string
VegaPubKey string
EthereumAddress string
Amount *num.Uint
BlockTime int64
}
func StakeRemovedFromProto(
s *vgproto.StakeRemoved,
blockNumber, logIndex uint64,
txID, id string,
) (*StakeRemoved, error) {
amount := num.UintZero()
if len(s.Amount) > 0 {
var overflowed bool
amount, overflowed = num.UintFromString(s.Amount, 10)
if overflowed {
return nil, errors.New("invalid amount (not a base 10 uint)")
}
}
return &StakeRemoved{
ID: id,
BlockNumber: blockNumber,
LogIndex: logIndex,
TxID: txID,
VegaPubKey: s.VegaPublicKey,
EthereumAddress: crypto.EthereumChecksumAddress(s.EthereumAddress),
Amount: amount,
BlockTime: s.BlockTime,
}, nil
}
func (s StakeRemoved) String() string {
return fmt.Sprintf(
"ID(%s) txID(%s) blockNumber(%v) logIndex(%v) vegaPubKey(%s) ethereumAddress(%s) amount(%s) blockTime(%v)",
s.ID,
s.TxID,
s.BlockNumber,
s.LogIndex,
s.VegaPubKey,
s.EthereumAddress,
stringer.UintPointerToString(s.Amount),
s.BlockTime,
)
}
func (s *StakeRemoved) IntoStakeLinking() *StakeLinking {
return &StakeLinking{
ID: s.ID,
Type: StakeLinkingTypeRemoved,
TS: s.BlockTime,
Party: s.VegaPubKey,
Amount: s.Amount.Clone(),
TxHash: s.TxID,
BlockHeight: s.BlockNumber,
BlockTime: s.BlockTime,
LogIndex: s.LogIndex,
EthereumAddress: crypto.EthereumChecksumAddress(s.EthereumAddress),
}
}