From e280265591b5159af722447653845ad38a596070 Mon Sep 17 00:00:00 2001 From: tarumi Date: Thu, 2 Nov 2023 19:14:29 +0100 Subject: [PATCH] refactor tld keeper --- docs/static/openapi.yml | 28 ++-- ...extend_top_level_domain_expiration_date.go | 16 +- .../msg_server_register_top_level_domain.go | 21 +-- x/registry/keeper/top_level_domain.go | 139 +++++++++--------- x/registry/keeper/top_level_domain_fee.go | 65 ++++++++ x/registry/keeper/validate_registration.go | 36 ----- 6 files changed, 149 insertions(+), 156 deletions(-) create mode 100644 x/registry/keeper/top_level_domain_fee.go diff --git a/docs/static/openapi.yml b/docs/static/openapi.yml index 2fe8a355..7b9325ec 100644 --- a/docs/static/openapi.yml +++ b/docs/static/openapi.yml @@ -50303,9 +50303,9 @@ paths: properties: name: type: string - expirationDateInUnixNano: + expirationDate: type: string - format: int64 + format: date-time metadata: type: object additionalProperties: @@ -50531,9 +50531,9 @@ paths: properties: name: type: string - expirationDateInUnixNano: + expirationDate: type: string - format: int64 + format: date-time metadata: type: object additionalProperties: @@ -81193,9 +81193,9 @@ definitions: properties: name: type: string - expirationDateInUnixNano: + expirationDate: type: string - format: int64 + format: date-time metadata: type: object additionalProperties: @@ -81361,9 +81361,9 @@ definitions: properties: name: type: string - expirationDateInUnixNano: + expirationDate: type: string - format: int64 + format: date-time metadata: type: object additionalProperties: @@ -81653,9 +81653,9 @@ definitions: properties: name: type: string - expirationDateInUnixNano: + expirationDate: type: string - format: int64 + format: date-time metadata: type: object additionalProperties: @@ -81838,9 +81838,9 @@ definitions: properties: name: type: string - expirationDateInUnixNano: + expirationDate: type: string - format: int64 + format: date-time metadata: type: object additionalProperties: @@ -82107,9 +82107,9 @@ definitions: properties: name: type: string - expirationDateInUnixNano: + expirationDate: type: string - format: int64 + format: date-time metadata: type: object additionalProperties: diff --git a/x/registry/keeper/msg_server_extend_top_level_domain_expiration_date.go b/x/registry/keeper/msg_server_extend_top_level_domain_expiration_date.go index 62ed7218..a79e6530 100644 --- a/x/registry/keeper/msg_server_extend_top_level_domain_expiration_date.go +++ b/x/registry/keeper/msg_server_extend_top_level_domain_expiration_date.go @@ -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" @@ -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 } diff --git a/x/registry/keeper/msg_server_register_top_level_domain.go b/x/registry/keeper/msg_server_register_top_level_domain.go index a9c23ea9..18d44992 100644 --- a/x/registry/keeper/msg_server_register_top_level_domain.go +++ b/x/registry/keeper/msg_server_register_top_level_domain.go @@ -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) { @@ -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 } diff --git a/x/registry/keeper/top_level_domain.go b/x/registry/keeper/top_level_domain.go index 789c2eaf..57ca75ed 100644 --- a/x/registry/keeper/top_level_domain.go +++ b/x/registry/keeper/top_level_domain.go @@ -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" @@ -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 @@ -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 { @@ -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 } diff --git a/x/registry/keeper/top_level_domain_fee.go b/x/registry/keeper/top_level_domain_fee.go new file mode 100644 index 00000000..1542b3f0 --- /dev/null +++ b/x/registry/keeper/top_level_domain_fee.go @@ -0,0 +1,65 @@ +package keeper + +import ( + "fmt" + + "cosmossdk.io/math" + sdk "github.com/cosmos/cosmos-sdk/types" + + "github.com/mycel-domain/mycel/app/params" + "github.com/mycel-domain/mycel/x/registry/types" +) + +// 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 +} + +// Get top-level-domain fee +func (k Keeper) GetTopLevelDomainFee(ctx sdk.Context, topLevelDomain types.TopLevelDomain, registrationPeriodInYear uint64) (topLevelDomainFee 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 := topLevelDomain.GetRegistrationFeeAmountInDenom(denom, baseFeeInUsd, registrationPeriodInYear) + if err != nil { + return types.TopLevelDomainFee{}, err + } + + // Get burn weight (=W) + weight, err := k.GetBurnWeight(ctx) + if err != nil { + return types.TopLevelDomainFee{}, err + } + topLevelDomainFee.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) + topLevelDomainFee.FeeToBurn = sdk.NewCoin(denom, amountToBurn) + topLevelDomainFee.FeeToTreasury = sdk.NewCoin(denom, amountToTreasury) + + return topLevelDomainFee, nil +} diff --git a/x/registry/keeper/validate_registration.go b/x/registry/keeper/validate_registration.go index 544acbb6..c2425693 100644 --- a/x/registry/keeper/validate_registration.go +++ b/x/registry/keeper/validate_registration.go @@ -20,19 +20,6 @@ func (k Keeper) GetIsParentDomainExist(ctx sdk.Context, domain types.SecondLevel return isParentDomainExist } -// Get is domain already taken -func (k Keeper) GetIsTopLevelDomainAlreadyTaken(ctx sdk.Context, domain types.TopLevelDomain) (isDomainAlreadyTaken bool) { - _, isDomainAlreadyTaken = k.GetTopLevelDomain(ctx, domain.Name) - return isDomainAlreadyTaken -} - -// Validate TLD registration -func (k Keeper) ValidateRegisterTLD(ctx sdk.Context, domain types.TopLevelDomain) (err error) { - // TODO: Validate TLD - // TODO: Is Staked enough to register TLD - return err -} - // Validate SLD registration func (k Keeper) ValidateRegisterSLD(ctx sdk.Context, domain types.SecondLevelDomain) (err error) { isParentDomainExist := k.GetIsParentDomainExist(ctx, domain) @@ -84,26 +71,3 @@ func (k Keeper) ValidateSecondLevelDomain(ctx sdk.Context, domain types.SecondLe return err } - -// Validate top-level-domain -func (k Keeper) ValidateTopLevelDomain(ctx sdk.Context, domain types.TopLevelDomain) (err error) { - // Type check - err = domain.Validate() - if err != nil { - return err - } - // Check if domain is already taken - isDomainAlreadyTaken := k.GetIsTopLevelDomainAlreadyTaken(ctx, domain) - if isDomainAlreadyTaken { - err = errorsmod.Wrapf(types.ErrDomainIsAlreadyTaken, "%s", domain.Name) - return err - } - - // Validate TLD - err = k.ValidateRegisterTLD(ctx, domain) - if err != nil { - return err - } - - return err -}