-
Notifications
You must be signed in to change notification settings - Fork 3
/
pool.go
191 lines (161 loc) · 4.78 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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package types
import (
"fmt"
"strconv"
"strings"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
farmingtypes "github.com/cosmosquad-labs/squad/x/farming/types"
)
var (
_ PoolI = (*PoolInfo)(nil)
// TODO: add RangedPoolInfo for v2
)
// NewPool returns a new pool object.
func NewPool(id, pairId uint64) Pool {
return Pool{
Id: id,
PairId: pairId,
ReserveAddress: PoolReserveAddress(id).String(),
PoolCoinDenom: PoolCoinDenom(id),
LastDepositRequestId: 0,
LastWithdrawRequestId: 0,
Disabled: false,
}
}
func (pool Pool) GetReserveAddress() sdk.AccAddress {
addr, err := sdk.AccAddressFromBech32(pool.ReserveAddress)
if err != nil {
panic(err)
}
return addr
}
func (pool Pool) Validate() error {
if pool.Id == 0 {
return fmt.Errorf("pool id must not be 0")
}
if pool.PairId == 0 {
return fmt.Errorf("pair id must not be 0")
}
if _, err := sdk.AccAddressFromBech32(pool.ReserveAddress); err != nil {
return fmt.Errorf("invalid reserve address %s: %w", pool.ReserveAddress, err)
}
if err := sdk.ValidateDenom(pool.PoolCoinDenom); err != nil {
return fmt.Errorf("invalid pool coin denom: %w", err)
}
return nil
}
// PoolReserveAddress returns a unique pool reserve account address for each pool.
func PoolReserveAddress(poolId uint64) sdk.AccAddress {
return farmingtypes.DeriveAddress(
AddressType,
ModuleName,
strings.Join([]string{PoolReserveAddressPrefix, strconv.FormatUint(poolId, 10)}, ModuleAddressNameSplitter),
)
}
// PoolCoinDenom returns a unique pool coin denom for a pool.
func PoolCoinDenom(poolId uint64) string {
return fmt.Sprintf("pool%d", poolId)
}
// ParsePoolCoinDenom trims pool prefix from the pool coin denom and returns pool id.
func ParsePoolCoinDenom(denom string) uint64 {
if !strings.HasPrefix(denom, "pool") {
return 0
}
poolId, err := strconv.ParseUint(strings.TrimPrefix(denom, "pool"), 10, 64)
if err != nil {
return 0
}
return poolId
}
type PoolI interface {
Balance() (rx, ry sdk.Int)
PoolCoinSupply() sdk.Int
Price() sdk.Dec
}
type PoolInfo struct {
RX, RY sdk.Int
PS sdk.Int
}
func NewPoolInfo(rx, ry, ps sdk.Int) PoolInfo {
return PoolInfo{
RX: rx,
RY: ry,
PS: ps,
}
}
func (info PoolInfo) Balance() (rx, ry sdk.Int) {
return info.RX, info.RY
}
func (info PoolInfo) PoolCoinSupply() sdk.Int {
return info.PS
}
func (info PoolInfo) Price() sdk.Dec {
if info.RX.IsZero() || info.RY.IsZero() {
panic("pool price is not defined for a depleted pool")
}
return info.RX.ToDec().Quo(info.RY.ToDec())
}
func IsDepletedPool(pool PoolI) bool {
ps := pool.PoolCoinSupply()
if ps.IsZero() {
return true
}
rx, ry := pool.Balance()
if rx.IsZero() || ry.IsZero() {
return true
}
return false
}
// DepositToPool returns accepted x amount, accepted y amount and
// minted pool coin amount.
func DepositToPool(pool PoolI, x, y sdk.Int) (ax, ay, pc sdk.Int) {
// Calculate accepted amount and minting amount.
// Note that we take as many coins as possible(by ceiling numbers)
// from depositor and mint as little coins as possible.
rx, ry := pool.Balance()
ps := pool.PoolCoinSupply().ToDec()
// pc = min(ps * (x / rx), ps * (y / ry))
pc = sdk.MinDec(
ps.MulTruncate(x.ToDec().QuoTruncate(rx.ToDec())),
ps.MulTruncate(y.ToDec().QuoTruncate(ry.ToDec())),
).TruncateInt()
mintProportion := pc.ToDec().Quo(ps) // pc / ps
ax = rx.ToDec().Mul(mintProportion).Ceil().TruncateInt() // rx * mintProportion
ay = ry.ToDec().Mul(mintProportion).Ceil().TruncateInt() // ry * mintProportion
return
}
func WithdrawFromPool(pool PoolI, pc sdk.Int, feeRate sdk.Dec) (x, y sdk.Int) {
rx, ry := pool.Balance()
ps := pool.PoolCoinSupply()
// Redeeming the last pool coin
if pc.Equal(ps) {
x = rx
y = ry
return
}
proportion := pc.ToDec().QuoTruncate(ps.ToDec()) // pc / ps
multiplier := sdk.OneDec().Sub(feeRate) // 1 - feeRate
x = rx.ToDec().MulTruncate(proportion).MulTruncate(multiplier).TruncateInt() // rx * proportion * multiplier
y = ry.ToDec().MulTruncate(proportion).MulTruncate(multiplier).TruncateInt() // ry * proportion * multiplier
return
}
// MustMarshalPool returns the pool bytes.
// It throws panic if it fails.
func MustMarshalPool(cdc codec.BinaryCodec, pool Pool) []byte {
return cdc.MustMarshal(&pool)
}
// MustUnmarshalPool return the unmarshalled pool from bytes.
// It throws panic if it fails.
func MustUnmarshalPool(cdc codec.BinaryCodec, value []byte) Pool {
pool, err := UnmarshalPool(cdc, value)
if err != nil {
panic(err)
}
return pool
}
// UnmarshalPool returns the pool from bytes.
func UnmarshalPool(cdc codec.BinaryCodec, value []byte) (pool Pool, err error) {
err = cdc.Unmarshal(value, &pool)
return pool, err
}