github.com/Finschia/finschia-sdk@v0.48.1/x/staking/types/delegation.go (about) 1 package types 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "strings" 7 "time" 8 9 yaml "gopkg.in/yaml.v2" 10 11 "github.com/Finschia/finschia-sdk/codec" 12 sdk "github.com/Finschia/finschia-sdk/types" 13 ) 14 15 // Implements Delegation interface 16 var _ DelegationI = Delegation{} 17 18 // String implements the Stringer interface for a DVPair object. 19 func (dv DVPair) String() string { 20 out, _ := yaml.Marshal(dv) 21 return string(out) 22 } 23 24 // String implements the Stringer interface for a DVVTriplet object. 25 func (dvv DVVTriplet) String() string { 26 out, _ := yaml.Marshal(dvv) 27 return string(out) 28 } 29 30 // NewDelegation creates a new delegation object 31 // 32 //nolint:interfacer 33 func NewDelegation(delegatorAddr sdk.AccAddress, validatorAddr sdk.ValAddress, shares sdk.Dec) Delegation { 34 return Delegation{ 35 DelegatorAddress: delegatorAddr.String(), 36 ValidatorAddress: validatorAddr.String(), 37 Shares: shares, 38 } 39 } 40 41 // MustMarshalDelegation returns the delegation bytes. Panics if fails 42 func MustMarshalDelegation(cdc codec.BinaryCodec, delegation Delegation) []byte { 43 return cdc.MustMarshal(&delegation) 44 } 45 46 // MustUnmarshalDelegation return the unmarshaled delegation from bytes. 47 // Panics if fails. 48 func MustUnmarshalDelegation(cdc codec.BinaryCodec, value []byte) Delegation { 49 delegation, err := UnmarshalDelegation(cdc, value) 50 if err != nil { 51 panic(err) 52 } 53 54 return delegation 55 } 56 57 // return the delegation 58 func UnmarshalDelegation(cdc codec.BinaryCodec, value []byte) (delegation Delegation, err error) { 59 err = cdc.Unmarshal(value, &delegation) 60 return delegation, err 61 } 62 63 func (d Delegation) GetDelegatorAddr() sdk.AccAddress { 64 delAddr := sdk.MustAccAddressFromBech32(d.DelegatorAddress) 65 66 return delAddr 67 } 68 69 func (d Delegation) GetValidatorAddr() sdk.ValAddress { 70 addr, err := sdk.ValAddressFromBech32(d.ValidatorAddress) 71 if err != nil { 72 panic(err) 73 } 74 return addr 75 } 76 func (d Delegation) GetShares() sdk.Dec { return d.Shares } 77 78 // String returns a human readable string representation of a Delegation. 79 func (d Delegation) String() string { 80 out, _ := yaml.Marshal(d) 81 return string(out) 82 } 83 84 // Delegations is a collection of delegations 85 type Delegations []Delegation 86 87 func (d Delegations) String() (out string) { 88 for _, del := range d { 89 out += del.String() + "\n" 90 } 91 92 return strings.TrimSpace(out) 93 } 94 95 func NewUnbondingDelegationEntry(creationHeight int64, completionTime time.Time, balance sdk.Int) UnbondingDelegationEntry { 96 return UnbondingDelegationEntry{ 97 CreationHeight: creationHeight, 98 CompletionTime: completionTime, 99 InitialBalance: balance, 100 Balance: balance, 101 } 102 } 103 104 // String implements the stringer interface for a UnbondingDelegationEntry. 105 func (e UnbondingDelegationEntry) String() string { 106 out, _ := yaml.Marshal(e) 107 return string(out) 108 } 109 110 // IsMature - is the current entry mature 111 func (e UnbondingDelegationEntry) IsMature(currentTime time.Time) bool { 112 return !e.CompletionTime.After(currentTime) 113 } 114 115 // NewUnbondingDelegation - create a new unbonding delegation object 116 // 117 //nolint:interfacer 118 func NewUnbondingDelegation( 119 delegatorAddr sdk.AccAddress, validatorAddr sdk.ValAddress, 120 creationHeight int64, minTime time.Time, balance sdk.Int, 121 ) UnbondingDelegation { 122 return UnbondingDelegation{ 123 DelegatorAddress: delegatorAddr.String(), 124 ValidatorAddress: validatorAddr.String(), 125 Entries: []UnbondingDelegationEntry{ 126 NewUnbondingDelegationEntry(creationHeight, minTime, balance), 127 }, 128 } 129 } 130 131 // AddEntry - append entry to the unbonding delegation 132 func (ubd *UnbondingDelegation) AddEntry(creationHeight int64, minTime time.Time, balance sdk.Int) { 133 entry := NewUnbondingDelegationEntry(creationHeight, minTime, balance) 134 ubd.Entries = append(ubd.Entries, entry) 135 } 136 137 // RemoveEntry - remove entry at index i to the unbonding delegation 138 func (ubd *UnbondingDelegation) RemoveEntry(i int64) { 139 ubd.Entries = append(ubd.Entries[:i], ubd.Entries[i+1:]...) 140 } 141 142 // return the unbonding delegation 143 func MustMarshalUBD(cdc codec.BinaryCodec, ubd UnbondingDelegation) []byte { 144 return cdc.MustMarshal(&ubd) 145 } 146 147 // unmarshal a unbonding delegation from a store value 148 func MustUnmarshalUBD(cdc codec.BinaryCodec, value []byte) UnbondingDelegation { 149 ubd, err := UnmarshalUBD(cdc, value) 150 if err != nil { 151 panic(err) 152 } 153 154 return ubd 155 } 156 157 // unmarshal a unbonding delegation from a store value 158 func UnmarshalUBD(cdc codec.BinaryCodec, value []byte) (ubd UnbondingDelegation, err error) { 159 err = cdc.Unmarshal(value, &ubd) 160 return ubd, err 161 } 162 163 // String returns a human readable string representation of an UnbondingDelegation. 164 func (ubd UnbondingDelegation) String() string { 165 out := fmt.Sprintf(`Unbonding Delegations between: 166 Delegator: %s 167 Validator: %s 168 Entries:`, ubd.DelegatorAddress, ubd.ValidatorAddress) 169 for i, entry := range ubd.Entries { 170 out += fmt.Sprintf(` Unbonding Delegation %d: 171 Creation Height: %v 172 Min time to unbond (unix): %v 173 Expected balance: %s`, i, entry.CreationHeight, 174 entry.CompletionTime, entry.Balance) 175 } 176 177 return out 178 } 179 180 // UnbondingDelegations is a collection of UnbondingDelegation 181 type UnbondingDelegations []UnbondingDelegation 182 183 func (ubds UnbondingDelegations) String() (out string) { 184 for _, u := range ubds { 185 out += u.String() + "\n" 186 } 187 188 return strings.TrimSpace(out) 189 } 190 191 func NewRedelegationEntry(creationHeight int64, completionTime time.Time, balance sdk.Int, sharesDst sdk.Dec) RedelegationEntry { 192 return RedelegationEntry{ 193 CreationHeight: creationHeight, 194 CompletionTime: completionTime, 195 InitialBalance: balance, 196 SharesDst: sharesDst, 197 } 198 } 199 200 // String implements the Stringer interface for a RedelegationEntry object. 201 func (e RedelegationEntry) String() string { 202 out, _ := yaml.Marshal(e) 203 return string(out) 204 } 205 206 // IsMature - is the current entry mature 207 func (e RedelegationEntry) IsMature(currentTime time.Time) bool { 208 return !e.CompletionTime.After(currentTime) 209 } 210 211 //nolint:interfacer 212 func NewRedelegation( 213 delegatorAddr sdk.AccAddress, validatorSrcAddr, validatorDstAddr sdk.ValAddress, 214 creationHeight int64, minTime time.Time, balance sdk.Int, sharesDst sdk.Dec, 215 ) Redelegation { 216 return Redelegation{ 217 DelegatorAddress: delegatorAddr.String(), 218 ValidatorSrcAddress: validatorSrcAddr.String(), 219 ValidatorDstAddress: validatorDstAddr.String(), 220 Entries: []RedelegationEntry{ 221 NewRedelegationEntry(creationHeight, minTime, balance, sharesDst), 222 }, 223 } 224 } 225 226 // AddEntry - append entry to the unbonding delegation 227 func (red *Redelegation) AddEntry(creationHeight int64, minTime time.Time, balance sdk.Int, sharesDst sdk.Dec) { 228 entry := NewRedelegationEntry(creationHeight, minTime, balance, sharesDst) 229 red.Entries = append(red.Entries, entry) 230 } 231 232 // RemoveEntry - remove entry at index i to the unbonding delegation 233 func (red *Redelegation) RemoveEntry(i int64) { 234 red.Entries = append(red.Entries[:i], red.Entries[i+1:]...) 235 } 236 237 // MustMarshalRED returns the Redelegation bytes. Panics if fails. 238 func MustMarshalRED(cdc codec.BinaryCodec, red Redelegation) []byte { 239 return cdc.MustMarshal(&red) 240 } 241 242 // MustUnmarshalRED unmarshals a redelegation from a store value. Panics if fails. 243 func MustUnmarshalRED(cdc codec.BinaryCodec, value []byte) Redelegation { 244 red, err := UnmarshalRED(cdc, value) 245 if err != nil { 246 panic(err) 247 } 248 249 return red 250 } 251 252 // UnmarshalRED unmarshals a redelegation from a store value 253 func UnmarshalRED(cdc codec.BinaryCodec, value []byte) (red Redelegation, err error) { 254 err = cdc.Unmarshal(value, &red) 255 return red, err 256 } 257 258 // String returns a human readable string representation of a Redelegation. 259 func (red Redelegation) String() string { 260 out := fmt.Sprintf(`Redelegations between: 261 Delegator: %s 262 Source Validator: %s 263 Destination Validator: %s 264 Entries: 265 `, 266 red.DelegatorAddress, red.ValidatorSrcAddress, red.ValidatorDstAddress, 267 ) 268 269 for i, entry := range red.Entries { 270 out += fmt.Sprintf(` Redelegation Entry #%d: 271 Creation height: %v 272 Min time to unbond (unix): %v 273 Dest Shares: %s 274 `, 275 i, entry.CreationHeight, entry.CompletionTime, entry.SharesDst, 276 ) 277 } 278 279 return strings.TrimRight(out, "\n") 280 } 281 282 // Redelegations are a collection of Redelegation 283 type Redelegations []Redelegation 284 285 func (d Redelegations) String() (out string) { 286 for _, red := range d { 287 out += red.String() + "\n" 288 } 289 290 return strings.TrimSpace(out) 291 } 292 293 // ---------------------------------------------------------------------------- 294 // Client Types 295 296 // NewDelegationResp creates a new DelegationResponse instance 297 func NewDelegationResp( 298 delegatorAddr sdk.AccAddress, validatorAddr sdk.ValAddress, shares sdk.Dec, balance sdk.Coin, 299 ) DelegationResponse { 300 return DelegationResponse{ 301 Delegation: NewDelegation(delegatorAddr, validatorAddr, shares), 302 Balance: balance, 303 } 304 } 305 306 // String implements the Stringer interface for DelegationResponse. 307 func (d DelegationResponse) String() string { 308 return fmt.Sprintf("%s\n Balance: %s", d.Delegation.String(), d.Balance) 309 } 310 311 type delegationRespAlias DelegationResponse 312 313 // MarshalJSON implements the json.Marshaler interface. This is so we can 314 // achieve a flattened structure while embedding other types. 315 func (d DelegationResponse) MarshalJSON() ([]byte, error) { 316 return json.Marshal((delegationRespAlias)(d)) 317 } 318 319 // UnmarshalJSON implements the json.Unmarshaler interface. This is so we can 320 // achieve a flattened structure while embedding other types. 321 func (d *DelegationResponse) UnmarshalJSON(bz []byte) error { 322 return json.Unmarshal(bz, (*delegationRespAlias)(d)) 323 } 324 325 // DelegationResponses is a collection of DelegationResp 326 type DelegationResponses []DelegationResponse 327 328 // String implements the Stringer interface for DelegationResponses. 329 func (d DelegationResponses) String() (out string) { 330 for _, del := range d { 331 out += del.String() + "\n" 332 } 333 334 return strings.TrimSpace(out) 335 } 336 337 // NewRedelegationResponse crates a new RedelegationEntryResponse instance. 338 // 339 //nolint:interfacer 340 func NewRedelegationResponse( 341 delegatorAddr sdk.AccAddress, validatorSrc, validatorDst sdk.ValAddress, entries []RedelegationEntryResponse, 342 ) RedelegationResponse { 343 return RedelegationResponse{ 344 Redelegation: Redelegation{ 345 DelegatorAddress: delegatorAddr.String(), 346 ValidatorSrcAddress: validatorSrc.String(), 347 ValidatorDstAddress: validatorDst.String(), 348 }, 349 Entries: entries, 350 } 351 } 352 353 // NewRedelegationEntryResponse creates a new RedelegationEntryResponse instance. 354 func NewRedelegationEntryResponse( 355 creationHeight int64, completionTime time.Time, sharesDst sdk.Dec, initialBalance, balance sdk.Int, 356 ) RedelegationEntryResponse { 357 return RedelegationEntryResponse{ 358 RedelegationEntry: NewRedelegationEntry(creationHeight, completionTime, initialBalance, sharesDst), 359 Balance: balance, 360 } 361 } 362 363 type redelegationRespAlias RedelegationResponse 364 365 // MarshalJSON implements the json.Marshaler interface. This is so we can 366 // achieve a flattened structure while embedding other types. 367 func (r RedelegationResponse) MarshalJSON() ([]byte, error) { 368 return json.Marshal((redelegationRespAlias)(r)) 369 } 370 371 // UnmarshalJSON implements the json.Unmarshaler interface. This is so we can 372 // achieve a flattened structure while embedding other types. 373 func (r *RedelegationResponse) UnmarshalJSON(bz []byte) error { 374 return json.Unmarshal(bz, (*redelegationRespAlias)(r)) 375 } 376 377 // RedelegationResponses are a collection of RedelegationResp 378 type RedelegationResponses []RedelegationResponse 379 380 func (r RedelegationResponses) String() (out string) { 381 for _, red := range r { 382 out += red.String() + "\n" 383 } 384 385 return strings.TrimSpace(out) 386 }