From 0af179c198e7421cc8e554eacea13ca2ed9f239f Mon Sep 17 00:00:00 2001 From: zemyblue Date: Mon, 25 Sep 2023 14:48:55 +0900 Subject: [PATCH 01/12] chore: add more validate unittest for MsgIssueFT of x/collection --- x/collection/msgs_test.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/x/collection/msgs_test.go b/x/collection/msgs_test.go index 254d82eac4..1d72a87f8c 100644 --- a/x/collection/msgs_test.go +++ b/x/collection/msgs_test.go @@ -638,6 +638,16 @@ func TestMsgIssueFT(t *testing.T) { amount: sdk.OneInt(), err: collection.ErrInvalidTokenDecimals, }, + "invalid decimals - negative": { + contractID: contractID, + owner: addrs[0], + to: addrs[1], + name: name, + meta: meta, + decimals: -1, + amount: sdk.OneInt(), + err: collection.ErrInvalidTokenDecimals, + }, "daphne compat": { contractID: contractID, owner: addrs[0], From 79f9841b515f02d02727015d9f7b943593184eb5 Mon Sep 17 00:00:00 2001 From: zemyblue Date: Mon, 25 Sep 2023 14:53:59 +0900 Subject: [PATCH 02/12] chore: add more validate unittest for MsgMintFT and MsgBurnFT of x/collection --- x/collection/msgs_test.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/x/collection/msgs_test.go b/x/collection/msgs_test.go index 1d72a87f8c..e9f09eb000 100644 --- a/x/collection/msgs_test.go +++ b/x/collection/msgs_test.go @@ -774,6 +774,12 @@ func TestMsgMintFT(t *testing.T) { to: addrs[1], amount: amount, }, + // for daphne compatible + "valid msg - zero amount": { + contractID: contractID, + operator: addrs[0], + to: addrs[1], + }, "invalid contract id": { operator: addrs[0], to: addrs[1], @@ -961,6 +967,11 @@ func TestMsgBurnFT(t *testing.T) { from: addrs[0], amount: amount, }, + // for daphne compatible + "valid msg - zero amount": { + contractID: "deadbeef", + from: addrs[0], + }, "invalid contract id": { from: addrs[0], amount: amount, From e8cefc9d7ecb1088b1f57d45d69c9876ec0c8aca Mon Sep 17 00:00:00 2001 From: zemyblue Date: Mon, 25 Sep 2023 19:33:30 +0900 Subject: [PATCH 03/12] chore: add more unittest for MsgIssueFT of x/collection --- x/collection/keeper/msg_server_test.go | 59 +++++++++++++++++++++++--- 1 file changed, 53 insertions(+), 6 deletions(-) diff --git a/x/collection/keeper/msg_server_test.go b/x/collection/keeper/msg_server_test.go index de2377e198..24b796c5b4 100644 --- a/x/collection/keeper/msg_server_test.go +++ b/x/collection/keeper/msg_server_test.go @@ -1,6 +1,7 @@ package keeper_test import ( + "fmt" abci "github.com/tendermint/tendermint/abci/types" sdk "github.com/Finschia/finschia-sdk/types" @@ -415,13 +416,48 @@ func (s *KeeperTestSuite) TestMsgIssueFT() { contractID: s.contractID, owner: s.vendor, amount: sdk.ZeroInt(), - events: sdk.Events{sdk.Event{Type: "lbm.collection.v1.EventCreatedFTClass", Attributes: []abci.EventAttribute{{Key: []uint8{0x63, 0x6f, 0x6e, 0x74, 0x72, 0x61, 0x63, 0x74, 0x5f, 0x69, 0x64}, Value: []uint8{0x22, 0x39, 0x62, 0x65, 0x31, 0x37, 0x31, 0x36, 0x35, 0x22}, Index: false}, {Key: []uint8{0x64, 0x65, 0x63, 0x69, 0x6d, 0x61, 0x6c, 0x73}, Value: []uint8{0x30}, Index: false}, {Key: []uint8{0x6d, 0x65, 0x74, 0x61}, Value: []uint8{0x22, 0x22}, Index: false}, {Key: []uint8{0x6d, 0x69, 0x6e, 0x74, 0x61, 0x62, 0x6c, 0x65}, Value: []uint8{0x66, 0x61, 0x6c, 0x73, 0x65}, Index: false}, {Key: []uint8{0x6e, 0x61, 0x6d, 0x65}, Value: []uint8{0x22, 0x22}, Index: false}, {Key: []uint8{0x6f, 0x70, 0x65, 0x72, 0x61, 0x74, 0x6f, 0x72}, Value: []uint8{0x22, 0x6c, 0x69, 0x6e, 0x6b, 0x31, 0x76, 0x39, 0x6a, 0x78, 0x67, 0x75, 0x6e, 0x39, 0x77, 0x64, 0x65, 0x6e, 0x71, 0x61, 0x32, 0x78, 0x7a, 0x66, 0x78, 0x22}, Index: false}, {Key: []uint8{0x74, 0x6f, 0x6b, 0x65, 0x6e, 0x5f, 0x69, 0x64}, Value: []uint8{0x22, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x32, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x22}, Index: false}}}}, + events: sdk.Events{ + sdk.Event{ + Type: "lbm.collection.v1.EventCreatedFTClass", + Attributes: []abci.EventAttribute{ + {Key: []uint8("contract_id"), Value: []uint8("\"9be17165\""), Index: false}, + {Key: []uint8("decimals"), Value: []uint8("0"), Index: false}, + {Key: []uint8("meta"), Value: []uint8("\"\""), Index: false}, + {Key: []uint8("mintable"), Value: []uint8("false"), Index: false}, + {Key: []uint8("name"), Value: []uint8("\"\""), Index: false}, + {Key: []uint8("operator"), Value: []uint8(fmt.Sprintf("\"%s\"", s.vendor)), Index: false}, + {Key: []uint8("token_id"), Value: []uint8("\"0000000200000000\""), Index: false}, + }, + }, + }, }, "valid request with supply": { contractID: s.contractID, owner: s.vendor, amount: sdk.OneInt(), - events: sdk.Events{sdk.Event{Type: "lbm.collection.v1.EventCreatedFTClass", Attributes: []abci.EventAttribute{{Key: []uint8{0x63, 0x6f, 0x6e, 0x74, 0x72, 0x61, 0x63, 0x74, 0x5f, 0x69, 0x64}, Value: []uint8{0x22, 0x39, 0x62, 0x65, 0x31, 0x37, 0x31, 0x36, 0x35, 0x22}, Index: false}, {Key: []uint8{0x64, 0x65, 0x63, 0x69, 0x6d, 0x61, 0x6c, 0x73}, Value: []uint8{0x30}, Index: false}, {Key: []uint8{0x6d, 0x65, 0x74, 0x61}, Value: []uint8{0x22, 0x22}, Index: false}, {Key: []uint8{0x6d, 0x69, 0x6e, 0x74, 0x61, 0x62, 0x6c, 0x65}, Value: []uint8{0x66, 0x61, 0x6c, 0x73, 0x65}, Index: false}, {Key: []uint8{0x6e, 0x61, 0x6d, 0x65}, Value: []uint8{0x22, 0x22}, Index: false}, {Key: []uint8{0x6f, 0x70, 0x65, 0x72, 0x61, 0x74, 0x6f, 0x72}, Value: []uint8{0x22, 0x6c, 0x69, 0x6e, 0x6b, 0x31, 0x76, 0x39, 0x6a, 0x78, 0x67, 0x75, 0x6e, 0x39, 0x77, 0x64, 0x65, 0x6e, 0x71, 0x61, 0x32, 0x78, 0x7a, 0x66, 0x78, 0x22}, Index: false}, {Key: []uint8{0x74, 0x6f, 0x6b, 0x65, 0x6e, 0x5f, 0x69, 0x64}, Value: []uint8{0x22, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x32, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x22}, Index: false}}}, sdk.Event{Type: "lbm.collection.v1.EventMintedFT", Attributes: []abci.EventAttribute{{Key: []uint8{0x61, 0x6d, 0x6f, 0x75, 0x6e, 0x74}, Value: []uint8{0x5b, 0x7b, 0x22, 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0x5f, 0x69, 0x64, 0x22, 0x3a, 0x22, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x32, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x22, 0x2c, 0x22, 0x61, 0x6d, 0x6f, 0x75, 0x6e, 0x74, 0x22, 0x3a, 0x22, 0x31, 0x22, 0x7d, 0x5d}, Index: false}, {Key: []uint8{0x63, 0x6f, 0x6e, 0x74, 0x72, 0x61, 0x63, 0x74, 0x5f, 0x69, 0x64}, Value: []uint8{0x22, 0x39, 0x62, 0x65, 0x31, 0x37, 0x31, 0x36, 0x35, 0x22}, Index: false}, {Key: []uint8{0x6f, 0x70, 0x65, 0x72, 0x61, 0x74, 0x6f, 0x72}, Value: []uint8{0x22, 0x6c, 0x69, 0x6e, 0x6b, 0x31, 0x76, 0x39, 0x6a, 0x78, 0x67, 0x75, 0x6e, 0x39, 0x77, 0x64, 0x65, 0x6e, 0x71, 0x61, 0x32, 0x78, 0x7a, 0x66, 0x78, 0x22}, Index: false}, {Key: []uint8{0x74, 0x6f}, Value: []uint8{0x22, 0x6c, 0x69, 0x6e, 0x6b, 0x31, 0x76, 0x39, 0x6a, 0x78, 0x67, 0x75, 0x6e, 0x39, 0x77, 0x64, 0x65, 0x6e, 0x79, 0x6a, 0x71, 0x79, 0x79, 0x78, 0x75, 0x22}, Index: false}}}}, + events: sdk.Events{ + sdk.Event{ + Type: "lbm.collection.v1.EventCreatedFTClass", + Attributes: []abci.EventAttribute{ + {Key: []uint8("contract_id"), Value: []uint8("\"9be17165\""), Index: false}, + {Key: []uint8("decimals"), Value: []uint8("0"), Index: false}, + {Key: []uint8("meta"), Value: []uint8("\"\""), Index: false}, + {Key: []uint8("mintable"), Value: []uint8("false"), Index: false}, + {Key: []uint8("name"), Value: []uint8("\"\""), Index: false}, + {Key: []uint8("operator"), Value: []uint8(fmt.Sprintf("\"%s\"", s.vendor)), Index: false}, + {Key: []uint8("token_id"), Value: []uint8("\"0000000200000000\""), Index: false}, + }, + }, + sdk.Event{ + Type: "lbm.collection.v1.EventMintedFT", + Attributes: []abci.EventAttribute{ + {Key: []uint8("amount"), Value: []uint8("[{\"token_id\":\"0000000200000000\",\"amount\":\"1\"}]"), Index: false}, + {Key: []uint8("contract_id"), Value: []uint8("\"9be17165\""), Index: false}, + {Key: []uint8("operator"), Value: []uint8(fmt.Sprintf("\"%s\"", s.vendor)), Index: false}, + {Key: []uint8("to"), Value: []uint8(fmt.Sprintf("\"%s\"", s.customer)), Index: false}, + }, + }, + }, }, "contract not found": { contractID: "deadbeef", @@ -454,10 +490,21 @@ func (s *KeeperTestSuite) TestMsgIssueFT() { } s.Require().NotNil(res) - - if s.deterministic { - s.Require().Equal(tc.events, ctx.EventManager().Events()) - } + s.Require().Equal(tc.events, ctx.EventManager().Events()) + + // check balance and tokenId + tokenId := res.TokenId + "00000000" + bal, err := s.queryServer.Balance(sdk.WrapSDKContext(ctx), &collection.QueryBalanceRequest{ + ContractId: s.contractID, + Address: s.customer.String(), + TokenId: tokenId, + }) + s.Require().NoError(err) + expectedCoin := collection.Coin{ + TokenId: tokenId, + Amount: tc.amount, + } + s.Require().Equal(expectedCoin, bal.Balance) }) } } From 4c75664f393c2775cd2c550543aa7fa1ef786e1d Mon Sep 17 00:00:00 2001 From: zemyblue Date: Mon, 25 Sep 2023 21:37:36 +0900 Subject: [PATCH 04/12] chore: add more unittest for MsgMintFT of x/collection --- x/collection/keeper/msg_server_test.go | 173 ++++++++++++++++++++++++- 1 file changed, 169 insertions(+), 4 deletions(-) diff --git a/x/collection/keeper/msg_server_test.go b/x/collection/keeper/msg_server_test.go index 24b796c5b4..4af104f93d 100644 --- a/x/collection/keeper/msg_server_test.go +++ b/x/collection/keeper/msg_server_test.go @@ -493,7 +493,7 @@ func (s *KeeperTestSuite) TestMsgIssueFT() { s.Require().Equal(tc.events, ctx.EventManager().Events()) // check balance and tokenId - tokenId := res.TokenId + "00000000" + tokenId := collection.NewFTID(res.TokenId) bal, err := s.queryServer.Balance(sdk.WrapSDKContext(ctx), &collection.QueryBalanceRequest{ ContractId: s.contractID, Address: s.customer.String(), @@ -571,7 +571,17 @@ func (s *KeeperTestSuite) TestMsgMintFT() { contractID: s.contractID, from: s.vendor, amount: amount, - events: sdk.Events{sdk.Event{Type: "lbm.collection.v1.EventMintedFT", Attributes: []abci.EventAttribute{{Key: []uint8{0x61, 0x6d, 0x6f, 0x75, 0x6e, 0x74}, Value: []uint8{0x5b, 0x7b, 0x22, 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0x5f, 0x69, 0x64, 0x22, 0x3a, 0x22, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x22, 0x2c, 0x22, 0x61, 0x6d, 0x6f, 0x75, 0x6e, 0x74, 0x22, 0x3a, 0x22, 0x31, 0x22, 0x7d, 0x5d}, Index: false}, {Key: []uint8{0x63, 0x6f, 0x6e, 0x74, 0x72, 0x61, 0x63, 0x74, 0x5f, 0x69, 0x64}, Value: []uint8{0x22, 0x39, 0x62, 0x65, 0x31, 0x37, 0x31, 0x36, 0x35, 0x22}, Index: false}, {Key: []uint8{0x6f, 0x70, 0x65, 0x72, 0x61, 0x74, 0x6f, 0x72}, Value: []uint8{0x22, 0x6c, 0x69, 0x6e, 0x6b, 0x31, 0x76, 0x39, 0x6a, 0x78, 0x67, 0x75, 0x6e, 0x39, 0x77, 0x64, 0x65, 0x6e, 0x71, 0x61, 0x32, 0x78, 0x7a, 0x66, 0x78, 0x22}, Index: false}, {Key: []uint8{0x74, 0x6f}, Value: []uint8{0x22, 0x6c, 0x69, 0x6e, 0x6b, 0x31, 0x76, 0x39, 0x6a, 0x78, 0x67, 0x75, 0x6e, 0x39, 0x77, 0x64, 0x65, 0x6e, 0x79, 0x6a, 0x71, 0x79, 0x79, 0x78, 0x75, 0x22}, Index: false}}}}, + events: sdk.Events{ + sdk.Event{ + Type: "lbm.collection.v1.EventMintedFT", + Attributes: []abci.EventAttribute{ + {Key: []uint8("amount"), Value: []uint8("[{\"token_id\":\"0000000100000000\",\"amount\":\"1\"}]"), Index: false}, + {Key: []uint8("contract_id"), Value: []uint8("\"9be17165\""), Index: false}, + {Key: []uint8("operator"), Value: []uint8(fmt.Sprintf("\"%s\"", s.vendor)), Index: false}, + {Key: []uint8("to"), Value: []uint8(fmt.Sprintf("\"%s\"", s.customer)), Index: false}, + }, + }, + }, }, "contract not found": { contractID: "deadbeef", @@ -593,12 +603,38 @@ func (s *KeeperTestSuite) TestMsgMintFT() { ), err: collection.ErrTokenNotExist, }, + "valid request - empty amount": { + contractID: s.contractID, + from: s.vendor, + events: sdk.Events{ + sdk.Event{ + Type: "lbm.collection.v1.EventMintedFT", + Attributes: []abci.EventAttribute{ + {Key: []uint8("amount"), Value: []uint8("[]"), Index: false}, + {Key: []uint8("contract_id"), Value: []uint8("\"9be17165\""), Index: false}, + {Key: []uint8("operator"), Value: []uint8(fmt.Sprintf("\"%s\"", s.vendor)), Index: false}, + {Key: []uint8("to"), Value: []uint8(fmt.Sprintf("\"%s\"", s.customer)), Index: false}, + }, + }, + }, + }, } for name, tc := range testCases { s.Run(name, func() { ctx, _ := s.ctx.CacheContext() + var prevAmount collection.Coins + for _, am := range tc.amount { + bal, err := s.queryServer.Balance(sdk.WrapSDKContext(ctx), &collection.QueryBalanceRequest{ + ContractId: tc.contractID, + Address: s.customer.String(), + TokenId: am.TokenId, + }) + s.Require().NoError(err) + prevAmount = append(prevAmount, bal.Balance) + } + req := &collection.MsgMintFT{ ContractId: tc.contractID, From: tc.from.String(), @@ -612,9 +648,138 @@ func (s *KeeperTestSuite) TestMsgMintFT() { } s.Require().NotNil(res) + s.Require().Equal(tc.events, ctx.EventManager().Events()) - if s.deterministic { - s.Require().Equal(tc.events, ctx.EventManager().Events()) + // check results + for i, am := range tc.amount { + bal, err := s.queryServer.Balance(sdk.WrapSDKContext(ctx), &collection.QueryBalanceRequest{ + ContractId: tc.contractID, + Address: s.customer.String(), + TokenId: am.TokenId, + }) + s.Require().NoError(err) + expected := collection.Coin{ + TokenId: am.TokenId, + Amount: prevAmount[i].Amount.Add(am.Amount), + } + s.Require().Equal(expected, bal.Balance) + } + }) + } +} + +func (s *KeeperTestSuite) TestMsgMintFTMultiAmount() { + // prepare multi tokens for test + // create a fungible token class (mintable true) + mintableFTClassID, err := s.keeper.CreateTokenClass(s.ctx, s.contractID, &collection.FTClass{ + Name: "tibetian fox2", + Mintable: true, + }) + s.Require().NoError(err) + + // create a fungible token class (mintable false) + nonmintableFTClassID, err := s.keeper.CreateTokenClass(s.ctx, s.contractID, &collection.FTClass{ + Name: "tibetian fox3", + Mintable: false, + }) + s.Require().NoError(err) + + testCases := map[string]struct { + contractID string + from sdk.AccAddress + amount []collection.Coin + err error + events sdk.Events + }{ + "include invalid tokenId among 2 tokens": { + contractID: s.contractID, + from: s.vendor, + amount: collection.NewCoins( + collection.NewFTCoin(s.ftClassID, sdk.OneInt()), + collection.NewFTCoin("00bab10b", sdk.OneInt()), // no exist tokenId + ), + err: collection.ErrTokenNotExist, + }, + "mintable false tokenId": { + contractID: s.contractID, + from: s.vendor, + amount: collection.NewCoins(collection.NewFTCoin(*nonmintableFTClassID, sdk.OneInt())), + err: collection.ErrTokenNotMintable, + }, + "include mintable false among 2 tokens": { + contractID: s.contractID, + from: s.vendor, + amount: collection.NewCoins( + collection.NewFTCoin(*mintableFTClassID, sdk.OneInt()), + collection.NewFTCoin(*nonmintableFTClassID, sdk.OneInt()), + ), + err: collection.ErrTokenNotMintable, + }, + "valid request - multi tokens": { + contractID: s.contractID, + from: s.vendor, + amount: collection.NewCoins( + collection.NewFTCoin(s.ftClassID, sdk.OneInt()), + collection.NewFTCoin(*mintableFTClassID, sdk.OneInt()), + ), + events: sdk.Events{ + sdk.Event{ + Type: "lbm.collection.v1.EventMintedFT", + Attributes: []abci.EventAttribute{ + {Key: []uint8("amount"), Value: []uint8("[{\"token_id\":\"0000000100000000\",\"amount\":\"1\"},{\"token_id\":\"0000000200000000\",\"amount\":\"1\"}]"), Index: false}, + {Key: []uint8("contract_id"), Value: []uint8("\"9be17165\""), Index: false}, + {Key: []uint8("operator"), Value: []uint8(fmt.Sprintf("\"%s\"", s.vendor)), Index: false}, + {Key: []uint8("to"), Value: []uint8(fmt.Sprintf("\"%s\"", s.customer)), Index: false}, + }, + }, + }, + }, + } + + for name, tc := range testCases { + s.Run(name, func() { + ctx, _ := s.ctx.CacheContext() + + // save previous amount + var prevAmount collection.Coins + for _, am := range tc.amount { + bal, err := s.queryServer.Balance(sdk.WrapSDKContext(ctx), &collection.QueryBalanceRequest{ + ContractId: tc.contractID, + Address: s.customer.String(), + TokenId: am.TokenId, + }) + s.Require().NoError(err) + prevAmount = append(prevAmount, bal.Balance) + } + + req := &collection.MsgMintFT{ + ContractId: tc.contractID, + From: tc.from.String(), + To: s.customer.String(), + Amount: tc.amount, + } + res, err := s.msgServer.MintFT(sdk.WrapSDKContext(ctx), req) + s.Require().ErrorIs(err, tc.err) + if tc.err != nil { + return + } + + s.Require().NotNil(res) + s.Require().Equal(tc.events, ctx.EventManager().Events()) + + // check results + for i, am := range tc.amount { + bal, err := s.queryServer.Balance(sdk.WrapSDKContext(ctx), &collection.QueryBalanceRequest{ + ContractId: tc.contractID, + Address: s.customer.String(), + TokenId: am.TokenId, + }) + s.Require().NoError(err) + expected := collection.Coin{ + TokenId: am.TokenId, + Amount: prevAmount[i].Amount.Add(am.Amount), + } + s.Require().Equal(expected, bal.Balance) } }) } From 0c7af53f27043a40cf25cd682fb085407b80299c Mon Sep 17 00:00:00 2001 From: zemyblue Date: Tue, 26 Sep 2023 20:03:44 +0900 Subject: [PATCH 05/12] chore: add more unittest for MsgMintFT and MsgBurnFT of x/collection --- x/collection/keeper/msg_server_test.go | 181 +++++++++++++++++++++++-- 1 file changed, 172 insertions(+), 9 deletions(-) diff --git a/x/collection/keeper/msg_server_test.go b/x/collection/keeper/msg_server_test.go index 4af104f93d..c9579c64db 100644 --- a/x/collection/keeper/msg_server_test.go +++ b/x/collection/keeper/msg_server_test.go @@ -2,11 +2,11 @@ package keeper_test import ( "fmt" - abci "github.com/tendermint/tendermint/abci/types" sdk "github.com/Finschia/finschia-sdk/types" "github.com/Finschia/finschia-sdk/x/collection" "github.com/Finschia/finschia-sdk/x/token/class" + abci "github.com/tendermint/tendermint/abci/types" ) func (s *KeeperTestSuite) TestMsgSendFT() { @@ -625,7 +625,10 @@ func (s *KeeperTestSuite) TestMsgMintFT() { ctx, _ := s.ctx.CacheContext() var prevAmount collection.Coins + var prevSupply []sdk.Int + var prevMinted []sdk.Int for _, am := range tc.amount { + // save balance bal, err := s.queryServer.Balance(sdk.WrapSDKContext(ctx), &collection.QueryBalanceRequest{ ContractId: tc.contractID, Address: s.customer.String(), @@ -633,6 +636,22 @@ func (s *KeeperTestSuite) TestMsgMintFT() { }) s.Require().NoError(err) prevAmount = append(prevAmount, bal.Balance) + + // save supply + supply, err := s.queryServer.FTSupply(sdk.WrapSDKContext(ctx), &collection.QueryFTSupplyRequest{ + ContractId: tc.contractID, + TokenId: am.TokenId, + }) + s.Require().NoError(err) + prevSupply = append(prevSupply, supply.Supply) + + // save minted + minted, err := s.queryServer.FTMinted(sdk.WrapSDKContext(ctx), &collection.QueryFTMintedRequest{ + ContractId: tc.contractID, + TokenId: am.TokenId, + }) + s.Require().NoError(err) + prevMinted = append(prevMinted, minted.Minted) } req := &collection.MsgMintFT{ @@ -663,12 +682,30 @@ func (s *KeeperTestSuite) TestMsgMintFT() { Amount: prevAmount[i].Amount.Add(am.Amount), } s.Require().Equal(expected, bal.Balance) + + // check total supply + supply, err := s.queryServer.FTSupply(sdk.WrapSDKContext(ctx), &collection.QueryFTSupplyRequest{ + ContractId: tc.contractID, + TokenId: am.TokenId, + }) + s.Require().NoError(err) + expectedSupply := prevSupply[i].Add(am.Amount) + s.Require().True(expectedSupply.Equal(supply.Supply)) + + // check minted + minted, err := s.queryServer.FTMinted(sdk.WrapSDKContext(ctx), &collection.QueryFTMintedRequest{ + ContractId: tc.contractID, + TokenId: am.TokenId, + }) + s.Require().NoError(err) + expectedMinted := prevMinted[i].Add(am.Amount) + s.Require().True(expectedMinted.Equal(minted.Minted)) } }) } } -func (s *KeeperTestSuite) TestMsgMintFTMultiAmount() { +func (s *KeeperTestSuite) TestMsgMintFTMultiTokens() { // prepare multi tokens for test // create a fungible token class (mintable true) mintableFTClassID, err := s.keeper.CreateTokenClass(s.ctx, s.contractID, &collection.FTClass{ @@ -850,9 +887,22 @@ func (s *KeeperTestSuite) TestMsgMintNFT() { } func (s *KeeperTestSuite) TestMsgBurnFT() { + // prepare mutli token burn test amount := collection.NewCoins( collection.NewFTCoin(s.ftClassID, s.balance), ) + + // create a fungible token class + mintableFTClassID, err := s.keeper.CreateTokenClass(s.ctx, s.contractID, &collection.FTClass{ + Name: "tibetian fox2", + Mintable: true, + }) + s.Require().NoError(err) + // mintft + mintedCoin := collection.NewFTCoin(*mintableFTClassID, sdk.NewInt(1000000)) + err = s.keeper.MintFT(s.ctx, s.contractID, s.vendor, []collection.Coin{mintedCoin}) + s.Require().NoError(err) + testCases := map[string]struct { contractID string from sdk.AccAddress @@ -860,28 +910,81 @@ func (s *KeeperTestSuite) TestMsgBurnFT() { err error events sdk.Events }{ - "valid request": { + "valid request": { // tc10 contractID: s.contractID, from: s.vendor, amount: amount, - events: sdk.Events{sdk.Event{Type: "lbm.collection.v1.EventBurned", Attributes: []abci.EventAttribute{{Key: []uint8{0x61, 0x6d, 0x6f, 0x75, 0x6e, 0x74}, Value: []uint8{0x5b, 0x7b, 0x22, 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0x5f, 0x69, 0x64, 0x22, 0x3a, 0x22, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x22, 0x2c, 0x22, 0x61, 0x6d, 0x6f, 0x75, 0x6e, 0x74, 0x22, 0x3a, 0x22, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x22, 0x7d, 0x5d}, Index: false}, {Key: []uint8{0x63, 0x6f, 0x6e, 0x74, 0x72, 0x61, 0x63, 0x74, 0x5f, 0x69, 0x64}, Value: []uint8{0x22, 0x39, 0x62, 0x65, 0x31, 0x37, 0x31, 0x36, 0x35, 0x22}, Index: false}, {Key: []uint8{0x66, 0x72, 0x6f, 0x6d}, Value: []uint8{0x22, 0x6c, 0x69, 0x6e, 0x6b, 0x31, 0x76, 0x39, 0x6a, 0x78, 0x67, 0x75, 0x6e, 0x39, 0x77, 0x64, 0x65, 0x6e, 0x71, 0x61, 0x32, 0x78, 0x7a, 0x66, 0x78, 0x22}, Index: false}, {Key: []uint8{0x6f, 0x70, 0x65, 0x72, 0x61, 0x74, 0x6f, 0x72}, Value: []uint8{0x22, 0x6c, 0x69, 0x6e, 0x6b, 0x31, 0x76, 0x39, 0x6a, 0x78, 0x67, 0x75, 0x6e, 0x39, 0x77, 0x64, 0x65, 0x6e, 0x71, 0x61, 0x32, 0x78, 0x7a, 0x66, 0x78, 0x22}, Index: false}}}}, + events: sdk.Events{ + sdk.Event{ + Type: "lbm.collection.v1.EventBurned", + Attributes: []abci.EventAttribute{ + {Key: []uint8("amount"), Value: []uint8("[{\"token_id\":\"0000000100000000\",\"amount\":\"1000000\"}]"), Index: false}, + {Key: []uint8("contract_id"), Value: []uint8("\"9be17165\""), Index: false}, + {Key: []uint8("from"), Value: []uint8(fmt.Sprintf("\"%s\"", s.vendor)), Index: false}, + {Key: []uint8("operator"), Value: []uint8(fmt.Sprintf("\"%s\"", s.vendor)), Index: false}, + }, + }, + }, }, - "contract not found": { + "contract not found": { // tc1 contractID: "deadbeef", from: s.vendor, amount: amount, err: class.ErrContractNotExist, }, - "no permission": { + "no permission": { // tc5 contractID: s.contractID, from: s.customer, amount: amount, err: collection.ErrTokenNoPermission, }, - "insufficient funds": { + "insufficient funds": { // tc7 + contractID: s.contractID, + from: s.vendor, + amount: collection.NewCoins( + collection.NewFTCoin("00bab10c", sdk.OneInt()), + ), + err: collection.ErrInsufficientToken, + }, + "no amount - valid": { // tc8 + contractID: s.contractID, + from: s.vendor, + events: sdk.Events{ + sdk.Event{ + Type: "lbm.collection.v1.EventBurned", + Attributes: []abci.EventAttribute{ + {Key: []uint8("amount"), Value: []uint8("[]"), Index: false}, + {Key: []uint8("contract_id"), Value: []uint8("\"9be17165\""), Index: false}, + {Key: []uint8("from"), Value: []uint8(fmt.Sprintf("\"%s\"", s.vendor)), Index: false}, + {Key: []uint8("operator"), Value: []uint8(fmt.Sprintf("\"%s\"", s.vendor)), Index: false}, + }, + }, + }, + }, + "valid multi amount burn": { // tc11 contractID: s.contractID, from: s.vendor, amount: collection.NewCoins( + collection.NewFTCoin(s.ftClassID, s.balance), + mintedCoin, + ), + events: sdk.Events{ + sdk.Event{ + Type: "lbm.collection.v1.EventBurned", + Attributes: []abci.EventAttribute{ + {Key: []uint8("amount"), Value: []uint8("[{\"token_id\":\"0000000100000000\",\"amount\":\"1000000\"},{\"token_id\":\"0000000200000000\",\"amount\":\"1000000\"}]"), Index: false}, + {Key: []uint8("contract_id"), Value: []uint8("\"9be17165\""), Index: false}, + {Key: []uint8("from"), Value: []uint8(fmt.Sprintf("\"%s\"", s.vendor)), Index: false}, + {Key: []uint8("operator"), Value: []uint8(fmt.Sprintf("\"%s\"", s.vendor)), Index: false}, + }, + }, + }, + }, + "include insufficient funds amount 2 amounts": { // tc9 + contractID: s.contractID, + from: s.vendor, + amount: collection.NewCoins( + collection.NewFTCoin(s.ftClassID, s.balance), collection.NewFTCoin("00bab10c", sdk.OneInt()), ), err: collection.ErrInsufficientToken, @@ -892,6 +995,37 @@ func (s *KeeperTestSuite) TestMsgBurnFT() { s.Run(name, func() { ctx, _ := s.ctx.CacheContext() + // save previous amount + var prevAmount collection.Coins + var prevSupply []sdk.Int + var prevBurnt []sdk.Int + for _, am := range tc.amount { + // save balance + bal, err := s.queryServer.Balance(sdk.WrapSDKContext(ctx), &collection.QueryBalanceRequest{ + ContractId: tc.contractID, + Address: tc.from.String(), + TokenId: am.TokenId, + }) + s.Require().NoError(err) + prevAmount = append(prevAmount, bal.Balance) + + // save supply + supply, err := s.queryServer.FTSupply(sdk.WrapSDKContext(ctx), &collection.QueryFTSupplyRequest{ + ContractId: tc.contractID, + TokenId: am.TokenId, + }) + s.Require().NoError(err) + prevSupply = append(prevSupply, supply.Supply) + + // save burnt + burnt, err := s.queryServer.FTBurnt(sdk.WrapSDKContext(ctx), &collection.QueryFTBurntRequest{ + ContractId: tc.contractID, + TokenId: am.TokenId, + }) + s.Require().NoError(err) + prevBurnt = append(prevBurnt, burnt.Burnt) + } + req := &collection.MsgBurnFT{ ContractId: tc.contractID, From: tc.from.String(), @@ -904,9 +1038,38 @@ func (s *KeeperTestSuite) TestMsgBurnFT() { } s.Require().NotNil(res) + s.Require().Equal(tc.events, ctx.EventManager().Events()) - if s.deterministic { - s.Require().Equal(tc.events, ctx.EventManager().Events()) + // check changed amount + for i, am := range tc.amount { + // check balance + bal, err := s.queryServer.Balance(sdk.WrapSDKContext(ctx), &collection.QueryBalanceRequest{ + ContractId: tc.contractID, + Address: tc.from.String(), + TokenId: am.TokenId, + }) + s.Require().NoError(err) + expectedBalance := prevAmount[i].Amount.Sub(am.Amount) + s.Require().Equal(am.TokenId, bal.Balance.TokenId) + s.Require().True(expectedBalance.Equal(bal.Balance.Amount)) + + // check total supply + supply, err := s.queryServer.FTSupply(sdk.WrapSDKContext(ctx), &collection.QueryFTSupplyRequest{ + ContractId: tc.contractID, + TokenId: am.TokenId, + }) + s.Require().NoError(err) + expectedSupply := prevSupply[i].Sub(am.Amount) + s.Require().True(expectedSupply.Equal(supply.Supply)) + + // check burnt + burnt, err := s.queryServer.FTBurnt(sdk.WrapSDKContext(ctx), &collection.QueryFTBurntRequest{ + ContractId: tc.contractID, + TokenId: am.TokenId, + }) + s.Require().NoError(err) + expectedBurnt := prevBurnt[i].Add(am.Amount) + s.Require().True(expectedBurnt.Equal(burnt.Burnt)) } }) } From c2c7697904807f99e64f8fb6a7960f7ccfdc8ab8 Mon Sep 17 00:00:00 2001 From: zemyblue Date: Tue, 26 Sep 2023 20:28:55 +0900 Subject: [PATCH 06/12] chore: refactoring MsgMintFT and MsgBrunFT test --- x/collection/keeper/msg_server_test.go | 288 +++++++++---------------- 1 file changed, 99 insertions(+), 189 deletions(-) diff --git a/x/collection/keeper/msg_server_test.go b/x/collection/keeper/msg_server_test.go index c9579c64db..c553e96713 100644 --- a/x/collection/keeper/msg_server_test.go +++ b/x/collection/keeper/msg_server_test.go @@ -557,9 +557,25 @@ func (s *KeeperTestSuite) TestMsgIssueNFT() { } func (s *KeeperTestSuite) TestMsgMintFT() { + // prepare multi tokens for test + // create a fungible token class (mintable true) + mintableFTClassID, err := s.keeper.CreateTokenClass(s.ctx, s.contractID, &collection.FTClass{ + Name: "tibetian fox2", + Mintable: true, + }) + s.Require().NoError(err) + + // create a fungible token class (mintable false) + nonmintableFTClassID, err := s.keeper.CreateTokenClass(s.ctx, s.contractID, &collection.FTClass{ + Name: "tibetian fox3", + Mintable: false, + }) + s.Require().NoError(err) + amount := collection.NewCoins( collection.NewFTCoin(s.ftClassID, sdk.OneInt()), ) + testCases := map[string]struct { contractID string from sdk.AccAddress @@ -618,116 +634,6 @@ func (s *KeeperTestSuite) TestMsgMintFT() { }, }, }, - } - - for name, tc := range testCases { - s.Run(name, func() { - ctx, _ := s.ctx.CacheContext() - - var prevAmount collection.Coins - var prevSupply []sdk.Int - var prevMinted []sdk.Int - for _, am := range tc.amount { - // save balance - bal, err := s.queryServer.Balance(sdk.WrapSDKContext(ctx), &collection.QueryBalanceRequest{ - ContractId: tc.contractID, - Address: s.customer.String(), - TokenId: am.TokenId, - }) - s.Require().NoError(err) - prevAmount = append(prevAmount, bal.Balance) - - // save supply - supply, err := s.queryServer.FTSupply(sdk.WrapSDKContext(ctx), &collection.QueryFTSupplyRequest{ - ContractId: tc.contractID, - TokenId: am.TokenId, - }) - s.Require().NoError(err) - prevSupply = append(prevSupply, supply.Supply) - - // save minted - minted, err := s.queryServer.FTMinted(sdk.WrapSDKContext(ctx), &collection.QueryFTMintedRequest{ - ContractId: tc.contractID, - TokenId: am.TokenId, - }) - s.Require().NoError(err) - prevMinted = append(prevMinted, minted.Minted) - } - - req := &collection.MsgMintFT{ - ContractId: tc.contractID, - From: tc.from.String(), - To: s.customer.String(), - Amount: tc.amount, - } - res, err := s.msgServer.MintFT(sdk.WrapSDKContext(ctx), req) - s.Require().ErrorIs(err, tc.err) - if tc.err != nil { - return - } - - s.Require().NotNil(res) - s.Require().Equal(tc.events, ctx.EventManager().Events()) - - // check results - for i, am := range tc.amount { - bal, err := s.queryServer.Balance(sdk.WrapSDKContext(ctx), &collection.QueryBalanceRequest{ - ContractId: tc.contractID, - Address: s.customer.String(), - TokenId: am.TokenId, - }) - s.Require().NoError(err) - expected := collection.Coin{ - TokenId: am.TokenId, - Amount: prevAmount[i].Amount.Add(am.Amount), - } - s.Require().Equal(expected, bal.Balance) - - // check total supply - supply, err := s.queryServer.FTSupply(sdk.WrapSDKContext(ctx), &collection.QueryFTSupplyRequest{ - ContractId: tc.contractID, - TokenId: am.TokenId, - }) - s.Require().NoError(err) - expectedSupply := prevSupply[i].Add(am.Amount) - s.Require().True(expectedSupply.Equal(supply.Supply)) - - // check minted - minted, err := s.queryServer.FTMinted(sdk.WrapSDKContext(ctx), &collection.QueryFTMintedRequest{ - ContractId: tc.contractID, - TokenId: am.TokenId, - }) - s.Require().NoError(err) - expectedMinted := prevMinted[i].Add(am.Amount) - s.Require().True(expectedMinted.Equal(minted.Minted)) - } - }) - } -} - -func (s *KeeperTestSuite) TestMsgMintFTMultiTokens() { - // prepare multi tokens for test - // create a fungible token class (mintable true) - mintableFTClassID, err := s.keeper.CreateTokenClass(s.ctx, s.contractID, &collection.FTClass{ - Name: "tibetian fox2", - Mintable: true, - }) - s.Require().NoError(err) - - // create a fungible token class (mintable false) - nonmintableFTClassID, err := s.keeper.CreateTokenClass(s.ctx, s.contractID, &collection.FTClass{ - Name: "tibetian fox3", - Mintable: false, - }) - s.Require().NoError(err) - - testCases := map[string]struct { - contractID string - from sdk.AccAddress - amount []collection.Coin - err error - events sdk.Events - }{ "include invalid tokenId among 2 tokens": { contractID: s.contractID, from: s.vendor, @@ -773,21 +679,41 @@ func (s *KeeperTestSuite) TestMsgMintFTMultiTokens() { }, } + // query the values to be effected by MintFT + queryMintFTDatas := func(ctx sdk.Context, coins collection.Coins, contractID string) (balances collection.Coins, supply []sdk.Int, minted []sdk.Int) { + for _, am := range coins { + // save balance + bal, err := s.queryServer.Balance(sdk.WrapSDKContext(ctx), &collection.QueryBalanceRequest{ + ContractId: contractID, + Address: s.customer.String(), + TokenId: am.TokenId, + }) + s.Require().NoError(err) + balances = append(balances, bal.Balance) + + // save supply + res, err := s.queryServer.FTSupply(sdk.WrapSDKContext(ctx), &collection.QueryFTSupplyRequest{ + ContractId: contractID, + TokenId: am.TokenId, + }) + s.Require().NoError(err) + supply = append(supply, res.Supply) + + // save minted + m, err := s.queryServer.FTMinted(sdk.WrapSDKContext(ctx), &collection.QueryFTMintedRequest{ + ContractId: contractID, + TokenId: am.TokenId, + }) + s.Require().NoError(err) + minted = append(minted, m.Minted) + } + return + } + for name, tc := range testCases { s.Run(name, func() { ctx, _ := s.ctx.CacheContext() - - // save previous amount - var prevAmount collection.Coins - for _, am := range tc.amount { - bal, err := s.queryServer.Balance(sdk.WrapSDKContext(ctx), &collection.QueryBalanceRequest{ - ContractId: tc.contractID, - Address: s.customer.String(), - TokenId: am.TokenId, - }) - s.Require().NoError(err) - prevAmount = append(prevAmount, bal.Balance) - } + prevAmount, prevSupply, prevMinted := queryMintFTDatas(ctx, tc.amount, tc.contractID) req := &collection.MsgMintFT{ ContractId: tc.contractID, @@ -805,18 +731,19 @@ func (s *KeeperTestSuite) TestMsgMintFTMultiTokens() { s.Require().Equal(tc.events, ctx.EventManager().Events()) // check results + afterAmount, afterSupply, afterMinted := queryMintFTDatas(ctx, tc.amount, tc.contractID) for i, am := range tc.amount { - bal, err := s.queryServer.Balance(sdk.WrapSDKContext(ctx), &collection.QueryBalanceRequest{ - ContractId: tc.contractID, - Address: s.customer.String(), - TokenId: am.TokenId, - }) - s.Require().NoError(err) - expected := collection.Coin{ + expectedBalance := collection.Coin{ TokenId: am.TokenId, Amount: prevAmount[i].Amount.Add(am.Amount), } - s.Require().Equal(expected, bal.Balance) + s.Require().Equal(expectedBalance, afterAmount[i]) + + expectedSupply := prevSupply[i].Add(am.Amount) + s.Require().True(expectedSupply.Equal(afterSupply[i])) + + expectedMinted := prevMinted[i].Add(am.Amount) + s.Require().True(expectedMinted.Equal(afterMinted[i])) } }) } @@ -965,14 +892,14 @@ func (s *KeeperTestSuite) TestMsgBurnFT() { contractID: s.contractID, from: s.vendor, amount: collection.NewCoins( - collection.NewFTCoin(s.ftClassID, s.balance), - mintedCoin, + collection.NewFTCoin(s.ftClassID, sdk.NewInt(50000)), + collection.NewFTCoin(*mintableFTClassID, sdk.NewInt(60000)), ), events: sdk.Events{ sdk.Event{ Type: "lbm.collection.v1.EventBurned", Attributes: []abci.EventAttribute{ - {Key: []uint8("amount"), Value: []uint8("[{\"token_id\":\"0000000100000000\",\"amount\":\"1000000\"},{\"token_id\":\"0000000200000000\",\"amount\":\"1000000\"}]"), Index: false}, + {Key: []uint8("amount"), Value: []uint8("[{\"token_id\":\"0000000100000000\",\"amount\":\"50000\"},{\"token_id\":\"0000000200000000\",\"amount\":\"60000\"}]"), Index: false}, {Key: []uint8("contract_id"), Value: []uint8("\"9be17165\""), Index: false}, {Key: []uint8("from"), Value: []uint8(fmt.Sprintf("\"%s\"", s.vendor)), Index: false}, {Key: []uint8("operator"), Value: []uint8(fmt.Sprintf("\"%s\"", s.vendor)), Index: false}, @@ -991,40 +918,41 @@ func (s *KeeperTestSuite) TestMsgBurnFT() { }, } + // query the values to be effected by BurnFT + queryBurnTDatas := func(ctx sdk.Context, coins collection.Coins, contractID, from string) (balances collection.Coins, supply []sdk.Int, burnt []sdk.Int) { + for _, am := range coins { + // save balance + bal, err := s.queryServer.Balance(sdk.WrapSDKContext(ctx), &collection.QueryBalanceRequest{ + ContractId: contractID, + Address: from, + TokenId: am.TokenId, + }) + s.Require().NoError(err) + balances = append(balances, bal.Balance) + + // save supply + res, err := s.queryServer.FTSupply(sdk.WrapSDKContext(ctx), &collection.QueryFTSupplyRequest{ + ContractId: contractID, + TokenId: am.TokenId, + }) + s.Require().NoError(err) + supply = append(supply, res.Supply) + + // save minted + b, err := s.queryServer.FTBurnt(sdk.WrapSDKContext(ctx), &collection.QueryFTBurntRequest{ + ContractId: contractID, + TokenId: am.TokenId, + }) + s.Require().NoError(err) + burnt = append(burnt, b.Burnt) + } + return + } + for name, tc := range testCases { s.Run(name, func() { ctx, _ := s.ctx.CacheContext() - - // save previous amount - var prevAmount collection.Coins - var prevSupply []sdk.Int - var prevBurnt []sdk.Int - for _, am := range tc.amount { - // save balance - bal, err := s.queryServer.Balance(sdk.WrapSDKContext(ctx), &collection.QueryBalanceRequest{ - ContractId: tc.contractID, - Address: tc.from.String(), - TokenId: am.TokenId, - }) - s.Require().NoError(err) - prevAmount = append(prevAmount, bal.Balance) - - // save supply - supply, err := s.queryServer.FTSupply(sdk.WrapSDKContext(ctx), &collection.QueryFTSupplyRequest{ - ContractId: tc.contractID, - TokenId: am.TokenId, - }) - s.Require().NoError(err) - prevSupply = append(prevSupply, supply.Supply) - - // save burnt - burnt, err := s.queryServer.FTBurnt(sdk.WrapSDKContext(ctx), &collection.QueryFTBurntRequest{ - ContractId: tc.contractID, - TokenId: am.TokenId, - }) - s.Require().NoError(err) - prevBurnt = append(prevBurnt, burnt.Burnt) - } + prevAmount, prevSupply, prevBurnt := queryBurnTDatas(ctx, tc.amount, tc.contractID, tc.from.String()) req := &collection.MsgBurnFT{ ContractId: tc.contractID, @@ -1041,35 +969,17 @@ func (s *KeeperTestSuite) TestMsgBurnFT() { s.Require().Equal(tc.events, ctx.EventManager().Events()) // check changed amount + afterAmount, afterSupply, afterBurnt := queryBurnTDatas(ctx, tc.amount, tc.contractID, tc.from.String()) for i, am := range tc.amount { - // check balance - bal, err := s.queryServer.Balance(sdk.WrapSDKContext(ctx), &collection.QueryBalanceRequest{ - ContractId: tc.contractID, - Address: tc.from.String(), - TokenId: am.TokenId, - }) - s.Require().NoError(err) expectedBalance := prevAmount[i].Amount.Sub(am.Amount) - s.Require().Equal(am.TokenId, bal.Balance.TokenId) - s.Require().True(expectedBalance.Equal(bal.Balance.Amount)) - - // check total supply - supply, err := s.queryServer.FTSupply(sdk.WrapSDKContext(ctx), &collection.QueryFTSupplyRequest{ - ContractId: tc.contractID, - TokenId: am.TokenId, - }) - s.Require().NoError(err) + s.Require().Equal(am.TokenId, afterAmount[i].TokenId) + s.Require().True(expectedBalance.Equal(afterAmount[i].Amount)) + expectedSupply := prevSupply[i].Sub(am.Amount) - s.Require().True(expectedSupply.Equal(supply.Supply)) - - // check burnt - burnt, err := s.queryServer.FTBurnt(sdk.WrapSDKContext(ctx), &collection.QueryFTBurntRequest{ - ContractId: tc.contractID, - TokenId: am.TokenId, - }) - s.Require().NoError(err) + s.Require().True(expectedSupply.Equal(afterSupply[i])) + expectedBurnt := prevBurnt[i].Add(am.Amount) - s.Require().True(expectedBurnt.Equal(burnt.Burnt)) + s.Require().True(expectedBurnt.Equal(afterBurnt[i])) } }) } From 2572ca17041f39220b689d62bcfd949b15c77a9d Mon Sep 17 00:00:00 2001 From: zemyblue Date: Tue, 26 Sep 2023 21:04:55 +0900 Subject: [PATCH 07/12] chore: refactoring MsgMintFT and MsgBrunFT test (add multiple times test) --- x/collection/keeper/msg_server_test.go | 125 ++++++++++++++----------- 1 file changed, 68 insertions(+), 57 deletions(-) diff --git a/x/collection/keeper/msg_server_test.go b/x/collection/keeper/msg_server_test.go index c553e96713..9e6b275f48 100644 --- a/x/collection/keeper/msg_server_test.go +++ b/x/collection/keeper/msg_server_test.go @@ -573,7 +573,7 @@ func (s *KeeperTestSuite) TestMsgMintFT() { s.Require().NoError(err) amount := collection.NewCoins( - collection.NewFTCoin(s.ftClassID, sdk.OneInt()), + collection.NewFTCoin(s.ftClassID, sdk.NewInt(100000)), ) testCases := map[string]struct { @@ -591,7 +591,7 @@ func (s *KeeperTestSuite) TestMsgMintFT() { sdk.Event{ Type: "lbm.collection.v1.EventMintedFT", Attributes: []abci.EventAttribute{ - {Key: []uint8("amount"), Value: []uint8("[{\"token_id\":\"0000000100000000\",\"amount\":\"1\"}]"), Index: false}, + {Key: []uint8("amount"), Value: []uint8("[{\"token_id\":\"0000000100000000\",\"amount\":\"100000\"}]"), Index: false}, {Key: []uint8("contract_id"), Value: []uint8("\"9be17165\""), Index: false}, {Key: []uint8("operator"), Value: []uint8(fmt.Sprintf("\"%s\"", s.vendor)), Index: false}, {Key: []uint8("to"), Value: []uint8(fmt.Sprintf("\"%s\"", s.customer)), Index: false}, @@ -662,14 +662,14 @@ func (s *KeeperTestSuite) TestMsgMintFT() { contractID: s.contractID, from: s.vendor, amount: collection.NewCoins( - collection.NewFTCoin(s.ftClassID, sdk.OneInt()), - collection.NewFTCoin(*mintableFTClassID, sdk.OneInt()), + collection.NewFTCoin(s.ftClassID, sdk.NewInt(100000)), + collection.NewFTCoin(*mintableFTClassID, sdk.NewInt(200000)), ), events: sdk.Events{ sdk.Event{ Type: "lbm.collection.v1.EventMintedFT", Attributes: []abci.EventAttribute{ - {Key: []uint8("amount"), Value: []uint8("[{\"token_id\":\"0000000100000000\",\"amount\":\"1\"},{\"token_id\":\"0000000200000000\",\"amount\":\"1\"}]"), Index: false}, + {Key: []uint8("amount"), Value: []uint8("[{\"token_id\":\"0000000100000000\",\"amount\":\"100000\"},{\"token_id\":\"0000000200000000\",\"amount\":\"200000\"}]"), Index: false}, {Key: []uint8("contract_id"), Value: []uint8("\"9be17165\""), Index: false}, {Key: []uint8("operator"), Value: []uint8(fmt.Sprintf("\"%s\"", s.vendor)), Index: false}, {Key: []uint8("to"), Value: []uint8(fmt.Sprintf("\"%s\"", s.customer)), Index: false}, @@ -712,38 +712,43 @@ func (s *KeeperTestSuite) TestMsgMintFT() { for name, tc := range testCases { s.Run(name, func() { + // test multiple times ctx, _ := s.ctx.CacheContext() - prevAmount, prevSupply, prevMinted := queryMintFTDatas(ctx, tc.amount, tc.contractID) + for t := 0; t < 3; t++ { + ctx, _ = ctx.CacheContext() - req := &collection.MsgMintFT{ - ContractId: tc.contractID, - From: tc.from.String(), - To: s.customer.String(), - Amount: tc.amount, - } - res, err := s.msgServer.MintFT(sdk.WrapSDKContext(ctx), req) - s.Require().ErrorIs(err, tc.err) - if tc.err != nil { - return - } + prevAmount, prevSupply, prevMinted := queryMintFTDatas(ctx, tc.amount, tc.contractID) - s.Require().NotNil(res) - s.Require().Equal(tc.events, ctx.EventManager().Events()) - - // check results - afterAmount, afterSupply, afterMinted := queryMintFTDatas(ctx, tc.amount, tc.contractID) - for i, am := range tc.amount { - expectedBalance := collection.Coin{ - TokenId: am.TokenId, - Amount: prevAmount[i].Amount.Add(am.Amount), + req := &collection.MsgMintFT{ + ContractId: tc.contractID, + From: tc.from.String(), + To: s.customer.String(), + Amount: tc.amount, + } + res, err := s.msgServer.MintFT(sdk.WrapSDKContext(ctx), req) + s.Require().ErrorIs(err, tc.err) + if tc.err != nil { + return } - s.Require().Equal(expectedBalance, afterAmount[i]) - expectedSupply := prevSupply[i].Add(am.Amount) - s.Require().True(expectedSupply.Equal(afterSupply[i])) + s.Require().NotNil(res) + s.Require().Equal(tc.events, ctx.EventManager().Events()) + + // check results + afterAmount, afterSupply, afterMinted := queryMintFTDatas(ctx, tc.amount, tc.contractID) + for i, am := range tc.amount { + expectedBalance := collection.Coin{ + TokenId: am.TokenId, + Amount: prevAmount[i].Amount.Add(am.Amount), + } + s.Require().Equal(expectedBalance, afterAmount[i]) - expectedMinted := prevMinted[i].Add(am.Amount) - s.Require().True(expectedMinted.Equal(afterMinted[i])) + expectedSupply := prevSupply[i].Add(am.Amount) + s.Require().True(expectedSupply.Equal(afterSupply[i])) + + expectedMinted := prevMinted[i].Add(am.Amount) + s.Require().True(expectedMinted.Equal(afterMinted[i])) + } } }) } @@ -840,12 +845,14 @@ func (s *KeeperTestSuite) TestMsgBurnFT() { "valid request": { // tc10 contractID: s.contractID, from: s.vendor, - amount: amount, + amount: collection.NewCoins( + collection.NewFTCoin(s.ftClassID, sdk.NewInt(50000)), + ), events: sdk.Events{ sdk.Event{ Type: "lbm.collection.v1.EventBurned", Attributes: []abci.EventAttribute{ - {Key: []uint8("amount"), Value: []uint8("[{\"token_id\":\"0000000100000000\",\"amount\":\"1000000\"}]"), Index: false}, + {Key: []uint8("amount"), Value: []uint8("[{\"token_id\":\"0000000100000000\",\"amount\":\"50000\"}]"), Index: false}, {Key: []uint8("contract_id"), Value: []uint8("\"9be17165\""), Index: false}, {Key: []uint8("from"), Value: []uint8(fmt.Sprintf("\"%s\"", s.vendor)), Index: false}, {Key: []uint8("operator"), Value: []uint8(fmt.Sprintf("\"%s\"", s.vendor)), Index: false}, @@ -951,35 +958,39 @@ func (s *KeeperTestSuite) TestMsgBurnFT() { for name, tc := range testCases { s.Run(name, func() { + // test multiple times ctx, _ := s.ctx.CacheContext() - prevAmount, prevSupply, prevBurnt := queryBurnTDatas(ctx, tc.amount, tc.contractID, tc.from.String()) - - req := &collection.MsgBurnFT{ - ContractId: tc.contractID, - From: tc.from.String(), - Amount: tc.amount, - } - res, err := s.msgServer.BurnFT(sdk.WrapSDKContext(ctx), req) - s.Require().ErrorIs(err, tc.err) - if tc.err != nil { - return - } + for t := 0; t < 3; t++ { + ctx, _ = ctx.CacheContext() + prevAmount, prevSupply, prevBurnt := queryBurnTDatas(ctx, tc.amount, tc.contractID, tc.from.String()) + + req := &collection.MsgBurnFT{ + ContractId: tc.contractID, + From: tc.from.String(), + Amount: tc.amount, + } + res, err := s.msgServer.BurnFT(sdk.WrapSDKContext(ctx), req) + s.Require().ErrorIs(err, tc.err) + if tc.err != nil { + return + } - s.Require().NotNil(res) - s.Require().Equal(tc.events, ctx.EventManager().Events()) + s.Require().NotNil(res) + s.Require().Equal(tc.events, ctx.EventManager().Events()) - // check changed amount - afterAmount, afterSupply, afterBurnt := queryBurnTDatas(ctx, tc.amount, tc.contractID, tc.from.String()) - for i, am := range tc.amount { - expectedBalance := prevAmount[i].Amount.Sub(am.Amount) - s.Require().Equal(am.TokenId, afterAmount[i].TokenId) - s.Require().True(expectedBalance.Equal(afterAmount[i].Amount)) + // check changed amount + afterAmount, afterSupply, afterBurnt := queryBurnTDatas(ctx, tc.amount, tc.contractID, tc.from.String()) + for i, am := range tc.amount { + expectedBalance := prevAmount[i].Amount.Sub(am.Amount) + s.Require().Equal(am.TokenId, afterAmount[i].TokenId) + s.Require().True(expectedBalance.Equal(afterAmount[i].Amount)) - expectedSupply := prevSupply[i].Sub(am.Amount) - s.Require().True(expectedSupply.Equal(afterSupply[i])) + expectedSupply := prevSupply[i].Sub(am.Amount) + s.Require().True(expectedSupply.Equal(afterSupply[i])) - expectedBurnt := prevBurnt[i].Add(am.Amount) - s.Require().True(expectedBurnt.Equal(afterBurnt[i])) + expectedBurnt := prevBurnt[i].Add(am.Amount) + s.Require().True(expectedBurnt.Equal(afterBurnt[i])) + } } }) } From 138951ab3fb283a855139446038330ca889102e4 Mon Sep 17 00:00:00 2001 From: zemyblue Date: Tue, 26 Sep 2023 21:12:09 +0900 Subject: [PATCH 08/12] chore: remove develop comment --- x/collection/keeper/msg_server_test.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/x/collection/keeper/msg_server_test.go b/x/collection/keeper/msg_server_test.go index 9e6b275f48..4c70bfacfd 100644 --- a/x/collection/keeper/msg_server_test.go +++ b/x/collection/keeper/msg_server_test.go @@ -842,7 +842,7 @@ func (s *KeeperTestSuite) TestMsgBurnFT() { err error events sdk.Events }{ - "valid request": { // tc10 + "valid request": { contractID: s.contractID, from: s.vendor, amount: collection.NewCoins( @@ -860,19 +860,19 @@ func (s *KeeperTestSuite) TestMsgBurnFT() { }, }, }, - "contract not found": { // tc1 + "contract not found": { contractID: "deadbeef", from: s.vendor, amount: amount, err: class.ErrContractNotExist, }, - "no permission": { // tc5 + "no permission": { contractID: s.contractID, from: s.customer, amount: amount, err: collection.ErrTokenNoPermission, }, - "insufficient funds": { // tc7 + "insufficient funds": { contractID: s.contractID, from: s.vendor, amount: collection.NewCoins( @@ -880,7 +880,7 @@ func (s *KeeperTestSuite) TestMsgBurnFT() { ), err: collection.ErrInsufficientToken, }, - "no amount - valid": { // tc8 + "no amount - valid": { contractID: s.contractID, from: s.vendor, events: sdk.Events{ @@ -895,7 +895,7 @@ func (s *KeeperTestSuite) TestMsgBurnFT() { }, }, }, - "valid multi amount burn": { // tc11 + "valid multi amount burn": { contractID: s.contractID, from: s.vendor, amount: collection.NewCoins( @@ -914,7 +914,7 @@ func (s *KeeperTestSuite) TestMsgBurnFT() { }, }, }, - "include insufficient funds amount 2 amounts": { // tc9 + "include insufficient funds amount 2 amounts": { contractID: s.contractID, from: s.vendor, amount: collection.NewCoins( From 71037b79002a2a2d3cd4e52b8c01994ea791f2de Mon Sep 17 00:00:00 2001 From: zemyblue Date: Tue, 26 Sep 2023 21:14:55 +0900 Subject: [PATCH 09/12] chore: update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b176895439..106843f48f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -54,6 +54,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (refactor) [\#1114](https://github.com/Finschia/finschia-sdk/pull/1114) Check statistics and balance on x/collection mint and burn operations * (x/token) [\#1128](https://github.com/Finschia/finschia-sdk/pull/1128) add more unittest for MsgIssue of x/token * (x/token) [\#1129](https://github.com/Finschia/finschia-sdk/pull/1129) add more unittest for `MsgGrantPermission` and `MsgRevokePermission` of x/token +* (x/collection) [\#1131](https://github.com/Finschia/finschia-sdk/pull/1131) add additional unittest of x/collection(`MsgIssueFT`, `MsgMintFT`, `MsgBurnFT`) ### Bug Fixes * (ledger) [\#1040](https://github.com/Finschia/finschia-sdk/pull/1040) Fix a bug(unable to connect nano S plus ledger on ubuntu) From 235d9f78389b4a172b86fc45a5fa6b406000b09d Mon Sep 17 00:00:00 2001 From: zemyblue Date: Tue, 26 Sep 2023 21:20:26 +0900 Subject: [PATCH 10/12] chore: fix typo --- x/collection/keeper/msg_server_test.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/x/collection/keeper/msg_server_test.go b/x/collection/keeper/msg_server_test.go index 4c70bfacfd..79677c5c49 100644 --- a/x/collection/keeper/msg_server_test.go +++ b/x/collection/keeper/msg_server_test.go @@ -680,7 +680,7 @@ func (s *KeeperTestSuite) TestMsgMintFT() { } // query the values to be effected by MintFT - queryMintFTDatas := func(ctx sdk.Context, coins collection.Coins, contractID string) (balances collection.Coins, supply []sdk.Int, minted []sdk.Int) { + queryValuesEffectedByMintFT := func(ctx sdk.Context, coins collection.Coins, contractID string) (balances collection.Coins, supply []sdk.Int, minted []sdk.Int) { for _, am := range coins { // save balance bal, err := s.queryServer.Balance(sdk.WrapSDKContext(ctx), &collection.QueryBalanceRequest{ @@ -717,7 +717,7 @@ func (s *KeeperTestSuite) TestMsgMintFT() { for t := 0; t < 3; t++ { ctx, _ = ctx.CacheContext() - prevAmount, prevSupply, prevMinted := queryMintFTDatas(ctx, tc.amount, tc.contractID) + prevAmount, prevSupply, prevMinted := queryValuesEffectedByMintFT(ctx, tc.amount, tc.contractID) req := &collection.MsgMintFT{ ContractId: tc.contractID, @@ -735,7 +735,7 @@ func (s *KeeperTestSuite) TestMsgMintFT() { s.Require().Equal(tc.events, ctx.EventManager().Events()) // check results - afterAmount, afterSupply, afterMinted := queryMintFTDatas(ctx, tc.amount, tc.contractID) + afterAmount, afterSupply, afterMinted := queryValuesEffectedByMintFT(ctx, tc.amount, tc.contractID) for i, am := range tc.amount { expectedBalance := collection.Coin{ TokenId: am.TokenId, @@ -926,7 +926,7 @@ func (s *KeeperTestSuite) TestMsgBurnFT() { } // query the values to be effected by BurnFT - queryBurnTDatas := func(ctx sdk.Context, coins collection.Coins, contractID, from string) (balances collection.Coins, supply []sdk.Int, burnt []sdk.Int) { + queryValuesAffectedByBurnFT := func(ctx sdk.Context, coins collection.Coins, contractID, from string) (balances collection.Coins, supply []sdk.Int, burnt []sdk.Int) { for _, am := range coins { // save balance bal, err := s.queryServer.Balance(sdk.WrapSDKContext(ctx), &collection.QueryBalanceRequest{ @@ -962,7 +962,7 @@ func (s *KeeperTestSuite) TestMsgBurnFT() { ctx, _ := s.ctx.CacheContext() for t := 0; t < 3; t++ { ctx, _ = ctx.CacheContext() - prevAmount, prevSupply, prevBurnt := queryBurnTDatas(ctx, tc.amount, tc.contractID, tc.from.String()) + prevAmount, prevSupply, prevBurnt := queryValuesAffectedByBurnFT(ctx, tc.amount, tc.contractID, tc.from.String()) req := &collection.MsgBurnFT{ ContractId: tc.contractID, @@ -979,7 +979,7 @@ func (s *KeeperTestSuite) TestMsgBurnFT() { s.Require().Equal(tc.events, ctx.EventManager().Events()) // check changed amount - afterAmount, afterSupply, afterBurnt := queryBurnTDatas(ctx, tc.amount, tc.contractID, tc.from.String()) + afterAmount, afterSupply, afterBurnt := queryValuesAffectedByBurnFT(ctx, tc.amount, tc.contractID, tc.from.String()) for i, am := range tc.amount { expectedBalance := prevAmount[i].Amount.Sub(am.Amount) s.Require().Equal(am.TokenId, afterAmount[i].TokenId) From 2aaf510dcead1b3a68d4f7a4b76f76ee31d9dd7d Mon Sep 17 00:00:00 2001 From: zemyblue Date: Tue, 26 Sep 2023 21:35:07 +0900 Subject: [PATCH 11/12] chore: modify initialize context --- x/collection/keeper/msg_server_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/x/collection/keeper/msg_server_test.go b/x/collection/keeper/msg_server_test.go index 79677c5c49..ef0141b26f 100644 --- a/x/collection/keeper/msg_server_test.go +++ b/x/collection/keeper/msg_server_test.go @@ -713,7 +713,7 @@ func (s *KeeperTestSuite) TestMsgMintFT() { for name, tc := range testCases { s.Run(name, func() { // test multiple times - ctx, _ := s.ctx.CacheContext() + ctx := s.ctx for t := 0; t < 3; t++ { ctx, _ = ctx.CacheContext() @@ -959,7 +959,7 @@ func (s *KeeperTestSuite) TestMsgBurnFT() { for name, tc := range testCases { s.Run(name, func() { // test multiple times - ctx, _ := s.ctx.CacheContext() + ctx := s.ctx for t := 0; t < 3; t++ { ctx, _ = ctx.CacheContext() prevAmount, prevSupply, prevBurnt := queryValuesAffectedByBurnFT(ctx, tc.amount, tc.contractID, tc.from.String()) From 4ce0bdaa7cef3dbc3ec299370b2b98f25ec7c3b0 Mon Sep 17 00:00:00 2001 From: zemyblue Date: Thu, 5 Oct 2023 18:37:33 +0900 Subject: [PATCH 12/12] chore: rearrange import packages in Update x/collection/keeper/msg_server_test.go Co-authored-by: Jayden Lee <41176085+tkxkd0159@users.noreply.github.com> --- x/collection/keeper/msg_server_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/x/collection/keeper/msg_server_test.go b/x/collection/keeper/msg_server_test.go index ef0141b26f..5e21e07050 100644 --- a/x/collection/keeper/msg_server_test.go +++ b/x/collection/keeper/msg_server_test.go @@ -3,10 +3,11 @@ package keeper_test import ( "fmt" + abci "github.com/tendermint/tendermint/abci/types" + sdk "github.com/Finschia/finschia-sdk/types" "github.com/Finschia/finschia-sdk/x/collection" "github.com/Finschia/finschia-sdk/x/token/class" - abci "github.com/tendermint/tendermint/abci/types" ) func (s *KeeperTestSuite) TestMsgSendFT() {