-
Notifications
You must be signed in to change notification settings - Fork 352
/
liquidation.go
70 lines (60 loc) · 1.46 KB
/
liquidation.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
package types
import (
"sort"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// ValuationMap holds the USD value of various coin types
type ValuationMap struct {
Usd map[string]sdk.Dec
}
// NewValuationMap returns a new instance of ValuationMap
func NewValuationMap() ValuationMap {
return ValuationMap{
Usd: make(map[string]sdk.Dec),
}
}
// Get returns the USD value for a specific denom
func (m ValuationMap) Get(denom string) sdk.Dec {
return m.Usd[denom]
}
// SetZero sets the USD value for a specific denom to 0
func (m ValuationMap) SetZero(denom string) {
m.Usd[denom] = sdk.ZeroDec()
}
// Increment increments the USD value of a denom
func (m ValuationMap) Increment(denom string, amount sdk.Dec) {
_, ok := m.Usd[denom]
if !ok {
m.Usd[denom] = amount
return
}
m.Usd[denom] = m.Usd[denom].Add(amount)
}
// Decrement decrements the USD value of a denom
func (m ValuationMap) Decrement(denom string, amount sdk.Dec) {
_, ok := m.Usd[denom]
if !ok {
m.Usd[denom] = amount
return
}
m.Usd[denom] = m.Usd[denom].Sub(amount)
}
// Sum returns the total USD value of all coins in the map
func (m ValuationMap) Sum() sdk.Dec {
sum := sdk.ZeroDec()
for _, v := range m.Usd {
sum = sum.Add(v)
}
return sum
}
// GetSortedKeys returns an array of the map's keys in alphabetical order
func (m ValuationMap) GetSortedKeys() []string {
keys := make([]string, len(m.Usd))
i := 0
for k := range m.Usd {
keys[i] = k
i++
}
sort.Strings(keys)
return keys
}