github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/custom_fixed_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 type Fee interface { 31 _ToProtobuf() *services.CustomFee 32 validateNetworkOnIDs(client *Client) error 33 } 34 35 // A fixed fee transfers a specified amount of the token, to the specified collection account(s), 36 // each time a token transfer is initiated. The custom token fee does not depend on the amount of the 37 // token that is being transferred. 38 type CustomFixedFee struct { 39 CustomFee 40 Amount int64 41 DenominationTokenID *TokenID 42 } 43 44 // A fixed fee transfers a specified amount of the token, to the specified collection account(s), 45 // each time a token transfer is initiated. The custom token fee does not depend on the amount of the 46 // token that is being transferred. 47 func NewCustomFixedFee() *CustomFixedFee { 48 return &CustomFixedFee{ 49 CustomFee: CustomFee{}, 50 Amount: 0, 51 DenominationTokenID: nil, 52 } 53 } 54 55 func _CustomFixedFeeFromProtobuf(fixedFee *services.FixedFee, customFee CustomFee) CustomFixedFee { 56 return CustomFixedFee{ 57 CustomFee: customFee, 58 Amount: fixedFee.Amount, 59 DenominationTokenID: _TokenIDFromProtobuf(fixedFee.DenominatingTokenId), 60 } 61 } 62 63 func (fee CustomFixedFee) validateNetworkOnIDs(client *Client) error { 64 if client == nil || !client.autoValidateChecksums { 65 return nil 66 } 67 if fee.DenominationTokenID != nil { 68 if fee.DenominationTokenID != nil { 69 if err := fee.DenominationTokenID.ValidateChecksum(client); err != nil { 70 return err 71 } 72 } 73 } 74 75 if fee.FeeCollectorAccountID != nil { 76 if fee.FeeCollectorAccountID != nil { 77 if err := fee.FeeCollectorAccountID.ValidateChecksum(client); err != nil { 78 return err 79 } 80 } 81 } 82 83 return nil 84 } 85 86 func (fee CustomFixedFee) _ToProtobuf() *services.CustomFee { 87 var tokenID *services.TokenID 88 if fee.DenominationTokenID != nil { 89 tokenID = fee.DenominationTokenID._ToProtobuf() 90 } 91 92 var FeeCollectorAccountID *services.AccountID 93 if fee.FeeCollectorAccountID != nil { 94 FeeCollectorAccountID = fee.CustomFee.FeeCollectorAccountID._ToProtobuf() 95 } 96 97 return &services.CustomFee{ 98 Fee: &services.CustomFee_FixedFee{ 99 FixedFee: &services.FixedFee{ 100 Amount: fee.Amount, 101 DenominatingTokenId: tokenID, 102 }, 103 }, 104 FeeCollectorAccountId: FeeCollectorAccountID, 105 AllCollectorsAreExempt: fee.AllCollectorsAreExempt, 106 } 107 } 108 109 // SetAmount sets the amount of the fixed fee in tinybar 110 func (fee *CustomFixedFee) SetAmount(tinybar int64) *CustomFixedFee { 111 fee.Amount = tinybar 112 return fee 113 } 114 115 // GetAmount returns the amount of the fixed fee 116 func (fee *CustomFixedFee) GetAmount() Hbar { 117 return NewHbar(float64(fee.Amount)) 118 } 119 120 // SetHbarAmount sets the amount of the fixed fee in hbar 121 func (fee *CustomFixedFee) SetHbarAmount(hbar Hbar) *CustomFixedFee { 122 fee.Amount = int64(hbar.As(HbarUnits.Tinybar)) 123 fee.DenominationTokenID = nil 124 return fee 125 } 126 127 // GetHbarAmount returns the amount of the fixed fee in hbar 128 func (fee *CustomFixedFee) GetHbarAmount() Hbar { 129 return NewHbar(float64(fee.Amount)) 130 } 131 132 // SetDenominatingTokenToSameToken sets the denomination token ID to the same token as the fee 133 func (fee *CustomFixedFee) SetDenominatingTokenToSameToken() *CustomFixedFee { 134 fee.DenominationTokenID = &TokenID{0, 0, 0, nil} 135 return fee 136 } 137 138 // SetDenominatingTokenID sets the denomination token ID 139 func (fee *CustomFixedFee) SetDenominatingTokenID(id TokenID) *CustomFixedFee { 140 fee.DenominationTokenID = &id 141 return fee 142 } 143 144 // GetDenominatingTokenID returns the denomination token ID 145 func (fee *CustomFixedFee) GetDenominatingTokenID() TokenID { 146 if fee.DenominationTokenID != nil { 147 return *fee.DenominationTokenID 148 } 149 150 return TokenID{} 151 } 152 153 // SetFeeCollectorAccountID sets the account ID that will receive the custom fee 154 func (fee *CustomFixedFee) SetFeeCollectorAccountID(id AccountID) *CustomFixedFee { 155 fee.FeeCollectorAccountID = &id 156 return fee 157 } 158 159 // GetFeeCollectorAccountID returns the account ID that will receive the custom fee 160 func (fee *CustomFixedFee) GetFeeCollectorAccountID() AccountID { 161 return *fee.FeeCollectorAccountID 162 } 163 164 // SetAllCollectorsAreExempt sets whether all collectors are exempt from the custom fee 165 func (fee *CustomFixedFee) SetAllCollectorsAreExempt(exempt bool) *CustomFixedFee { 166 fee.AllCollectorsAreExempt = exempt 167 return fee 168 } 169 170 // ToBytes returns the byte representation of the CustomFixedFee 171 func (fee *CustomFixedFee) ToBytes() []byte { 172 data, err := protobuf.Marshal(fee._ToProtobuf()) 173 if err != nil { 174 return make([]byte, 0) 175 } 176 177 return data 178 } 179 180 // String returns a string representation of the CustomFixedFee 181 func (fee *CustomFixedFee) String() string { 182 if fee.DenominationTokenID != nil { 183 return fmt.Sprintf("feeCollectorAccountID: %s, amount: %d, denominatingTokenID: %s", fee.FeeCollectorAccountID.String(), fee.Amount, fee.DenominationTokenID.String()) 184 } 185 186 return fmt.Sprintf("feeCollectorAccountID: %s, amount: %d", fee.FeeCollectorAccountID.String(), fee.Amount) 187 }