Skip to content

Commit

Permalink
Merge pull request #994 from EmpowerPlastic/pc-metadata-improv
Browse files Browse the repository at this point in the history
Don't save duplicate metadata uris
  • Loading branch information
maheshKRex authored Jul 13, 2023
2 parents 1f7eb31 + f6cec34 commit abb06d6
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
9 changes: 7 additions & 2 deletions chain/x/plasticcredit/keeper/credit.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/cosmos/gogoproto/proto"
"golang.org/x/exp/slices"

"github.com/EmpowerPlastic/empowerchain/utils"
"github.com/EmpowerPlastic/empowerchain/x/certificates"
Expand Down Expand Up @@ -278,8 +279,12 @@ func (k Keeper) issueCredits(ctx sdk.Context, creator string, projectID uint64,
} else {
// If collection already exists, add new credits to the total and append new data if present
creditCollection.TotalAmount.Active += amount
creditCollection.MetadataUris = append(creditCollection.MetadataUris, metadataUris...)

// avoid adding duplicate metadata uris
for _, uri := range metadataUris {
if !slices.Contains(creditCollection.MetadataUris, uri) {
creditCollection.MetadataUris = append(creditCollection.MetadataUris, uri)
}
}
}

err = k.setCreditCollection(ctx, creditCollection)
Expand Down
1 change: 1 addition & 0 deletions chain/x/plasticcredit/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1264,6 +1264,7 @@ func (s *TestSuite) TestIssueCredits() {
s.Require().Equal(uint64(0), resp.Collection.TotalAmount.Retired)
s.Require().Equal(uint64(0), creditCollection.TotalAmount.Retired)
s.Require().Equal(uint64(0), ownerBalance.Balance.Retired)
s.Require().Equal(tc.msg.MetadataUris, creditCollection.MetadataUris)
s.Require().Len(events, 1)
parsedEvent, err := sdk.ParseTypedEvent(events[0])
s.Require().NoError(err)
Expand Down

0 comments on commit abb06d6

Please sign in to comment.