code.vegaprotocol.io/vega@v0.79.0/core/types/staking.go (about) 1 // Copyright (C) 2023 Gobalsky Labs Limited 2 // 3 // This program is free software: you can redistribute it and/or modify 4 // it under the terms of the GNU Affero General Public License as 5 // published by the Free Software Foundation, either version 3 of the 6 // License, or (at your option) any later version. 7 // 8 // This program is distributed in the hope that it will be useful, 9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 // GNU Affero General Public License for more details. 12 // 13 // You should have received a copy of the GNU Affero General Public License 14 // along with this program. If not, see <http://www.gnu.org/licenses/>. 15 16 package types 17 18 import ( 19 "encoding/hex" 20 "errors" 21 "fmt" 22 "strconv" 23 24 "code.vegaprotocol.io/vega/libs/crypto" 25 "code.vegaprotocol.io/vega/libs/num" 26 "code.vegaprotocol.io/vega/libs/stringer" 27 vgproto "code.vegaprotocol.io/vega/protos/vega" 28 eventspb "code.vegaprotocol.io/vega/protos/vega/events/v1" 29 ) 30 31 type StakeLinkingType = eventspb.StakeLinking_Type 32 33 const ( 34 StakeLinkingTypeUnspecified StakeLinkingType = eventspb.StakeLinking_TYPE_UNSPECIFIED 35 StakeLinkingTypeDeposited = eventspb.StakeLinking_TYPE_LINK 36 StakeLinkingTypeRemoved = eventspb.StakeLinking_TYPE_UNLINK 37 ) 38 39 type StakeLinkingStatus = eventspb.StakeLinking_Status 40 41 const ( 42 StakeLinkingStatusUnspecified StakeLinkingStatus = eventspb.StakeLinking_STATUS_UNSPECIFIED 43 StakeLinkingStatusPending = eventspb.StakeLinking_STATUS_PENDING 44 StakeLinkingStatusAccepted = eventspb.StakeLinking_STATUS_ACCEPTED 45 StakeLinkingStatusRejected = eventspb.StakeLinking_STATUS_REJECTED 46 ) 47 48 type StakeTotalSupply struct { 49 TokenAddress string 50 TotalSupply *num.Uint 51 } 52 53 func (s *StakeTotalSupply) IntoProto() *vgproto.StakeTotalSupply { 54 return &vgproto.StakeTotalSupply{ 55 TokenAddress: crypto.EthereumChecksumAddress(s.TokenAddress), 56 TotalSupply: s.TotalSupply.String(), 57 } 58 } 59 60 func (s *StakeTotalSupply) String() string { 61 return fmt.Sprintf( 62 "tokenAddress(%s) totalSupply(%s)", 63 s.TokenAddress, 64 stringer.PtrToString(s.TotalSupply), 65 ) 66 } 67 68 func StakeTotalSupplyFromProto(s *vgproto.StakeTotalSupply) (*StakeTotalSupply, error) { 69 totalSupply := num.UintZero() 70 if len(s.TotalSupply) > 0 { 71 var overflowed bool 72 totalSupply, overflowed = num.UintFromString(s.TotalSupply, 10) 73 if overflowed { 74 return nil, errors.New("invalid amount (not a base 10 uint)") 75 } 76 } 77 return &StakeTotalSupply{ 78 TokenAddress: crypto.EthereumChecksumAddress(s.TokenAddress), 79 TotalSupply: totalSupply, 80 }, nil 81 } 82 83 type StakeLinking struct { 84 ID string 85 Type StakeLinkingType 86 TS int64 87 Party string 88 Amount *num.Uint 89 Status StakeLinkingStatus 90 FinalizedAt int64 91 TxHash string 92 BlockHeight uint64 93 BlockTime int64 94 LogIndex uint64 95 EthereumAddress string 96 } 97 98 func (s StakeLinking) Hash() string { 99 bn, li := strconv.FormatUint(s.BlockHeight, 10), strconv.FormatUint(s.LogIndex, 10) 100 return hex.EncodeToString( 101 crypto.Hash( 102 []byte(bn + li + s.TxHash + s.Party + s.EthereumAddress + s.Amount.String() + s.Type.String()), 103 ), 104 ) 105 } 106 107 func (s *StakeLinking) String() string { 108 return fmt.Sprintf( 109 "ID(%s) type(%s) ts(%v) party(%s) amount(%s) status(%s) finalizedAt(%v) txHash(%s) blockHeight(%v) blockTime(%v) logIndex(%v) ethereumAddress(%s)", 110 s.ID, 111 s.Type.String(), 112 s.TS, 113 s.Party, 114 stringer.PtrToString(s.Amount), 115 s.Status.String(), 116 s.FinalizedAt, 117 s.TxHash, 118 s.BlockHeight, 119 s.BlockTime, 120 s.LogIndex, 121 s.EthereumAddress, 122 ) 123 } 124 125 func (s *StakeLinking) IntoProto() *eventspb.StakeLinking { 126 return &eventspb.StakeLinking{ 127 Id: s.ID, 128 Type: s.Type, 129 Ts: s.TS, 130 Party: s.Party, 131 Amount: num.UintToString(s.Amount), 132 Status: s.Status, 133 FinalizedAt: s.FinalizedAt, 134 TxHash: s.TxHash, 135 BlockHeight: s.BlockHeight, 136 BlockTime: s.BlockTime, 137 LogIndex: s.LogIndex, 138 EthereumAddress: s.EthereumAddress, 139 } 140 } 141 142 func StakeLinkingFromProto(sl *eventspb.StakeLinking) *StakeLinking { 143 amt, _ := num.UintFromString(sl.Amount, 10) 144 var ethereumAddress string 145 if len(sl.EthereumAddress) > 0 { 146 ethereumAddress = crypto.EthereumChecksumAddress(sl.EthereumAddress) 147 } 148 return &StakeLinking{ 149 ID: sl.Id, 150 Type: sl.Type, 151 TS: sl.Ts, 152 Party: sl.Party, 153 Amount: amt, 154 Status: sl.Status, 155 FinalizedAt: sl.FinalizedAt, 156 TxHash: sl.TxHash, 157 BlockHeight: sl.BlockHeight, 158 BlockTime: sl.BlockTime, 159 LogIndex: sl.LogIndex, 160 EthereumAddress: ethereumAddress, 161 } 162 } 163 164 type StakeDeposited struct { 165 BlockNumber, LogIndex uint64 166 TxID string // hash 167 168 ID string 169 VegaPubKey string 170 EthereumAddress string 171 Amount *num.Uint 172 BlockTime int64 173 } 174 175 func StakeDepositedFromProto( 176 s *vgproto.StakeDeposited, 177 blockNumber, logIndex uint64, 178 txID, id string, 179 ) (*StakeDeposited, error) { 180 amount := num.UintZero() 181 if len(s.Amount) > 0 { 182 var overflowed bool 183 amount, overflowed = num.UintFromString(s.Amount, 10) 184 if overflowed { 185 return nil, errors.New("invalid amount (not a base 10 uint)") 186 } 187 } 188 189 return &StakeDeposited{ 190 ID: id, 191 BlockNumber: blockNumber, 192 LogIndex: logIndex, 193 TxID: txID, 194 VegaPubKey: s.VegaPublicKey, 195 EthereumAddress: crypto.EthereumChecksumAddress(s.EthereumAddress), 196 Amount: amount, 197 BlockTime: s.BlockTime, 198 }, nil 199 } 200 201 func (s *StakeDeposited) IntoStakeLinking() *StakeLinking { 202 return &StakeLinking{ 203 ID: s.ID, 204 Type: StakeLinkingTypeDeposited, 205 TS: s.BlockTime, 206 Party: s.VegaPubKey, 207 Amount: s.Amount.Clone(), 208 TxHash: s.TxID, 209 BlockHeight: s.BlockNumber, 210 BlockTime: s.BlockTime, 211 LogIndex: s.LogIndex, 212 EthereumAddress: s.EthereumAddress, 213 } 214 } 215 216 func (s StakeDeposited) String() string { 217 return fmt.Sprintf( 218 "ID(%s) txID(%s) blockNumber(%v) logIndex(%v) vegaPubKey(%s) ethereumAddress(%s) amount(%s) blockTime(%v)", 219 s.ID, 220 s.TxID, 221 s.BlockNumber, 222 s.LogIndex, 223 s.VegaPubKey, 224 s.EthereumAddress, 225 stringer.PtrToString(s.Amount), 226 s.BlockTime, 227 ) 228 } 229 230 type StakeRemoved struct { 231 BlockNumber, LogIndex uint64 232 TxID string // hash 233 234 ID string 235 VegaPubKey string 236 EthereumAddress string 237 Amount *num.Uint 238 BlockTime int64 239 } 240 241 func StakeRemovedFromProto( 242 s *vgproto.StakeRemoved, 243 blockNumber, logIndex uint64, 244 txID, id string, 245 ) (*StakeRemoved, error) { 246 amount := num.UintZero() 247 if len(s.Amount) > 0 { 248 var overflowed bool 249 amount, overflowed = num.UintFromString(s.Amount, 10) 250 if overflowed { 251 return nil, errors.New("invalid amount (not a base 10 uint)") 252 } 253 } 254 255 return &StakeRemoved{ 256 ID: id, 257 BlockNumber: blockNumber, 258 LogIndex: logIndex, 259 TxID: txID, 260 VegaPubKey: s.VegaPublicKey, 261 EthereumAddress: crypto.EthereumChecksumAddress(s.EthereumAddress), 262 Amount: amount, 263 BlockTime: s.BlockTime, 264 }, nil 265 } 266 267 func (s StakeRemoved) String() string { 268 return fmt.Sprintf( 269 "ID(%s) txID(%s) blockNumber(%v) logIndex(%v) vegaPubKey(%s) ethereumAddress(%s) amount(%s) blockTime(%v)", 270 s.ID, 271 s.TxID, 272 s.BlockNumber, 273 s.LogIndex, 274 s.VegaPubKey, 275 s.EthereumAddress, 276 stringer.PtrToString(s.Amount), 277 s.BlockTime, 278 ) 279 } 280 281 func (s *StakeRemoved) IntoStakeLinking() *StakeLinking { 282 return &StakeLinking{ 283 ID: s.ID, 284 Type: StakeLinkingTypeRemoved, 285 TS: s.BlockTime, 286 Party: s.VegaPubKey, 287 Amount: s.Amount.Clone(), 288 TxHash: s.TxID, 289 BlockHeight: s.BlockNumber, 290 BlockTime: s.BlockTime, 291 LogIndex: s.LogIndex, 292 EthereumAddress: crypto.EthereumChecksumAddress(s.EthereumAddress), 293 } 294 }