github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/hbar.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 "math" 26 "regexp" 27 "strconv" 28 29 "github.com/pkg/errors" 30 ) 31 32 // Hbar is a typesafe wrapper around values of HBAR providing foolproof conversions to other denominations. 33 type Hbar struct { 34 tinybar int64 35 } 36 37 // MaxHbar is the maximum amount the Hbar type can wrap. 38 var MaxHbar = Hbar{math.MaxInt64} 39 40 // MinHbar is the minimum amount the Hbar type can wrap. 41 var MinHbar = Hbar{math.MinInt64} 42 43 // ZeroHbar wraps a 0 value of Hbar. 44 var ZeroHbar = Hbar{0} 45 46 // HbarFrom creates a representation of Hbar in tinybar on the unit provided 47 func HbarFrom(bars float64, unit HbarUnit) Hbar { 48 return HbarFromTinybar(int64(bars * float64(unit._NumberOfTinybar()))) 49 } 50 51 // HbarFromTinybar creates a representation of Hbar in tinybars 52 func HbarFromTinybar(tinybar int64) Hbar { 53 return Hbar{tinybar} 54 } 55 56 // NewHbar constructs a new Hbar from a possibly fractional amount of hbar. 57 func NewHbar(hbar float64) Hbar { 58 return HbarFrom(hbar, HbarUnits.Hbar) 59 } 60 61 // AsTinybar returns the equivalent tinybar amount. 62 func (hbar Hbar) AsTinybar() int64 { 63 return hbar.tinybar 64 } 65 66 // As returns the equivalent amount in the given unit. 67 func (hbar Hbar) As(unit HbarUnit) float64 { 68 return float64(hbar.tinybar) / float64(unit._NumberOfTinybar()) 69 } 70 71 // String returns a string representation of the Hbar value. 72 func (hbar Hbar) String() string { 73 // Format the string as tinybar if the value is 1000 tinybar or less 74 if -10000 <= hbar.tinybar && hbar.tinybar <= 10000 { 75 return fmt.Sprintf("%v %s", hbar.tinybar, HbarUnits.Tinybar.Symbol()) 76 } 77 78 return fmt.Sprintf("%v %s", float64(hbar.tinybar)/float64(HbarUnits.Hbar._NumberOfTinybar()), HbarUnits.Hbar.Symbol()) 79 } 80 81 // HbarFromString returns a Hbar representation of the string provided. 82 func HbarFromString(hbar string) (Hbar, error) { 83 var err error 84 match := regexp.MustCompile(`^((?:\+|\-)?\d+(?:\.\d+)?)(?: (tℏ|μℏ|mℏ|ℏ|kℏ|Mℏ|Gℏ))?$`) 85 86 matchArray := match.FindStringSubmatch(hbar) 87 if len(matchArray) == 0 { 88 return Hbar{}, errors.New("invalid number and/or symbol") 89 } 90 91 a, err := strconv.ParseFloat(matchArray[1], 64) 92 if err != nil { 93 return Hbar{}, err 94 } 95 96 return HbarFrom(a, _HbarUnitFromString(matchArray[2])), nil 97 } 98 99 func _HbarUnitFromString(symbol string) HbarUnit { 100 switch symbol { 101 case HbarUnits.Tinybar.Symbol(): 102 return HbarUnits.Tinybar 103 case HbarUnits.Microbar.Symbol(): 104 return HbarUnits.Microbar 105 case HbarUnits.Millibar.Symbol(): 106 return HbarUnits.Millibar 107 case HbarUnits.Kilobar.Symbol(): 108 return HbarUnits.Kilobar 109 case HbarUnits.Megabar.Symbol(): 110 return HbarUnits.Megabar 111 case HbarUnits.Gigabar.Symbol(): 112 return HbarUnits.Gigabar 113 default: 114 return HbarUnits.Hbar 115 } 116 } 117 118 // ToString returns a string representation of the Hbar value in the given unit. 119 func (hbar Hbar) ToString(unit HbarUnit) string { 120 return fmt.Sprintf("%v %v", float64(hbar.tinybar)/float64(unit._NumberOfTinybar()), unit.Symbol()) 121 } 122 123 // Negated returns the negated value of the Hbar. 124 func (hbar Hbar) Negated() Hbar { 125 return Hbar{ 126 tinybar: -hbar.tinybar, 127 } 128 }