Skip to content

Commit

Permalink
wip on tests
Browse files Browse the repository at this point in the history
  • Loading branch information
omerlavanet committed Nov 26, 2024
1 parent 2b56061 commit c2fc7bc
Showing 1 changed file with 37 additions and 8 deletions.
45 changes: 37 additions & 8 deletions x/dualstaking/keeper/delegate_credit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,45 +27,74 @@ func TestCalculateCredit(t *testing.T) {
name string
delegation types.Delegation
expectedCredit sdk.Coin
currentTime time.Time
expectedCreditTimestamp int64
}{
{
name: "initial delegation",
name: "initial delegation 10days ago with no credit",
delegation: types.Delegation{
Amount: sdk.NewCoin(bondDenom, sdk.NewInt(1000)),
Credit: sdk.NewCoin(bondDenom, sdk.ZeroInt()),
Timestamp: timeNow.Add(-time.Hour * 24 * 10).Unix(),
Timestamp: timeNow.Add(-time.Hour * 24 * 10).Unix(), // was done 10 days ago
CreditTimestamp: 0,
},
expectedCredit: sdk.NewCoin(bondDenom, sdk.NewInt(1000)),
currentTime: timeNow,
expectedCreditTimestamp: timeNow.Add(-time.Hour * 24 * 10).Unix(),
},
{
name: "delegation with existing credit",
name: "delegation with existing credit equal time increase",
delegation: types.Delegation{
Amount: sdk.NewCoin(bondDenom, sdk.NewInt(2000)),
Credit: sdk.NewCoin(bondDenom, sdk.NewInt(1000)),
Timestamp: timeNow.Add(-time.Hour * 24 * 5).Unix(),
CreditTimestamp: timeNow.Add(-time.Hour * 24 * 10).Unix(),
},
expectedCredit: sdk.NewCoin(bondDenom, sdk.NewInt(1500)),
expectedCreditTimestamp: timeNow.Add(-time.Hour * 24 * 5).Unix(),
currentTime: timeNow,
expectedCreditTimestamp: timeNow.Add(-time.Hour * 24 * 10).Unix(),
},
{
name: "delegation older than 30 days",
name: "delegation with existing credit equal time decrease",
delegation: types.Delegation{
Amount: sdk.NewCoin(bondDenom, sdk.NewInt(1000)),
Credit: sdk.NewCoin(bondDenom, sdk.NewInt(2000)),
Timestamp: timeNow.Add(-time.Hour * 24 * 5).Unix(),
CreditTimestamp: timeNow.Add(-time.Hour * 24 * 10).Unix(),
},
expectedCredit: sdk.NewCoin(bondDenom, sdk.NewInt(1500)),
currentTime: timeNow,
expectedCreditTimestamp: timeNow.Add(-time.Hour * 24 * 10).Unix(),
},
{
name: "delegation older than 30 days no credit",
delegation: types.Delegation{
Amount: sdk.NewCoin(bondDenom, sdk.NewInt(3000)),
Credit: sdk.NewCoin(bondDenom, sdk.NewInt(1500)),
Credit: sdk.NewCoin(bondDenom, sdk.ZeroInt()),
Timestamp: timeNow.Add(-time.Hour * 24 * 40).Unix(),
CreditTimestamp: timeNow.Add(-time.Hour * 24 * 35).Unix(),
CreditTimestamp: 0,
},
expectedCredit: sdk.NewCoin(bondDenom, sdk.NewInt(3000)),
currentTime: timeNow,
expectedCreditTimestamp: timeNow.Add(-time.Hour * 24 * 30).Unix(),
},
{
name: "delegation older than 30 days with credit",
delegation: types.Delegation{
Amount: sdk.NewCoin(bondDenom, sdk.NewInt(3000)),
Credit: sdk.NewCoin(bondDenom, sdk.NewInt(7000)),
Timestamp: timeNow.Add(-time.Hour * 24 * 40).Unix(),
CreditTimestamp: timeNow.Add(-time.Hour * 24 * 50).Unix(),
},
expectedCredit: sdk.NewCoin(bondDenom, sdk.NewInt(3000)),
expectedCreditTimestamp: timeNow.Add(-time.Hour * 24 * 40).Unix(),
currentTime: timeNow,
expectedCreditTimestamp: timeNow.Add(-time.Hour * 24 * 30).Unix(),
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx = ctx.WithBlockTime(tt.currentTime)
credit, creditTimestamp := k.CalculateCredit(ctx, tt.delegation)
require.Equal(t, tt.expectedCredit, credit)
require.Equal(t, tt.expectedCreditTimestamp, creditTimestamp)
Expand Down

0 comments on commit c2fc7bc

Please sign in to comment.