Skip to content

Commit

Permalink
finished tutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
nashqueue committed Aug 24, 2022
1 parent 251282a commit 5e92edf
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 6 deletions.
51 changes: 49 additions & 2 deletions x/nameservice/keeper/msg_server_buy_name.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,64 @@
// x/nameservice/keeper/msg_server_buy_name.go

package keeper

import (
"context"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"

"nameservice/x/nameservice/types"
)

func (k msgServer) BuyName(goCtx context.Context, msg *types.MsgBuyName) (*types.MsgBuyNameResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

// TODO: Handling the message
_ = ctx
// Try getting a name from the store
whois, isFound := k.GetWhois(ctx, msg.Name)

// Set the price at which the name has to be bought if it didn't have an owner before
minPrice := sdk.Coins{sdk.NewInt64Coin("token", 10)}

// Convert price and bid strings to sdk.Coins
price, _ := sdk.ParseCoinsNormalized(whois.Price)
bid, _ := sdk.ParseCoinsNormalized(msg.Bid)

// Convert owner and buyer address strings to sdk.AccAddress
owner, _ := sdk.AccAddressFromBech32(whois.Owner)
buyer, _ := sdk.AccAddressFromBech32(msg.Creator)

// If a name is found in store
if isFound {
// If the current price is higher than the bid
if price.IsAllGT(bid) {
// Throw an error
return nil, sdkerrors.Wrap(sdkerrors.ErrInsufficientFunds, "Bid is not high enough")
}

// Otherwise (when the bid is higher), send tokens from the buyer to the owner
k.bankKeeper.SendCoins(ctx, buyer, owner, bid)
} else { // If the name is not found in the store
// If the minimum price is higher than the bid
if minPrice.IsAllGT(bid) {
// Throw an error
return nil, sdkerrors.Wrap(sdkerrors.ErrInsufficientFunds, "Bid is less than min amount")
}

// Otherwise (when the bid is higher), send tokens from the buyer's account to the module's account (as a payment for the name)
k.bankKeeper.SendCoinsFromAccountToModule(ctx, buyer, types.ModuleName, bid)
}

// Create an updated whois record
newWhois := types.Whois{
Index: msg.Name,
Name: msg.Name,
Value: whois.Value,
Price: bid.String(),
Owner: buyer.String(),
}

// Write whois information to the store
k.SetWhois(ctx, newWhois)
return &types.MsgBuyNameResponse{}, nil
}
20 changes: 18 additions & 2 deletions x/nameservice/keeper/msg_server_delete_name.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
// x/nameservice/keeper/msg_server_delete_name.go

package keeper

import (
"context"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"

"nameservice/x/nameservice/types"
)

func (k msgServer) DeleteName(goCtx context.Context, msg *types.MsgDeleteName) (*types.MsgDeleteNameResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

// TODO: Handling the message
_ = ctx
// Try getting name information from the store
whois, isFound := k.GetWhois(ctx, msg.Name)

// If a name is not found, throw an error
if !isFound {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Name doesn't exist")
}

// If the message sender address doesn't match the name owner, throw an error
if !(whois.Owner == msg.Creator) {
return nil, sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "Incorrect Owner")
}

// Otherwise, remove the name information from the store
k.RemoveWhois(ctx, msg.Name)
return &types.MsgDeleteNameResponse{}, nil
}
24 changes: 22 additions & 2 deletions x/nameservice/keeper/msg_server_set_name.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,37 @@
// x/nameservice/keeper/msg_server_set_name.go

package keeper

import (
"context"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"

"nameservice/x/nameservice/types"
)

func (k msgServer) SetName(goCtx context.Context, msg *types.MsgSetName) (*types.MsgSetNameResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

// TODO: Handling the message
_ = ctx
// Try getting name information from the store
whois, _ := k.GetWhois(ctx, msg.Name)

// If the message sender address doesn't match the name owner, throw an error
if !(msg.Creator == whois.Owner) {
return nil, sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "Incorrect Owner")
}

// Otherwise, create an updated whois record
newWhois := types.Whois{
Index: msg.Name,
Name: msg.Name,
Value: msg.Value,
Owner: whois.Owner,
Price: whois.Price,
}

// Write whois information to the store
k.SetWhois(ctx, newWhois)
return &types.MsgSetNameResponse{}, nil
}
2 changes: 2 additions & 0 deletions x/nameservice/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ type AccountKeeper interface {
type BankKeeper interface {
SpendableCoins(ctx sdk.Context, addr sdk.AccAddress) sdk.Coins
// Methods imported from bank should be defined here
SendCoinsFromAccountToModule(ctx sdk.Context, senderAddr sdk.AccAddress, recipientModule string, amt sdk.Coins) error
SendCoins(ctx sdk.Context, fromAddr sdk.AccAddress, toAddr sdk.AccAddress, amt sdk.Coins) error
}

0 comments on commit 5e92edf

Please sign in to comment.