-
Notifications
You must be signed in to change notification settings - Fork 201
/
pool.go
213 lines (173 loc) · 5.49 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package types
import (
"errors"
fmt "fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
)
/*
Returns the *base* denomination of a pool share token for a given poolId.
args:
poolId: the pool id number
ret:
poolDenom: the pool denomination name of the poolId
*/
func GetPoolShareBaseDenom(poolId uint64) (poolDenom string) {
return fmt.Sprintf("nibiru/pool/%d", poolId)
}
/*
Returns the *display* denomination of a pool share token for a given poolId.
Display denom means the denomination showed to the user, which could be many exponents greater than the base denom.
e.g. 1 atom is the display denom, but 10^6 uatom is the base denom.
In Nibiru, a display denom is 10^18 base denoms.
args:
poolId: the pool id number
ret:
poolDenom: the pool denomination name of the poolId
*/
func GetPoolShareDisplayDenom(poolId uint64) (poolDenom string) {
return fmt.Sprintf("NIBIRU-POOL-%d", poolId)
}
/*
Creates a new pool and sets the initial assets.
args:
poolId: the pool numeric id
poolAccountAddr: the pool's account address for holding funds
poolParams: pool configuration options
poolAssets: the initial pool assets and weights
ret:
pool: a new pool
err: error if any
*/
func NewPool(
poolId uint64,
poolAccountAddr sdk.Address,
poolParams PoolParams,
poolAssets []PoolAsset,
) (pool Pool, err error) {
pool = Pool{
Id: poolId,
Address: poolAccountAddr.String(),
PoolParams: poolParams,
PoolAssets: nil,
TotalWeight: sdk.ZeroInt(),
TotalShares: sdk.NewCoin(GetPoolShareBaseDenom(poolId), InitPoolSharesSupply),
}
err = pool.setInitialPoolAssets(poolAssets)
if err != nil {
return Pool{}, err
}
return pool, nil
}
/*
Adds tokens to a pool and updates the pool balances (i.e. liquidity).
args:
- tokensIn: the tokens to add to the pool
ret:
- numShares: the number of LP shares given to the user for the deposit
- remCoins: the number of coins remaining after the deposit
- err: error if any
*/
func (pool *Pool) AddTokensToPool(tokensIn sdk.Coins) (
numShares sdk.Int, remCoins sdk.Coins, err error,
) {
if tokensIn.Len() != len(pool.PoolAssets) {
return sdk.ZeroInt(), sdk.Coins{}, errors.New("wrong number of assets to deposit into the pool")
}
// Calculate max amount of tokensIn we can deposit into pool (no swap)
numShares, remCoins, err = pool.numSharesOutFromTokensIn(tokensIn)
if err != nil {
return sdk.ZeroInt(), sdk.Coins{}, err
}
if err := pool.incrementBalances(numShares, tokensIn.Sub(remCoins)); err != nil {
return sdk.ZeroInt(), sdk.Coins{}, err
}
return numShares, remCoins, nil
}
/*
Fetch the pool's address as an sdk.Address.
*/
func (pool Pool) GetAddress() (addr sdk.AccAddress) {
addr, err := sdk.AccAddressFromBech32(pool.Address)
if err != nil {
panic(fmt.Sprintf("could not bech32 decode address of pool with id: %d", pool.Id))
}
return addr
}
/*
Given the amount of pool shares to exit, calculates the amount of coins to exit
from the pool and modifies the pool. Accounts for an exit fee, if any, on the pool.
args:
- exitingShares: the number of pool shares to exit from the pool
*/
func (pool *Pool) ExitPool(exitingShares sdk.Int) (
exitedCoins sdk.Coins, err error,
) {
if exitingShares.GT(pool.TotalShares.Amount) {
return sdk.Coins{}, errors.New("too many shares out")
}
exitedCoins, err = pool.tokensOutFromPoolSharesIn(exitingShares)
if err != nil {
return sdk.Coins{}, err
}
// update the pool's balances
for _, exitedCoin := range exitedCoins {
err = pool.SubtractPoolAssetBalance(exitedCoin.Denom, exitedCoin.Amount)
if err != nil {
return sdk.Coins{}, err
}
}
pool.TotalShares = sdk.NewCoin(pool.TotalShares.Denom, pool.TotalShares.Amount.Sub(exitingShares))
return exitedCoins, nil
}
/*
Updates the pool's asset liquidity using the provided tokens.
args:
- tokens: the new token liquidity in the pool
ret:
- err: error if any
*/
func (pool *Pool) updatePoolAssetBalances(tokens sdk.Coins) (err error) {
// Ensures that there are no duplicate denoms, all denom's are valid,
// and amount is > 0
if len(tokens) != len(pool.PoolAssets) {
return errors.New("provided tokens do not match number of assets in pool")
}
if err = tokens.Validate(); err != nil {
return fmt.Errorf("provided coins are invalid, %v", err)
}
for _, coin := range tokens {
assetIndex, existingAsset, err := pool.getPoolAssetAndIndex(coin.Denom)
if err != nil {
return err
}
existingAsset.Token = coin
pool.PoolAssets[assetIndex].Token = coin
}
return nil
}
// setInitialPoolAssets sets the PoolAssets in the pool.
// It is only designed to be called at the pool's creation.
// If the same denom's PoolAsset exists, will return error.
// The list of PoolAssets must be sorted. This is done to enable fast searching for a PoolAsset by denomination.
func (p *Pool) setInitialPoolAssets(poolAssets []PoolAsset) (err error) {
exists := make(map[string]bool)
newTotalWeight := sdk.ZeroInt()
scaledPoolAssets := make([]PoolAsset, 0, len(poolAssets))
for _, asset := range poolAssets {
if err = asset.Validate(); err != nil {
return err
}
if exists[asset.Token.Denom] {
return fmt.Errorf("same PoolAsset already exists")
}
exists[asset.Token.Denom] = true
// Scale weight from the user provided weight to the correct internal weight
asset.Weight = asset.Weight.MulRaw(GuaranteedWeightPrecision)
scaledPoolAssets = append(scaledPoolAssets, asset)
newTotalWeight = newTotalWeight.Add(asset.Weight)
}
p.PoolAssets = scaledPoolAssets
sortPoolAssetsByDenom(p.PoolAssets)
p.TotalWeight = newTotalWeight
return nil
}