github.com/Finschia/finschia-sdk@v0.48.1/x/token/keeper/supply_test.go (about)

     1  package keeper_test
     2  
     3  import (
     4  	sdk "github.com/Finschia/finschia-sdk/types"
     5  	"github.com/Finschia/finschia-sdk/x/token"
     6  )
     7  
     8  func (s *KeeperTestSuite) TestIssue() {
     9  	ctx, _ := s.ctx.CacheContext()
    10  
    11  	// create a not mintable class
    12  	class := token.Contract{
    13  		Name:     "NOT Mintable",
    14  		Symbol:   "NO",
    15  		Mintable: false,
    16  	}
    17  	contractID := s.keeper.Issue(ctx, class, s.vendor, s.vendor, sdk.OneInt())
    18  
    19  	mintPermissions := []token.Permission{
    20  		token.PermissionMint,
    21  		token.PermissionBurn,
    22  	}
    23  	for _, permission := range mintPermissions {
    24  		s.Require().Nil(s.keeper.GetGrant(ctx, contractID, s.vendor, permission))
    25  	}
    26  	s.Require().NotNil(s.keeper.GetGrant(ctx, contractID, s.vendor, token.PermissionModify))
    27  }
    28  
    29  func (s *KeeperTestSuite) TestMint() {
    30  	testCases := map[string]struct {
    31  		grantee sdk.AccAddress
    32  		err     error
    33  	}{
    34  		"valid request": {
    35  			grantee: s.operator,
    36  		},
    37  		"no permission": {
    38  			grantee: s.customer,
    39  			err:     token.ErrTokenNoPermission,
    40  		},
    41  	}
    42  
    43  	for name, tc := range testCases {
    44  		s.Run(name, func() {
    45  			ctx, _ := s.ctx.CacheContext()
    46  
    47  			err := s.keeper.Mint(ctx, s.contractID, tc.grantee, s.stranger, sdk.OneInt())
    48  			s.Require().ErrorIs(err, tc.err)
    49  			if tc.err != nil {
    50  				return
    51  			}
    52  		})
    53  	}
    54  }
    55  
    56  func (s *KeeperTestSuite) TestBurn() {
    57  	testCases := map[string]struct {
    58  		from   sdk.AccAddress
    59  		amount sdk.Int
    60  		err    error
    61  	}{
    62  		"valid request": {
    63  			from:   s.vendor,
    64  			amount: s.balance,
    65  		},
    66  		"no permission": {
    67  			from:   s.customer,
    68  			amount: s.balance,
    69  			err:    token.ErrTokenNoPermission,
    70  		},
    71  		"insufficient tokens": {
    72  			from:   s.vendor,
    73  			amount: s.balance.Add(sdk.OneInt()),
    74  			err:    token.ErrInsufficientBalance,
    75  		},
    76  	}
    77  
    78  	for name, tc := range testCases {
    79  		s.Run(name, func() {
    80  			ctx, _ := s.ctx.CacheContext()
    81  
    82  			err := s.keeper.Burn(ctx, s.contractID, tc.from, tc.amount)
    83  			s.Require().ErrorIs(err, tc.err)
    84  			if tc.err != nil {
    85  				return
    86  			}
    87  		})
    88  	}
    89  }
    90  
    91  func (s *KeeperTestSuite) TestOperatorBurn() {
    92  	testCases := map[string]struct {
    93  		operator sdk.AccAddress
    94  		from     sdk.AccAddress
    95  		amount   sdk.Int
    96  		err      error
    97  	}{
    98  		"valid request": {
    99  			operator: s.operator,
   100  			from:     s.customer,
   101  			amount:   s.balance,
   102  		},
   103  		"not authorized": {
   104  			operator: s.vendor,
   105  			from:     s.stranger,
   106  			amount:   s.balance,
   107  			err:      token.ErrTokenNotApproved,
   108  		},
   109  		"no permission": {
   110  			operator: s.stranger,
   111  			from:     s.customer,
   112  			amount:   s.balance,
   113  			err:      token.ErrTokenNoPermission,
   114  		},
   115  		"insufficient tokens": {
   116  			operator: s.operator,
   117  			from:     s.customer,
   118  			amount:   s.balance.Add(sdk.OneInt()),
   119  			err:      token.ErrInsufficientBalance,
   120  		},
   121  	}
   122  
   123  	for name, tc := range testCases {
   124  		s.Run(name, func() {
   125  			ctx, _ := s.ctx.CacheContext()
   126  
   127  			err := s.keeper.OperatorBurn(ctx, s.contractID, tc.operator, tc.from, tc.amount)
   128  			s.Require().ErrorIs(err, tc.err)
   129  			if tc.err != nil {
   130  				return
   131  			}
   132  		})
   133  	}
   134  }
   135  
   136  func (s *KeeperTestSuite) TestModify() {
   137  	changes := []token.Attribute{
   138  		{Key: token.AttributeKeyName.String(), Value: "new name"},
   139  		{Key: token.AttributeKeyURI.String(), Value: "new uri"},
   140  		{Key: token.AttributeKeyMeta.String(), Value: "new meta"},
   141  	}
   142  
   143  	ctx, _ := s.ctx.CacheContext()
   144  
   145  	err := s.keeper.Modify(ctx, s.contractID, s.vendor, changes)
   146  	s.Require().NoError(err)
   147  
   148  	class, err := s.keeper.GetClass(ctx, s.contractID)
   149  	s.Require().NoError(err)
   150  
   151  	s.Require().Equal(changes[0].Value, class.Name)
   152  	s.Require().Equal(changes[1].Value, class.Uri)
   153  	s.Require().Equal(changes[2].Value, class.Meta)
   154  }