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

Commit

Permalink
refactor tld keeper
Browse files Browse the repository at this point in the history
  • Loading branch information
taryune committed Nov 2, 2023
1 parent efa78c7 commit e280265
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 156 deletions.
28 changes: 14 additions & 14 deletions docs/static/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50303,9 +50303,9 @@ paths:
properties:
name:
type: string
expirationDateInUnixNano:
expirationDate:
type: string
format: int64
format: date-time
metadata:
type: object
additionalProperties:
Expand Down Expand Up @@ -50531,9 +50531,9 @@ paths:
properties:
name:
type: string
expirationDateInUnixNano:
expirationDate:
type: string
format: int64
format: date-time
metadata:
type: object
additionalProperties:
Expand Down Expand Up @@ -81193,9 +81193,9 @@ definitions:
properties:
name:
type: string
expirationDateInUnixNano:
expirationDate:
type: string
format: int64
format: date-time
metadata:
type: object
additionalProperties:
Expand Down Expand Up @@ -81361,9 +81361,9 @@ definitions:
properties:
name:
type: string
expirationDateInUnixNano:
expirationDate:
type: string
format: int64
format: date-time
metadata:
type: object
additionalProperties:
Expand Down Expand Up @@ -81653,9 +81653,9 @@ definitions:
properties:
name:
type: string
expirationDateInUnixNano:
expirationDate:
type: string
format: int64
format: date-time
metadata:
type: object
additionalProperties:
Expand Down Expand Up @@ -81838,9 +81838,9 @@ definitions:
properties:
name:
type: string
expirationDateInUnixNano:
expirationDate:
type: string
format: int64
format: date-time
metadata:
type: object
additionalProperties:
Expand Down Expand Up @@ -82107,9 +82107,9 @@ definitions:
properties:
name:
type: string
expirationDateInUnixNano:
expirationDate:
type: string
format: int64
format: date-time
metadata:
type: object
additionalProperties:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package keeper
import (
"context"

errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/mycel-domain/mycel/x/registry/types"
Expand All @@ -12,24 +11,13 @@ import (
func (k msgServer) ExtendTopLevelDomainExpirationDate(goCtx context.Context, msg *types.MsgExtendTopLevelDomainExpirationDate) (*types.MsgExtendTopLevelDomainExpirationDateResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

domain, found := k.GetTopLevelDomain(ctx, msg.Name)
if !found {
return nil, errorsmod.Wrapf(types.ErrDomainNotFound, "%s", msg.Name)
}

// Check if the domain is editable
_, err := domain.IsEditable(msg.Creator)
if err != nil {
return nil, err
}

fee, err := k.Keeper.ExtendTopLevelDomainExpirationDate(ctx, msg.Creator, &domain, msg.ExtensionPeriodInYear)
topLevelDomain, fee, err := k.Keeper.ExtendTopLevelDomainExpirationDate(ctx, msg.Creator, msg.Name, msg.ExtensionPeriodInYear)
if err != nil {
return nil, err
}

return &types.MsgExtendTopLevelDomainExpirationDateResponse{
TopLevelDomain: &domain,
TopLevelDomain: &topLevelDomain,
Fee: &fee,
}, nil
}
21 changes: 2 additions & 19 deletions x/registry/keeper/msg_server_register_top_level_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/mycel-domain/mycel/app/params"
)

func (k msgServer) RegisterTopLevelDomain(goCtx context.Context, msg *types.MsgRegisterTopLevelDomain) (*types.MsgRegisterTopLevelDomainResponse, error) {
Expand All @@ -17,29 +16,13 @@ func (k msgServer) RegisterTopLevelDomain(goCtx context.Context, msg *types.MsgR
return nil, errorsmod.Wrapf(types.ErrInvalidRegistrationPeriod, "%d year(s)", msg.RegistrationPeriodInYear)
}

currentTime := ctx.BlockTime()
expirationDate := currentTime.AddDate(0, 0, params.OneYearInDays*int(msg.RegistrationPeriodInYear))
accessControl := map[string]types.DomainRole{
msg.Creator: types.DomainRole_OWNER,
}

defaultRegistrationConfig := types.GetDefaultSubdomainConfig(3030)
domain := types.TopLevelDomain{
Name: msg.Name,
ExpirationDate: expirationDate,
Metadata: nil,
SubdomainConfig: &defaultRegistrationConfig,
AccessControl: accessControl,
TotalWithdrawalAmount: sdk.NewCoins(),
}

fee, err := k.Keeper.RegisterTopLevelDomain(ctx, domain, msg.Creator, msg.RegistrationPeriodInYear)
topLevelDomain, fee, err := k.Keeper.RegisterTopLevelDomain(ctx, msg.Creator, msg.Name, msg.RegistrationPeriodInYear)
if err != nil {
return nil, err
}

return &types.MsgRegisterTopLevelDomainResponse{
TopLevelDomain: &domain,
TopLevelDomain: &topLevelDomain,
Fee: &fee,
}, nil
}
139 changes: 66 additions & 73 deletions x/registry/keeper/top_level_domain.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package keeper

import (
"fmt"
"time"

errorsmod "cosmossdk.io/errors"
"cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"

Expand Down Expand Up @@ -70,6 +68,12 @@ func (k Keeper) GetAllTopLevelDomain(ctx sdk.Context) (list []types.TopLevelDoma
return
}

// Get is top-level-domain already taken
func (k Keeper) GetIsTopLevelDomainAlreadyTaken(ctx sdk.Context, domain types.TopLevelDomain) (isDomainAlreadyTaken bool) {
_, isDomainAlreadyTaken = k.GetTopLevelDomain(ctx, domain.Name)
return isDomainAlreadyTaken
}

// Get valid top level domain
func (k Keeper) GetValidTopLevelDomain(ctx sdk.Context, name string) (topLevelDomain types.TopLevelDomain, err error) {
// Regex validation
Expand All @@ -92,58 +96,14 @@ func (k Keeper) GetValidTopLevelDomain(ctx sdk.Context, name string) (topLevelDo
return topLevelDomain, nil
}

// Get burn weight
func (k Keeper) GetBurnWeight(ctx sdk.Context) (weight math.LegacyDec, err error) {
inflation := k.mintKeeper.GetMinter(ctx).Inflation
bondedRatio := k.mintKeeper.BondedRatio(ctx)

// TODO: Get alpha from params
stakingInflationRatio := k.GetParams(ctx).StakingInflationRatio
alpha := math.LegacyMustNewDecFromStr(fmt.Sprintf("%f", stakingInflationRatio))

w1 := alpha.Mul(bondedRatio)
w2 := inflation.Mul(math.LegacyMustNewDecFromStr("1").Sub(alpha))
weight = w1.Add(w2)
return weight, nil
}

// Pay TLD registration fee
func (k Keeper) PayTopLevelDomainFee(ctx sdk.Context, payer sdk.AccAddress, domain types.TopLevelDomain, registrationPeriodInYear uint64) (registrationFee types.TopLevelDomainFee, err error) {
// TODO: Support other denoms
denom := params.DefaultBondDenom

// Get base fee
baseFeeInUsd := k.GetParams(ctx).TopLevelDomainBaseFeeInUsd
if baseFeeInUsd == 0 {
panic("base fee is not set")
}

// Get Registration fee (=X)
fee, err := domain.GetRegistrationFeeAmountInDenom(denom, baseFeeInUsd, registrationPeriodInYear)
// Get registration fee
registrationFee, err = k.GetTopLevelDomainFee(ctx, domain, registrationPeriodInYear)
if err != nil {
return types.TopLevelDomainFee{}, err
}

// Get burn weight (=W)
weight, err := k.GetBurnWeight(ctx)
if err != nil {
return types.TopLevelDomainFee{}, err
}
registrationFee.BurnWeight = weight.String()

// Get price (=P)
price, err := types.GetMycelPrice(denom)
if err != nil {
return types.TopLevelDomainFee{}, err
}

// Calc burn amount (=WX/P)
amountToBurn := weight.Mul(math.LegacyNewDecFromBigInt(fee.BigInt())).Quo(math.LegacyNewDecFromBigInt(price.BigInt())).TruncateInt()
amountToTreasury := fee.Sub(amountToBurn)

registrationFee.FeeToBurn = sdk.NewCoin(denom, amountToBurn)
registrationFee.FeeToTreasury = sdk.NewCoin(denom, amountToTreasury)

// Send coins to treasury
err = k.distributionKeeper.FundCommunityPool(ctx, sdk.NewCoins(registrationFee.FeeToTreasury), payer)
if err != nil {
Expand Down Expand Up @@ -171,58 +131,91 @@ func (k Keeper) PayTopLevelDomainFee(ctx sdk.Context, payer sdk.AccAddress, doma
return registrationFee, nil
}

// Register top level domain
func (k Keeper) RegisterTopLevelDomain(ctx sdk.Context, domain types.TopLevelDomain, creator string, registrationPeriodInYear uint64) (fee types.TopLevelDomainFee, err error) {
creatorAddress, err := sdk.AccAddressFromBech32(creator)
// Register top-level-domain
func (k Keeper) RegisterTopLevelDomain(ctx sdk.Context, creator string, domainName string, registrationPeriodInYear uint64) (topLevelDomain types.TopLevelDomain, fee types.TopLevelDomainFee, err error) {
// Create top-level-domain
currentTime := ctx.BlockTime()
expirationDate := currentTime.AddDate(0, 0, params.OneYearInDays*int(registrationPeriodInYear))
accessControl := map[string]types.DomainRole{
creator: types.DomainRole_OWNER,
}
defaultRegistrationConfig := types.GetDefaultSubdomainConfig(303)
topLevelDomain = types.TopLevelDomain{
Name: domainName,
ExpirationDate: expirationDate,
Metadata: nil,
SubdomainConfig: &defaultRegistrationConfig,
AccessControl: accessControl,
TotalWithdrawalAmount: sdk.NewCoins(),
}

// Validate top-level-domain
err = topLevelDomain.Validate()
if err != nil {
return types.TopLevelDomainFee{}, err
return types.TopLevelDomain{}, types.TopLevelDomainFee{}, err
}

// Validate domain
err = k.ValidateTopLevelDomain(ctx, domain)
if err != nil {
return types.TopLevelDomainFee{}, err
// Check if top-level-domain is already taken
isTaken := k.GetIsTopLevelDomainAlreadyTaken(ctx, topLevelDomain)
if isTaken {
err = errorsmod.Wrapf(types.ErrDomainIsAlreadyTaken, "%s", topLevelDomain.Name)
return types.TopLevelDomain{}, types.TopLevelDomainFee{}, err
}

// Pay TLD registration fee
fee, err = k.PayTopLevelDomainFee(ctx, creatorAddress, domain, registrationPeriodInYear)
creatorAddress, err := sdk.AccAddressFromBech32(creator)
if err != nil {
return types.TopLevelDomainFee{}, err
return types.TopLevelDomain{}, types.TopLevelDomainFee{}, err
}
fee, err = k.PayTopLevelDomainFee(ctx, creatorAddress, topLevelDomain, registrationPeriodInYear)
if err != nil {
return types.TopLevelDomain{}, types.TopLevelDomainFee{}, err
}

// Set domain
k.SetTopLevelDomain(ctx, domain)
k.SetTopLevelDomain(ctx, topLevelDomain)

// Emit event
EmitRegisterTopLevelDomainEvent(ctx, domain, fee)
EmitRegisterTopLevelDomainEvent(ctx, topLevelDomain, fee)

return fee, nil
return topLevelDomain, fee, nil
}

// Extend top level domain expirationDate
func (k Keeper) ExtendTopLevelDomainExpirationDate(ctx sdk.Context, creator string, domain *types.TopLevelDomain, extensionPeriodInYear uint64) (fee types.TopLevelDomainFee, err error) {
// Extend expiration date
func (k Keeper) ExtendTopLevelDomainExpirationDate(ctx sdk.Context, creator string, domainName string, extensionPeriodInYear uint64) (topLevelDomain types.TopLevelDomain, fee types.TopLevelDomainFee, err error) {
// Get domain
topLevelDomain, found := k.GetTopLevelDomain(ctx, domainName)
if !found {
return types.TopLevelDomain{}, types.TopLevelDomainFee{}, errorsmod.Wrapf(types.ErrDomainNotFound, "%s", domainName)
}

// Check if the domain is editable
_, err = topLevelDomain.IsEditable(creator)
if err != nil {
return types.TopLevelDomain{}, types.TopLevelDomainFee{}, err
}

creatorAddress, err := sdk.AccAddressFromBech32(creator)
if err != nil {
return types.TopLevelDomainFee{}, err
return types.TopLevelDomain{}, types.TopLevelDomainFee{}, err
}
// Check if the domain is editable
_, err = domain.IsEditable(creator)
_, err = topLevelDomain.IsEditable(creator)
if err != nil {
return types.TopLevelDomainFee{}, err
return types.TopLevelDomain{}, types.TopLevelDomainFee{}, err
}

// Pay TLD extend fee
fee, err = k.PayTopLevelDomainFee(ctx, creatorAddress, *domain, extensionPeriodInYear)
fee, err = k.PayTopLevelDomainFee(ctx, creatorAddress, topLevelDomain, extensionPeriodInYear)
if err != nil {
return types.TopLevelDomainFee{}, err
return types.TopLevelDomain{}, types.TopLevelDomainFee{}, err
}

// Update domain store
domain.ExtendExpirationDate(domain.ExpirationDate, extensionPeriodInYear)
k.SetTopLevelDomain(ctx, *domain)
topLevelDomain.ExtendExpirationDate(topLevelDomain.ExpirationDate, extensionPeriodInYear)
k.SetTopLevelDomain(ctx, topLevelDomain)

// Emit event
EmitExtendTopLevelDomainExpirationDateEvent(ctx, *domain, fee)
EmitExtendTopLevelDomainExpirationDateEvent(ctx, topLevelDomain, fee)

return fee, err
return topLevelDomain, fee, err
}
Loading

0 comments on commit e280265

Please sign in to comment.