github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/custom_fractional_fee.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 "fmt" 25 26 "github.com/hashgraph/hedera-protobufs-go/services" 27 protobuf "google.golang.org/protobuf/proto" 28 ) 29 30 // A fractional fee transfers the specified fraction of the total value of the tokens that are being transferred 31 // to the specified fee-collecting account. Along with setting a custom fractional fee, you can 32 type CustomFractionalFee struct { 33 CustomFee 34 Numerator int64 35 Denominator int64 36 MinimumAmount int64 37 MaximumAmount int64 38 AssessmentMethod FeeAssessmentMethod 39 } 40 41 // A fractional fee transfers the specified fraction of the total value of the tokens that are being transferred 42 // to the specified fee-collecting account. Along with setting a custom fractional fee, you can 43 func NewCustomFractionalFee() *CustomFractionalFee { 44 return &CustomFractionalFee{ 45 CustomFee: CustomFee{}, 46 Numerator: 0, 47 Denominator: 0, 48 MinimumAmount: 0, 49 MaximumAmount: 0, 50 AssessmentMethod: false, 51 } 52 } 53 54 // SetFeeCollectorAccountID sets the account ID that will receive the custom fee 55 func (fee *CustomFractionalFee) SetFeeCollectorAccountID(id AccountID) *CustomFractionalFee { 56 fee.FeeCollectorAccountID = &id 57 return fee 58 } 59 60 // SetNumerator sets the numerator of the fractional fee 61 func (fee *CustomFractionalFee) SetNumerator(numerator int64) *CustomFractionalFee { 62 fee.Numerator = numerator 63 return fee 64 } 65 66 // SetDenominator sets the denominator of the fractional fee 67 func (fee *CustomFractionalFee) SetDenominator(denominator int64) *CustomFractionalFee { 68 fee.Denominator = denominator 69 return fee 70 } 71 72 // SetMin sets the minimum amount of the fractional fee 73 func (fee *CustomFractionalFee) SetMin(min int64) *CustomFractionalFee { 74 fee.MinimumAmount = min 75 return fee 76 } 77 78 // SetMax sets the maximum amount of the fractional fee 79 func (fee *CustomFractionalFee) SetMax(max int64) *CustomFractionalFee { 80 fee.MaximumAmount = max 81 return fee 82 } 83 84 // GetFeeCollectorAccountID returns the account ID that will receive the custom fee 85 func (fee *CustomFractionalFee) GetFeeCollectorAccountID() AccountID { 86 if fee.FeeCollectorAccountID != nil { 87 return *fee.FeeCollectorAccountID 88 } 89 90 return AccountID{} 91 } 92 93 // GetNumerator returns the numerator of the fractional fee 94 func (fee *CustomFractionalFee) GetNumerator() int64 { 95 return fee.Numerator 96 } 97 98 // GetDenominator returns the denominator of the fractional fee 99 func (fee *CustomFractionalFee) GetDenominator() int64 { 100 return fee.Denominator 101 } 102 103 // GetMin returns the minimum amount of the fractional fee 104 func (fee *CustomFractionalFee) GetMin() int64 { 105 return fee.MinimumAmount 106 } 107 108 // GetMax returns the maximum amount of the fractional fee 109 func (fee *CustomFractionalFee) GetMax() int64 { 110 return fee.MaximumAmount 111 } 112 113 // GetAssessmentMethod returns the assessment method of the fractional fee 114 func (fee *CustomFractionalFee) GetAssessmentMethod() FeeAssessmentMethod { 115 return fee.AssessmentMethod 116 } 117 118 // SetAssessmentMethod sets the assessment method of the fractional fee 119 func (fee *CustomFractionalFee) SetAssessmentMethod(feeAssessmentMethod FeeAssessmentMethod) *CustomFractionalFee { 120 fee.AssessmentMethod = feeAssessmentMethod 121 return fee 122 } 123 124 func _CustomFractionalFeeFromProtobuf(fractionalFee *services.FractionalFee, fee CustomFee) CustomFractionalFee { 125 return CustomFractionalFee{ 126 CustomFee: fee, 127 Numerator: fractionalFee.FractionalAmount.Numerator, 128 Denominator: fractionalFee.FractionalAmount.Denominator, 129 MinimumAmount: fractionalFee.MinimumAmount, 130 MaximumAmount: fractionalFee.MaximumAmount, 131 AssessmentMethod: FeeAssessmentMethod(fractionalFee.NetOfTransfers), 132 } 133 } 134 135 func (fee CustomFractionalFee) validateNetworkOnIDs(client *Client) error { 136 if client == nil || !client.autoValidateChecksums { 137 return nil 138 } 139 140 if fee.FeeCollectorAccountID != nil { 141 if fee.FeeCollectorAccountID != nil { 142 if err := fee.FeeCollectorAccountID.ValidateChecksum(client); err != nil { 143 return err 144 } 145 } 146 } 147 148 return nil 149 } 150 151 // SetAllCollectorsAreExempt sets the flag that indicates if all collectors are exempt from the custom fee 152 func (fee *CustomFractionalFee) SetAllCollectorsAreExempt(exempt bool) *CustomFractionalFee { 153 fee.AllCollectorsAreExempt = exempt 154 return fee 155 } 156 157 func (fee CustomFractionalFee) _ToProtobuf() *services.CustomFee { 158 var FeeCollectorAccountID *services.AccountID 159 if fee.FeeCollectorAccountID != nil { 160 FeeCollectorAccountID = fee.CustomFee.FeeCollectorAccountID._ToProtobuf() 161 } 162 163 return &services.CustomFee{ 164 Fee: &services.CustomFee_FractionalFee{ 165 FractionalFee: &services.FractionalFee{ 166 FractionalAmount: &services.Fraction{ 167 Numerator: fee.Numerator, 168 Denominator: fee.Denominator, 169 }, 170 MinimumAmount: fee.MinimumAmount, 171 MaximumAmount: fee.MaximumAmount, 172 NetOfTransfers: bool(fee.AssessmentMethod), 173 }, 174 }, 175 FeeCollectorAccountId: FeeCollectorAccountID, 176 AllCollectorsAreExempt: fee.AllCollectorsAreExempt, 177 } 178 } 179 180 // ToBytes returns a byte array representation of the CustomFractionalFee 181 func (fee CustomFractionalFee) ToBytes() []byte { 182 data, err := protobuf.Marshal(fee._ToProtobuf()) 183 if err != nil { 184 return make([]byte, 0) 185 } 186 187 return data 188 } 189 190 // String returns a string representation of the CustomFractionalFee 191 func (fee CustomFractionalFee) String() string { 192 return fmt.Sprintf("feeCollectorAccountID: %s, numerator: %d, denominator: %d, min: %d, Max: %d, assessmentMethod: %s", fee.FeeCollectorAccountID.String(), fee.Numerator, fee.Denominator, fee.MinimumAmount, fee.MaximumAmount, fee.AssessmentMethod.String()) 193 }