Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented the soulbound feature for NFTs #693

Merged
merged 7 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1643,6 +1643,7 @@ ClassFeature defines possible features of non-fungible token class.
| freezing | 1 | |
| whitelisting | 2 | |
| disable_sending | 3 | |
| soulbound | 4 | |


<!-- end enums -->
Expand Down
3 changes: 2 additions & 1 deletion docs/static/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -7287,7 +7287,8 @@
"burning",
"freezing",
"whitelisting",
"disable_sending"
"disable_sending",
"soulbound"
],
"default": "burning",
"description": "ClassFeature defines possible features of non-fungible token class."
Expand Down
85 changes: 85 additions & 0 deletions integration-tests/modules/assetnft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2054,6 +2054,91 @@ func TestAssetNFTClassWhitelist(t *testing.T) {
requireT.NoError(err)
}

func TestAssetNFTSoulbound(t *testing.T) {
t.Parallel()

ctx, chain := integrationtests.NewCoreumTestingContext(t)

requireT := require.New(t)
issuer := chain.GenAccount()
recipient := chain.GenAccount()

chain.FundAccountWithOptions(ctx, t, issuer, integration.BalancesOptions{
Messages: []sdk.Msg{
&assetnfttypes.MsgIssueClass{},
&assetnfttypes.MsgMint{},
&nft.MsgSend{},
},
Amount: chain.QueryAssetNFTParams(ctx, t).MintFee.Amount,
})

chain.FundAccountWithOptions(ctx, t, recipient, integration.BalancesOptions{
Messages: []sdk.Msg{
&nft.MsgSend{},
},
})

// issue new NFT class
issueMsg := &assetnfttypes.MsgIssueClass{
Issuer: issuer.String(),
Symbol: "NFTClassSymbol",
Features: []assetnfttypes.ClassFeature{
assetnfttypes.ClassFeature_soulbound,
},
}

// mint new token in that class
classID := assetnfttypes.BuildClassID(issueMsg.Symbol, issuer)
nftID := "id-1"
mintMsg1 := &assetnfttypes.MsgMint{
Sender: issuer.String(),
ID: nftID,
ClassID: classID,
}

msgList := []sdk.Msg{issueMsg, mintMsg1}
_, err := client.BroadcastTx(
ctx,
chain.ClientContext.WithFromAddress(issuer),
chain.TxFactory().WithGas(chain.GasLimitByMsgs(msgList...)),
msgList...,
)
requireT.NoError(err)

// try to send from issuer to recipient (it is allowed)
sendMsg := &nft.MsgSend{
ClassId: classID,
Id: nftID,
Sender: issuer.String(),
Receiver: recipient.String(),
}

_, err = client.BroadcastTx(
ctx,
chain.ClientContext.WithFromAddress(issuer),
chain.TxFactory().WithGas(chain.GasLimitByMsgs(sendMsg)),
sendMsg,
)
requireT.NoError(err)

// try to send from recipient to issuer (it is not allowed)
sendMsg = &nft.MsgSend{
ClassId: classID,
Id: nftID,
Sender: recipient.String(),
Receiver: issuer.String(),
}

_, err = client.BroadcastTx(
ctx,
chain.ClientContext.WithFromAddress(recipient),
chain.TxFactory().WithGas(chain.GasLimitByMsgs(sendMsg)),
sendMsg,
)
requireT.Error(err)
requireT.ErrorIs(err, cosmoserrors.ErrUnauthorized)
}

// TestAssetNFTSendAuthorization tests that assetnft SendAuthorization works as expected.
func TestAssetNFTSendAuthorization(t *testing.T) {
t.Parallel()
Expand Down
1 change: 1 addition & 0 deletions proto/coreum/asset/nft/v1/nft.proto
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ enum ClassFeature {
freezing = 1;
whitelisting = 2;
disable_sending = 3;
soulbound = 4;
}

// ClassDefinition defines the non-fungible token class settings to store.
Expand Down
5 changes: 5 additions & 0 deletions x/asset/nft/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,11 @@ func (k Keeper) isNFTSendable(ctx sdk.Context, classID, nftID string) error {
return sdkerrors.Wrapf(cosmoserrors.ErrUnauthorized, "nft with classID:%s and ID:%s has sending disabled", classID, nftID)
}

// we check for soulbound only after the check for issuer, since the issuer should be able to send the token.
if classDefinition.IsFeatureEnabled(types.ClassFeature_soulbound) {
return sdkerrors.Wrapf(cosmoserrors.ErrUnauthorized, "nft with classID:%s and ID:%s is soulbound and cannot be sent", classID, nftID)
}

isFrozen, err := k.IsFrozen(ctx, classID, nftID)
if err != nil {
if errors.Is(err, types.ErrFeatureDisabled) {
Expand Down
109 changes: 109 additions & 0 deletions x/asset/nft/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ import (
cosmoserrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/query"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/cosmos/cosmos-sdk/x/nft"
"github.com/stretchr/testify/require"

"github.com/CoreumFoundation/coreum/v3/pkg/config/constant"
"github.com/CoreumFoundation/coreum/v3/testutil/event"
"github.com/CoreumFoundation/coreum/v3/testutil/simapp"
"github.com/CoreumFoundation/coreum/v3/x/asset/nft/keeper"
"github.com/CoreumFoundation/coreum/v3/x/asset/nft/types"
wnftkeeper "github.com/CoreumFoundation/coreum/v3/x/wnft/keeper"
)

func TestKeeper_IssueClass(t *testing.T) {
Expand Down Expand Up @@ -1281,6 +1283,107 @@ func TestKeeper_ClassFreeze_Nonexistent(t *testing.T) {
requireT.ErrorIs(err, types.ErrClassNotFound)
}

func TestKeeper_Soulbound(t *testing.T) {
requireT := require.New(t)
testApp := simapp.New()
ctx := testApp.NewContext(false, tmproto.Header{})
assetNFTKeeper := testApp.AssetNFTKeeper
nftKeeper := testApp.NFTKeeper

nftParams := types.Params{
MintFee: sdk.NewInt64Coin(constant.DenomDev, 0),
}
requireT.NoError(assetNFTKeeper.SetParams(ctx, nftParams))

issuer := sdk.AccAddress(ed25519.GenPrivKey().PubKey().Address())
classSettings := types.IssueClassSettings{
Issuer: issuer,
Symbol: "symbol",
Features: []types.ClassFeature{
types.ClassFeature_soulbound,
},
}

classID, err := assetNFTKeeper.IssueClass(ctx, classSettings)
requireT.NoError(err)

// mint NFT
recipient := sdk.AccAddress(ed25519.GenPrivKey().PubKey().Address())
settings := types.MintSettings{
Sender: issuer,
Recipient: recipient,
ClassID: classID,
ID: "my-id",
URI: "https://my-nft-meta.invalid/1",
URIHash: "content-hash",
}

requireT.NoError(assetNFTKeeper.Mint(ctx, settings))
nftID := settings.ID

// transfer must be rejected
recipient2 := sdk.AccAddress(ed25519.GenPrivKey().PubKey().Address())
err = nftKeeper.Transfer(ctx, classID, nftID, recipient2)
requireT.Error(err)
requireT.ErrorIs(err, cosmoserrors.ErrUnauthorized)

// transfer to issuer must also be rejected
err = nftKeeper.Transfer(ctx, classID, nftID, issuer)
requireT.Error(err)
requireT.ErrorIs(err, cosmoserrors.ErrUnauthorized)
}

func TestKeeper_Soulbound_Burning(t *testing.T) {
requireT := require.New(t)
testApp := simapp.New()
ctx := testApp.NewContext(false, tmproto.Header{})
assetNFTKeeper := testApp.AssetNFTKeeper
nftKeeper := testApp.NFTKeeper

nftParams := types.Params{
MintFee: sdk.NewInt64Coin(constant.DenomDev, 0),
}
requireT.NoError(assetNFTKeeper.SetParams(ctx, nftParams))

issuer := sdk.AccAddress(ed25519.GenPrivKey().PubKey().Address())
classSettings := types.IssueClassSettings{
Issuer: issuer,
Symbol: "symbol",
Features: []types.ClassFeature{
types.ClassFeature_soulbound,
types.ClassFeature_burning,
},
}

classID, err := assetNFTKeeper.IssueClass(ctx, classSettings)
requireT.NoError(err)

// mint NFT
recipient := sdk.AccAddress(ed25519.GenPrivKey().PubKey().Address())
settings := types.MintSettings{
Sender: issuer,
Recipient: recipient,
ClassID: classID,
ID: "my-id",
URI: "https://my-nft-meta.invalid/1",
URIHash: "content-hash",
}

requireT.NoError(assetNFTKeeper.Mint(ctx, settings))
nftID := settings.ID

// transfer must be rejected
recipient2 := sdk.AccAddress(ed25519.GenPrivKey().PubKey().Address())
err = nftKeeper.Transfer(ctx, classID, nftID, recipient2)
requireT.Error(err)
requireT.ErrorIs(err, cosmoserrors.ErrUnauthorized)

// burning is allowed
err = assetNFTKeeper.Burn(ctx, recipient, classID, nftID)
requireT.NoError(err)
requireT.False(nftKeeper.HasNFT(ctx, classID, nftID))
}

func genNFTData(requireT *require.Assertions) *codectypes.Any {
dataString := "metadata"
dataValue, err := codectypes.NewAnyWithValue(&types.DataBytes{Data: []byte(dataString)})
Expand Down Expand Up @@ -1309,3 +1412,9 @@ func assertFrozen(t *testing.T, ctx sdk.Context, k keeper.Keeper, classID, nftID
require.NoError(t, err)
require.EqualValues(t, frozen, expected)
}

func assertOwner(t *testing.T, ctx sdk.Context, k wnftkeeper.Wrapper, classID, nftID string, expectedAddress sdk.AccAddress) {
res, err := k.Owner(ctx, &nft.QueryOwnerRequest{ClassId: classID, Id: nftID})
require.NoError(t, err)
require.EqualValues(t, expectedAddress.String(), res.Owner)
}
70 changes: 37 additions & 33 deletions x/asset/nft/types/nft.pb.go

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

Loading