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

Commit

Permalink
pay sld registration fee on registration
Browse files Browse the repository at this point in the history
  • Loading branch information
taryune committed Oct 18, 2023
1 parent fd061b8 commit 0ff6aea
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 37 deletions.
64 changes: 35 additions & 29 deletions x/registry/keeper/msg_server_register_second_level_domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"errors"
"fmt"

authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/mycel-domain/mycel/testutil"
"github.com/mycel-domain/mycel/x/registry/types"

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

func (suite *KeeperTestSuite) TestRegisterSecondLevelDomain() {
Expand Down Expand Up @@ -85,23 +85,21 @@ func (suite *KeeperTestSuite) TestRegisterSecondLevelDomain() {
RegistrationPeriodInYear: tc.registrationPeriodInYear,
}

// domain := &types.SecondLevelDomain{
// Name: tc.name,
// Parent: tc.parent,
// }
// parentsName := domain.ParseParent()
// parent, found := suite.app.RegistryKeeper.GetTopLevelDomain(suite.ctx, parentsName)
// suite.Require().True(found)
// beforeSubdomainCount := parent.SubdomainCount

// Run test case function
tc.fn()

// Register domain
_, err := suite.msgServer.RegisterSecondLevelDomain(suite.ctx, registerMsg)
fmt.Println("----Case_", i, "---01", err)
if tc.expErr == nil {
parent, found := suite.app.RegistryKeeper.GetTopLevelDomain(suite.ctx, tc.parent)
suite.Require().True(found)
beforeSubdomainCount := parent.SubdomainCount

moduleAddress := authtypes.NewModuleAddress(types.ModuleName)
beforeModuleBalance := suite.app.BankKeeper.GetBalance(suite.ctx, moduleAddress, types.MycelDenom)

// Register second level domain
_, err := suite.msgServer.RegisterSecondLevelDomain(suite.ctx, registerMsg)
suite.Require().Nil(err)

if err == nil {
// Evalute domain ownership
domainOwnership, found := suite.app.RegistryKeeper.GetDomainOwnership(suite.ctx, tc.creator)
suite.Require().True(found)
Expand All @@ -112,25 +110,33 @@ func (suite *KeeperTestSuite) TestRegisterSecondLevelDomain() {
suite.Require().True(found)
suite.Require().Equal(domain.AccessControl[tc.creator], types.DomainRole_OWNER)

// // Evalute if parent's subdomainCount is increased
// parent, found = suite.app.RegistryKeeper.GetTopLevelDomain(suite.ctx, parentsName)
// suite.Require().True(found)
// afterSubdomainCount := parent.SubdomainCount
// suite.Require().Equal(beforeSubdomainCount+1, afterSubdomainCount)
// Evalute if parent's subdomainCount is increased
parent, found = suite.app.RegistryKeeper.GetTopLevelDomain(suite.ctx, tc.parent)
suite.Require().True(found)
afterSubdomainCount := parent.SubdomainCount
suite.Require().Equal(beforeSubdomainCount+1, afterSubdomainCount)

// Get registration fee
config := suite.app.RegistryKeeper.GetParentsSubdomainConfig(suite.ctx, domain)
fee, err := config.GetRegistrationFee(tc.name, tc.registrationPeriodInYear)
suite.Require().Nil(err)

// Evalute if module account balance is increased
afterModuleBalance := suite.app.BankKeeper.GetBalance(suite.ctx, moduleAddress, types.MycelDenom)
suite.Require().Equal(beforeModuleBalance.Add(*fee), afterModuleBalance)

// Evalute events
suite.Require().Nil(err)
events := sdk.StringifyEvents(suite.ctx.EventManager().ABCIEvents())
eventIndex := len(events) - 1
suite.Require().EqualValues(sdk.StringEvent{
Type: types.EventTypeRegsterDomain,
Attributes: []sdk.Attribute{
{Key: types.AttributeRegisterSecondLevelDomainEventName, Value: tc.name},
{Key: types.AttributeRegisterSecondLevelDomainEventParent, Value: tc.parent},
{Key: types.AttributeRegisterSecondLevelDomainEventExpirationDate, Value: events[eventIndex].Attributes[2].Value},
},
}, events[eventIndex])
events, found := testutil.FindEventsByType(suite.ctx.EventManager().Events(), types.EventTypeRegsterDomain)
suite.Require().True(found)
for _, event := range events {
suite.Require().Equal(tc.name, event.Attributes[0].Value)
suite.Require().Equal(tc.parent, event.Attributes[1].Value)
suite.Require().Equal(fee.String(), event.Attributes[3].Value)
}
} else {
// Register second level domain
_, err := suite.msgServer.RegisterSecondLevelDomain(suite.ctx, registerMsg)
suite.Require().EqualError(err, tc.expErr.Error())
}

Expand Down
25 changes: 18 additions & 7 deletions x/registry/keeper/register_second_level_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,26 @@ func (k Keeper) GetParentsSubdomainConfig(ctx sdk.Context, domain types.SecondLe
}

// Pay SLD registration fee
func (k Keeper) PaySLDRegstrationFee(ctx sdk.Context, payer sdk.AccAddress, domain types.SecondLevelDomain, registrationPeriodInYear uint64) (err error) {
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)
fee, err = config.GetRegistrationFee(domain.Name, registrationPeriodInYear)
if err != nil {
return err
return fee, err
}

// TODO: Pay fee
fee = fee
return 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.RegistrationFees.Add(*fee)
k.SetTopLevelDomain(ctx, parent)

return fee, err
}

func (k Keeper) AppendToOwnedDomain(ctx sdk.Context, owner string, name string, parent string) {
Expand Down Expand Up @@ -89,8 +98,9 @@ func (k Keeper) RegisterSecondLevelDomain(ctx sdk.Context, domain types.SecondLe

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

// Pay SLD registration fee
err = k.PaySLDRegstrationFee(ctx, owner, domain, registrationPeriodIYear)
fee, err := k.PaySLDRegstrationFee(ctx, owner, domain, registrationPeriodIYear)
if err != nil {
return err
}
Expand All @@ -107,6 +117,7 @@ func (k Keeper) RegisterSecondLevelDomain(ctx sdk.Context, domain types.SecondLe
sdk.NewAttribute(types.AttributeRegisterSecondLevelDomainEventName, domain.Name),
sdk.NewAttribute(types.AttributeRegisterSecondLevelDomainEventParent, domain.Parent),
sdk.NewAttribute(types.AttributeRegisterSecondLevelDomainEventExpirationDate, strconv.FormatInt(domain.ExpirationDate, 10)),
sdk.NewAttribute(types.AttributeRegisterSecondLevelDomainEventRegistrationFee, fee.String()),
),
)

Expand Down
3 changes: 2 additions & 1 deletion x/registry/types/events.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package types

// Register domain event
// Register second-level-domain event
const (
EventTypeRegsterDomain = "register-domain"

AttributeRegisterSecondLevelDomainEventName = "name"
AttributeRegisterSecondLevelDomainEventParent = "parent"
AttributeRegisterSecondLevelDomainEventExpirationDate = "expiration-date"
AttributeRegisterSecondLevelDomainEventRegistrationFee = "registration-fee"
)

// Register top-level-domain event
Expand Down

0 comments on commit 0ff6aea

Please sign in to comment.