syntax = "proto3";

package regen.ecocredit.v1alpha1;

option go_package = "github.com/regen-network/regen-ledger/x/ecocredit";

// Msg is the regen.ecocredit.v1alpha1 Msg service.
service Msg {

    // CreateClass creates a new credit class with an approved list of issuers and optional metadata.
    rpc CreateClass(MsgCreateClassRequest) returns (MsgCreateClassResponse);

    // CreateBatch creates a new batch of credits for an existing credit class. This will create a new batch denom
    // with a fixed supply. Issued credits can be distributed to recipients in either tradable or retired form.
    rpc CreateBatch(MsgCreateBatchRequest) returns (MsgCreateBatchResponse);

    // Send sends tradeable credits from one account to another account. Sent credits can either be tradable or retired on receipt.
    rpc Send(MsgSendRequest) returns (MsgSendResponse);

    // Retire retires a specified number of credits in the holder's account.
    rpc Retire(MsgRetireRequest) returns (MsgRetireResponse);

    // SetPrecision allows an issuer to increase the decimal precision of a credit batch. It is an experimental feature
    // to concretely explore an idea proposed in https://github.com/cosmos/cosmos-sdk/issues/7113. The number of decimal
    // places allowed for a credit batch is determined by the original number of decimal places used with calling CreatBatch.
    // SetPrecision allows the number of allowed decimal places to be increased, effectively making the supply more
    // granular without actually changing any balances. It allows asset issuers to be able to issue an asset without needing
    // to think about how many subdivisions are needed upfront. While it may not be relevant for credits which likely have
    // a fairly stable market value, I wanted to experiment a bit and this serves as a proof of concept for a broader
    // bank redesign where say for instance a coin like the ATOM or XRN could be issued in its own units rather than
    // micro or nano-units. Instead an operation like SetPrecision would allow trading in micro, nano or pico in the future
    // based on market demand. Arbitrary, unbounded precision is not desirable because this can lead to spam attacks (like
    // sending 0.000000000000000000000000000001 coins). This is effectively fixed precision so under the hood it is still
    // basically an integer, but the fixed precision can be increased so its more adaptable long term than just an integer.
    rpc SetPrecision(MsgSetPrecisionRequest) returns (MsgSetPrecisionResponse);
}

// MsgCreateClassRequest is the Msg/CreateClass request type.
message MsgCreateClassRequest {

    // designer is the address of the account which designed the credit class. The designer has special permissions
    // to change the list of issuers and perform other administrative operations.
    string designer = 1;

    // issuers are the account addresses of the approved issuers.
    repeated string issuers = 2;

    // metadata is any arbitrary metadata to attached to the credit class.
    bytes metadata = 3;
}

// MsgCreateClassResponse is the Msg/CreateClass response type.
message MsgCreateClassResponse {

    // class_id is the unique ID of the newly created credit class.
    string class_id = 1;
}

// MsgCreateBatchRequest is the Msg/CreateBatch request type.
message MsgCreateBatchRequest {

    // issuer is the address of the batch issuer.
    string issuer = 1;

    // class_id is the unique ID of the class.
    string class_id = 2;

    // issuance are the credits issued in the batch.
    repeated BatchIssuance issuance = 3;

    // metadata is any arbitrary metadata to attached to the credit batch.
    bytes metadata = 4;

    // BatchIssuance represents the issuance of some credits in a batch to a single recipient.
    message BatchIssuance {

        // recipient is the account of the recipient.
        string recipient = 1;

        // tradable_units are the units of credits in this issuance that can be traded by this recipient.
        // Decimal values are acceptable.
        string tradable_units = 2;

        // retired_units are the units of credits in this issuance that are effectively retired by the issuer on receipt.
        // Decimal values are acceptable.
        string retired_units = 3;
    }
}

// MsgCreateBatchResponse is the Msg/CreateBatch response type.
message MsgCreateBatchResponse {

    // batch_denom is the unique denomination ID of the newly created batch.
    string batch_denom = 1;
}

// MsgSendRequest is the Msg/Send request type.
message MsgSendRequest {

    // sender is the address of the account sending credits.
    string sender = 1;

    // sender is the address of the account receiving credits.
    string recipient = 2;

    // credits are the credits being sent.
    repeated SendUnits credits = 3;

    // SendUnits are the tradable and retired units of a credit batch to send.
    message SendUnits {

        // batch_denom is the unique ID of the credit batch.
        string batch_denom = 1;

        // tradable_units are the units of credits in this issuance that can be traded by this recipient.
        // Decimal values are acceptable within the precision returned by Query/Precision.
        string tradable_units = 2;

        // retired_units are the units of credits in this issuance that are effectively retired by the issuer on receipt.
        // Decimal values are acceptable within the precision returned by Query/Precision.
        string retired_units = 3;
    }
}

// MsgSendResponse is the Msg/Send response type.
message MsgSendResponse { }

// MsgRetireRequest is the Msg/Retire request type.
message MsgRetireRequest {

    // holder is the credit holder address.
    string holder = 1;

    // credits are the credits being retired.
    repeated RetireUnits credits = 2;

    // RetireUnits are the units of the batch being retired.
    message RetireUnits {

        // batch_denom is the unique ID of the credit batch.
        string batch_denom = 1;

        // retired_units are the units of credits being retired.
        // Decimal values are acceptable within the precision returned by Query/Precision.
        string units = 2;
    }
}

// MsgRetireRequest is the Msg/Retire response type.
message MsgRetireResponse { }

// MsgRetireRequest is the Msg/SetPrecision request type.
message MsgSetPrecisionRequest {

    // issuer is the address of the batch issuer.
    string issuer = 1;

    // batch_denom is the unique ID of the credit batch.
    string batch_denom = 2;

    // max_decimal_places is the new maximum number of decimal places that can be used to represent some quantity of
    // credit units. It is an experimental feature to concretely explore an idea proposed in https://github.com/cosmos/cosmos-sdk/issues/7113.
    uint32 max_decimal_places = 3;
}

// MsgRetireRequest is the Msg/SetPrecision response type.
message MsgSetPrecisionResponse { }
