/
state_coin.go
162 lines (129 loc) · 3.22 KB
/
state_coin.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package state
import (
"github.com/MinterTeam/minter-go-node/formula"
"github.com/MinterTeam/minter-go-node/helpers"
"io"
"fmt"
"github.com/MinterTeam/minter-go-node/core/types"
"github.com/MinterTeam/minter-go-node/rlp"
"math/big"
)
// stateCoin represents a coin which is being modified.
type stateCoin struct {
symbol types.CoinSymbol
data Coin
db *StateDB
isDeleted bool
onDirty func(symbol types.CoinSymbol) // Callback method to mark a state coin newly dirty
}
type Coin struct {
Name string
Symbol types.CoinSymbol
Volume *big.Int
Crr uint
ReserveBalance *big.Int
}
func (coin Coin) String() string {
return fmt.Sprintf("%s (%s), volume: %s, reserve: %s, crr: %d", coin.Name, coin.Symbol, coin.Volume,
coin.ReserveBalance, coin.Crr)
}
func (c *stateCoin) IsToDelete() bool {
if c == nil {
return false
}
// Delete coin if reserve is less than 100 bips
if c.ReserveBalance().Cmp(helpers.BipToPip(big.NewInt(100))) == -1 {
return true
}
// Delete coin if volume is less than 1 coin
if c.Volume().Cmp(helpers.BipToPip(big.NewInt(1))) == -1 {
return true
}
// Delete coin if price of 1 coin is less than 0.0001 bip
price := formula.CalculateSaleReturn(c.Volume(), c.ReserveBalance(), c.Crr(), helpers.BipToPip(big.NewInt(1)))
minPrice := big.NewInt(100000000000000) // 0.0001 bip
if price.Cmp(minPrice) == -1 {
return true
}
return false
}
// newCoin creates a state coin.
func newCoin(db *StateDB, symbol types.CoinSymbol, data Coin, onDirty func(symbol types.CoinSymbol)) *stateCoin {
coin := &stateCoin{
db: db,
symbol: symbol,
data: data,
onDirty: onDirty,
}
coin.onDirty(coin.symbol)
return coin
}
// EncodeRLP implements rlp.Encoder.
func (c *stateCoin) EncodeRLP(w io.Writer) error {
return rlp.Encode(w, c.data)
}
func (c *stateCoin) AddVolume(amount *big.Int) {
if amount.Sign() == 0 {
return
}
c.SetVolume(new(big.Int).Add(c.Volume(), amount))
}
func (c *stateCoin) SubVolume(amount *big.Int) {
if amount.Sign() == 0 {
return
}
c.SetVolume(new(big.Int).Sub(c.Volume(), amount))
}
func (c *stateCoin) SetVolume(amount *big.Int) {
c.setVolume(amount)
}
func (c *stateCoin) setVolume(amount *big.Int) {
c.data.Volume = amount
if c.onDirty != nil {
c.onDirty(c.Symbol())
c.onDirty = nil
}
}
func (c *stateCoin) AddReserve(amount *big.Int) {
if amount.Sign() == 0 {
return
}
c.SetReserve(new(big.Int).Add(c.ReserveBalance(), amount))
}
func (c *stateCoin) SubReserve(amount *big.Int) {
if amount.Sign() == 0 {
return
}
c.SetReserve(new(big.Int).Sub(c.ReserveBalance(), amount))
}
func (c *stateCoin) SetReserve(amount *big.Int) {
c.setReserve(amount)
}
func (c *stateCoin) setReserve(amount *big.Int) {
c.data.ReserveBalance = amount
if c.onDirty != nil {
c.onDirty(c.Symbol())
c.onDirty = nil
}
}
//
// Attribute accessors
//
func (c *stateCoin) Symbol() types.CoinSymbol {
return c.symbol
}
func (c *stateCoin) Data() Coin {
return c.data
}
func (c *stateCoin) Volume() *big.Int {
return c.data.Volume
}
func (c *stateCoin) ReserveBalance() *big.Int {
return c.data.ReserveBalance
}
func (c *stateCoin) Crr() uint {
return c.data.Crr
}
func (c *stateCoin) Name() string {
return c.data.Name
}