/
utils.go
135 lines (121 loc) · 3.61 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// SPDX-License-Identifier: LGPL-3.0-or-later
// Copyright 2019 DNA Dev team
//
/*
* Copyright (C) 2018 The ontology Authors
* This file is part of The ontology library.
*
* The ontology is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The ontology is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with The ontology. If not, see <http://www.gnu.org/licenses/>.
*/
package utils
import (
"bytes"
"encoding/json"
"fmt"
"github.com/DNAProject/DNA/common/constants"
"io/ioutil"
"math"
"math/big"
"os"
"strings"
)
const (
PRECISION_ONG = 9
PRECISION_ONT = 0
)
//FormatAssetAmount return asset amount multiplied by math.Pow10(precision) to raw float string
//For example 1000000000123456789 => 1000000000.123456789
func FormatAssetAmount(amount uint64, precision byte) string {
if precision == 0 {
return fmt.Sprintf("%d", amount)
}
divisor := math.Pow10(int(precision))
intPart := amount / uint64(divisor)
fracPart := amount - intPart*uint64(divisor)
if fracPart == 0 {
return fmt.Sprintf("%d", intPart)
}
bf := new(big.Float).SetUint64(fracPart)
bf.Quo(bf, new(big.Float).SetFloat64(math.Pow10(int(precision))))
bf.Add(bf, new(big.Float).SetUint64(intPart))
return bf.Text('f', -1)
}
//ParseAssetAmount return raw float string to uint64 multiplied by math.Pow10(precision)
//For example 1000000000.123456789 => 1000000000123456789
func ParseAssetAmount(rawAmount string, precision byte) uint64 {
bf, ok := new(big.Float).SetString(rawAmount)
if !ok {
return 0
}
bf.Mul(bf, new(big.Float).SetFloat64(math.Pow10(int(precision))))
amount, _ := bf.Uint64()
return amount
}
func FormatOng(amount uint64) string {
return FormatAssetAmount(amount, PRECISION_ONG)
}
func ParseOng(rawAmount string) uint64 {
return ParseAssetAmount(rawAmount, PRECISION_ONG)
}
func FormatOnt(amount uint64) string {
return FormatAssetAmount(amount, PRECISION_ONT)
}
func ParseOnt(rawAmount string) uint64 {
return ParseAssetAmount(rawAmount, PRECISION_ONT)
}
func CheckAssetAmount(asset string, amount uint64) error {
switch strings.ToLower(asset) {
case "gas":
if amount > constants.GAS_TOTAL_SUPPLY {
return fmt.Errorf("amount:%d larger than GAS total supply:%d", amount, constants.GAS_TOTAL_SUPPLY)
}
default:
return fmt.Errorf("unknown asset:%s", asset)
}
return nil
}
func GetJsonObjectFromFile(filePath string, jsonObject interface{}) error {
data, err := ioutil.ReadFile(filePath)
if err != nil {
return err
}
// Remove the UTF-8 Byte Order Mark
data = bytes.TrimPrefix(data, []byte("\xef\xbb\xbf"))
err = json.Unmarshal(data, jsonObject)
if err != nil {
return fmt.Errorf("json.Unmarshal %s error:%s", data, err)
}
return nil
}
func GetStoreDirPath(dataDir, networkName string) string {
return dataDir + string(os.PathSeparator) + networkName
}
func GenExportBlocksFileName(name string, start, end uint32) string {
index := strings.LastIndex(name, ".")
fileName := ""
fileExt := ""
if index < 0 {
fileName = name
} else {
fileName = name[0:index]
if index < len(name)-1 {
fileExt = name[index+1:]
}
}
fileName = fmt.Sprintf("%s_%d_%d", fileName, start, end)
if index > 0 {
fileName = fileName + "." + fileExt
}
return fileName
}