From eed396f8b06184844bfb6e1465b43362ae2c173c Mon Sep 17 00:00:00 2001 From: Nicolas Lara Date: Fri, 3 Mar 2023 19:37:45 +0100 Subject: [PATCH] Add unlocking lock id to BeginUnlocking response (#4489) * added unlocking lock id * added changelog (cherry picked from commit d5f8210a2b8b1ba090ae91a024c2a7b7687f9043) # Conflicts: # x/superfluid/keeper/migrate_test.go --- CHANGELOG.md | 6 + proto/osmosis/lockup/tx.proto | 5 +- x/lockup/keeper/iterator.go | 2 +- x/lockup/keeper/lock.go | 12 +- x/lockup/keeper/lock_test.go | 10 +- x/lockup/keeper/msg_server.go | 4 +- x/lockup/keeper/msg_server_test.go | 11 +- x/lockup/types/tx.pb.go | 118 ++++++---- x/superfluid/keeper/edge_case_test.go | 2 +- x/superfluid/keeper/migrate_test.go | 259 +++++++++++++++++++++ x/superfluid/keeper/unpool_test.go | 2 +- x/valset-pref/keeper_test.go | 2 +- x/valset-pref/types/expected_interfaces.go | 2 +- 13 files changed, 377 insertions(+), 58 deletions(-) create mode 100644 x/superfluid/keeper/migrate_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index d05b143a962..c5649dd6088 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -40,6 +40,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [Unreleased] + +### API Breaks + + * [#4489](https://github.com/osmosis-labs/osmosis/pull/4489) Add unlockingLockId to BeginUnlocking response + ## v15.0.0 This release containts the following new modules: diff --git a/proto/osmosis/lockup/tx.proto b/proto/osmosis/lockup/tx.proto index e50b682bfef..17dad239763 100644 --- a/proto/osmosis/lockup/tx.proto +++ b/proto/osmosis/lockup/tx.proto @@ -51,7 +51,10 @@ message MsgBeginUnlocking { (gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins" ]; } -message MsgBeginUnlockingResponse { bool success = 1; } +message MsgBeginUnlockingResponse { + bool success = 1; + uint64 unlockingLockID = 2; +} // MsgExtendLockup extends the existing lockup's duration. // The new duration is longer than the original. diff --git a/x/lockup/keeper/iterator.go b/x/lockup/keeper/iterator.go index bde6c6f6891..31a22088b2d 100644 --- a/x/lockup/keeper/iterator.go +++ b/x/lockup/keeper/iterator.go @@ -218,7 +218,7 @@ func (k Keeper) beginUnlockFromIterator(ctx sdk.Context, iterator db.Iterator) ( locks := k.getLocksFromIterator(ctx, iterator) for _, lock := range locks { - err := k.BeginUnlock(ctx, lock.ID, nil) + _, err := k.BeginUnlock(ctx, lock.ID, nil) if err != nil { return locks, err } diff --git a/x/lockup/keeper/lock.go b/x/lockup/keeper/lock.go index f95df27170e..77ecf69a39b 100644 --- a/x/lockup/keeper/lock.go +++ b/x/lockup/keeper/lock.go @@ -161,19 +161,19 @@ func (k Keeper) lock(ctx sdk.Context, lock types.PeriodLock, tokensToLock sdk.Co // BeginUnlock is a utility to start unlocking coins from NotUnlocking queue. // Returns an error if the lock has a synthetic lock. -func (k Keeper) BeginUnlock(ctx sdk.Context, lockID uint64, coins sdk.Coins) error { +func (k Keeper) BeginUnlock(ctx sdk.Context, lockID uint64, coins sdk.Coins) (uint64, error) { // prohibit BeginUnlock if synthetic locks are referring to this // TODO: In the future, make synthetic locks only get partial restrictions on the main lock. lock, err := k.GetLockByID(ctx, lockID) if err != nil { - return err + return 0, err } if k.HasAnySyntheticLockups(ctx, lock.ID) { - return fmt.Errorf("cannot BeginUnlocking a lock with synthetic lockup") + return 0, fmt.Errorf("cannot BeginUnlocking a lock with synthetic lockup") } - _, err = k.beginUnlock(ctx, *lock, coins) - return err + unlockingLock, err := k.beginUnlock(ctx, *lock, coins) + return unlockingLock, err } // BeginForceUnlock begins force unlock of the given lock. @@ -356,7 +356,7 @@ func (k Keeper) ForceUnlock(ctx sdk.Context, lock types.PeriodLock) error { } if !lock.IsUnlocking() { - err := k.BeginUnlock(ctx, lock.ID, nil) + _, err := k.BeginUnlock(ctx, lock.ID, nil) if err != nil { return err } diff --git a/x/lockup/keeper/lock_test.go b/x/lockup/keeper/lock_test.go index c427d4879f5..1bfc37ba817 100644 --- a/x/lockup/keeper/lock_test.go +++ b/x/lockup/keeper/lock_test.go @@ -97,7 +97,7 @@ func (suite *KeeperTestSuite) TestBeginForceUnlock() { if tc.expectSameLockID { suite.Require().Equal(lockID, lock.ID) } else { - suite.Require().Equal(lockID, lock.ID + 1) + suite.Require().Equal(lockID, lock.ID+1) } // new or updated lock @@ -139,6 +139,7 @@ func (suite *KeeperTestSuite) TestUnlock() { passedTime time.Duration expectedUnlockMaturedLockPass bool balanceAfterUnlock sdk.Coins + isPartial bool }{ { name: "normal unlocking case", @@ -177,6 +178,7 @@ func (suite *KeeperTestSuite) TestUnlock() { passedTime: time.Second, expectedUnlockMaturedLockPass: true, balanceAfterUnlock: sdk.Coins{sdk.NewInt64Coin("stake", 5)}, + isPartial: true, }, { name: "partial unlocking unknown tokens", @@ -213,11 +215,15 @@ func (suite *KeeperTestSuite) TestUnlock() { partialUnlocking := tc.unlockingCoins.IsAllLT(initialLockCoins) && tc.unlockingCoins != nil // begin unlocking - err = lockupKeeper.BeginUnlock(ctx, lock.ID, tc.unlockingCoins) + unlockingLock, err := lockupKeeper.BeginUnlock(ctx, lock.ID, tc.unlockingCoins) if tc.expectedBeginUnlockPass { suite.Require().NoError(err) + if tc.isPartial { + suite.Require().Equal(unlockingLock, lock.ID+1) + } + // check unlocking coins. When a lock is a partial lock // (i.e. tc.unlockingCoins is not nit and less than initialLockCoins), // we only unlock the partial amount of tc.unlockingCoins diff --git a/x/lockup/keeper/msg_server.go b/x/lockup/keeper/msg_server.go index 97104bbfc8d..c9ef08f1f07 100644 --- a/x/lockup/keeper/msg_server.go +++ b/x/lockup/keeper/msg_server.go @@ -91,14 +91,14 @@ func (server msgServer) BeginUnlocking(goCtx context.Context, msg *types.MsgBegi return nil, sdkerrors.Wrap(types.ErrNotLockOwner, fmt.Sprintf("msg sender (%s) and lock owner (%s) does not match", msg.Owner, lock.Owner)) } - err = server.keeper.BeginUnlock(ctx, lock.ID, msg.Coins) + unlockingLock, err := server.keeper.BeginUnlock(ctx, lock.ID, msg.Coins) if err != nil { return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, err.Error()) } // N.B. begin unlock event is emitted downstream in the keeper method. - return &types.MsgBeginUnlockingResponse{}, nil + return &types.MsgBeginUnlockingResponse{Success: true, UnlockingLockID: unlockingLock}, nil } // BeginUnlockingAll begins unlocking for all the locks that the account has by iterating all the not-unlocking locks the account holds. diff --git a/x/lockup/keeper/msg_server_test.go b/x/lockup/keeper/msg_server_test.go index c0a311876d7..a8331e52bad 100644 --- a/x/lockup/keeper/msg_server_test.go +++ b/x/lockup/keeper/msg_server_test.go @@ -115,6 +115,7 @@ func (suite *KeeperTestSuite) TestMsgBeginUnlocking() { name string param param expectPass bool + isPartial bool }{ { name: "unlock full amount of tokens via begin unlock", @@ -138,6 +139,7 @@ func (suite *KeeperTestSuite) TestMsgBeginUnlocking() { duration: time.Second, coinsInOwnerAddress: sdk.Coins{sdk.NewInt64Coin("stake", 10)}, }, + isPartial: true, expectPass: true, }, { @@ -163,6 +165,7 @@ func (suite *KeeperTestSuite) TestMsgBeginUnlocking() { coinsInOwnerAddress: sdk.Coins{sdk.NewInt64Coin("stake", 10)}, }, expectPass: false, + isPartial: true, }, } @@ -181,11 +184,17 @@ func (suite *KeeperTestSuite) TestMsgBeginUnlocking() { suite.Require().NoError(err) } - _, err = msgServer.BeginUnlocking(goCtx, types.NewMsgBeginUnlocking(test.param.lockOwner, resp.ID, test.param.coinsToUnlock)) + unlockingResponse, err := msgServer.BeginUnlocking(goCtx, types.NewMsgBeginUnlocking(test.param.lockOwner, resp.ID, test.param.coinsToUnlock)) if test.expectPass { suite.Require().NoError(err) suite.AssertEventEmitted(suite.Ctx, types.TypeEvtBeginUnlock, 1) + suite.Require().True(unlockingResponse.Success) + if test.isPartial { + suite.Require().Equal(unlockingResponse.UnlockingLockID, resp.ID+1) + } else { + suite.Require().Equal(unlockingResponse.UnlockingLockID, resp.ID) + } } else { suite.Require().Error(err) suite.AssertEventEmitted(suite.Ctx, types.TypeEvtBeginUnlock, 0) diff --git a/x/lockup/types/tx.pb.go b/x/lockup/types/tx.pb.go index 6371921928e..9cc91856655 100644 --- a/x/lockup/types/tx.pb.go +++ b/x/lockup/types/tx.pb.go @@ -288,7 +288,8 @@ func (m *MsgBeginUnlocking) GetCoins() github_com_cosmos_cosmos_sdk_types.Coins } type MsgBeginUnlockingResponse struct { - Success bool `protobuf:"varint,1,opt,name=success,proto3" json:"success,omitempty"` + Success bool `protobuf:"varint,1,opt,name=success,proto3" json:"success,omitempty"` + UnlockingLockID uint64 `protobuf:"varint,2,opt,name=unlockingLockID,proto3" json:"unlockingLockID,omitempty"` } func (m *MsgBeginUnlockingResponse) Reset() { *m = MsgBeginUnlockingResponse{} } @@ -331,6 +332,13 @@ func (m *MsgBeginUnlockingResponse) GetSuccess() bool { return false } +func (m *MsgBeginUnlockingResponse) GetUnlockingLockID() uint64 { + if m != nil { + return m.UnlockingLockID + } + return 0 +} + // MsgExtendLockup extends the existing lockup's duration. // The new duration is longer than the original. type MsgExtendLockup struct { @@ -562,47 +570,48 @@ func init() { func init() { proto.RegisterFile("osmosis/lockup/tx.proto", fileDescriptor_bcdad5af0d24735f) } var fileDescriptor_bcdad5af0d24735f = []byte{ - // 632 bytes of a gzipped FileDescriptorProto + // 645 bytes of a gzipped FileDescriptorProto 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xcc, 0x55, 0xcf, 0x6e, 0xd3, 0x4e, - 0x10, 0x8e, 0x93, 0x5f, 0x7f, 0x2d, 0xd3, 0x92, 0x52, 0xab, 0xd0, 0xd4, 0x02, 0x3b, 0x58, 0xd0, - 0x06, 0xa9, 0xf5, 0x92, 0x94, 0x5e, 0x38, 0x20, 0x11, 0x02, 0x52, 0xa5, 0x46, 0x42, 0x56, 0x2b, - 0x21, 0x0e, 0x48, 0xb6, 0xb3, 0x6c, 0xad, 0x38, 0x5e, 0x2b, 0x6b, 0x97, 0xe4, 0xce, 0x03, 0x70, - 0xe4, 0x19, 0x40, 0xe2, 0xc2, 0x4b, 0xf4, 0x58, 0x89, 0x0b, 0xa7, 0x14, 0x25, 0x37, 0x8e, 0x7d, - 0x02, 0xe4, 0x75, 0x6c, 0x39, 0x7f, 0xd4, 0x44, 0x48, 0x20, 0x4e, 0xce, 0xfa, 0x9b, 0xf9, 0x66, - 0xbe, 0x6f, 0x67, 0x62, 0xd8, 0xa0, 0xac, 0x45, 0x99, 0xcd, 0x90, 0x43, 0xad, 0x66, 0xe0, 0x21, - 0xbf, 0xa3, 0x79, 0x6d, 0xea, 0x53, 0x31, 0x3f, 0x04, 0xb4, 0x08, 0x90, 0xd6, 0x09, 0x25, 0x94, - 0x43, 0x28, 0xfc, 0x15, 0x45, 0x49, 0x32, 0xa1, 0x94, 0x38, 0x18, 0xf1, 0x93, 0x19, 0xbc, 0x45, - 0x8d, 0xa0, 0x6d, 0xf8, 0x36, 0x75, 0x63, 0xdc, 0xe2, 0x34, 0xc8, 0x34, 0x18, 0x46, 0xa7, 0x65, - 0x13, 0xfb, 0x46, 0x19, 0x59, 0xd4, 0x8e, 0xf1, 0xcd, 0xb1, 0xf2, 0xe1, 0x23, 0x82, 0xd4, 0xf7, - 0x59, 0xb8, 0x5e, 0x67, 0xe4, 0x90, 0x5a, 0xcd, 0x23, 0xda, 0xc4, 0x2e, 0x13, 0xb7, 0x60, 0x81, - 0xbe, 0x73, 0x71, 0xbb, 0x20, 0x14, 0x85, 0xd2, 0xb5, 0xea, 0x8d, 0xcb, 0x9e, 0xb2, 0xd2, 0x35, - 0x5a, 0xce, 0x63, 0x95, 0xbf, 0x56, 0xf5, 0x08, 0x16, 0x4f, 0x60, 0x29, 0x6e, 0xa3, 0x90, 0x2d, - 0x0a, 0xa5, 0xe5, 0xca, 0xa6, 0x16, 0xf5, 0xa9, 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aa, 0x21, 0xd7, 0xae, 0x63, 0x98, 0x2c, 0x3e, 0xa0, 0xd3, 0xf2, 0x3e, 0xea, - 0x24, 0x9f, 0xc8, 0x70, 0xc8, 0xcc, 0xff, 0xf9, 0x2a, 0xee, 0xfd, 0x0a, 0x00, 0x00, 0xff, 0xff, - 0xa5, 0x7e, 0xe5, 0x1f, 0x41, 0x07, 0x00, 0x00, + 0x10, 0x8e, 0x93, 0x5f, 0x7f, 0x2d, 0xd3, 0x92, 0x52, 0xab, 0xd0, 0xd4, 0x02, 0xbb, 0x58, 0xd0, + 0x06, 0xa9, 0xf5, 0x92, 0x14, 0x2e, 0x1c, 0x90, 0x08, 0x05, 0xa9, 0x52, 0x23, 0x21, 0xab, 0x95, + 0x10, 0x07, 0x90, 0xed, 0x2e, 0x5b, 0x2b, 0x8e, 0xd7, 0xca, 0xda, 0xa5, 0xb9, 0xf3, 0x00, 0x1c, + 0x79, 0x06, 0x90, 0xb8, 0xf0, 0x12, 0x3d, 0x56, 0xe2, 0xc2, 0x29, 0x45, 0xc9, 0x8d, 0x63, 0x9f, + 0x00, 0x79, 0x9d, 0xb5, 0x9c, 0x3f, 0x6a, 0x22, 0x24, 0x10, 0x27, 0x67, 0xf7, 0x9b, 0xf9, 0xe6, + 0x9b, 0xcf, 0x33, 0x31, 0xac, 0x50, 0xd6, 0xa4, 0xcc, 0x65, 0xc8, 0xa3, 0x4e, 0x23, 0x0a, 0x50, + 0x78, 0x62, 0x04, 0x2d, 0x1a, 0x52, 0xb9, 0xd8, 0x07, 0x8c, 0x04, 0x50, 0x96, 0x09, 0x25, 0x94, + 0x43, 0x28, 0xfe, 0x95, 0x44, 0x29, 0x2a, 0xa1, 0x94, 0x78, 0x18, 0xf1, 0x93, 0x1d, 0xbd, 0x45, + 0x87, 0x51, 0xcb, 0x0a, 0x5d, 0xea, 0x0b, 0xdc, 0xe1, 0x34, 0xc8, 0xb6, 0x18, 0x46, 0xc7, 0x15, + 0x1b, 0x87, 0x56, 0x05,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ce, 0x98, + 0xdc, 0x91, 0x28, 0x65, 0x73, 0x9a, 0xa8, 0xb4, 0xd0, 0x6b, 0x28, 0x0e, 0xed, 0xe7, 0xed, 0x89, + 0xf9, 0xca, 0xbd, 0x89, 0x21, 0x29, 0xff, 0x4b, 0x58, 0x18, 0x98, 0x76, 0x6d, 0x4c, 0x6a, 0x36, + 0x40, 0xd9, 0x98, 0x10, 0x90, 0x32, 0x1f, 0xc0, 0x7c, 0x76, 0xb8, 0xd4, 0x31, 0x79, 0x19, 0x5c, + 0x59, 0xbf, 0x1c, 0x17, 0xb4, 0xb5, 0xbd, 0xd3, 0xae, 0x2a, 0x9d, 0x75, 0x55, 0xe9, 0x47, 0x57, + 0x95, 0x3e, 0xf4, 0xd4, 0xdc, 0x59, 0x4f, 0xcd, 0x7d, 0xef, 0xa9, 0xb9, 0x57, 0xd5, 0xcc, 0x50, + 0xf5, 0xb9, 0xb6, 0x3c, 0xcb, 0x66, 0xe2, 0x80, 0x8e, 0x2b, 0x0f, 0xd1, 0x49, 0xfa, 0x31, 0x8d, + 0x87, 0xcc, 0xfe, 0x9f, 0xaf, 0xe2, 0xf6, 0xaf, 0x00, 0x00, 0x00, 0xff, 0xff, 0xc7, 0xb7, 0x60, + 0xef, 0x6b, 0x07, 0x00, 0x00, } // Reference imports to suppress errors if they are not otherwise used. @@ -1053,6 +1062,11 @@ func (m *MsgBeginUnlockingResponse) MarshalToSizedBuffer(dAtA []byte) (int, erro _ = i var l int _ = l + if m.UnlockingLockID != 0 { + i = encodeVarintTx(dAtA, i, uint64(m.UnlockingLockID)) + i-- + dAtA[i] = 0x10 + } if m.Success { i-- if m.Success { @@ -1327,6 +1341,9 @@ func (m *MsgBeginUnlockingResponse) Size() (n int) { if m.Success { n += 2 } + if m.UnlockingLockID != 0 { + n += 1 + sovTx(uint64(m.UnlockingLockID)) + } return n } @@ -1968,6 +1985,25 @@ func (m *MsgBeginUnlockingResponse) Unmarshal(dAtA []byte) error { } } m.Success = bool(v != 0) + case 2: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field UnlockingLockID", wireType) + } + m.UnlockingLockID = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTx + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.UnlockingLockID |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } default: iNdEx = preIndex skippy, err := skipTx(dAtA[iNdEx:]) diff --git a/x/superfluid/keeper/edge_case_test.go b/x/superfluid/keeper/edge_case_test.go index 3c04f84c66c..06aa03d53a7 100644 --- a/x/superfluid/keeper/edge_case_test.go +++ b/x/superfluid/keeper/edge_case_test.go @@ -136,7 +136,7 @@ func (suite *KeeperTestSuite) TestTryUnbondingSuperfluidLockupDirectly() { _, _, locks := suite.setupSuperfluidDelegations(valAddrs, tc.superDelegations, denoms) for _, lock := range locks { - err := suite.App.LockupKeeper.BeginUnlock(suite.Ctx, lock.ID, sdk.Coins{}) + _, err := suite.App.LockupKeeper.BeginUnlock(suite.Ctx, lock.ID, sdk.Coins{}) suite.Require().Error(err) } }) diff --git a/x/superfluid/keeper/migrate_test.go b/x/superfluid/keeper/migrate_test.go new file mode 100644 index 00000000000..205e679fb61 --- /dev/null +++ b/x/superfluid/keeper/migrate_test.go @@ -0,0 +1,259 @@ +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" + + cl "github.com/osmosis-labs/osmosis/v15/x/concentrated-liquidity" + + "github.com/osmosis-labs/osmosis/osmomath" + "github.com/osmosis-labs/osmosis/v15/x/gamm/pool-models/balancer" + gammtypes "github.com/osmosis-labs/osmosis/v15/x/gamm/types" + "github.com/osmosis-labs/osmosis/v15/x/superfluid/keeper" + "github.com/osmosis-labs/osmosis/v15/x/superfluid/types" +) + +// We test migrating in the following circumstances: +// 1. Migrating lock that is not superfluid delegated, not unlocking. +// 2. Migrating lock that is not superfluid delegated, unlocking. +// 3. Migrating lock that is superfluid delegated, not unlocking. +// 4. Migrating lock that is superfluid undelegating, not unlocking. +// 5. Migrating lock that is superfluid undelegating, unlocking. +func (suite *KeeperTestSuite) TestUnlockAndMigrate() { + testCases := []struct { + name string + superfluidDelegated bool + superfluidUndelegating bool + unlocking bool + percentOfSharesToMigrate sdk.Dec + }{ + { + "lock that is not superfluid delegated, not unlocking", + false, + false, + false, + sdk.MustNewDecFromStr("0.9"), + }, + { + "lock that is not superfluid delegated, unlocking", + false, + false, + true, + sdk.MustNewDecFromStr("0.6"), + }, + { + "lock that is superfluid delegated, not unlocking", + true, + false, + false, + sdk.MustNewDecFromStr("1"), + }, + { + "lock that is superfluid undelegating, not unlocking", + true, + true, + false, + sdk.MustNewDecFromStr("0.5"), + }, + { + "lock that is superfluid undelegating, unlocking", + true, + true, + true, + sdk.MustNewDecFromStr("0.3"), + }, + } + + 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 + poolmanagerKeeper := suite.App.PoolManagerKeeper + + // Generate and fund two accounts. + // Account 1 will be the account that creates the pool. + // Account 2 will be the account that joins the 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 a single validator. + valAddr := suite.SetupValidator(stakingtypes.BondStatus(stakingtypes.Bonded)) + + // Create a balancer pool of "stake" and "foo". + msg := balancer.NewMsgCreateBalancerPool(poolCreateAcc, balancer.PoolParams{ + SwapFee: sdk.NewDecWithPrec(1, 2), + ExitFee: sdk.NewDec(0), + }, defaultPoolAssets, defaultFutureGovernor) + balancerPooId, err := poolmanagerKeeper.CreatePool(ctx, msg) + suite.Require().NoError(err) + + // Join the balancer pool. + // Note the account balance before and after joining the pool. + balanceBeforeJoin := bankKeeper.GetAllBalances(ctx, poolJoinAcc) + _, _, err = gammKeeper.JoinPoolNoSwap(ctx, poolJoinAcc, balancerPooId, gammtypes.OneShare.MulRaw(50), sdk.Coins{}) + suite.Require().NoError(err) + balanceAfterJoin := bankKeeper.GetAllBalances(ctx, poolJoinAcc) + + // The balancer join pool amount is the difference between the account balance before and after joining the pool. + joinPoolAmt, _ := balanceBeforeJoin.SafeSub(balanceAfterJoin) + + // Determine the pool's LP token denomination. + balancerPool, err := gammKeeper.GetPoolAndPoke(ctx, balancerPooId) + suite.Require().NoError(err) + poolDenom := gammtypes.GetPoolShareDenom(balancerPool.GetId()) + + // Register the LP token as a superfluid asset + err = superfluidKeeper.AddNewSuperfluidAsset(ctx, types.SuperfluidAsset{ + Denom: poolDenom, + AssetType: types.SuperfluidAssetTypeLPShare, + }) + suite.Require().NoError(err) + + // Note how much of the LP token the account that joined the pool has. + poolShareOut := bankKeeper.GetBalance(ctx, poolJoinAcc, poolDenom) + + // Create a cl pool with the same underlying assets as the balancer pool. + clPool := suite.PrepareCustomConcentratedPool(poolCreateAcc, defaultPoolAssets[0].Token.Denom, defaultPoolAssets[1].Token.Denom, 1, sdk.NewInt(-6), sdk.ZeroDec()) + + // Add a sanctioned link between the balancer and concentrated liquidity pool. + migrationRecord := gammtypes.MigrationRecords{BalancerToConcentratedPoolLinks: []gammtypes.BalancerToConcentratedPoolLink{ + {BalancerPoolId: balancerPool.GetId(), ClPoolId: clPool.GetId()}, + }} + gammKeeper.SetMigrationInfo(ctx, migrationRecord) + + // The unbonding duration is the same as the staking module's unbonding duration. + unbondingDuration := stakingKeeper.GetParams(ctx).UnbondingTime + + // Lock the LP tokens for the duration of the unbonding period. + lockID := suite.LockTokens(poolJoinAcc, sdk.NewCoins(poolShareOut), unbondingDuration) + + // Superfluid delegate the lock if the test case requires it. + // Note the intermediary account that was created. + 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) + } + + // Superfluid undelegate the lock if the test case requires it. + if tc.superfluidUndelegating { + err = superfluidKeeper.SuperfluidUndelegate(ctx, poolJoinAcc.String(), lockID) + suite.Require().NoError(err) + } + + // Unlock the lock if the test case requires it. + if tc.unlocking { + // If lock was superfluid staked, we can't unlock via `BeginUnlock`, + // we 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) + + // Depending on the test case, we attempt to migrate a subset of the LP tokens we originally. + coinsToMigrate := poolShareOut + coinsToMigrate.Amount = coinsToMigrate.Amount.ToDec().Mul(tc.percentOfSharesToMigrate).RoundInt() + + // Run the unlock and migrate logic. + amount0, amount1, _, poolIdLeaving, poolIdEntering, newLockId, frozenUntil, err := superfluidKeeper.UnlockAndMigrate(ctx, poolJoinAcc, lockID, coinsToMigrate) + suite.Require().NoError(err) + suite.AssertEventEmitted(ctx, gammtypes.TypeEvtPoolExited, 1) + + newLock, err := lockupKeeper.GetLockByID(ctx, newLockId) + if tc.percentOfSharesToMigrate.LT(sdk.OneDec()) { + // If we migrated a subset of the LP tokens, we expect the new lock to have a the same end time. + suite.Require().NoError(err) + suite.Require().Equal(lock.EndTime, newLock.EndTime) + } else { + // If we migrated all of the LP tokens, we expect no new lock to be created. + suite.Require().Error(err) + suite.Require().Nil(newLock) + } + + // Check that concentrated liquidity position now exists + minTick, maxTick := cl.GetMinAndMaxTicksFromExponentAtPriceOne(clPool.GetPrecisionFactorAtPriceOne()) + position, err := suite.App.ConcentratedLiquidityKeeper.GetPosition(ctx, poolIdEntering, poolJoinAcc, minTick, maxTick, frozenUntil) + suite.Require().NoError(err) + suite.Require().NotNil(position) + + // Expect the poolIdLeaving to be the balancer pool id + // Expect the poolIdEntering to be the concentrated liquidity pool id + suite.Require().Equal(balancerPooId, poolIdLeaving) + suite.Require().Equal(clPool.GetId(), poolIdEntering) + + // exitPool has rounding difference. + // We test if correct amt has been exited and frozen by comparing with rounding tolerance. + defaultErrorTolerance := osmomath.ErrTolerance{ + AdditiveTolerance: sdk.NewDec(2), + RoundingDir: osmomath.RoundDown, + } + suite.Require().Equal(0, defaultErrorTolerance.Compare(joinPoolAmt.AmountOf("foo").ToDec().Mul(tc.percentOfSharesToMigrate).RoundInt(), amount0)) + suite.Require().Equal(0, defaultErrorTolerance.Compare(joinPoolAmt.AmountOf("stake").ToDec().Mul(tc.percentOfSharesToMigrate).RoundInt(), amount1)) + + // Check if the original lock was deleted. + _, err = lockupKeeper.GetLockByID(ctx, lockID) + suite.Require().Error(err) + + // If we didn't migrate the entire lock, we expect a new lock to be created we the remaining lock time and coins associated with it. + if tc.percentOfSharesToMigrate.LT(sdk.OneDec()) { + // Check if the new lock was created. + newLock, err := lockupKeeper.GetLockByID(ctx, newLockId) + suite.Require().NoError(err) + // The new lock should have the same owner and end time. + // The new lock should have the difference in coins between the original lock and the coins migrated. + suite.Require().Equal(sdk.NewCoins(poolShareOut.Sub(coinsToMigrate)).String(), newLock.Coins.String()) + suite.Require().Equal(lock.Owner, newLock.Owner) + suite.Require().Equal(lock.EndTime.String(), newLock.EndTime.String()) + // If original lock was unlocking, the new lock should also be unlocking. + if lock.IsUnlocking() { + suite.Require().True(newLock.IsUnlocking()) + } + } else { + suite.Require().Equal(uint64(0), newLockId) + } + + // Additional checks if the lock was superfluid staked. + if tc.superfluidDelegated { + // Check if migration deleted intermediary account connection. + addr := superfluidKeeper.GetLockIdIntermediaryAccountConnection(ctx, lockID) + suite.Require().Equal(addr.String(), "") + + // Check if migration deleted synthetic lockup. + _, err = lockupKeeper.GetSyntheticLockup(ctx, lockID, keeper.StakingSyntheticDenom(lock.Coins[0].Denom, valAddr.String())) + suite.Require().Error(err) + + // 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) + } + }) + } +} diff --git a/x/superfluid/keeper/unpool_test.go b/x/superfluid/keeper/unpool_test.go index dcab10d934a..ca2aa0b25de 100644 --- a/x/superfluid/keeper/unpool_test.go +++ b/x/superfluid/keeper/unpool_test.go @@ -176,7 +176,7 @@ func (suite *KeeperTestSuite) TestUnpool() { } else { lock, err := lockupKeeper.GetLockByID(ctx, lockID) suite.Require().NoError(err) - err = lockupKeeper.BeginUnlock(ctx, lockID, lock.Coins) + _, err = lockupKeeper.BeginUnlock(ctx, lockID, lock.Coins) suite.Require().NoError(err) // add time to current time to test lock end time diff --git a/x/valset-pref/keeper_test.go b/x/valset-pref/keeper_test.go index 203a5736eb6..cd666f5f594 100644 --- a/x/valset-pref/keeper_test.go +++ b/x/valset-pref/keeper_test.go @@ -246,7 +246,7 @@ func (suite *KeeperTestSuite) SetupLocks(delegator sdk.AccAddress) []lockuptypes unbondingLocks, err := suite.App.LockupKeeper.CreateLock(suite.Ctx, delegator, osmoToLock, twoWeekDuration) suite.Require().NoError(err) - err = suite.App.LockupKeeper.BeginUnlock(suite.Ctx, unbondingLocks.ID, nil) + _, err = suite.App.LockupKeeper.BeginUnlock(suite.Ctx, unbondingLocks.ID, nil) suite.Require().NoError(err) locks = append(locks, unbondingLocks) diff --git a/x/valset-pref/types/expected_interfaces.go b/x/valset-pref/types/expected_interfaces.go index 4d7e7a5a716..7eb585b4c31 100644 --- a/x/valset-pref/types/expected_interfaces.go +++ b/x/valset-pref/types/expected_interfaces.go @@ -35,6 +35,6 @@ type LockupKeeper interface { GetLockByID(ctx sdk.Context, lockID uint64) (*lockuptypes.PeriodLock, error) GetAllSyntheticLockupsByLockup(ctx sdk.Context, lockID uint64) []lockuptypes.SyntheticLock ForceUnlock(ctx sdk.Context, lock lockuptypes.PeriodLock) error - BeginUnlock(ctx sdk.Context, lockID uint64, coins sdk.Coins) error + BeginUnlock(ctx sdk.Context, lockID uint64, coins sdk.Coins) (uint64, error) GetPeriodLocks(ctx sdk.Context) ([]lockuptypes.PeriodLock, error) }