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

Commit

Permalink
rm register_*_domain and mv func to *_domain.go
Browse files Browse the repository at this point in the history
  • Loading branch information
taryune committed Nov 2, 2023
1 parent e54d273 commit ad59f14
Show file tree
Hide file tree
Showing 4 changed files with 217 additions and 226 deletions.
109 changes: 0 additions & 109 deletions x/registry/keeper/register_second_level_domain.go

This file was deleted.

113 changes: 0 additions & 113 deletions x/registry/keeper/register_top_level_domain.go

This file was deleted.

112 changes: 109 additions & 3 deletions x/registry/keeper/second_level_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
errosmod "github.com/cosmos/cosmos-sdk/types/errors"
errorsmod "github.com/cosmos/cosmos-sdk/types/errors"
)

// SetSecondLevelDomain set a specific second-level-domain in the store from its index
Expand Down Expand Up @@ -84,14 +84,120 @@ func (k Keeper) GetValidSecondLevelDomain(ctx sdk.Context, name string, parent s
// Get second level domain
secondLevelDomain, isFound := k.GetSecondLevelDomain(ctx, name, parent)
if !isFound {
return secondLevelDomain, errosmod.Wrapf(types.ErrDomainNotFound, "%s.%s", name, parent)
return secondLevelDomain, errorsmod.Wrapf(types.ErrDomainNotFound, "%s.%s", name, parent)
}

// Check if domain is not expired
expirationDate := time.Unix(0, secondLevelDomain.ExpirationDate)
if ctx.BlockTime().After(expirationDate) && secondLevelDomain.ExpirationDate != 0 {
return secondLevelDomain, errosmod.Wrapf(types.ErrDomainExpired, "%s", name)
return secondLevelDomain, errorsmod.Wrapf(types.ErrDomainExpired, "%s", name)
}

return secondLevelDomain, nil
}

// Get parent domain
func (k Keeper) GetParentDomain(ctx sdk.Context, domain types.SecondLevelDomain) (parentDomain types.TopLevelDomain, found bool) {
// Get parent domain
parent := domain.ParseParent()
parentDomain, found = k.GetTopLevelDomain(ctx, parent)
return parentDomain, found
}

// Get parent domain's subdomain config
func (k Keeper) GetParentsSubdomainConfig(ctx sdk.Context, domain types.SecondLevelDomain) types.SubdomainConfig {
// Get parent domain
parentDomain, found := k.GetParentDomain(ctx, domain)
if !found || parentDomain.SubdomainConfig == nil {
panic("parent domain or config not found")
}
return *parentDomain.SubdomainConfig
}

// Increment parents subdomain count
func (k Keeper) IncrementParentsSubdomainCount(ctx sdk.Context, domain types.SecondLevelDomain) {
// Increment parent's subdomain count
parent := domain.ParseParent()
parentDomain, found := k.GetTopLevelDomain(ctx, parent)
if !found {
panic("parent not found")
}
parentDomain.SubdomainCount++
k.SetTopLevelDomain(ctx, parentDomain)
}

// Pay SLD registration fee
func (k Keeper) PaySLDRegstrationFee(ctx sdk.Context, payer sdk.AccAddress, domain types.SecondLevelDomain, registrationPeriodInYear uint64) (fee *sdk.Coin, err error) {
config := k.GetParentsSubdomainConfig(ctx, domain)

fee, err = config.GetRegistrationFee(domain.Name, registrationPeriodInYear)
if err != nil {
return fee, err
}

// Send coins from payer to module account
k.bankKeeper.SendCoinsFromAccountToModule(ctx, payer, types.ModuleName, sdk.NewCoins(*fee))

// Update store
parent, found := k.GetTopLevelDomain(ctx, domain.Parent)
if !found {
panic("parent not found")
}
parent.TotalWithdrawalAmount = parent.TotalWithdrawalAmount.Add(*fee)
k.SetTopLevelDomain(ctx, parent)

return fee, err
}

// Append to owned domain
func (k Keeper) AppendToOwnedDomain(ctx sdk.Context, owner string, name string, parent string) {
domainOwnership, found := k.GetDomainOwnership(ctx, owner)
if found {
domainOwnership.Domains = append(domainOwnership.Domains, &types.OwnedDomain{Name: name, Parent: parent})
k.SetDomainOwnership(ctx, domainOwnership)
} else {
k.SetDomainOwnership(ctx, types.DomainOwnership{Owner: owner, Domains: []*types.OwnedDomain{{Name: name, Parent: parent}}})
}
}

// Register second level domain
func (k Keeper) RegisterSecondLevelDomain(ctx sdk.Context, domain types.SecondLevelDomain, owner sdk.AccAddress, registrationPeriodIYear uint64) (err error) {
// Validate domain
err = k.ValidateSecondLevelDomain(ctx, domain)
if err != nil {
return err
}

// Get parent domain of second level domain
parentDomain, found := k.GetParentDomain(ctx, domain)

if !found {
panic("parent not found")
}

// Check if parent domain has subdomain registration config
if parentDomain.SubdomainConfig.MaxSubdomainRegistrations <= parentDomain.SubdomainCount {
err = errorsmod.Wrapf(types.ErrMaxSubdomainCountReached, "%d", parentDomain.SubdomainCount)
return err
}

// Increment parents subdomain SubdomainCount
k.IncrementParentsSubdomainCount(ctx, domain)

// Pay SLD registration fee
fee, err := k.PaySLDRegstrationFee(ctx, owner, domain, registrationPeriodIYear)
if err != nil {
return err
}

// Append to owned domain
k.AppendToOwnedDomain(ctx, owner.String(), domain.Name, domain.Parent)

// Set domain
k.SetSecondLevelDomain(ctx, domain)

// Emit event
EmitRegisterSecondLevelDomainEvent(ctx, domain, *fee)

return err
}
Loading

0 comments on commit ad59f14

Please sign in to comment.