/
liquidity_pool.go
190 lines (164 loc) · 5.77 KB
/
liquidity_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
package types
import (
"strconv"
"strings"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// PoolName returns unique name of the pool consists of given reserve coin denoms and type id.
func PoolName(reserveCoinDenoms []string, poolTypeID uint32) string {
return strings.Join(append(SortDenoms(reserveCoinDenoms), strconv.FormatUint(uint64(poolTypeID), 10)), "/")
}
// Name returns the pool's name.
func (pool Pool) Name() string {
return PoolName(pool.ReserveCoinDenoms, pool.TypeId)
}
// Validate validates Pool.
func (pool Pool) Validate() error {
if pool.Id == 0 {
return ErrPoolNotExists
}
if pool.TypeId == 0 {
return ErrPoolTypeNotExists
}
if pool.ReserveCoinDenoms == nil || len(pool.ReserveCoinDenoms) == 0 {
return ErrNumOfReserveCoinDenoms
}
if uint32(len(pool.ReserveCoinDenoms)) > MaxReserveCoinNum || uint32(len(pool.ReserveCoinDenoms)) < MinReserveCoinNum {
return ErrNumOfReserveCoinDenoms
}
sortedDenomA, sortedDenomB := AlphabeticalDenomPair(pool.ReserveCoinDenoms[0], pool.ReserveCoinDenoms[1])
if sortedDenomA != pool.ReserveCoinDenoms[0] || sortedDenomB != pool.ReserveCoinDenoms[1] {
return ErrBadOrderingReserveCoinDenoms
}
if pool.ReserveAccountAddress == "" {
return ErrEmptyReserveAccountAddress
}
if pool.ReserveAccountAddress != GetPoolReserveAcc(pool.Name(), false).String() {
return ErrBadReserveAccountAddress
}
if pool.PoolCoinDenom == "" {
return ErrEmptyPoolCoinDenom
}
if pool.PoolCoinDenom != pool.Name() {
return ErrBadPoolCoinDenom
}
return nil
}
// NewPoolBatch creates a new PoolBatch object.
func NewPoolBatch(poolID, batchIndex uint64) PoolBatch {
return PoolBatch{
PoolId: poolID,
Index: batchIndex,
BeginHeight: 0,
DepositMsgIndex: 1,
WithdrawMsgIndex: 1,
SwapMsgIndex: 1,
Executed: false,
}
}
// MustMarshalPool returns the Pool bytes. Panics if fails.
func MustMarshalPool(cdc codec.BinaryCodec, liquidityPool Pool) []byte {
return cdc.MustMarshal(&liquidityPool)
}
// MustUnmarshalPool returns the Pool from bytes. Panics if fails.
func MustUnmarshalPool(cdc codec.BinaryCodec, value []byte) Pool {
liquidityPool, err := UnmarshalPool(cdc, value)
if err != nil {
panic(err)
}
return liquidityPool
}
// UnmarshalPool returns the Pool from bytes.
func UnmarshalPool(cdc codec.BinaryCodec, value []byte) (liquidityPool Pool, err error) {
err = cdc.Unmarshal(value, &liquidityPool)
return liquidityPool, err
}
// GetReserveAccount returns sdk.AccAddress of the pool's reserve account.
func (pool Pool) GetReserveAccount() sdk.AccAddress {
addr, err := sdk.AccAddressFromBech32(pool.ReserveAccountAddress)
if err != nil {
panic(err)
}
return addr
}
// GetPoolCoinDenom returns the pool coin's denom.
func (pool Pool) GetPoolCoinDenom() string { return pool.PoolCoinDenom }
// GetId returns id of the pool.
func (pool Pool) GetId() uint64 { return pool.Id } //nolint:revive
// Pools is a collection of pools.
type Pools []Pool
func (pools Pools) String() (out string) {
for _, del := range pools {
out += del.String() + "\n"
}
return strings.TrimSpace(out)
}
// MustMarshalPoolBatch returns the PoolBatch bytes. Panics if fails.
func MustMarshalPoolBatch(cdc codec.BinaryCodec, poolBatch PoolBatch) []byte {
return cdc.MustMarshal(&poolBatch)
}
// UnmarshalPoolBatch returns the PoolBatch from bytes.
func UnmarshalPoolBatch(cdc codec.BinaryCodec, value []byte) (poolBatch PoolBatch, err error) {
err = cdc.Unmarshal(value, &poolBatch)
return poolBatch, err
}
// MustUnmarshalPoolBatch returns the PoolBatch from bytes. Panics if fails.
func MustUnmarshalPoolBatch(cdc codec.BinaryCodec, value []byte) PoolBatch {
poolBatch, err := UnmarshalPoolBatch(cdc, value)
if err != nil {
panic(err)
}
return poolBatch
}
// MustMarshalDepositMsgState returns the DepositMsgState bytes. Panics if fails.
func MustMarshalDepositMsgState(cdc codec.BinaryCodec, msg DepositMsgState) []byte {
return cdc.MustMarshal(&msg)
}
// UnmarshalDepositMsgState returns the DepositMsgState from bytes.
func UnmarshalDepositMsgState(cdc codec.BinaryCodec, value []byte) (msg DepositMsgState, err error) {
err = cdc.Unmarshal(value, &msg)
return msg, err
}
// MustUnmarshalDepositMsgState returns the DepositMsgState from bytes. Panics if fails.
func MustUnmarshalDepositMsgState(cdc codec.BinaryCodec, value []byte) DepositMsgState {
msg, err := UnmarshalDepositMsgState(cdc, value)
if err != nil {
panic(err)
}
return msg
}
// MustMarshalWithdrawMsgState returns the WithdrawMsgState bytes. Panics if fails.
func MustMarshalWithdrawMsgState(cdc codec.BinaryCodec, msg WithdrawMsgState) []byte {
return cdc.MustMarshal(&msg)
}
// UnmarshalWithdrawMsgState returns the WithdrawMsgState from bytes.
func UnmarshalWithdrawMsgState(cdc codec.BinaryCodec, value []byte) (msg WithdrawMsgState, err error) {
err = cdc.Unmarshal(value, &msg)
return msg, err
}
// MustUnmarshalWithdrawMsgState returns the WithdrawMsgState from bytes. Panics if fails.
func MustUnmarshalWithdrawMsgState(cdc codec.BinaryCodec, value []byte) WithdrawMsgState {
msg, err := UnmarshalWithdrawMsgState(cdc, value)
if err != nil {
panic(err)
}
return msg
}
// MustMarshalSwapMsgState returns the SwapMsgState bytes. Panics if fails.
func MustMarshalSwapMsgState(cdc codec.BinaryCodec, msg SwapMsgState) []byte {
return cdc.MustMarshal(&msg)
}
// UnmarshalSwapMsgState returns the UnmarshalSwapMsgState from bytes.
func UnmarshalSwapMsgState(cdc codec.BinaryCodec, value []byte) (msg SwapMsgState, err error) {
err = cdc.Unmarshal(value, &msg)
return msg, err
}
// MustUnmarshalSwapMsgState returns the SwapMsgState from bytes. Panics if fails.
func MustUnmarshalSwapMsgState(cdc codec.BinaryCodec, value []byte) SwapMsgState {
msg, err := UnmarshalSwapMsgState(cdc, value)
if err != nil {
panic(err)
}
return msg
}