-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(checkers): coin checker functions
- Loading branch information
1 parent
d42cf1c
commit 820a6f2
Showing
3 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package checkers | ||
|
||
import ( | ||
"fmt" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
) | ||
|
||
// PositiveCoins checks if all coins are valida and amount is positive. | ||
func PositiveCoins(note string, coins ...sdk.Coin) []error { | ||
var errs []error | ||
for i := range coins { | ||
if err := coins[i].Validate(); err != nil { | ||
errs = append(errs, fmt.Errorf("%s coin[%d]: %w", note, i, err)) | ||
} else if !coins[i].Amount.IsPositive() { | ||
errs = append(errs, fmt.Errorf("%s coin[%d] amount must be positive", note, i)) | ||
} | ||
} | ||
return errs | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package checkers | ||
|
||
import ( | ||
"testing" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCoinPositive(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
c0 := sdk.NewInt64Coin("abc", 0) | ||
cNeg := sdk.NewInt64Coin("abc", 0) | ||
cPos := sdk.NewInt64Coin("abc", 1) | ||
|
||
assert.Empty(PositiveCoins("")) | ||
assert.Empty(PositiveCoins("", cPos)) | ||
assert.Empty(PositiveCoins("", cPos, cPos)) | ||
|
||
assert.Contains(errsToStr(PositiveCoins("", c0)), "coin[0]") | ||
assert.Contains(errsToStr(PositiveCoins("", cNeg)), "coin[0]") | ||
assert.Contains(errsToStr(PositiveCoins("", cPos, c0)), "coin[1]") | ||
assert.Contains(errsToStr(PositiveCoins("", cPos, cNeg)), "coin[1]") | ||
assert.NotContains(errsToStr(PositiveCoins("", cPos, cNeg)), "coin[0]") | ||
|
||
assert.Contains(errsToStr(PositiveCoins("", cPos, c0, cNeg)), "coin[1]") | ||
assert.Contains(errsToStr(PositiveCoins("", cPos, c0, cNeg)), "coin[2]") | ||
assert.NotContains(errsToStr(PositiveCoins("", cPos, c0, cNeg)), "coin[0]") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package checkers | ||
|
||
import ( | ||
"strings" | ||
) | ||
|
||
func errsToStr(errs []error) string { | ||
strs := make([]string, len(errs)) | ||
for i := range errs { | ||
strs[i] = errs[i].Error() | ||
} | ||
return strings.Join(strs, " ") | ||
} |