forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pool.go
118 lines (96 loc) · 3.87 KB
/
pool.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
package stake
import (
sdk "github.com/cosmos/cosmos-sdk/types"
)
// get the bond ratio of the global state
func (p Pool) bondedRatio() sdk.Rat {
if p.TotalSupply > 0 {
return sdk.NewRat(p.BondedPool, p.TotalSupply)
}
return sdk.ZeroRat
}
// get the exchange rate of bonded token per issued share
func (p Pool) bondedShareExRate() sdk.Rat {
if p.BondedShares.IsZero() {
return sdk.OneRat
}
return sdk.NewRat(p.BondedPool).Quo(p.BondedShares)
}
// get the exchange rate of unbonded tokens held in candidates per issued share
func (p Pool) unbondedShareExRate() sdk.Rat {
if p.UnbondedShares.IsZero() {
return sdk.OneRat
}
return sdk.NewRat(p.UnbondedPool).Quo(p.UnbondedShares)
}
// move a candidates asset pool from bonded to unbonded pool
func (p Pool) bondedToUnbondedPool(candidate Candidate) (Pool, Candidate) {
// replace bonded shares with unbonded shares
p, tokens := p.removeSharesBonded(candidate.Assets)
p, candidate.Assets = p.addTokensUnbonded(tokens)
candidate.Status = Unbonded
return p, candidate
}
// move a candidates asset pool from unbonded to bonded pool
func (p Pool) unbondedToBondedPool(candidate Candidate) (Pool, Candidate) {
// replace unbonded shares with bonded shares
p, tokens := p.removeSharesUnbonded(candidate.Assets)
p, candidate.Assets = p.addTokensBonded(tokens)
candidate.Status = Bonded
return p, candidate
}
//_______________________________________________________________________
func (p Pool) addTokensBonded(amount int64) (p2 Pool, issuedShares sdk.Rat) {
issuedShares = sdk.NewRat(amount).Quo(p.bondedShareExRate()) // (tokens/shares)^-1 * tokens
p.BondedPool += amount
p.BondedShares = p.BondedShares.Add(issuedShares)
return p, issuedShares
}
func (p Pool) removeSharesBonded(shares sdk.Rat) (p2 Pool, removedTokens int64) {
removedTokens = p.bondedShareExRate().Mul(shares).Evaluate() // (tokens/shares) * shares
p.BondedShares = p.BondedShares.Sub(shares)
p.BondedPool = p.BondedPool - removedTokens
return p, removedTokens
}
func (p Pool) addTokensUnbonded(amount int64) (p2 Pool, issuedShares sdk.Rat) {
issuedShares = p.unbondedShareExRate().Inv().Mul(sdk.NewRat(amount)) // (tokens/shares)^-1 * tokens
p.UnbondedShares = p.UnbondedShares.Add(issuedShares)
p.UnbondedPool += amount
return p, issuedShares
}
func (p Pool) removeSharesUnbonded(shares sdk.Rat) (p2 Pool, removedTokens int64) {
removedTokens = p.unbondedShareExRate().Mul(shares).Evaluate() // (tokens/shares) * shares
p.UnbondedShares = p.UnbondedShares.Sub(shares)
p.UnbondedPool -= removedTokens
return p, removedTokens
}
//_______________________________________________________________________
// add tokens to a candidate
func (p Pool) candidateAddTokens(candidate Candidate,
amount int64) (p2 Pool, candidate2 Candidate, issuedDelegatorShares sdk.Rat) {
exRate := candidate.delegatorShareExRate()
var receivedGlobalShares sdk.Rat
if candidate.Status == Bonded {
p, receivedGlobalShares = p.addTokensBonded(amount)
} else {
p, receivedGlobalShares = p.addTokensUnbonded(amount)
}
candidate.Assets = candidate.Assets.Add(receivedGlobalShares)
issuedDelegatorShares = exRate.Mul(receivedGlobalShares)
candidate.Liabilities = candidate.Liabilities.Add(issuedDelegatorShares)
return p, candidate, issuedDelegatorShares
}
// remove shares from a candidate
func (p Pool) candidateRemoveShares(candidate Candidate,
shares sdk.Rat) (p2 Pool, candidate2 Candidate, createdCoins int64) {
//exRate := candidate.delegatorShareExRate() //XXX make sure not used
globalPoolSharesToRemove := candidate.delegatorShareExRate().Mul(shares)
if candidate.Status == Bonded {
p, createdCoins = p.removeSharesBonded(globalPoolSharesToRemove)
} else {
p, createdCoins = p.removeSharesUnbonded(globalPoolSharesToRemove)
}
candidate.Assets = candidate.Assets.Sub(globalPoolSharesToRemove)
candidate.Liabilities = candidate.Liabilities.Sub(shares)
return p, candidate, createdCoins
}