Skip to content

Commit ad82870

Browse files
committed
cmd/tapcli: validate collectible amount when minting
Fixes #576 by making sure that the amount for collectible assets is always set at 1.
1 parent 62f625f commit ad82870

File tree

1 file changed

+40
-8
lines changed

1 file changed

+40
-8
lines changed

cmd/tapcli/assets.go

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,18 @@ var mintAssetCommand = cli.Command{
119119
},
120120
}
121121

122-
func parseAssetType(ctx *cli.Context) taprpc.AssetType {
123-
assetType := taprpc.AssetType_NORMAL
124-
if ctx.String(assetTypeName) == "collectible" {
125-
assetType = taprpc.AssetType_COLLECTIBLE
126-
}
122+
func parseAssetType(ctx *cli.Context) (taprpc.AssetType, error) {
123+
switch ctx.String(assetTypeName) {
124+
case "normal":
125+
return taprpc.AssetType_NORMAL, nil
126+
127+
case "collectible":
128+
return taprpc.AssetType_COLLECTIBLE, nil
127129

128-
return assetType
130+
default:
131+
return 0, fmt.Errorf("unknown asset type '%v'",
132+
ctx.String(assetTypeName))
133+
}
129134
}
130135

131136
func mintAsset(ctx *cli.Context) error {
@@ -178,16 +183,43 @@ func mintAsset(ctx *cli.Context) error {
178183
}
179184
}
180185

186+
assetType, err := parseAssetType(ctx)
187+
if err != nil {
188+
return err
189+
}
190+
191+
var (
192+
amount = ctx.Uint64(assetSupplyName)
193+
isCollectible = assetType == taprpc.AssetType_COLLECTIBLE
194+
)
195+
switch {
196+
// If the user did not specify the supply, we can silently assume they
197+
// are aware that the collectible amount is always 1.
198+
case isCollectible && !ctx.IsSet(assetSupplyName):
199+
amount = 1
200+
201+
// If the user explicitly supplied a supply that is incorrect, we must
202+
// inform them instead of silently changing the value to 1, otherwise
203+
// there will be surprises later.
204+
case isCollectible && amount != 1:
205+
return fmt.Errorf("supply must be 1 for collectibles")
206+
207+
// Check that the amount is greater than 0 for normal assets. This is
208+
// also checked in the RPC server, but we can avoid the round trip.
209+
case !isCollectible && amount == 0:
210+
return fmt.Errorf("supply must be set for normal assets")
211+
}
212+
181213
ctxc := getContext()
182214
client, cleanUp := getMintClient(ctx)
183215
defer cleanUp()
184216

185217
resp, err := client.MintAsset(ctxc, &mintrpc.MintAssetRequest{
186218
Asset: &mintrpc.MintAsset{
187-
AssetType: parseAssetType(ctx),
219+
AssetType: assetType,
188220
Name: ctx.String(assetTagName),
189221
AssetMeta: assetMeta,
190-
Amount: ctx.Uint64(assetSupplyName),
222+
Amount: amount,
191223
GroupKey: groupKey,
192224
GroupAnchor: ctx.String(assetGroupAnchorName),
193225
AssetVersion: taprpc.AssetVersion(

0 commit comments

Comments
 (0)