Skip to content

Commit

Permalink
🚧 Implement logic and update existed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
WeiJun0827 committed Nov 9, 2022
1 parent cb31462 commit dd1c392
Show file tree
Hide file tree
Showing 13 changed files with 309 additions and 232 deletions.
20 changes: 16 additions & 4 deletions x/likenft/client/cli/tx_listing.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import (

func CmdCreateListing() *cobra.Command {
cmd := &cobra.Command{
Use: "create-listing [class-id] [nft-id] [price] [expiration]",
Use: "create-listing [class-id] [nft-id] [price] [expiration] [full-pay-to-royalty]",
Short: "Create a new listing",
Args: cobra.ExactArgs(4),
Args: cobra.ExactArgs(5),
RunE: func(cmd *cobra.Command, args []string) (err error) {
// Get indexes
indexClassId := args[0]
Expand All @@ -31,6 +31,11 @@ func CmdCreateListing() *cobra.Command {
return nil
}

argFullPayToRoyalty, err := strconv.ParseBool(args[4])
if err != nil {
return err
}

clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
Expand All @@ -42,6 +47,7 @@ func CmdCreateListing() *cobra.Command {
indexNftId,
argPrice,
argExpiration,
argFullPayToRoyalty,
)
if err := msg.ValidateBasic(); err != nil {
return err
Expand All @@ -57,9 +63,9 @@ func CmdCreateListing() *cobra.Command {

func CmdUpdateListing() *cobra.Command {
cmd := &cobra.Command{
Use: "update-listing [class-id] [nft-id] [price] [expiration]",
Use: "update-listing [class-id] [nft-id] [price] [expiration] [full-pay-to-royalty]",
Short: "Update a listing",
Args: cobra.ExactArgs(4),
Args: cobra.ExactArgs(5),
RunE: func(cmd *cobra.Command, args []string) (err error) {
// Get indexes
indexClassId := args[0]
Expand All @@ -80,12 +86,18 @@ func CmdUpdateListing() *cobra.Command {
return err
}

argFullPayToRoyalty, err := strconv.ParseBool(args[4])
if err != nil {
return err
}

msg := types.NewMsgUpdateListing(
clientCtx.GetFromAddress().String(),
indexClassId,
indexNftId,
argPrice,
argExpiration,
argFullPayToRoyalty,
)
if err := msg.ValidateBasic(); err != nil {
return err
Expand Down
28 changes: 16 additions & 12 deletions x/likenft/e2e_test/e2e_listing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,12 @@ func TestEndToEndListing(t *testing.T) {
// Create Listing
price := 123456
expiration := time.Now().UTC().Add(30 * 24 * time.Hour)
fullPayToRoyalty := true

out, err = clitestutil.ExecTestCLICmd(
ctx,
cli.CmdCreateListing(),
append([]string{classId, nftId, fmt.Sprintf("%d", price), expiration.Format(time.RFC3339Nano)}, txArgs...),
append([]string{classId, nftId, fmt.Sprintf("%d", price), expiration.Format(time.RFC3339Nano), fmt.Sprintf("%t", fullPayToRoyalty)}, txArgs...),
)
require.NoError(t, err)

Expand All @@ -181,21 +182,23 @@ func TestEndToEndListing(t *testing.T) {

require.Len(t, listingRes.Listings, 1)
require.Equal(t, types.Listing{
ClassId: classId,
NftId: nftId,
Seller: userAddress.String(),
Price: uint64(price),
Expiration: expiration,
ClassId: classId,
NftId: nftId,
Seller: userAddress.String(),
Price: uint64(price),
Expiration: expiration,
FullPayToRoyalty: fullPayToRoyalty,
}, listingRes.Listings[0])

// Update Listing
newPrice := 987654
newExpiration := time.Now().UTC().Add(60 * 24 * time.Hour)
newFullPayToRoyalty := false

out, err = clitestutil.ExecTestCLICmd(
ctx,
cli.CmdUpdateListing(),
append([]string{classId, nftId, fmt.Sprintf("%d", newPrice), newExpiration.Format(time.RFC3339Nano)}, txArgs...),
append([]string{classId, nftId, fmt.Sprintf("%d", newPrice), newExpiration.Format(time.RFC3339Nano), fmt.Sprintf("%t", newFullPayToRoyalty)}, txArgs...),
)
require.NoError(t, err)

Expand All @@ -220,11 +223,12 @@ func TestEndToEndListing(t *testing.T) {
cfg.Codec.MustUnmarshalJSON(out.Bytes(), &listingRes)
require.Len(t, listingRes.Listings, 1)
require.Equal(t, types.Listing{
ClassId: classId,
NftId: nftId,
Seller: userAddress.String(),
Price: uint64(newPrice),
Expiration: newExpiration,
ClassId: classId,
NftId: nftId,
Seller: userAddress.String(),
Price: uint64(newPrice),
Expiration: newExpiration,
FullPayToRoyalty: newFullPayToRoyalty,
}, listingRes.Listings[0])

// Buy NFT
Expand Down
35 changes: 20 additions & 15 deletions x/likenft/keeper/listing_expire_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,14 @@ func TestListingExpireFeatureNormal(t *testing.T) {
nftId := "nft1"
seller := sdk.AccAddress([]byte{0, 1, 0, 1, 0, 1, 0, 1})
expireTime := time.Date(2022, 2, 1, 0, 0, 0, 0, time.UTC)
fullPayToRoyalty := false
app.LikeNftKeeper.SetListing(ctx, types.ListingStoreRecord{
ClassId: classId,
NftId: nftId,
Seller: seller,
Price: uint64(123456),
Expiration: expireTime,
ClassId: classId,
NftId: nftId,
Seller: seller,
Price: uint64(123456),
Expiration: expireTime,
FullPayToRoyalty: fullPayToRoyalty,
})
app.LikeNftKeeper.SetListingExpireQueueEntry(ctx, types.ListingExpireQueueEntry{
ExpireTime: expireTime,
Expand All @@ -78,11 +80,12 @@ func TestListingExpireFeatureNormal(t *testing.T) {
seller2 := sdk.AccAddress([]byte{1, 1, 1, 1, 0, 0, 0, 0})
expireTime2 := time.Date(2022, 3, 1, 0, 0, 0, 0, time.UTC)
app.LikeNftKeeper.SetListing(ctx, types.ListingStoreRecord{
ClassId: classId,
NftId: nftId,
Seller: seller2,
Price: uint64(987654),
Expiration: expireTime2,
ClassId: classId,
NftId: nftId,
Seller: seller2,
Price: uint64(987654),
Expiration: expireTime2,
FullPayToRoyalty: fullPayToRoyalty,
})
app.LikeNftKeeper.SetListingExpireQueueEntry(ctx, types.ListingExpireQueueEntry{
ExpireTime: expireTime2,
Expand Down Expand Up @@ -133,12 +136,14 @@ func TestListingExpireFeatureQueueMismatch(t *testing.T) {
nftId := "nft1"
seller := sdk.AccAddress([]byte{0, 1, 0, 1, 0, 1, 0, 1})
expireTime := time.Date(2022, 3, 1, 0, 0, 0, 0, time.UTC)
fullPayToRoyalty := false
app.LikeNftKeeper.SetListing(ctx, types.ListingStoreRecord{
ClassId: classId,
NftId: nftId,
Seller: seller,
Price: uint64(123456),
Expiration: expireTime,
ClassId: classId,
NftId: nftId,
Seller: seller,
Price: uint64(123456),
Expiration: expireTime,
FullPayToRoyalty: fullPayToRoyalty,
})
app.LikeNftKeeper.SetListingExpireQueueEntry(ctx, types.ListingExpireQueueEntry{
ExpireTime: expireTime,
Expand Down
11 changes: 6 additions & 5 deletions x/likenft/keeper/listing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ func createNListing(keeper *keeper.Keeper, ctx sdk.Context, nClass int, nNFT int
for j := 0; j < nNFT; j++ {
for k := 0; k < nListing; k++ {
listing := types.ListingStoreRecord{
ClassId: strconv.Itoa(i),
NftId: strconv.Itoa(j),
Seller: accounts[k],
Price: uint64(k),
Expiration: time.Date(2022, 1, 1+k, 0, 0, 0, 0, time.UTC),
ClassId: strconv.Itoa(i),
NftId: strconv.Itoa(j),
Seller: accounts[k],
Price: uint64(k),
Expiration: time.Date(2022, 1, 1+k, 0, 0, 0, 0, time.UTC),
FullPayToRoyalty: false,
}
items = append(items, listing)
keeper.SetListing(ctx, listing)
Expand Down
2 changes: 1 addition & 1 deletion x/likenft/keeper/msg_server_buy_nft.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (k msgServer) BuyNFT(goCtx context.Context, msg *types.MsgBuyNFT) (*types.M
royaltyConfig, found := k.GetRoyaltyConfig(ctx, msg.ClassId)
var royaltyAmount uint64
if found {
_royaltyAmount, allocations, err := k.ComputeRoyaltyAllocation(ctx, msg.Price, false, royaltyConfig) // TODO: remove hardcode false
_royaltyAmount, allocations, err := k.ComputeRoyaltyAllocation(ctx, msg.Price, listing.FullPayToRoyalty, royaltyConfig)
if err != nil {
return nil, err
}
Expand Down
72 changes: 42 additions & 30 deletions x/likenft/keeper/msg_server_buy_nft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,18 @@ func TestBuyNFTNormalRoyalty(t *testing.T) {
nftId := "nft1"
price := uint64(123456)
expiration := time.Date(2022, 4, 1, 0, 0, 0, 0, time.UTC)
fullPayToRoyalty := false
royaltyBasisPoints := uint64(234)
finalPrice := uint64(200000)

// Seed listing
k.SetListing(ctx, types.ListingStoreRecord{
ClassId: classId,
NftId: nftId,
Seller: sellerAddressBytes,
Price: price,
Expiration: expiration,
ClassId: classId,
NftId: nftId,
Seller: sellerAddressBytes,
Price: price,
Expiration: expiration,
FullPayToRoyalty: fullPayToRoyalty,
})

// Seed royalty config
Expand Down Expand Up @@ -124,15 +126,17 @@ func TestBuyNFTNormalNoRoyalty(t *testing.T) {
nftId := "nft1"
price := uint64(123456)
expiration := time.Date(2022, 4, 1, 0, 0, 0, 0, time.UTC)
fullPayToRoyalty := false
finalPrice := uint64(200000)

// Seed listing
k.SetListing(ctx, types.ListingStoreRecord{
ClassId: classId,
NftId: nftId,
Seller: sellerAddressBytes,
Price: price,
Expiration: expiration,
ClassId: classId,
NftId: nftId,
Seller: sellerAddressBytes,
Price: price,
Expiration: expiration,
FullPayToRoyalty: fullPayToRoyalty,
})

// no royalty config
Expand Down Expand Up @@ -234,15 +238,17 @@ func TestBuyNFTListingOwnerInvalid(t *testing.T) {
nftId := "nft1"
price := uint64(123456)
expiration := time.Date(2022, 4, 1, 0, 0, 0, 0, time.UTC)
fullPayToRoyalty := false
finalPrice := uint64(200000)

// Seed listing
k.SetListing(ctx, types.ListingStoreRecord{
ClassId: classId,
NftId: nftId,
Seller: sellerAddressBytes,
Price: price,
Expiration: expiration,
ClassId: classId,
NftId: nftId,
Seller: sellerAddressBytes,
Price: price,
Expiration: expiration,
FullPayToRoyalty: fullPayToRoyalty,
})

// Mock
Expand Down Expand Up @@ -290,15 +296,17 @@ func TestBuyNFTListingExpired(t *testing.T) {
nftId := "nft1"
price := uint64(123456)
expiration := time.Date(2021, 12, 31, 0, 0, 0, 0, time.UTC)
fullPayToRoyalty := false
finalPrice := uint64(200000)

// Seed listing
k.SetListing(ctx, types.ListingStoreRecord{
ClassId: classId,
NftId: nftId,
Seller: sellerAddressBytes,
Price: price,
Expiration: expiration,
ClassId: classId,
NftId: nftId,
Seller: sellerAddressBytes,
Price: price,
Expiration: expiration,
FullPayToRoyalty: fullPayToRoyalty,
})

// Mock
Expand Down Expand Up @@ -346,15 +354,17 @@ func TestBuyNFTPriceTooLow(t *testing.T) {
nftId := "nft1"
price := uint64(123456)
expiration := time.Date(2022, 3, 1, 0, 0, 0, 0, time.UTC)
fullPayToRoyalty := false
finalPrice := uint64(100000)

// Seed listing
k.SetListing(ctx, types.ListingStoreRecord{
ClassId: classId,
NftId: nftId,
Seller: sellerAddressBytes,
Price: price,
Expiration: expiration,
ClassId: classId,
NftId: nftId,
Seller: sellerAddressBytes,
Price: price,
Expiration: expiration,
FullPayToRoyalty: fullPayToRoyalty,
})

// Mock
Expand Down Expand Up @@ -402,15 +412,17 @@ func TestBuyNFTNotEnoughBalance(t *testing.T) {
nftId := "nft1"
price := uint64(123456)
expiration := time.Date(2022, 4, 1, 0, 0, 0, 0, time.UTC)
fullPayToRoyalty := false
finalPrice := uint64(200000)

// Seed listing
k.SetListing(ctx, types.ListingStoreRecord{
ClassId: classId,
NftId: nftId,
Seller: sellerAddressBytes,
Price: price,
Expiration: expiration,
ClassId: classId,
NftId: nftId,
Seller: sellerAddressBytes,
Price: price,
Expiration: expiration,
FullPayToRoyalty: fullPayToRoyalty,
})

// Mock
Expand Down
22 changes: 12 additions & 10 deletions x/likenft/keeper/msg_server_listing.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@ func (k msgServer) CreateListing(goCtx context.Context, msg *types.MsgCreateList

// Create new listing
var listing = types.ListingStoreRecord{
ClassId: msg.ClassId,
NftId: msg.NftId,
Seller: userAddress,
Price: msg.Price,
Expiration: msg.Expiration,
ClassId: msg.ClassId,
NftId: msg.NftId,
Seller: userAddress,
Price: msg.Price,
Expiration: msg.Expiration,
FullPayToRoyalty: msg.FullPayToRoyalty,
}

k.SetListing(
Expand Down Expand Up @@ -97,11 +98,12 @@ func (k msgServer) UpdateListing(goCtx context.Context, msg *types.MsgUpdateList
}

var newListing = types.ListingStoreRecord{
ClassId: msg.ClassId,
NftId: msg.NftId,
Seller: userAddress,
Price: msg.Price,
Expiration: msg.Expiration,
ClassId: msg.ClassId,
NftId: msg.NftId,
Seller: userAddress,
Price: msg.Price,
Expiration: msg.Expiration,
FullPayToRoyalty: msg.FullPayToRoyalty,
}

k.SetListing(ctx, newListing)
Expand Down
Loading

0 comments on commit dd1c392

Please sign in to comment.