github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/staking_info.go (about) 1 package hedera 2 3 /*- 4 * 5 * Hedera Go SDK 6 * 7 * Copyright (C) 2020 - 2024 Hedera Hashgraph, LLC 8 * 9 * Licensed under the Apache License, Version 2.0 (the "License"); 10 * you may not use this file except in compliance with the License. 11 * You may obtain a copy of the License at 12 * 13 * http://www.apache.org/licenses/LICENSE-2.0 14 * 15 * Unless required by applicable law or agreed to in writing, software 16 * distributed under the License is distributed on an "AS IS" BASIS, 17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 * See the License for the specific language governing permissions and 19 * limitations under the License. 20 * 21 */ 22 23 import ( 24 "time" 25 26 "github.com/hashgraph/hedera-protobufs-go/services" 27 protobuf "google.golang.org/protobuf/proto" 28 ) 29 30 type StakingInfo struct { 31 DeclineStakingReward bool 32 StakePeriodStart *time.Time 33 PendingReward int64 34 PendingHbarReward Hbar 35 StakedToMe Hbar 36 StakedAccountID *AccountID 37 StakedNodeID *int64 38 } 39 40 func _StakingInfoFromProtobuf(pb *services.StakingInfo) StakingInfo { 41 var start time.Time 42 if pb.StakePeriodStart != nil { 43 start = _TimeFromProtobuf(pb.StakePeriodStart) 44 } 45 46 body := StakingInfo{ 47 DeclineStakingReward: pb.DeclineReward, 48 StakePeriodStart: &start, 49 PendingReward: pb.PendingReward, 50 PendingHbarReward: HbarFromTinybar(pb.PendingReward), 51 StakedToMe: HbarFromTinybar(pb.StakedToMe), 52 } 53 54 switch temp := pb.StakedId.(type) { 55 case *services.StakingInfo_StakedAccountId: 56 body.StakedAccountID = _AccountIDFromProtobuf(temp.StakedAccountId) 57 case *services.StakingInfo_StakedNodeId: 58 body.StakedNodeID = &temp.StakedNodeId 59 } 60 61 return body 62 } 63 64 func (stakingInfo *StakingInfo) _ToProtobuf() *services.StakingInfo { // nolint 65 var pendingReward int64 66 67 if stakingInfo.PendingReward > 0 { 68 pendingReward = stakingInfo.PendingReward 69 } else { 70 pendingReward = stakingInfo.PendingHbarReward.AsTinybar() 71 } 72 73 body := services.StakingInfo{ 74 DeclineReward: stakingInfo.DeclineStakingReward, 75 PendingReward: pendingReward, 76 StakedToMe: stakingInfo.StakedToMe.AsTinybar(), 77 } 78 79 if stakingInfo.StakePeriodStart != nil { 80 body.StakePeriodStart = _TimeToProtobuf(*stakingInfo.StakePeriodStart) 81 } 82 83 if stakingInfo.StakedAccountID != nil { 84 body.StakedId = &services.StakingInfo_StakedAccountId{StakedAccountId: stakingInfo.StakedAccountID._ToProtobuf()} 85 } else if stakingInfo.StakedNodeID != nil { 86 body.StakedId = &services.StakingInfo_StakedNodeId{StakedNodeId: *stakingInfo.StakedNodeID} 87 } 88 89 return &body 90 } 91 92 // ToBytes returns the byte representation of the StakingInfo 93 func (stakingInfo *StakingInfo) ToBytes() []byte { 94 data, err := protobuf.Marshal(stakingInfo._ToProtobuf()) 95 if err != nil { 96 return make([]byte, 0) 97 } 98 99 return data 100 } 101 102 // StakingInfoFromBytes returns a StakingInfo object from a raw byte array 103 func StakingInfoFromBytes(data []byte) (StakingInfo, error) { 104 if data == nil { 105 return StakingInfo{}, errByteArrayNull 106 } 107 pb := services.StakingInfo{} 108 err := protobuf.Unmarshal(data, &pb) 109 if err != nil { 110 return StakingInfo{}, err 111 } 112 113 info := _StakingInfoFromProtobuf(&pb) 114 115 return info, nil 116 }