Skip to content

Commit

Permalink
Rename mint price denom param to price denom
Browse files Browse the repository at this point in the history
  • Loading branch information
elise-ng committed Jul 1, 2022
1 parent 24a4645 commit 5d8353b
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 56 deletions.
2 changes: 1 addition & 1 deletion proto/likechain/likenft/params.proto
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 10,5 @@ option go_package = "github.com/likecoin/likechain/x/likenft/types";
message Params {
option (gogoproto.goproto_stringer) = false;

string mint_price_denom = 1;
string price_denom = 1;
}
6 changes: 3 additions & 3 deletions x/likenft/keeper/msg_server_buy_nft.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 42,7 @@ func (k msgServer) BuyNFT(goCtx context.Context, msg *types.MsgBuyNFT) (*types.M
}

// check user has enough balance
if k.bankKeeper.GetBalance(ctx, buyerAddress, k.GetParams(ctx).MintPriceDenom).Amount.Uint64() < msg.Price {
if k.bankKeeper.GetBalance(ctx, buyerAddress, k.GetParams(ctx).PriceDenom).Amount.Uint64() < msg.Price {
return nil, types.ErrFailedToBuyNFT.Wrapf("User does not have enough balance")
}

Expand All @@ -62,15 62,15 @@ func (k msgServer) BuyNFT(goCtx context.Context, msg *types.MsgBuyNFT) (*types.M
if err != nil {
return nil, err
}
royaltyAmountCoins := sdk.NewCoins(sdk.NewCoin(k.GetParams(ctx).MintPriceDenom, sdk.NewInt(int64(royaltyAmount))))
royaltyAmountCoins := sdk.NewCoins(sdk.NewCoin(k.GetParams(ctx).PriceDenom, sdk.NewInt(int64(royaltyAmount))))
err = k.bankKeeper.SendCoins(ctx, buyerAddress, classParent.Owner, royaltyAmountCoins)
if err != nil {
return nil, types.ErrFailedToBuyNFT.Wrapf(err.Error())
}
}
// pay seller
netAmount := msg.Price - royaltyAmount
netAmountCoins := sdk.NewCoins(sdk.NewCoin(k.GetParams(ctx).MintPriceDenom, sdk.NewInt(int64(netAmount))))
netAmountCoins := sdk.NewCoins(sdk.NewCoin(k.GetParams(ctx).PriceDenom, sdk.NewInt(int64(netAmount))))
err = k.bankKeeper.SendCoins(ctx, buyerAddress, sellerAddress, netAmountCoins)
if err != nil {
return nil, types.ErrFailedToBuyNFT.Wrapf(err.Error())
Expand Down
4 changes: 2 additions & 2 deletions x/likenft/keeper/msg_server_mint_nft.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 52,12 @@ func (k msgServer) mintBlindBoxNFT(ctx sdk.Context, classId string, classData *t

// Pay price to owner if mintPrice is not zero and the minter is not the owner
if !ownerAddress.Equals(userAddress) && mintPeriod.MintPrice > 0 {
spentableTokens := k.bankKeeper.GetBalance(ctx, userAddress, params.GetMintPriceDenom())
spentableTokens := k.bankKeeper.GetBalance(ctx, userAddress, params.GetPriceDenom())
if spentableTokens.Amount.Uint64() < mintPeriod.MintPrice {
return nil, types.ErrInsufficientFunds.Wrapf("insufficient funds to mint tokenId %s", tokenId)
}

err = k.bankKeeper.SendCoins(ctx, userAddress, ownerAddress, sdk.NewCoins(sdk.NewCoin(params.GetMintPriceDenom(), sdk.NewInt(int64(mintPeriod.MintPrice)))))
err = k.bankKeeper.SendCoins(ctx, userAddress, ownerAddress, sdk.NewCoins(sdk.NewCoin(params.GetPriceDenom(), sdk.NewInt(int64(mintPeriod.MintPrice)))))
if err != nil {
return nil, types.ErrFailedToMintNFT.Wrapf("%s", err.Error())
}
Expand Down
7 changes: 3 additions & 4 deletions x/likenft/keeper/msg_server_offer.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 47,7 @@ func (k msgServer) CreateOffer(goCtx context.Context, msg *types.MsgCreateOffer)

// Take deposit if needed
if offer.Price > 0 {
// TODO: rename mint price denom
denom := k.MintPriceDenom(ctx)
denom := k.PriceDenom(ctx)
coins := sdk.NewCoins(sdk.NewCoin(denom, sdk.NewInt(int64(offer.Price))))
if k.bankKeeper.GetBalance(ctx, userAddress, denom).Amount.Uint64() < offer.Price {
return nil, types.ErrInsufficientFunds
Expand Down Expand Up @@ -118,7 117,7 @@ func (k msgServer) UpdateOffer(goCtx context.Context, msg *types.MsgUpdateOffer)
// Update deposit if needed
if oldOffer.Price != newOffer.Price {
// Check user has enough fund to pay extra
denom := k.MintPriceDenom(ctx)
denom := k.PriceDenom(ctx)
priceDiff := int64(newOffer.Price) - int64(oldOffer.Price)
if priceDiff > 0 && k.bankKeeper.GetBalance(ctx, userAddress, denom).Amount.Int64() < priceDiff {
return nil, types.ErrInsufficientFunds
Expand Down Expand Up @@ -180,7 179,7 @@ func (k msgServer) DeleteOffer(goCtx context.Context, msg *types.MsgDeleteOffer)

// Refund deposit if needed
if offer.Price > 0 {
denom := k.MintPriceDenom(ctx)
denom := k.PriceDenom(ctx)
coins := sdk.NewCoins(sdk.NewCoin(denom, sdk.NewInt(int64(offer.Price))))
if err := k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, userAddress, coins); err != nil {
return nil, types.ErrFailedToDeleteOffer.Wrapf(err.Error())
Expand Down
6 changes: 3 additions & 3 deletions x/likenft/keeper/msg_server_sell_nft.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 56,23 @@ func (k msgServer) SellNFT(goCtx context.Context, msg *types.MsgSellNFT) (*types
if err != nil {
return nil, err
}
royaltyAmountCoins := sdk.NewCoins(sdk.NewCoin(k.GetParams(ctx).MintPriceDenom, sdk.NewInt(int64(royaltyAmount))))
royaltyAmountCoins := sdk.NewCoins(sdk.NewCoin(k.GetParams(ctx).PriceDenom, sdk.NewInt(int64(royaltyAmount))))
err = k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, classParent.Owner, royaltyAmountCoins)
if err != nil {
return nil, types.ErrFailedToSellNFT.Wrapf(err.Error())
}
}
// pay seller
netAmount := msg.Price - royaltyAmount
netAmountCoins := sdk.NewCoins(sdk.NewCoin(k.GetParams(ctx).MintPriceDenom, sdk.NewInt(int64(netAmount))))
netAmountCoins := sdk.NewCoins(sdk.NewCoin(k.GetParams(ctx).PriceDenom, sdk.NewInt(int64(netAmount))))
err = k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, sellerAddress, netAmountCoins)
if err != nil {
return nil, types.ErrFailedToSellNFT.Wrapf(err.Error())
}
// refund remainder to buyer
remainder := offer.Price - msg.Price
if remainder > 0 {
remainCoins := sdk.NewCoins(sdk.NewCoin(k.GetParams(ctx).MintPriceDenom, sdk.NewInt(int64(remainder))))
remainCoins := sdk.NewCoins(sdk.NewCoin(k.GetParams(ctx).PriceDenom, sdk.NewInt(int64(remainder))))
err = k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, buyerAddress, remainCoins)
if err != nil {
return nil, types.ErrFailedToSellNFT.Wrapf(err.Error())
Expand Down
2 changes: 1 addition & 1 deletion x/likenft/keeper/offer_expire.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 13,7 @@ func (k Keeper) ExpireOffer(ctx sdk.Context, offer types.OfferStoreRecord) error

// Refund deposit if needed
if offer.Price > 0 {
denom := k.MintPriceDenom(ctx)
denom := k.PriceDenom(ctx)
coins := sdk.NewCoins(sdk.NewCoin(denom, sdk.NewInt(int64(offer.Price))))
if err := k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, offer.Buyer, coins); err != nil {
return types.ErrFailedToExpireOffer.Wrapf(err.Error())
Expand Down
6 changes: 3 additions & 3 deletions x/likenft/keeper/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 8,12 @@ import (
// GetParams get all parameters as types.Params
func (k Keeper) GetParams(ctx sdk.Context) types.Params {
return types.Params{
MintPriceDenom: k.MintPriceDenom(ctx),
PriceDenom: k.PriceDenom(ctx),
}
}

func (k Keeper) MintPriceDenom(ctx sdk.Context) (res string) {
k.paramstore.Get(ctx, types.ParamKeyMintPriceDenom, &res)
func (k Keeper) PriceDenom(ctx sdk.Context) (res string) {
k.paramstore.Get(ctx, types.ParamKeyPriceDenom, &res)
return
}

Expand Down
10 changes: 5 additions & 5 deletions x/likenft/keeper/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 17,22 @@ func TestParamsValidate(t *testing.T) {
require.NoError(t, err)

params = types.Params{
MintPriceDenom: "nanolike",
PriceDenom: "nanolike",
}
err = params.Validate()
require.NoError(t, err)

params = types.Params{
MintPriceDenom: "",
PriceDenom: "",
}
err = params.Validate()
require.Error(t, err, "should not accept empty mint price denom")
require.Error(t, err, "should not accept empty price denom")

params = types.Params{
MintPriceDenom: "nanolike123!!!??",
PriceDenom: "nanolike123!!!??",
}
err = params.Validate()
require.Error(t, err, "should not accept mint price denom with invalid characters")
require.Error(t, err, "should not accept price denom with invalid characters")

params = types.Params{}
err = params.Validate()
Expand Down
8 changes: 4 additions & 4 deletions x/likenft/types/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 142,7 @@ func TestGenesisState_Validate(t *testing.T) {
valid: false,
},
{
desc: "empty mint price denom param",
desc: "empty price denom param",
genState: &types.GenesisState{
ClassesByAccountList: []types.ClassesByAccount{
{
Expand All @@ -153,13 153,13 @@ func TestGenesisState_Validate(t *testing.T) {
},
},
Params: types.Params{
MintPriceDenom: "",
PriceDenom: "",
},
},
valid: false,
},
{
desc: "invalid mint price denom param",
desc: "invalid price denom param",
genState: &types.GenesisState{
ClassesByAccountList: []types.ClassesByAccount{
{
Expand All @@ -170,7 170,7 @@ func TestGenesisState_Validate(t *testing.T) {
},
},
Params: types.Params{
MintPriceDenom: "123456",
PriceDenom: "123456",
},
},
valid: false,
Expand Down
20 changes: 10 additions & 10 deletions x/likenft/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 8,11 @@ import (
)

var (
DefaultMintPriceDenom = "nanolike"
DefaultPriceDenom = "nanolike"
)

var (
ParamKeyMintPriceDenom = []byte("MintPriceDenom")
ParamKeyPriceDenom = []byte("PriceDenom")
)

var _ paramtypes.ParamSet = (*Params)(nil)
Expand All @@ -25,18 25,18 @@ func ParamKeyTable() paramtypes.KeyTable {
// DefaultParams returns a default set of parameters
func DefaultParams() Params {
return Params{
MintPriceDenom: DefaultMintPriceDenom,
PriceDenom: DefaultPriceDenom,
}
}

// Validate Mint price denom type
func validateMintPriceDenom(i interface{}) error {
// Validate price denom type
func validatePriceDenom(i interface{}) error {
s, ok := i.(string)
if !ok {
return fmt.Errorf("Mint price denom must be string, got type %T", i)
return fmt.Errorf("Price denom must be string, got type %T", i)
}
if s == "" {
return fmt.Errorf("Mint price denom is empty")
return fmt.Errorf("Price denom is empty")
}
if err := sdk.ValidateDenom(s); err != nil {
return err
Expand All @@ -48,14 48,14 @@ func validateMintPriceDenom(i interface{}) error {
// ParamSetPairs get the params.ParamSet
func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
return paramtypes.ParamSetPairs{
paramtypes.NewParamSetPair(ParamKeyMintPriceDenom, &p.MintPriceDenom, validateMintPriceDenom),
paramtypes.NewParamSetPair(ParamKeyPriceDenom, &p.PriceDenom, validatePriceDenom),
}
}

// Validate validates the set of params
func (p Params) Validate() error {
var err error
err = validateMintPriceDenom(p.MintPriceDenom)
err = validatePriceDenom(p.PriceDenom)
if err != nil {
return err
}
Expand All @@ -65,5 65,5 @@ func (p Params) Validate() error {
// String implements the Stringer interface.
func (p Params) String() string {
return fmt.Sprintf(`Params:
Mint Price denom: %s`, p.MintPriceDenom)
Price denom: %s`, p.PriceDenom)
}
39 changes: 19 additions & 20 deletions x/likenft/types/params.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5d8353b

Please sign in to comment.