-
Notifications
You must be signed in to change notification settings - Fork 0
/
decimal.go
66 lines (49 loc) · 1.26 KB
/
decimal.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
package main
import (
_ "embed"
"fmt"
"math/big"
)
//go:embed decimal.move.template
var decimal_template string
type decimalInfo struct {
BaseWidth uint
ModuleNameFmt string
Address string
DoTest bool
Decimal uint
Args string
Version string
}
type decimalGenerated struct {
decimalInfo
BaseTypeName string
ModuleName string
TypeName string
UnderlyingUnsignedType string
DoubleWidthModule string
Multiplier string
SquaredMultiplier string
}
// tenTo raise 10 to the power of `d`.
func tenTo(d uint) *big.Int {
ten := big.NewInt(10)
r := big.NewInt(1)
for ; d > 0; d-- {
r.Mul(r, ten)
}
return r
}
func newDecimalGenerated(s *decimalInfo) *decimalGenerated {
r := &decimalGenerated{decimalInfo: *s}
r.BaseTypeName = fmt.Sprintf("u%d", r.BaseWidth)
r.ModuleName = fmt.Sprintf(s.ModuleNameFmt, r.BaseWidth, r.Decimal)
r.TypeName = fmt.Sprintf("Decimal%dN%d", r.BaseWidth, r.Decimal)
r.DoubleWidthModule = getDoubleWidthModuleName(r.BaseWidth * 2)
r.Multiplier = tenTo(s.Decimal).String()
r.SquaredMultiplier = tenTo(s.Decimal * 2).String()
return r
}
func (d *decimalGenerated) GenText() (string, error) {
return GenText(d.ModuleName, decimal_template, d)
}