Skip to content

Commit

Permalink
finished delegation partial test
Browse files Browse the repository at this point in the history
  • Loading branch information
omerlavanet committed Dec 3, 2024
1 parent 88b0444 commit 8ad8b1a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
9 changes: 7 additions & 2 deletions x/pairing/keeper/delegator_rewards_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ import (
"github.com/stretchr/testify/require"
)

const (
exactConst = "exact"
)

// TestProviderDelegatorsRewards tests that the provider's reward (considering delegations) is as expected
// Also, it checks that the delegator reward map is updated as expected
func TestProviderDelegatorsRewards(t *testing.T) {
Expand Down Expand Up @@ -184,7 +188,7 @@ func TestPartialMonthDelegation(t *testing.T) {
_, err := ts.TxSubscriptionBuy(client, client, "free", 1, false, false) // extend by a month so the sub won't expire
require.NoError(t, err)

ts.AdvanceTimeHours(time.Hour * 24 * 15) // 15 days passed form the subs timer
ts.AdvanceTimeHours(time.Hour*24*15 + time.Hour*41) // results in 15 days of the provider being active (41 hours until subscription triggers payout)

// add another provider after 15 days
err = ts.addProvider(1)
Expand All @@ -196,7 +200,7 @@ func TestPartialMonthDelegation(t *testing.T) {
metadata.DelegateCommission = 50 // 50% commission
ts.Keepers.Epochstorage.SetMetadata(ts.Ctx, metadata)

ts.AdvanceTimeHours(time.Hour * 24 * 15) // 5 days passed from the provider stake, total 20 days
ts.AdvanceTimeHours(time.Hour * 24 * 5) // 5 days passed from the provider stake, total 20 days

stakeEntryResp, err := ts.Keepers.Pairing.Provider(ts.Ctx, &types.QueryProviderRequest{
Address: provider,
Expand All @@ -215,6 +219,7 @@ func TestPartialMonthDelegation(t *testing.T) {
require.Equal(t, stakeEntry.DelegateCommission, metadata.DelegateCommission)

relayPaymentMessage := sendRelay(ts, provider, clientAcc, []string{ts.spec.Index})
relayPaymentMessage.DescriptionString = exactConst
// we have a provider that staked after 15/30 days, and a delegator that staked after 20/30 days
// provider has 100k stake for 15 days, delegator has 150k stake for 10 days so they should divide half half, and the provider commission is 50%
ts.payAndVerifyBalance(relayPaymentMessage, clientAcc.Addr, providerAcc.Vault.Addr, true, true, 75)
Expand Down
13 changes: 11 additions & 2 deletions x/pairing/keeper/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,15 +231,24 @@ func (ts *tester) payAndVerifyBalance(
require.NotNil(ts.T, sub.Sub)
require.Equal(ts.T, originalSubCuLeft-totalCuUsed, sub.Sub.MonthCuLeft)
timeToExpiry := time.Unix(int64(sub.Sub.MonthExpiryTime), 0)
if timeToExpiry.After(ts.Ctx.BlockTime()) {
durLeft := sub.Sub.DurationLeft
if timeToExpiry.After(ts.Ctx.BlockTime()) && relayPayment.DescriptionString == exactConst {
ts.AdvanceTimeHours(timeToExpiry.Sub(ts.Ctx.BlockTime()))
// subs only pays after blocks to save
ts.AdvanceEpoch()
ts.AdvanceBlocks(ts.BlocksToSave() + 1)
} else {
// advance month + blocksToSave + 1 to trigger the provider monthly payment
ts.AdvanceMonths(1)
ts.AdvanceEpoch()
ts.AdvanceBlocks(ts.BlocksToSave() + 1)
}

if durLeft > 0 {
sub, err = ts.QuerySubscriptionCurrent(proj.Project.Subscription)
require.Nil(ts.T, err)
require.NotNil(ts.T, sub.Sub)
require.Equal(ts.T, durLeft-1, sub.Sub.DurationLeft, "month expiry time: %s current time: %s", time.Unix(int64(sub.Sub.MonthExpiryTime), 0).UTC(), ts.BlockTime().UTC())
}
// verify provider's balance
credit := sub.Sub.Credit.Amount.QuoRaw(int64(sub.Sub.DurationLeft))
want := sdk.ZeroInt()
Expand Down

0 comments on commit 8ad8b1a

Please sign in to comment.