-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
nashqueue
committed
Aug 24, 2022
1 parent
251282a
commit 5e92edf
Showing
4 changed files
with
91 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters