github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/tequilapi/contract/tokens.go (about) 1 /* 2 * Copyright (C) 2022 The "MysteriumNetwork/node" Authors. 3 * 4 * This program is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 3 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 package contract 19 20 import ( 21 "math/big" 22 23 "github.com/mysteriumnetwork/payments/crypto" 24 "github.com/shopspring/decimal" 25 ) 26 27 // TokensZero zero amount 28 var TokensZero = Tokens{Wei: "0", Ether: "0", Human: "0"} 29 30 // HumanPrecision default human ether amount precision 31 const HumanPrecision = 6 32 33 // Tokens a common response for ethereum blockchain monetary amount 34 type Tokens struct { 35 Wei string `json:"wei"` 36 Ether string `json:"ether"` 37 Human string `json:"human"` 38 } 39 40 // NewTokens convenience constructor for Tokens 41 func NewTokens(amount *big.Int) Tokens { 42 if amount == nil { 43 return TokensZero 44 } 45 46 ethers := crypto.BigMystToDecimal(amount) 47 48 return Tokens{ 49 Wei: amount.String(), 50 Ether: ethers.String(), 51 Human: ethers.Truncate(HumanPrecision).String(), 52 } 53 } 54 55 // NewTokensFromDecimal convenience constructor for Tokens 56 func NewTokensFromDecimal(amount decimal.Decimal) Tokens { 57 return Tokens{ 58 Wei: crypto.DecimalToBigMyst(amount).String(), 59 Ether: amount.String(), 60 Human: amount.Truncate(HumanPrecision).String(), 61 } 62 } 63 64 func (t Tokens) String() string { 65 return t.Human 66 }