From b4d7c1f5cbed1933d8260a19c8ea5585937a5c81 Mon Sep 17 00:00:00 2001
From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com>
Date: Fri, 17 Sep 2021 00:52:18 +0200
Subject: [PATCH] fix: Incorrect error message when periodlimit is not set on a
 feegrant tx (backport #10050) (#10168)

* fix: Incorrect error message when periodlimit is not set on a feegrant tx (#10050)

<!--
The default pull request template is for types feat, fix, or refactor.
For other templates, add one of the following parameters to the url:
- template=docs.md
- template=other.md
-->

## Description

I updated the error check for periodlimit on a feegrant tx to return that the user needs to set the period limit rather than that the number of arguments is incorrect.

Closes: #10049

<!-- Add a description of the changes that this PR introduces and the files that
are the most critical to review. -->

---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [x] added `!` to the type prefix if API or client breaking change
- [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [x] provided a link to the relevant issue or specification
- [x] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [x] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing)
- [x] added a changelog entry to `CHANGELOG.md`
- [x] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [x] updated the relevant documentation or specification
- [x] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)

(cherry picked from commit 7e90194ec6d5b3b03abb66129cbf10b79249da04)

# Conflicts:
#	CHANGELOG.md

* Fix changelog

* Update CHANGELOG.md

Co-authored-by: fkneeland-figure <86616427+fkneeland-figure@users.noreply.github.com>
Co-authored-by: Amaury M <1293565+amaurym@users.noreply.github.com>
Co-authored-by: Robert Zaremba <robert@zaremba.ch>
---
 CHANGELOG.md                |  1 +
 x/feegrant/client/cli/tx.go | 39 ++++++++++++++++++++-----------------
 2 files changed, 22 insertions(+), 18 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 15e6e2dcb69f..7ef9e0610879 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -45,6 +45,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
 * [\#9969](https://github.com/cosmos/cosmos-sdk/pull/9969) fix: use keyring in config for add-genesis-account cmd.
 * (x/genutil) [#10104](https://github.com/cosmos/cosmos-sdk/pull/10104) Ensure the `init` command reads the `--home` flag value correctly.
 * [\#10061](https://github.com/cosmos/cosmos-sdk/pull/10061) Ensure that `LegacyAminoPubKey` struct correctly unmarshals from JSON
+* (x/feegrant) [\#10049](https://github.com/cosmos/cosmos-sdk/issues/10049) Fixed the error message when `period` or `period-limit` flag is not set on a feegrant grant transaction.
 
 ### Client Breaking Changes
 
diff --git a/x/feegrant/client/cli/tx.go b/x/feegrant/client/cli/tx.go
index 44b673d809b0..66022abfa90a 100644
--- a/x/feegrant/client/cli/tx.go
+++ b/x/feegrant/client/cli/tx.go
@@ -125,25 +125,28 @@ Examples:
 					return err
 				}
 
-				if periodClock > 0 && periodLimit != nil {
-					periodReset := getPeriodReset(periodClock)
-					if exp != "" && periodReset.Sub(expiresAtTime) > 0 {
-						return fmt.Errorf("period(%d) cannot reset after expiration(%v)", periodClock, exp)
-					}
-
-					periodic := feegrant.PeriodicAllowance{
-						Basic:            basic,
-						Period:           getPeriod(periodClock),
-						PeriodReset:      getPeriodReset(periodClock),
-						PeriodSpendLimit: periodLimit,
-						PeriodCanSpend:   periodLimit,
-					}
-
-					grant = &periodic
-
-				} else {
-					return fmt.Errorf("invalid number of args %d", len(args))
+				if periodClock <= 0 {
+					return fmt.Errorf("period clock was not set")
 				}
+
+				if periodLimit == nil {
+					return fmt.Errorf("period limit was not set")
+				}
+
+				periodReset := getPeriodReset(periodClock)
+				if exp != "" && periodReset.Sub(expiresAtTime) > 0 {
+					return fmt.Errorf("period (%d) cannot reset after expiration (%v)", periodClock, exp)
+				}
+
+				periodic := feegrant.PeriodicAllowance{
+					Basic:            basic,
+					Period:           getPeriod(periodClock),
+					PeriodReset:      getPeriodReset(periodClock),
+					PeriodSpendLimit: periodLimit,
+					PeriodCanSpend:   periodLimit,
+				}
+
+				grant = &periodic
 			}
 
 			allowedMsgs, err := cmd.Flags().GetStringSlice(FlagAllowedMsgs)