-
Notifications
You must be signed in to change notification settings - Fork 631
/
Copy pathunpool_test.go
254 lines (216 loc) · 9.27 KB
/
unpool_test.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package keeper_test
import (
"time"
"github.com/cosmos/cosmos-sdk/simapp"
sdk "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/osmosis-labs/osmosis/v11/x/gamm/pool-models/balancer"
gammtypes "github.com/osmosis-labs/osmosis/v11/x/gamm/types"
"github.com/osmosis-labs/osmosis/v11/x/superfluid/keeper"
"github.com/osmosis-labs/osmosis/v11/x/superfluid/types"
)
var (
defaultSwapFee = sdk.MustNewDecFromStr("0.025")
defaultExitFee = sdk.MustNewDecFromStr("0.025")
defaultPoolParams = balancer.PoolParams{
SwapFee: defaultSwapFee,
ExitFee: defaultExitFee,
}
defaultFutureGovernor = ""
// pool assets
defaultFooAsset balancer.PoolAsset = balancer.PoolAsset{
Weight: sdk.NewInt(100),
Token: sdk.NewCoin("foo", sdk.NewInt(10000)),
}
defaultBondDenomAsset balancer.PoolAsset = balancer.PoolAsset{
Weight: sdk.NewInt(100),
Token: sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(10000)),
}
defaultPoolAssets []balancer.PoolAsset = []balancer.PoolAsset{defaultFooAsset, defaultBondDenomAsset}
defaultAcctFunds sdk.Coins = sdk.NewCoins(
sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(10000000000)),
sdk.NewCoin("uosmo", sdk.NewInt(10000000000)),
sdk.NewCoin("foo", sdk.NewInt(10000000)),
sdk.NewCoin("bar", sdk.NewInt(10000000)),
sdk.NewCoin("baz", sdk.NewInt(10000000)),
)
)
// we test unpooling in the following circumstances:
// 1. test unpooling lock that is not superfluid delegated, not unlocking
// 2. test unpooling lock that is not superfluid delegated, unlocking
// 3. test unpooling lock that is superfluid delegated, not unlocking
// 4. test unpooling lock that is superfluid undelegating, not unlocking
// 5. test unpooling lock that is superfluid undelegating, unlocking
func (suite *KeeperTestSuite) TestUnpool() {
testCases := []struct {
name string
superfluidDelegated bool
superfluidUndelegating bool
unlocking bool
}{
{
"lock that is not superfluid delegated, not unlocking",
false,
false,
false,
},
{
"lock that is not superfluid delegated, unlocking",
false,
false,
true,
},
{
"lock that is superfluid delegated, not unlocking",
true,
false,
false,
},
{
"lock that is superfluid undelegating, not unlocking",
true,
true,
false,
},
{
"lock that is superfluid undelegating, unlocking",
true,
true,
true,
},
}
for _, tc := range testCases {
tc := tc
suite.Run(tc.name, func() {
suite.SetupTest()
ctx := suite.Ctx
bankKeeper := suite.App.BankKeeper
gammKeeper := suite.App.GAMMKeeper
superfluidKeeper := suite.App.SuperfluidKeeper
lockupKeeper := suite.App.LockupKeeper
stakingKeeper := suite.App.StakingKeeper
// generate one delegator Addr, one gamm pool
delAddrs := CreateRandomAccounts(2)
poolCreateAcc := delAddrs[0]
poolJoinAcc := delAddrs[1]
for _, acc := range delAddrs {
err := simapp.FundAccount(bankKeeper, ctx, acc, defaultAcctFunds)
suite.Require().NoError(err)
}
// set up validator
valAddr := suite.SetupValidator(stakingtypes.BondStatus(stakingtypes.Bonded))
// create pool of "stake" and "foo"
msg := balancer.NewMsgCreateBalancerPool(poolCreateAcc, balancer.PoolParams{
SwapFee: sdk.NewDecWithPrec(1, 2),
ExitFee: sdk.NewDec(0),
}, defaultPoolAssets, defaultFutureGovernor)
poolId, err := gammKeeper.CreatePool(ctx, msg)
suite.Require().NoError(err)
// join pool
balanceBeforeJoin := bankKeeper.GetAllBalances(ctx, poolJoinAcc)
_, _, err = gammKeeper.JoinPoolNoSwap(ctx, poolJoinAcc, poolId, gammtypes.OneShare.MulRaw(50), sdk.Coins{})
suite.Require().NoError(err)
balanceAfterJoin := bankKeeper.GetAllBalances(ctx, poolJoinAcc)
joinPoolAmt, _ := balanceBeforeJoin.SafeSub(balanceAfterJoin)
pool, err := gammKeeper.GetPoolAndPoke(ctx, poolId)
suite.Require().NoError(err)
poolDenom := gammtypes.GetPoolShareDenom(pool.GetId())
poolShareOut := bankKeeper.GetBalance(ctx, poolJoinAcc, poolDenom)
// register a LP token as a superfluid asset
err = superfluidKeeper.AddNewSuperfluidAsset(ctx, types.SuperfluidAsset{
Denom: poolDenom,
AssetType: types.SuperfluidAssetTypeLPShare,
})
suite.Require().NoError(err)
// whitelist designated pools
// this should be done via `RunForkLogic` at upgrade
whitelistedPool := []uint64{poolId}
superfluidKeeper.SetUnpoolAllowedPools(ctx, whitelistedPool)
coinsToLock := poolShareOut
unbondingDuration := stakingKeeper.GetParams(ctx).UnbondingTime
// create lock
lockID := suite.LockTokens(poolJoinAcc, sdk.NewCoins(coinsToLock), unbondingDuration)
// settings prior to testing for superfluid delegated cases
intermediaryAcc := types.SuperfluidIntermediaryAccount{}
if tc.superfluidDelegated {
err = superfluidKeeper.SuperfluidDelegate(ctx, poolJoinAcc.String(), lockID, valAddr.String())
suite.Require().NoError(err)
intermediaryAccConnection := superfluidKeeper.GetLockIdIntermediaryAccountConnection(ctx, lockID)
intermediaryAcc = superfluidKeeper.GetIntermediaryAccount(ctx, intermediaryAccConnection)
}
// settings prior to testing for superfluid undelegating cases
if tc.superfluidUndelegating {
err = superfluidKeeper.SuperfluidUndelegate(ctx, poolJoinAcc.String(), lockID)
suite.Require().NoError(err)
}
// settings prior to testing for unlocking cases
if tc.unlocking {
// if lock was superfluid staked, we can't unlock via `BeginUnlock`,
// need to unlock lock via `SuperfluidUnbondLock`
if tc.superfluidUndelegating {
err = superfluidKeeper.SuperfluidUnbondLock(ctx, lockID, poolJoinAcc.String())
suite.Require().NoError(err)
} else {
lock, err := lockupKeeper.GetLockByID(ctx, lockID)
suite.Require().NoError(err)
err = lockupKeeper.BeginUnlock(ctx, lockID, lock.Coins)
suite.Require().NoError(err)
// add time to current time to test lock end time
ctx = ctx.WithBlockTime(ctx.BlockTime().Add(time.Hour * 24))
}
}
lock, err := lockupKeeper.GetLockByID(ctx, lockID)
suite.Require().NoError(err)
// run unpooling logic
newLockIDs, err := superfluidKeeper.UnpoolAllowedPools(ctx, poolJoinAcc, poolId, lockID)
suite.Require().NoError(err)
suite.AssertEventEmitted(ctx, gammtypes.TypeEvtPoolExited, 1)
cumulativeNewLockCoins := sdk.NewCoins()
for _, newLockId := range newLockIDs {
newLock, err := lockupKeeper.GetLockByID(ctx, newLockId)
suite.Require().NoError(err)
// check lock end time has been preserved after unpooling
// if lock wasn't unlocking, it should be initiated unlocking
// if lock was unlocking, lock end time should be preserved
if tc.unlocking {
suite.Require().Equal(lock.EndTime, newLock.EndTime)
} else {
suite.Require().Equal(ctx.BlockTime().Add(unbondingDuration), newLock.EndTime)
}
cumulativeNewLockCoins = cumulativeNewLockCoins.Add(newLock.Coins...)
}
// check if the new lock created has the same amount as pool exited
// exitPool has rounding difference,
// we test if correct amt has been exited and locked via comparing with rounding tolerance
roundingToleranceCoins := sdk.NewCoins(sdk.NewCoin(defaultFooAsset.Token.Denom, sdk.NewInt(5)), sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(5)))
roundDownTolerance, _ := joinPoolAmt.SafeSub(roundingToleranceCoins)
roundUpTolerance := joinPoolAmt.Add(roundingToleranceCoins...)
suite.Require().True(cumulativeNewLockCoins.AmountOf("foo").GTE(roundDownTolerance.AmountOf("foo")))
suite.Require().True(cumulativeNewLockCoins.AmountOf(sdk.DefaultBondDenom).GTE(roundDownTolerance.AmountOf(sdk.DefaultBondDenom)))
suite.Require().True(cumulativeNewLockCoins.AmountOf("foo").LTE(roundUpTolerance.AmountOf("foo")))
suite.Require().True(cumulativeNewLockCoins.AmountOf(sdk.DefaultBondDenom).LTE(roundUpTolerance.AmountOf(sdk.DefaultBondDenom)))
// check if old lock is deleted
_, err = lockupKeeper.GetLockByID(ctx, lockID)
suite.Require().Error(err)
// check for locks that were superfluid staked.
if tc.superfluidDelegated {
// check if unpooling deleted intermediary account connection
addr := superfluidKeeper.GetLockIdIntermediaryAccountConnection(ctx, lockID)
suite.Require().Equal(addr.String(), "")
// check bonding synthetic lockup deletion
_, err = lockupKeeper.GetSyntheticLockup(ctx, lockID, keeper.StakingSyntheticDenom(lock.Coins[0].Denom, valAddr.String()))
suite.Require().Error(err)
// check unbonding synthetic lockup creation
// unbondingDuration := stakingKeeper.GetParams(ctx).UnbondingTime
// synthLock, err := lockupKeeper.GetSyntheticLockup(ctx, lockID, keeper.UnstakingSyntheticDenom(lock.Coins[0].Denom, valAddr.String()))
// suite.Require().NoError(err)
// suite.Require().Equal(synthLock.UnderlyingLockId, lockID)
// suite.Require().Equal(synthLock.SynthDenom, keeper.UnstakingSyntheticDenom(lock.Coins[0].Denom, valAddr.String()))
// suite.Require().Equal(synthLock.EndTime, ctx.BlockTime().Add(unbondingDuration))
// check if delegation has reduced from intermediary account
delegation, found := stakingKeeper.GetDelegation(ctx, intermediaryAcc.GetAccAddress(), valAddr)
suite.Require().False(found, "expected no delegation, found delegation w/ %d shares", delegation.Shares)
}
})
}
}