Skip to content

Commit 5e92edf

Browse files
author
nashqueue
committed
finished tutorial
1 parent 251282a commit 5e92edf

File tree

4 files changed

+91
-6
lines changed

4 files changed

+91
-6
lines changed
Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,64 @@
1+
// x/nameservice/keeper/msg_server_buy_name.go
2+
13
package keeper
24

35
import (
46
"context"
57

68
sdk "github.com/cosmos/cosmos-sdk/types"
9+
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
10+
711
"nameservice/x/nameservice/types"
812
)
913

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

13-
// TODO: Handling the message
14-
_ = ctx
17+
// Try getting a name from the store
18+
whois, isFound := k.GetWhois(ctx, msg.Name)
19+
20+
// Set the price at which the name has to be bought if it didn't have an owner before
21+
minPrice := sdk.Coins{sdk.NewInt64Coin("token", 10)}
22+
23+
// Convert price and bid strings to sdk.Coins
24+
price, _ := sdk.ParseCoinsNormalized(whois.Price)
25+
bid, _ := sdk.ParseCoinsNormalized(msg.Bid)
26+
27+
// Convert owner and buyer address strings to sdk.AccAddress
28+
owner, _ := sdk.AccAddressFromBech32(whois.Owner)
29+
buyer, _ := sdk.AccAddressFromBech32(msg.Creator)
30+
31+
// If a name is found in store
32+
if isFound {
33+
// If the current price is higher than the bid
34+
if price.IsAllGT(bid) {
35+
// Throw an error
36+
return nil, sdkerrors.Wrap(sdkerrors.ErrInsufficientFunds, "Bid is not high enough")
37+
}
38+
39+
// Otherwise (when the bid is higher), send tokens from the buyer to the owner
40+
k.bankKeeper.SendCoins(ctx, buyer, owner, bid)
41+
} else { // If the name is not found in the store
42+
// If the minimum price is higher than the bid
43+
if minPrice.IsAllGT(bid) {
44+
// Throw an error
45+
return nil, sdkerrors.Wrap(sdkerrors.ErrInsufficientFunds, "Bid is less than min amount")
46+
}
47+
48+
// Otherwise (when the bid is higher), send tokens from the buyer's account to the module's account (as a payment for the name)
49+
k.bankKeeper.SendCoinsFromAccountToModule(ctx, buyer, types.ModuleName, bid)
50+
}
51+
52+
// Create an updated whois record
53+
newWhois := types.Whois{
54+
Index: msg.Name,
55+
Name: msg.Name,
56+
Value: whois.Value,
57+
Price: bid.String(),
58+
Owner: buyer.String(),
59+
}
1560

61+
// Write whois information to the store
62+
k.SetWhois(ctx, newWhois)
1663
return &types.MsgBuyNameResponse{}, nil
1764
}
Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,33 @@
1+
// x/nameservice/keeper/msg_server_delete_name.go
2+
13
package keeper
24

35
import (
46
"context"
57

68
sdk "github.com/cosmos/cosmos-sdk/types"
9+
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
10+
711
"nameservice/x/nameservice/types"
812
)
913

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

13-
// TODO: Handling the message
14-
_ = ctx
17+
// Try getting name information from the store
18+
whois, isFound := k.GetWhois(ctx, msg.Name)
19+
20+
// If a name is not found, throw an error
21+
if !isFound {
22+
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Name doesn't exist")
23+
}
24+
25+
// If the message sender address doesn't match the name owner, throw an error
26+
if !(whois.Owner == msg.Creator) {
27+
return nil, sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "Incorrect Owner")
28+
}
1529

30+
// Otherwise, remove the name information from the store
31+
k.RemoveWhois(ctx, msg.Name)
1632
return &types.MsgDeleteNameResponse{}, nil
1733
}
Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,37 @@
1+
// x/nameservice/keeper/msg_server_set_name.go
2+
13
package keeper
24

35
import (
46
"context"
57

68
sdk "github.com/cosmos/cosmos-sdk/types"
9+
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
10+
711
"nameservice/x/nameservice/types"
812
)
913

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

13-
// TODO: Handling the message
14-
_ = ctx
17+
// Try getting name information from the store
18+
whois, _ := k.GetWhois(ctx, msg.Name)
19+
20+
// If the message sender address doesn't match the name owner, throw an error
21+
if !(msg.Creator == whois.Owner) {
22+
return nil, sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "Incorrect Owner")
23+
}
24+
25+
// Otherwise, create an updated whois record
26+
newWhois := types.Whois{
27+
Index: msg.Name,
28+
Name: msg.Name,
29+
Value: msg.Value,
30+
Owner: whois.Owner,
31+
Price: whois.Price,
32+
}
1533

34+
// Write whois information to the store
35+
k.SetWhois(ctx, newWhois)
1636
return &types.MsgSetNameResponse{}, nil
1737
}

x/nameservice/types/expected_keepers.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,6 @@ type AccountKeeper interface {
1515
type BankKeeper interface {
1616
SpendableCoins(ctx sdk.Context, addr sdk.AccAddress) sdk.Coins
1717
// Methods imported from bank should be defined here
18+
SendCoinsFromAccountToModule(ctx sdk.Context, senderAddr sdk.AccAddress, recipientModule string, amt sdk.Coins) error
19+
SendCoins(ctx sdk.Context, fromAddr sdk.AccAddress, toAddr sdk.AccAddress, amt sdk.Coins) error
1820
}

0 commit comments

Comments
 (0)