/
state_batch.go
54 lines (41 loc) · 1.45 KB
/
state_batch.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package v1
import (
"cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/regen-network/regen-ledger/x/ecocredit/v3"
"github.com/regen-network/regen-ledger/x/ecocredit/v3/base"
)
// Validate performs basic validation of the Batch state type
func (m *Batch) Validate() error {
if m.Key == 0 {
return ecocredit.ErrParseFailure.Wrapf("key cannot be zero")
}
if _, err := sdk.AccAddressFromBech32(sdk.AccAddress(m.Issuer).String()); err != nil {
return ecocredit.ErrParseFailure.Wrapf("issuer: %s", err)
}
if m.ProjectKey == 0 {
return ecocredit.ErrParseFailure.Wrapf("project key cannot be zero")
}
if err := base.ValidateBatchDenom(m.Denom); err != nil {
return errors.Wrap(err, "denom") // returns parse error
}
if len(m.Metadata) > base.MaxMetadataLength {
return ecocredit.ErrParseFailure.Wrap("metadata cannot be more than 256 characters")
}
if m.StartDate == nil {
return ecocredit.ErrParseFailure.Wrapf("must provide a start date for the credit batch")
}
if m.EndDate == nil {
return ecocredit.ErrParseFailure.Wrapf("must provide an end date for the credit batch")
}
if m.EndDate.Compare(*m.StartDate) != 1 {
return ecocredit.ErrParseFailure.Wrapf(
"the batch end date (%s) must be the same as or after the batch start date (%s)",
m.EndDate, m.StartDate,
)
}
if m.IssuanceDate == nil {
return ecocredit.ErrParseFailure.Wrapf("must provide an issuance date for the credit batch")
}
return nil
}