Overview

StateSet Commerce Network provides a Cosmos SDK based blockchain network for managing orders and payments for the StateSet Commerce platform. This guide will walk you through the steps to get started with creating Orders on the StateSet Commerce Network.

The Order Module

StateSet Commerce Network provides an API for creating Orders. The API is available at https://api.stateset.network

An order is a customer’s completed request to purchase one or more products from a seller. An order is created when a customer completes the checkout process.

The Order is braodcasted to the StateSet Commerce Network and the Order Module maintains the state of the Order.

The States of an Order are:

  • UNFULFILLED
  • FULFILLED
  • CANCELLED

Order Messages

The x/order module implements an order state machine using the following messages:

  • MsgCreateOrder
  • MsgFulfillOrder
  • MsgCancelOrder
// Msg defines the order Msg service.
service Msg {
  // MsgCreateOrder defines a method for creating an order
  rpc Create(MsgCreateOrder) returns (MsgCreateResponse);

  // MsgFulfillOrder defines a method for fulfilling an order
  rpc Fulfill(MsgFulfillOrder) returns (MsgFulfillResponse);

  // MsgCancelOrder defines a method for cancelling an order
  rpc Cancel(MsgCancelOrder) returns (MsgCancelResponse);
}
Use these messages to make state changes on the network

Create an Order Client


    import { Type, Field } from "protobufjs";

    const MsgCreateOrder = new Type("MsgCreateOrder")
    .add(new Field("creator", 1, "string"))
    .add(new Field("seller", 2, "string"))
    .add(new Field("amount", 3, "string"))
    .add(new Field("fee", 4, "string"))
    .add(new Field("order_number", 5, "string"))
    .add(new Field("name", 6, "string"))
    .add(new Field("state", 7, "string"));


    const handleSubmitOrder = async () => {
        setStatus(prevStatus => ({ ...prevStatus, submitting: true }))

        const myRegistry = new Registry(defaultStargateTypes);
        myRegistry.register("/stateset.core.order.MsgCreateOrder", MsgCreateOrder);

        const wallet = await DirectSecp256k1HdWallet.fromMnemonic(
            inputs.mnemonic,
            { prefix: "stateset" },
        );

        const firstAccount = await wallet.getAccounts();

        var creator_address;

        if (firstAccount) {

            creator_address = firstAccount[0].address;

        }

        const rpcEndpoint = "https://rpc.stateset.zone";

        const client = await SigningStateSetClient.connectWithSigner(rpcEndpoint, wallet, { registry: myRegistry, gasPrice: "0.00025state" });

        if (client) {

            var _uuid = uuid();

            // collateral
            const collateral = {
                amount: [
                    {
                        denom: "ustate",
                        amount: inputs.collateral,
                    },
                ],
            };

            const message = {
                typeUrl: "/stateset.core.order.MsgCreateOrder",
                value: {
                    buyer: creator_address,
                    seller: inputs.seller,
                    amount: inputs.amount,
                    fee: inputs.fee,
                    order_number: _uuid,
                    name: inputs.name,
                    state: "UNFULFILLED"

                },
            };


            // Fee
            const fee = {
                amount: [
                    {
                        denom: "state",
                        amount: "0",
                    },
                ],
                gas: "10000",
            };

            const result = await client.signAndBroadcast(creator_address, [message], "auto", 'creating an order from the stateset zone');

            if (result) {

                setStatus(prevStatus => ({
                    ...prevStatus,
                    submitted: true,
                }))

                setOrderID(_uuid);
            }

        }
    }

Query an Order

Create a new page called index.js in the pages/order/[id] directory.


const Order = () => {

    const router = useRouter();
    const id = router.query;
    var id_ = id.id;

    const [orders, setOrders] = useState([]);

    useEffect(() => {
        async function getOrders() {
            const res = await fetch(`https://rest-api.stateset.zone/stateset/core/order/order/${id_}`, {
                method: 'GET'
            });
            const order_data = await res.json();
            setOrdets(order_data.Order);
        };

        getOrders();
    }, []);

    return (<OrderPage order={orders} />);

}

export default Order

Fulfill an Order


    import { Type, Field } from "protobufjs";

    const MsgFulfillOrder = new Type("MsgFulfillOrder")
    .add(new Field("creator", 1, "string"))
    .add(new Field("state", 2, "string"));


    const handleFulfillOrder = async () => {

        setStatus(prevStatus => ({ ...prevStatus, submitting: true }))

        const myRegistry = new Registry(defaultStargateTypes);
        myRegistry.register("/stateset.core.order.MsgFulfillOrder", MsgFulfillOrder);

        const wallet = await DirectSecp256k1HdWallet.fromMnemonic(
            inputs.mnemonic,
            { prefix: "stateset" },
        );

        const firstAccount = await wallet.getAccounts();

        var creator_address;

        if (firstAccount) {

            creator_address = firstAccount[0].address;

        }

        const rpcEndpoint = "https://rpc.stateset.zone";

        const client = await SigningStateSetClient.connectWithSigner(rpcEndpoint, wallet, { registry: myRegistry, gasPrice: "0.00025state" });

        if (client) {

            // collateral
            const collateral = {
                amount: [
                    {
                        denom: "ustate",
                        amount: inputs.collateral,
                    },
                ],
            };

            const message = {
                typeUrl: "/stateset.core.order.MsgFulfillOrder",
                value: {
                    creator: creator_address,
                    state: "FULFILLED"

                },
            }

        }
    }

Submitting an Order using the CLI


statesetd tx order create-order \
    --from <key-name> \
    --seller <seller-address> \
    --amount <amount> \
    --fee <fee> \
    --order-number <order-number> \
    --name <name> \
    --state <state> \
    --chain-id stateset-1 \
    --node tcp://rpc.stateset.zone:26657 \
    --gas-prices 0.00025state \
    --gas auto \
    --broadcast-mode block \
    --yes

Querying an Order using the CLI


statesetd query order order <order-id> \
    --chain-id stateset-1 \
    --node tcp://rpc.stateset.zone:26657

Fulfilling an Order using the CLI


statesetd tx order fulfill-order \
    --from <key-name> \
    --state <state> \
    --chain-id stateset-1 \
    --node tcp://rpc.stateset.zone:26657 \
    --gas-prices 0.00025state \
    --gas auto \
    --broadcast-mode block \
    --yes

Additional Information (Coming Soon)