Skip to content
This repository has been archived by the owner on Oct 24, 2024. It is now read-only.

Commit

Permalink
add TopLevelDomain.ExtendExpirationDate
Browse files Browse the repository at this point in the history
  • Loading branch information
taryune committed Nov 2, 2023
1 parent aef36f9 commit 127269f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
10 changes: 10 additions & 0 deletions x/registry/types/top_level_domain.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package types

import (
"time"

errorsmod "cosmossdk.io/errors"
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -50,3 +52,11 @@ func (topLevelDomain TopLevelDomain) IsEditable(sender string) (isEditable bool,
isEditable = topLevelDomain.AccessControl[sender] == DomainRole_EDITOR || topLevelDomain.AccessControl[sender] == DomainRole_OWNER
return isEditable, err
}

func (topLevelDomain *TopLevelDomain) ExtendExpirationDate(from time.Time, extensionPeriodInYear uint64) (expirationDateInUnixNano int64) {
newExpirationDate := from.AddDate(0, 0, params.OneYearInDays*int(extensionPeriodInYear))
expirationDateInUnixNano = newExpirationDate.UnixNano()
topLevelDomain.ExpirationDate = expirationDateInUnixNano

return expirationDateInUnixNano
}
35 changes: 29 additions & 6 deletions x/registry/types/top_level_domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,12 @@ package types

import (
"testing"
"time"

errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"
)

type TopLevelDomainTest struct {
Domain TopLevelDomain
DomainPrice sdk.Coins
}

func TestTopLevelDomainValidate(t *testing.T) {
testCases := []struct {
domain TopLevelDomain
Expand Down Expand Up @@ -49,3 +44,31 @@ func TestTopLevelDomainValidate(t *testing.T) {
}
}
}

func TestExtendExpirationDate(t *testing.T) {
testCases := []struct {
from time.Time
extensionPeriodInYear uint64
expectedExpirationDate time.Time
}{
{
from: time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC),
extensionPeriodInYear: 1,
expectedExpirationDate: time.Date(2020, 12, 31, 0, 0, 0, 0, time.UTC),
},
{
from: time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC),
extensionPeriodInYear: 2,
expectedExpirationDate: time.Date(2021, 12, 31, 0, 0, 0, 0, time.UTC),
},
}

for _, tc := range testCases {
domain := TopLevelDomain{
Name: "myc",
}
extendExpirationDateInUnixNano := domain.ExtendExpirationDate(tc.from, tc.extensionPeriodInYear)
require.Equal(t, tc.expectedExpirationDate.UnixNano(), domain.ExpirationDate)
require.Equal(t, tc.expectedExpirationDate.UnixNano(), extendExpirationDateInUnixNano)
}
}

0 comments on commit 127269f

Please sign in to comment.