Skip to content

Commit

Permalink
remove withdraw unlocked coins
Browse files Browse the repository at this point in the history
  • Loading branch information
sontrinh16 committed Dec 4, 2024
1 parent af32a94 commit 54c8f5c
Show file tree
Hide file tree
Showing 16 changed files with 1,512 additions and 2,311 deletions.
1,031 changes: 991 additions & 40 deletions api/cosmos/accounts/defaults/lockup/v1/query.pulsar.go

Large diffs are not rendered by default.

1,391 changes: 38 additions & 1,353 deletions api/cosmos/accounts/defaults/lockup/v1/tx.pulsar.go

Large diffs are not rendered by default.

30 changes: 0 additions & 30 deletions tests/integration/accounts/lockup/continous_lockup_test_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ func (s *IntegrationTestSuite) TestContinuousLockingAccount() {
require.NoError(t, err)
s.fundAccount(app, ctx, accOwner, sdk.Coins{sdk.NewCoin("stake", math.NewInt(1000000))})
randAcc := sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address())
withdrawAcc := sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address())

_, accountAddr, err := app.AccountsKeeper.Init(ctx, lockupaccount.CONTINUOUS_LOCKING_ACCOUNT, accOwner, &types.MsgInitLockupAccount{
Owner: ownerAddrStr,
Expand Down Expand Up @@ -63,19 +62,6 @@ func (s *IntegrationTestSuite) TestContinuousLockingAccount() {
err := s.executeTx(ctx, msg, app, accountAddr, accOwner)
require.NotNil(t, err)
})
t.Run("error - execute withdraw message, no withdrawable token", func(t *testing.T) {
ownerAddr, err := app.AuthKeeper.AddressCodec().BytesToString(accOwner)
require.NoError(t, err)
withdrawAddr, err := app.AuthKeeper.AddressCodec().BytesToString(withdrawAcc)
require.NoError(t, err)
msg := &types.MsgWithdraw{
Withdrawer: ownerAddr,
ToAddress: withdrawAddr,
Denoms: []string{"stake"},
}
err = s.executeTx(ctx, msg, app, accountAddr, accOwner)
require.NotNil(t, err)
})

// Update context time
// 12 sec = 1/5 of a minute so 200stake should be released
Expand All @@ -96,23 +82,7 @@ func (s *IntegrationTestSuite) TestContinuousLockingAccount() {
balance := app.BankKeeper.GetBalance(ctx, randAcc, "stake")
require.True(t, balance.Amount.Equal(math.NewInt(100)))
})
t.Run("ok - execute withdraw message", func(t *testing.T) {
ownerAddr, err := app.AuthKeeper.AddressCodec().BytesToString(accOwner)
require.NoError(t, err)
withdrawAddr, err := app.AuthKeeper.AddressCodec().BytesToString(withdrawAcc)
require.NoError(t, err)
msg := &types.MsgWithdraw{
Withdrawer: ownerAddr,
ToAddress: withdrawAddr,
Denoms: []string{"stake"},
}
err = s.executeTx(ctx, msg, app, accountAddr, accOwner)
require.NoError(t, err)

// withdrawable amount should be 200 - 100 = 100stake
balance := app.BankKeeper.GetBalance(ctx, withdrawAcc, "stake")
require.True(t, balance.Amount.Equal(math.NewInt(100)))
})
t.Run("ok - execute delegate message", func(t *testing.T) {
msg := &types.MsgDelegate{
Sender: ownerAddrStr,
Expand Down
33 changes: 0 additions & 33 deletions tests/integration/accounts/lockup/delayed_lockup_test_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ func (s *IntegrationTestSuite) TestDelayedLockingAccount() {
require.NoError(t, err)
s.fundAccount(app, ctx, accOwner, sdk.Coins{sdk.NewCoin("stake", math.NewInt(1000000))})
randAcc := sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address())
withdrawAcc := sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address())

_, accountAddr, err := app.AccountsKeeper.Init(ctx, lockupaccount.DELAYED_LOCKING_ACCOUNT, accOwner, &types.MsgInitLockupAccount{
Owner: ownerAddrStr,
Expand Down Expand Up @@ -62,19 +61,6 @@ func (s *IntegrationTestSuite) TestDelayedLockingAccount() {
err := s.executeTx(ctx, msg, app, accountAddr, accOwner)
require.NotNil(t, err)
})
t.Run("error - execute withdraw message, no withdrawable token", func(t *testing.T) {
ownerAddr, err := app.AuthKeeper.AddressCodec().BytesToString(accOwner)
require.NoError(t, err)
withdrawAddr, err := app.AuthKeeper.AddressCodec().BytesToString(withdrawAcc)
require.NoError(t, err)
msg := &types.MsgWithdraw{
Withdrawer: ownerAddr,
ToAddress: withdrawAddr,
Denoms: []string{"stake"},
}
err = s.executeTx(ctx, msg, app, accountAddr, accOwner)
require.NotNil(t, err)
})
t.Run("ok - execute delegate message", func(t *testing.T) {
msg := &types.MsgDelegate{
Sender: ownerAddrStr,
Expand Down Expand Up @@ -167,23 +153,4 @@ func (s *IntegrationTestSuite) TestDelayedLockingAccount() {
entries := unbondingEntriesResponse.UnbondingEntries
require.Len(t, entries, 0)
})
// Test to withdraw all the remain funds to an account of choice
t.Run("ok - execute withdraw message", func(t *testing.T) {
ownerAddr, err := app.AuthKeeper.AddressCodec().BytesToString(accOwner)
require.NoError(t, err)
withdrawAddr, err := app.AuthKeeper.AddressCodec().BytesToString(withdrawAcc)
require.NoError(t, err)
msg := &types.MsgWithdraw{
Withdrawer: ownerAddr,
ToAddress: withdrawAddr,
Denoms: []string{"stake"},
}
err = s.executeTx(ctx, msg, app, accountAddr, accOwner)
require.NoError(t, err)

// withdrawable amount should be
// 1000stake - 100stake( above sent amt ) = 800stake
balance := app.BankKeeper.GetBalance(ctx, withdrawAcc, "stake")
require.True(t, balance.Amount.Equal(math.NewInt(900)))
})
}
33 changes: 0 additions & 33 deletions tests/integration/accounts/lockup/periodic_lockup_test_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ func (s *IntegrationTestSuite) TestPeriodicLockingAccount() {
require.NoError(t, err)
s.fundAccount(app, ctx, accOwner, sdk.Coins{sdk.NewCoin("stake", math.NewInt(1000000))})
randAcc := sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address())
withdrawAcc := sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address())

_, accountAddr, err := app.AccountsKeeper.Init(ctx, lockupaccount.PERIODIC_LOCKING_ACCOUNT, accOwner, &types.MsgInitPeriodicLockingAccount{
Owner: ownerAddrStr,
Expand Down Expand Up @@ -76,19 +75,6 @@ func (s *IntegrationTestSuite) TestPeriodicLockingAccount() {
err := s.executeTx(ctx, msg, app, accountAddr, accOwner)
require.NotNil(t, err)
})
t.Run("error - execute withdraw message, no withdrawable token", func(t *testing.T) {
ownerAddr, err := app.AuthKeeper.AddressCodec().BytesToString(accOwner)
require.NoError(t, err)
withdrawAddr, err := app.AuthKeeper.AddressCodec().BytesToString(withdrawAcc)
require.NoError(t, err)
msg := &types.MsgWithdraw{
Withdrawer: ownerAddr,
ToAddress: withdrawAddr,
Denoms: []string{"stake"},
}
err = s.executeTx(ctx, msg, app, accountAddr, accOwner)
require.NotNil(t, err)
})

// Update context time
// After first period 500stake should be unlock
Expand Down Expand Up @@ -116,25 +102,6 @@ func (s *IntegrationTestSuite) TestPeriodicLockingAccount() {
Time: currentTime.Add(time.Minute * 2),
})

t.Run("oke - execute withdraw message", func(t *testing.T) {
ownerAddr, err := app.AuthKeeper.AddressCodec().BytesToString(accOwner)
require.NoError(t, err)
withdrawAddr, err := app.AuthKeeper.AddressCodec().BytesToString(withdrawAcc)
require.NoError(t, err)
msg := &types.MsgWithdraw{
Withdrawer: ownerAddr,
ToAddress: withdrawAddr,
Denoms: []string{"stake"},
}
err = s.executeTx(ctx, msg, app, accountAddr, accOwner)
require.NoError(t, err)

// withdrawable amount should be
// 1000stake - 500stake( above sent amt ) = 500stake
balance := app.BankKeeper.GetBalance(ctx, withdrawAcc, "stake")
require.True(t, balance.Amount.Equal(math.NewInt(500)))
})

t.Run("ok - execute delegate message", func(t *testing.T) {
msg := &types.MsgDelegate{
Sender: ownerAddrStr,
Expand Down
25 changes: 18 additions & 7 deletions x/accounts/defaults/lockup/continuous_locking_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,6 @@ func (cva *ContinuousLockingAccount) SendCoins(ctx context.Context, msg *lockupt
return cva.BaseLockup.SendCoins(ctx, msg, cva.GetLockedCoinsWithDenoms)
}

func (cva *ContinuousLockingAccount) WithdrawUnlockedCoins(ctx context.Context, msg *lockuptypes.MsgWithdraw) (
*lockuptypes.MsgWithdrawResponse, error,
) {
return cva.BaseLockup.WithdrawUnlockedCoins(ctx, msg, cva.GetLockedCoinsWithDenoms)
}

// GetLockCoinsInfo returns the total number of unlocked and locked coins.
func (cva ContinuousLockingAccount) GetLockCoinsInfo(ctx context.Context, blockTime time.Time) (unlockedCoins, lockedCoins sdk.Coins, err error) {
unlockedCoins = sdk.Coins{}
Expand Down Expand Up @@ -186,6 +180,23 @@ func (cva ContinuousLockingAccount) QueryLockupAccountInfo(ctx context.Context,
return resp, nil
}

func (cva ContinuousLockingAccount) QuerySpendableTokens(ctx context.Context, req *lockuptypes.QuerySpendableAmountRequest) (
*lockuptypes.QuerySpendableAmountResponse, error,
) {
hs := cva.headerService.HeaderInfo(ctx)
_, lockedCoins, err := cva.GetLockCoinsInfo(ctx, hs.Time)
if err != nil {
return nil, err
}

resp, err := cva.BaseLockup.QuerySpendableTokens(ctx, lockedCoins)
if err != nil {
return nil, err
}

return resp, nil
}

// Implement smart account interface
func (cva ContinuousLockingAccount) RegisterInitHandler(builder *accountstd.InitBuilder) {
accountstd.RegisterInitHandler(builder, cva.Init)
Expand All @@ -194,11 +205,11 @@ func (cva ContinuousLockingAccount) RegisterInitHandler(builder *accountstd.Init
func (cva ContinuousLockingAccount) RegisterExecuteHandlers(builder *accountstd.ExecuteBuilder) {
accountstd.RegisterExecuteHandler(builder, cva.Delegate)
accountstd.RegisterExecuteHandler(builder, cva.SendCoins)
accountstd.RegisterExecuteHandler(builder, cva.WithdrawUnlockedCoins)
cva.BaseLockup.RegisterExecuteHandlers(builder)
}

func (cva ContinuousLockingAccount) RegisterQueryHandlers(builder *accountstd.QueryBuilder) {
accountstd.RegisterQueryHandler(builder, cva.QueryLockupAccountInfo)
accountstd.RegisterQueryHandler(builder, cva.QuerySpendableTokens)
cva.BaseLockup.RegisterQueryHandlers(builder)
}
30 changes: 0 additions & 30 deletions x/accounts/defaults/lockup/continuous_locking_account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,36 +149,6 @@ func TestContinousAccountSendCoins(t *testing.T) {
require.NoError(t, err)
}

func TestContinousAccountWithdrawUnlockedCoins(t *testing.T) {
ctx, ss := newMockContext(t)
sdkCtx := sdk.NewContext(nil, true, log.NewNopLogger()).WithContext(ctx).WithHeaderInfo(header.Info{
Time: time.Now(),
})

acc := setupContinousAccount(t, sdkCtx, ss)
_, err := acc.WithdrawUnlockedCoins(sdkCtx, &lockuptypes.MsgWithdraw{
Withdrawer: "owner",
ToAddress: "receiver",
Denoms: []string{"test"},
})
require.Error(t, err)

startTime, err := acc.StartTime.Get(sdkCtx)
require.NoError(t, err)

// Update context time to unlocked half of the original locking amount
sdkCtx = sdkCtx.WithHeaderInfo(header.Info{
Time: startTime.Add(time.Minute * 1),
})

_, err = acc.WithdrawUnlockedCoins(sdkCtx, &lockuptypes.MsgWithdraw{
Withdrawer: "owner",
ToAddress: "receiver",
Denoms: []string{"test"},
})
require.NoError(t, err)
}

func TestContinousAccountGetLockCoinInfo(t *testing.T) {
ctx, ss := newMockContext(t)
sdkCtx := sdk.NewContext(nil, true, log.NewNopLogger()).WithContext(ctx).WithHeaderInfo(header.Info{
Expand Down
25 changes: 18 additions & 7 deletions x/accounts/defaults/lockup/delayed_locking_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,6 @@ func (dva *DelayedLockingAccount) SendCoins(ctx context.Context, msg *lockuptype
return dva.BaseLockup.SendCoins(ctx, msg, dva.GetLockedCoinsWithDenoms)
}

func (dva *DelayedLockingAccount) WithdrawUnlockedCoins(ctx context.Context, msg *lockuptypes.MsgWithdraw) (
*lockuptypes.MsgWithdrawResponse, error,
) {
return dva.BaseLockup.WithdrawUnlockedCoins(ctx, msg, dva.GetLockedCoinsWithDenoms)
}

// GetLockCoinsInfo returns the total number of unlocked and locked coins.
func (dva DelayedLockingAccount) GetLockCoinsInfo(ctx context.Context, blockTime time.Time) (sdk.Coins, sdk.Coins, error) {
endTime, err := dva.EndTime.Get(ctx)
Expand Down Expand Up @@ -135,6 +129,23 @@ func (dva DelayedLockingAccount) QueryVestingAccountInfo(ctx context.Context, re
return resp, nil
}

func (dva DelayedLockingAccount) QuerySpendableTokens(ctx context.Context, req *lockuptypes.QuerySpendableAmountRequest) (
*lockuptypes.QuerySpendableAmountResponse, error,
) {
hs := dva.headerService.HeaderInfo(ctx)
_, lockedCoins, err := dva.GetLockCoinsInfo(ctx, hs.Time)
if err != nil {
return nil, err
}

resp, err := dva.BaseLockup.QuerySpendableTokens(ctx, lockedCoins)
if err != nil {
return nil, err
}

return resp, nil
}

// Implement smart account interface
func (dva DelayedLockingAccount) RegisterInitHandler(builder *accountstd.InitBuilder) {
accountstd.RegisterInitHandler(builder, dva.Init)
Expand All @@ -143,11 +154,11 @@ func (dva DelayedLockingAccount) RegisterInitHandler(builder *accountstd.InitBui
func (dva DelayedLockingAccount) RegisterExecuteHandlers(builder *accountstd.ExecuteBuilder) {
accountstd.RegisterExecuteHandler(builder, dva.Delegate)
accountstd.RegisterExecuteHandler(builder, dva.SendCoins)
accountstd.RegisterExecuteHandler(builder, dva.WithdrawUnlockedCoins)
dva.BaseLockup.RegisterExecuteHandlers(builder)
}

func (dva DelayedLockingAccount) RegisterQueryHandlers(builder *accountstd.QueryBuilder) {
accountstd.RegisterQueryHandler(builder, dva.QueryVestingAccountInfo)
accountstd.RegisterQueryHandler(builder, dva.QuerySpendableTokens)
dva.BaseLockup.RegisterQueryHandlers(builder)
}
30 changes: 0 additions & 30 deletions x/accounts/defaults/lockup/delayed_locking_account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,36 +148,6 @@ func TestDelayedAccountSendCoins(t *testing.T) {
require.NoError(t, err)
}

func TestDelayedAccountWithdrawUnlockedCoins(t *testing.T) {
ctx, ss := newMockContext(t)
sdkCtx := sdk.NewContext(nil, true, log.NewNopLogger()).WithContext(ctx).WithHeaderInfo(header.Info{
Time: time.Now(),
})

acc := setupDelayedAccount(t, sdkCtx, ss)
_, err := acc.WithdrawUnlockedCoins(sdkCtx, &lockuptypes.MsgWithdraw{
Withdrawer: "owner",
ToAddress: "receiver",
Denoms: []string{"test"},
})
require.Error(t, err)

endTime, err := acc.EndTime.Get(sdkCtx)
require.NoError(t, err)

// Update context time to unlocked all the original locking amount
sdkCtx = sdkCtx.WithHeaderInfo(header.Info{
Time: endTime.Add(time.Second),
})

_, err = acc.WithdrawUnlockedCoins(sdkCtx, &lockuptypes.MsgWithdraw{
Withdrawer: "owner",
ToAddress: "receiver",
Denoms: []string{"test"},
})
require.NoError(t, err)
}

func TestDelayedAccountGetLockCoinInfo(t *testing.T) {
ctx, ss := newMockContext(t)
sdkCtx := sdk.NewContext(nil, true, log.NewNopLogger()).WithContext(ctx).WithHeaderInfo(header.Info{
Expand Down
Loading

0 comments on commit 54c8f5c

Please sign in to comment.