From ad59f146b4720d40d867588f9b3b9bde37afdeb0 Mon Sep 17 00:00:00 2001 From: tarumi Date: Thu, 2 Nov 2023 14:08:51 +0100 Subject: [PATCH] rm register_*_domain and mv func to *_domain.go --- .../keeper/register_second_level_domain.go | 109 ----------------- .../keeper/register_top_level_domain.go | 113 ------------------ x/registry/keeper/second_level_domain.go | 112 ++++++++++++++++- x/registry/keeper/top_level_domain.go | 109 ++++++++++++++++- 4 files changed, 217 insertions(+), 226 deletions(-) delete mode 100644 x/registry/keeper/register_second_level_domain.go delete mode 100644 x/registry/keeper/register_top_level_domain.go diff --git a/x/registry/keeper/register_second_level_domain.go b/x/registry/keeper/register_second_level_domain.go deleted file mode 100644 index 6a5b456a..00000000 --- a/x/registry/keeper/register_second_level_domain.go +++ /dev/null @@ -1,109 +0,0 @@ -package keeper - -import ( - "github.com/mycel-domain/mycel/x/registry/types" - - errorsmod "cosmossdk.io/errors" - sdk "github.com/cosmos/cosmos-sdk/types" -) - -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 -} - -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 -} - -// 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 -} - -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}}}) - } -} - -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) -} - -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 -} diff --git a/x/registry/keeper/register_top_level_domain.go b/x/registry/keeper/register_top_level_domain.go deleted file mode 100644 index fbc9db28..00000000 --- a/x/registry/keeper/register_top_level_domain.go +++ /dev/null @@ -1,113 +0,0 @@ -package keeper - -import ( - "fmt" - - "cosmossdk.io/math" - - "github.com/mycel-domain/mycel/x/registry/types" - - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/mycel-domain/mycel/app/params" - furnacetypes "github.com/mycel-domain/mycel/x/furnace/types" -) - -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) PayTLDRegstrationFee(ctx sdk.Context, payer sdk.AccAddress, domain types.TopLevelDomain, registrationPeriodInYear uint64) (registrationFee types.TopLevelDomainRegistrationFee, 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) - if err != nil { - return types.TopLevelDomainRegistrationFee{}, err - } - - // Get burn weight (=W) - weight, err := k.GetBurnWeight(ctx) - if err != nil { - return types.TopLevelDomainRegistrationFee{}, err - } - registrationFee.BurnWeight = weight - - // Get price (=P) - price, err := types.GetMycelPrice(denom) - if err != nil { - return types.TopLevelDomainRegistrationFee{}, err - } - - // Calc burn amount (=WX/P) - amountToBurn := weight.Mul(math.LegacyNewDecFromBigInt(fee.BigInt())).Quo(math.LegacyNewDecFromBigInt(price.BigInt())).TruncateInt() - amountToTreasury := fee.Sub(amountToBurn) - - registrationFee.RegistrationFeeToBurn = sdk.NewCoin(denom, amountToBurn) - registrationFee.RegistrationFeeToTreasury = sdk.NewCoin(denom, amountToTreasury) - - // Send coins to treasury - err = k.distributionKeeper.FundCommunityPool(ctx, sdk.NewCoins(registrationFee.RegistrationFeeToTreasury), payer) - if err != nil { - return types.TopLevelDomainRegistrationFee{}, err - } - - // Send coins to furnace module - err = k.bankKeeper.SendCoinsFromAccountToModule(ctx, payer, furnacetypes.ModuleName, sdk.NewCoins(registrationFee.RegistrationFeeToBurn)) - if err != nil { - return types.TopLevelDomainRegistrationFee{}, err - } - // Store burn amount - _, err = k.furnaceKeeper.AddRegistrationFeeToBurnAmounts(ctx, registrationPeriodInYear, registrationFee.RegistrationFeeToBurn) - if err != nil { - return types.TopLevelDomainRegistrationFee{}, err - } - - // Set total registration fee - if registrationFee.RegistrationFeeToBurn.Denom == registrationFee.RegistrationFeeToTreasury.Denom { - registrationFee.TotalRegistrationFee = sdk.NewCoins(registrationFee.RegistrationFeeToBurn.Add(registrationFee.RegistrationFeeToTreasury)) - } else { - registrationFee.TotalRegistrationFee = sdk.NewCoins(registrationFee.RegistrationFeeToBurn, registrationFee.RegistrationFeeToTreasury) - } - - return registrationFee, nil -} - -func (k Keeper) RegisterTopLevelDomain(ctx sdk.Context, domain types.TopLevelDomain, owner sdk.AccAddress, registrationPeriodIYear uint64) (err error) { - // Validate domain - err = k.ValidateTopLevelDomain(ctx, domain) - if err != nil { - return err - } - - // Pay TLD registration fee - topLevelDomainRegistrationFee, err := k.PayTLDRegstrationFee(ctx, owner, domain, registrationPeriodIYear) - if err != nil { - return err - } - - // Set domain - k.SetTopLevelDomain(ctx, domain) - - // Emit event - EmitRegisterTopLevelDomainEvent(ctx, domain, topLevelDomainRegistrationFee) - - return err -} diff --git a/x/registry/keeper/second_level_domain.go b/x/registry/keeper/second_level_domain.go index 6f7be26c..0a6cf636 100644 --- a/x/registry/keeper/second_level_domain.go +++ b/x/registry/keeper/second_level_domain.go @@ -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 @@ -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 +} diff --git a/x/registry/keeper/top_level_domain.go b/x/registry/keeper/top_level_domain.go index fd9983e6..7d966925 100644 --- a/x/registry/keeper/top_level_domain.go +++ b/x/registry/keeper/top_level_domain.go @@ -1,11 +1,16 @@ 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" + + "github.com/mycel-domain/mycel/app/params" + furnacetypes "github.com/mycel-domain/mycel/x/furnace/types" "github.com/mycel-domain/mycel/x/registry/types" ) @@ -66,7 +71,6 @@ func (k Keeper) GetAllTopLevelDomain(ctx sdk.Context) (list []types.TopLevelDoma } // Get valid top level domain - func (k Keeper) GetValidTopLevelDomain(ctx sdk.Context, name string) (topLevelDomain types.TopLevelDomain, err error) { // Regex validation err = types.ValidateTopLevelDomainName(name) @@ -88,3 +92,106 @@ 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) PayTLDRegstrationFee(ctx sdk.Context, payer sdk.AccAddress, domain types.TopLevelDomain, registrationPeriodInYear uint64) (registrationFee types.TopLevelDomainRegistrationFee, 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) + if err != nil { + return types.TopLevelDomainRegistrationFee{}, err + } + + // Get burn weight (=W) + weight, err := k.GetBurnWeight(ctx) + if err != nil { + return types.TopLevelDomainRegistrationFee{}, err + } + registrationFee.BurnWeight = weight + + // Get price (=P) + price, err := types.GetMycelPrice(denom) + if err != nil { + return types.TopLevelDomainRegistrationFee{}, err + } + + // Calc burn amount (=WX/P) + amountToBurn := weight.Mul(math.LegacyNewDecFromBigInt(fee.BigInt())).Quo(math.LegacyNewDecFromBigInt(price.BigInt())).TruncateInt() + amountToTreasury := fee.Sub(amountToBurn) + + registrationFee.RegistrationFeeToBurn = sdk.NewCoin(denom, amountToBurn) + registrationFee.RegistrationFeeToTreasury = sdk.NewCoin(denom, amountToTreasury) + + // Send coins to treasury + err = k.distributionKeeper.FundCommunityPool(ctx, sdk.NewCoins(registrationFee.RegistrationFeeToTreasury), payer) + if err != nil { + return types.TopLevelDomainRegistrationFee{}, err + } + + // Send coins to furnace module + err = k.bankKeeper.SendCoinsFromAccountToModule(ctx, payer, furnacetypes.ModuleName, sdk.NewCoins(registrationFee.RegistrationFeeToBurn)) + if err != nil { + return types.TopLevelDomainRegistrationFee{}, err + } + // Store burn amount + _, err = k.furnaceKeeper.AddRegistrationFeeToBurnAmounts(ctx, registrationPeriodInYear, registrationFee.RegistrationFeeToBurn) + if err != nil { + return types.TopLevelDomainRegistrationFee{}, err + } + + // Set total registration fee + if registrationFee.RegistrationFeeToBurn.Denom == registrationFee.RegistrationFeeToTreasury.Denom { + registrationFee.TotalRegistrationFee = sdk.NewCoins(registrationFee.RegistrationFeeToBurn.Add(registrationFee.RegistrationFeeToTreasury)) + } else { + registrationFee.TotalRegistrationFee = sdk.NewCoins(registrationFee.RegistrationFeeToBurn, registrationFee.RegistrationFeeToTreasury) + } + + return registrationFee, nil +} + +// Register top level domain +func (k Keeper) RegisterTopLevelDomain(ctx sdk.Context, domain types.TopLevelDomain, owner sdk.AccAddress, registrationPeriodIYear uint64) (err error) { + // Validate domain + err = k.ValidateTopLevelDomain(ctx, domain) + if err != nil { + return err + } + + // Pay TLD registration fee + topLevelDomainRegistrationFee, err := k.PayTLDRegstrationFee(ctx, owner, domain, registrationPeriodIYear) + if err != nil { + return err + } + + // Set domain + k.SetTopLevelDomain(ctx, domain) + + // Emit event + EmitRegisterTopLevelDomainEvent(ctx, domain, topLevelDomainRegistrationFee) + + return err +}