-
Notifications
You must be signed in to change notification settings - Fork 2
/
supply.go
68 lines (55 loc) · 1.65 KB
/
supply.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
package types
import (
"fmt"
"gopkg.in/yaml.v2"
sdk "github.com/hdac-io/friday/types"
"github.com/hdac-io/friday/x/supply/exported"
)
// Implements Delegation interface
var _ exported.SupplyI = Supply{}
// Supply represents a struct that passively keeps track of the total supply amounts in the network
type Supply struct {
Total sdk.Coins `json:"total" yaml:"total"` // total supply of tokens registered on the chain
}
// SetTotal sets the total supply.
func (supply Supply) SetTotal(total sdk.Coins) exported.SupplyI {
supply.Total = total
return supply
}
// GetTotal returns the supply total.
func (supply Supply) GetTotal() sdk.Coins {
return supply.Total
}
// NewSupply creates a new Supply instance
func NewSupply(total sdk.Coins) exported.SupplyI {
return Supply{total}
}
// DefaultSupply creates an empty Supply
func DefaultSupply() exported.SupplyI {
return NewSupply(sdk.NewCoins())
}
// Inflate adds coins to the total supply
func (supply Supply) Inflate(amount sdk.Coins) exported.SupplyI {
supply.Total = supply.Total.Add(amount)
return supply
}
// Deflate subtracts coins from the total supply
func (supply Supply) Deflate(amount sdk.Coins) exported.SupplyI {
supply.Total = supply.Total.Sub(amount)
return supply
}
// String returns a human readable string representation of a supplier.
func (supply Supply) String() string {
b, err := yaml.Marshal(supply)
if err != nil {
panic(err)
}
return string(b)
}
// ValidateBasic validates the Supply coins and returns error if invalid
func (supply Supply) ValidateBasic() error {
if !supply.Total.IsValid() {
return fmt.Errorf("invalid total supply: %s", supply.Total.String())
}
return nil
}