Skip to content

Commit

Permalink
Merge branch 'aaronc/independent-projects-cli' into aaronc/independen…
Browse files Browse the repository at this point in the history
…t-projects-impl
  • Loading branch information
aaronc committed May 2, 2024
2 parents f4fd66a + 5724801 commit 2b8e175
Show file tree
Hide file tree
Showing 3 changed files with 257 additions and 26 deletions.
63 changes: 63 additions & 0 deletions x/ecocredit/base/client/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -667,3 +667,66 @@ func QueryAllowedBridgeChainsCmd() *cobra.Command {
flags.AddPaginationFlagsToCmd(cmd, "allowed-bridge-chains")
return qflags(cmd)
}

func QueryProjectEnrollment() *cobra.Command {
cmd := &cobra.Command{
Use: "project-enrollment [project-id] [class-id]",
Short: "Retrieve project enrollment information",
Long: "Retrieve project enrollment information.",
Example: "regen q ecocredit project-enrollment P001 C01",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
c, ctx, err := mkQueryClient(cmd)
if err != nil {
return err
}
res, err := c.ProjectEnrollment(cmd.Context(), &types.QueryProjectEnrollmentRequest{
ProjectId: args[0],
ClassId: args[1],
})
return printQueryResponse(ctx, res, err)
},
}
return qflags(cmd)
}

func QueryProjectEnrollments() *cobra.Command {
var projectID, classID string

cmd := &cobra.Command{
Use: "project-enrollments",
Short: "Retrieve project enrollments",
Long: `Retrieve project enrollments with optional pagination and filtering by project or credit class.
Flags:
--project string Filter by project ID
--class string Filter by credit class ID
`,
Example: `regen q ecocredit project-enrollments --limit 10 --offset 10
regen q ecocredit project-enrollments --project P001
regen q ecocredit project-enrollments --class C01`,
RunE: func(cmd *cobra.Command, args []string) error {
c, ctx, err := mkQueryClient(cmd)
if err != nil {
return err
}

pagination, err := client.ReadPageRequest(cmd.Flags())
if err != nil {
return err
}

res, err := c.ProjectEnrollments(cmd.Context(), &types.QueryProjectEnrollmentsRequest{
ProjectId: projectID,
ClassId: classID,
Pagination: pagination,
})
return printQueryResponse(ctx, res, err)
},
}

cmd.Flags().StringVar(&projectID, FlagProject, "", "Filter by project ID")
cmd.Flags().StringVar(&classID, FlagClass, "", "Filter by credit class ID")

return qflags(cmd)
}
218 changes: 192 additions & 26 deletions x/ecocredit/base/client/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@ const (
FlagAddIssuers string = "add-issuers"
FlagReason string = "reason"
FlagRemoveIssuers string = "remove-issuers"
FlagClass string = "class"
FlagProject string = "project"
FlagReferenceID string = "reference-id"
FlagRetirementJurisdiction string = "retirement-jurisdiction"
FlagRetirementReason string = "retirement-reason"
FlagClassFee string = "class-fee"
FlagProjectFee string = "project-fee"
FlagMetadata string = "metadata"
)

// TxCreateClassCmd returns a transaction command that creates a credit class.
Expand Down Expand Up @@ -109,48 +113,77 @@ regen tx ecocredit create-class regen1elq7ys34gpkj3jyvqee0h6yk4h9wsfxmgqelsw,reg

// TxCreateProjectCmd returns a transaction command that creates a new project.
func TxCreateProjectCmd() *cobra.Command {
var classID, referenceID, projectFee string

cmd := &cobra.Command{
Use: "create-project [class-id] [jurisdiction] [metadata] [flags]",
Short: "Create a new project within a credit class",
Long: `Create a new project within a credit class.
Use: "create-project [jurisdiction] [metadata] [flags]",
Short: "Create a new project",
Long: `Create a new project. By default, this creates an unregistered project that is not enrolled in any credit
class. If the class-id flag is provided, the signer must be an issuer of the class and the project will be enrolled in
the class automatically.
Parameters:
- class-id: the ID of the credit class
- jurisdiction: the jurisdiction of the project
- metadata: any arbitrary metadata to attach to the project
Optional Flags:
Flags:
- reference-id: a reference ID for the project`,
Example: `regen tx ecocredit create-project C01 "US-WA 98225" regen:13toVgf5UjYBz6J29x28pLQyjKz5FpcW3f4bT5uRKGxGREWGKjEdXYG.rdf
regen tx ecocredit create-project C01 "US-WA 98225" regen:13toVgf5UjYBz6J29x28pLQyjKz5FpcW3f4bT5uRKGxGREWGKjEdXYG.rdf --reference-id VCS-001`,
Args: cobra.ExactArgs(3),
- project-fee: the fee that the project creator will pay to create the project. It must be >= the
required project creation fee param. If the project creation fee is zero, no fee is required.
We explicitly include the project creation fee here so that the project creator acknowledges paying
the fee and is not surprised to learn that the they paid a fee without consent.
- class: the ID of the credit class. If this is provided, the signer must be an issuer of the class.
- reference-id: a reference ID for the project. Only valid if class-id is also provided.`,
Example: `regen tx ecocredit create-project "US-WA 98225" regen:13toVgf5UjYBz6J29x28pLQyjKz5FpcW3f4bT5uRKGxGREWGKjEdXYG.rdf
regen tx ecocredit create-project "US-WA 98225" regen:13toVgf5UjYBz6J29x28pLQyjKz5FpcW3f4bT5uRKGxGREWGKjEdXYG.rdf --class-id C01 --reference-id VCS-001`,
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := sdkclient.GetClientTxContext(cmd)
if err != nil {
return err
}

referenceID, err := cmd.Flags().GetString(FlagReferenceID)
if err != nil {
return err
jurisdiction := args[0]
metadata := args[1]

// parse and normalize project fee
var fee sdk.Coin
if projectFee != "" {
var err error
fee, err = sdk.ParseCoinNormalized(projectFee)
if err != nil {
return fmt.Errorf("failed to parse class-fee: %w", err)
}
}

msg := types.MsgCreateProject{
Admin: clientCtx.GetFromAddress().String(),
ClassId: args[0],
Jurisdiction: args[1],
Metadata: args[2],
ReferenceId: referenceID,
var msg sdk.Msg
if classID != "" {
msg = &types.MsgCreateProject{
Admin: clientCtx.GetFromAddress().String(),
ClassId: classID,
Jurisdiction: jurisdiction,
Metadata: metadata,
ReferenceId: referenceID,
Fee: &fee,
}
} else {
msg = &types.MsgCreateUnregisteredProject{
Admin: clientCtx.GetFromAddress().String(),
Jurisdiction: jurisdiction,
Metadata: metadata,
Fee: &fee,
}
}

return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), &msg)
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)

},
}

cmd.Flags().String(FlagReferenceID, "", "a reference ID for the project")
cmd.Flags().StringVar(&classID, FlagClass, "", "the ID of the credit class into which the project will be enrolled")
cmd.Flags().StringVar(&referenceID, FlagReferenceID, "", "a reference ID for the project")
cmd.Flags().StringVar(&projectFee, FlagProjectFee, "", "the fee that the project creator will pay to create the project (e.g. \"20regen\")")

return txFlags(cmd)
}
Expand All @@ -159,24 +192,25 @@ regen tx ecocredit create-project C01 "US-WA 98225" regen:13toVgf5UjYBz6J29x28pL
// represent a new credit batch.
func TxGenBatchJSONCmd() *cobra.Command {
cmd := &cobra.Command{
Use: `gen-batch-json [issuer] [project-id] [issuance-count] [metadata] [start-date] [end-date]`,
Use: `gen-batch-json [issuer] [project-id] [class-id] [issuance-count] [metadata] [start-date] [end-date]`,
Short: "Generates JSON to represent a new credit batch for use with create-batch command",
Long: `Generates JSON to represent a new credit batch for use with create-batch command.
Parameters:
- issuer: the account address of the credit batch issuer
- project-id: the ID of the project
- class-id: the ID of the credit class to which the batch belongs and in which the project is enrolled
- issuance-count: the number of issuance items to generate
- metadata: any arbitrary metadata to attach to the credit batch
- start-date: the beginning of the period during which this credit batch was
quantified and verified (format: yyyy-mm-dd)
- end-date: the end of the period during which this credit batch was
quantified and verified (format: yyyy-mm-dd)`,
Example: "regen tx ecocredit gen-batch-json regen1elq7ys34gpkj3jyvqee0h6yk4h9wsfxmgqelsw C01-001 2 regen:13toVgf5UjYBz6J29x28pLQyjKz5FpcW3f4bT5uRKGxGREWGKjEdXYG.rdf 2020-01-01 2021-01-01",
Args: cobra.ExactArgs(6),
Args: cobra.ExactArgs(7),
RunE: func(cmd *cobra.Command, args []string) error {
issuanceCount, err := strconv.ParseUint(args[2], 10, 8)
issuanceCount, err := strconv.ParseUint(args[3], 10, 8)
if err != nil {
return err
}
Expand All @@ -186,21 +220,22 @@ Parameters:
issuance[i] = &types.BatchIssuance{}
}

startDate, err := regentypes.ParseDate("start date", args[4])
startDate, err := regentypes.ParseDate("start date", args[5])
if err != nil {
return err
}

endDate, err := regentypes.ParseDate("end date", args[5])
endDate, err := regentypes.ParseDate("end date", args[6])
if err != nil {
return err
}

msg := &types.MsgCreateBatch{
Issuer: args[0],
ProjectId: args[1],
ClassId: args[2],
Issuance: issuance,
Metadata: args[3],
Metadata: args[4],
StartDate: &startDate,
EndDate: &endDate,
}
Expand Down Expand Up @@ -795,3 +830,134 @@ Example JSON:

return txFlags(cmd)
}

func TxCreateOrUpdateApplicationCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "create-or-update-application [project-id] [class-id] [flags]",
Short: "Create or update an application",
Long: `Create or update an application.
Parameters:
- project-id: the ID of the project creating or updating the application
- class-id: the ID of the credit class to which the application is being made
Flags:
- metadata: optional metadata to attach to the application
`,
Example: `regen tx ecocredit create-or-update-application P001 C01 --metadata regen:13toVgf5UjYBz6J29x28pLQyjKz5FpcW3f4bT5uRKGxGREWGKjEdXYG.rdf`,
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
return runCreateOrUpdateApplication(cmd, args, false)
},
}

cmd.Flags().String(FlagMetadata, "", "optional metadata to attach to the application")

return txFlags(cmd)
}

func TxWithdrawApplicationCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "withdraw-application [project-id] [class-id] [flags]",
Short: "Withdraw an application",
Long: `Withdraw an application.
Parameters:
- project-id: the ID of the project withdrawing the application
- class-id: the ID of the credit class from which the application is being withdrawn
Flags:
- metadata: optional metadata to attach to the withdrawal
`,
Example: `regen tx ecocredit withdraw-application P001 C01`,
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
return runCreateOrUpdateApplication(cmd, args, true)
},
}

cmd.Flags().String(FlagMetadata, "", "optional metadata to attach to the withdrawal")

return txFlags(cmd)
}

func runCreateOrUpdateApplication(cmd *cobra.Command, args []string, withdraw bool) error {
clientCtx, err := sdkclient.GetClientTxContext(cmd)
if err != nil {
return err
}

metadata, err := cmd.Flags().GetString(FlagMetadata)
if err != nil {
return err
}

msg := &types.MsgCreateOrUpdateApplication{
ProjectAdmin: clientCtx.GetFromAddress().String(),
ProjectId: args[0],
ClassId: args[1],
Metadata: metadata,
Withdraw: withdraw,
}

return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
}

func TxUpdateProjectEnrollment() *cobra.Command {
var metadata string

cmd := &cobra.Command{
Use: "update-project-enrollment [project-id] [class-id] [status] [flags]",
Short: "Update project enrollment",
Long: `Update project enrollment.
Parameters:
- project-id: the ID of the project to update
- class-id: the ID of the credit class to update
- status: the new status of the project enrollment, must be one of ACCEPTED, CHANGES_REQUESTED, REJECTED or TERMINATED
Flags:
- metadata: optional metadata to attach to the enrollment
`,
Example: `regen tx ecocredit update-project-enrollment P001 C01 ACCEPTED --metadata regen:13toVgf5UjYBz6J29x28pLQyjKz5FpcW3f4bT5uRKGxGREWGKjEdXYG.rdf`,
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := sdkclient.GetClientTxContext(cmd)
if err != nil {
return err
}

statusArg := args[2]
var status types.ProjectEnrollmentStatus
switch statusArg {
case "ACCEPTED":
status = types.ProjectEnrollmentStatus_PROJECT_ENROLLMENT_STATUS_ACCEPTED
case "CHANGES_REQUESTED":
status = types.ProjectEnrollmentStatus_PROJECT_ENROLLMENT_STATUS_CHANGES_REQUESTED
case "REJECTED":
status = types.ProjectEnrollmentStatus_PROJECT_ENROLLMENT_STATUS_REJECTED
case "TERMINATED":
status = types.ProjectEnrollmentStatus_PROJECT_ENROLLMENT_STATUS_TERMINATED
default:
return fmt.Errorf("invalid status: %s", statusArg)
}

msg := &types.MsgUpdateProjectEnrollment{
Issuer: clientCtx.GetFromAddress().String(),
ProjectId: args[0],
ClassId: args[1],
NewStatus: status,
Metadata: metadata,
}

return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}

cmd.Flags().StringVar(&metadata, FlagMetadata, "", "optional metadata to attach to the enrollment")

return txFlags(cmd)
}
2 changes: 2 additions & 0 deletions x/ecocredit/client/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ func TxCmd(name string) *cobra.Command {
baseclient.TxUpdateProjectMetadataCmd(),
baseclient.TxUpdateBatchMetadataCmd(),
baseclient.TxBridgeCmd(),
baseclient.TxCreateOrUpdateApplicationCmd(),
baseclient.TxWithdrawApplicationCmd(),
basketclient.TxCreateBasketCmd(),
basketclient.TxPutInBasketCmd(),
basketclient.TxTakeFromBasketCmd(),
Expand Down

0 comments on commit 2b8e175

Please sign in to comment.