Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DO NOT MERGE] example test for calc failure #22474

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 63 additions & 2 deletions x/accounts/defaults/lockup/lockup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package lockup

import (
"context"
"cosmossdk.io/core/header"
"cosmossdk.io/log"
"github.com/stretchr/testify/assert"
"testing"
"time"

"github.com/stretchr/testify/require"

"cosmossdk.io/core/header"
"cosmossdk.io/core/store"
"cosmossdk.io/log"
"cosmossdk.io/math"
lockuptypes "cosmossdk.io/x/accounts/defaults/lockup/v1"

Expand Down Expand Up @@ -281,4 +282,64 @@ func TestQueryLockupAccountBaseInfo(t *testing.T) {
require.Equal(t, res.OriginalLocking.AmountOf("test"), math.NewInt(10))
require.Equal(t, res.Owner, "owner")
require.NoError(t, err)

}

func TestWithdrawUnlockedCoins(t *testing.T) {
const (
liquid = 6
locked = 4
initialBalance = liquid + locked // note: 10 is setup in the bank mock
staked = 7
unstaked = staked
denom = "test"
)
ctx, ss := newMockContext(t)
now := time.Now()
sdkCtx := sdk.NewContext(nil, true, log.NewNopLogger()).WithContext(ctx).WithHeaderInfo(header.Info{
Time: now,
})

baseLockup := setup(t, ctx, ss)
require.NoError(t, baseLockup.OriginalLocking.Set(ctx, denom, math.NewInt(locked)))

foreverLocked := func(ctx context.Context, time time.Time, denoms ...string) (sdk.Coins, error) {
return sdk.NewCoins(sdk.NewCoin(denom, math.NewInt(locked))), nil
}

// delegate
_, err := baseLockup.Delegate(sdkCtx, &lockuptypes.MsgDelegate{
Sender: "owner",
ValidatorAddress: "validator",
Amount: sdk.NewCoin(denom, math.NewInt(staked)),
}, foreverLocked)
require.NoError(t, err)
// balance should be 10 - 7 = 3

_, err = baseLockup.Undelegate(sdkCtx, &lockuptypes.MsgUndelegate{
Sender: "owner",
ValidatorAddress: "validator",
Amount: sdk.NewCoin(denom, math.NewInt(unstaked)),
})
require.NoError(t, err)
// when unbonding time has expired
sdkCtx = sdkCtx.WithHeaderInfo(header.Info{Time: now.Add(21*24*time.Hour + time.Minute)})

// balance should be 3 + 7 = 10 now
_, err = baseLockup.UnbondEntries.Get(sdkCtx, "validator")
require.NoError(t, err)
amount, err := baseLockup.DelegatedLocking.Get(sdkCtx, denom)
require.NoError(t, err)
assert.Equal(t, amount.String(), math.NewInt(locked).String())

// when WithdrawUnlockedCoins is called,
// only the liquid amount should be returned
res, err := baseLockup.WithdrawUnlockedCoins(sdkCtx, &lockuptypes.MsgWithdraw{
Withdrawer: "owner",
ToAddress: "my-receiver",
Denoms: []string{denom},
}, foreverLocked)
// 4 stake is still locked. nothing to return
require.Equal(t, math.NewInt(0).String(), res.AmountReceived.AmountOf(denom).String())
require.NoError(t, err)
}
3 changes: 2 additions & 1 deletion x/accounts/defaults/lockup/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ func newMockContext(t *testing.T) (context.Context, store.KVStoreService) {
return &stakingtypes.MsgDelegateResponse{}, nil
case "/cosmos.staking.v1beta1.MsgUndelegate":
return &stakingtypes.MsgUndelegateResponse{
Amount: sdk.NewCoin("test", math.NewInt(1)),
CompletionTime: time.Now().Add(stakingtypes.DefaultUnbondingTime),
Amount: msg.(*stakingtypes.MsgUndelegate).Amount,
}, nil
case "/cosmos.distribution.v1beta1.MsgWithdrawDelegatorReward":
return &distrtypes.MsgWithdrawDelegatorRewardResponse{}, nil
Expand Down
Loading