-
Notifications
You must be signed in to change notification settings - Fork 16
/
msg_server_buy.go
81 lines (59 loc) · 1.79 KB
/
msg_server_buy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package keeper
import (
"context"
"strings"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/jackalLabs/canine-chain/x/rns/types"
)
func (k Keeper) BuyName(ctx sdk.Context, sender string, nm string) error {
nm = strings.ToLower(nm)
buyer, err := sdk.AccAddressFromBech32(sender)
if err != nil {
return err
}
sale, found := k.GetForsale(ctx, nm)
if !found {
return sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "Name not for sale.")
}
n, tld, err := GetNameAndTLD(nm)
if err != nil {
return err
}
name, nfound := k.GetNames(ctx, n, tld)
if !nfound {
return sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "Name does not exist or has expired.")
}
blockHeight := ctx.BlockHeight()
if blockHeight > name.Expires {
return sdkerrors.Wrap(sdkerrors.ErrNotFound, "Name does not exist or has expired.")
}
if name.Value == sender {
return sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "You cannot buy your own name.")
}
seller, _ := sdk.AccAddressFromBech32(sale.Owner)
price, ok := sdk.NewIntFromString(sale.Price)
if !ok {
return sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "Price is not a valid number.")
}
coin := sdk.NewCoin("ujkl", price)
coins := sdk.NewCoins(coin)
err = k.bankKeeper.SendCoinsFromAccountToModule(ctx, buyer, types.ModuleName, coins)
if err != nil {
return err
}
err = k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, seller, coins)
if err != nil {
return err
}
k.RemoveForsale(ctx, sale.Name)
name.Value = sender
name.Data = "{}"
k.SetNames(ctx, name)
return nil
}
func (k msgServer) Buy(goCtx context.Context, msg *types.MsgBuy) (*types.MsgBuyResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
err := k.BuyName(ctx, msg.Creator, msg.Name)
return &types.MsgBuyResponse{}, err
}