CreateOrder creates a new order with given group id and specifications. It returns created order
(ctx sdk.Context, gid dtypes.GroupID, spec dvbeta.GroupSpec, reclamation *dtypes.DeploymentReclamation)
| 157 | |
| 158 | // CreateOrder creates a new order with given group id and specifications. It returns created order |
| 159 | func (k Keeper) CreateOrder(ctx sdk.Context, gid dtypes.GroupID, spec dvbeta.GroupSpec, reclamation *dtypes.DeploymentReclamation) (types.Order, error) { |
| 160 | oseq := uint32(1) |
| 161 | var err error |
| 162 | |
| 163 | k.WithOrdersForGroup(ctx, gid, types.OrderActive, func(_ types.Order) bool { |
| 164 | err = mv1.ErrOrderActive |
| 165 | return true |
| 166 | }) |
| 167 | |
| 168 | k.WithOrdersForGroup(ctx, gid, types.OrderOpen, func(_ types.Order) bool { |
| 169 | err = mv1.ErrOrderActive |
| 170 | return true |
| 171 | }) |
| 172 | |
| 173 | k.WithOrdersForGroup(ctx, gid, types.OrderClosed, func(_ types.Order) bool { |
| 174 | oseq++ |
| 175 | return false |
| 176 | }) |
| 177 | |
| 178 | if err != nil { |
| 179 | return types.Order{}, fmt.Errorf("%w: create order: active order exists", err) |
| 180 | } |
| 181 | |
| 182 | orderID := mv1.MakeOrderID(gid, oseq) |
| 183 | |
| 184 | pk := keys.OrderIDToKey(orderID) |
| 185 | has, err := k.orders.Has(ctx, pk) |
| 186 | if err != nil { |
| 187 | return types.Order{}, err |
| 188 | } |
| 189 | if has { |
| 190 | return types.Order{}, mv1.ErrOrderExists |
| 191 | } |
| 192 | |
| 193 | order := types.Order{ |
| 194 | ID: orderID, |
| 195 | Spec: spec, |
| 196 | State: types.OrderOpen, |
| 197 | CreatedAt: ctx.BlockHeight(), |
| 198 | Reclamation: reclamation, |
| 199 | } |
| 200 | |
| 201 | if err := k.orders.Set(ctx, pk, order); err != nil { |
| 202 | return types.Order{}, fmt.Errorf("failed to create order: %w", err) |
| 203 | } |
| 204 | |
| 205 | ctx.Logger().Info("created order", "order", order.ID) |
| 206 | |
| 207 | err = ctx.EventManager().EmitTypedEvent( |
| 208 | &mv1.EventOrderCreated{ID: order.ID}, |
| 209 | ) |
| 210 | if err != nil { |
| 211 | return types.Order{}, err |
| 212 | } |
| 213 | |
| 214 | return order, nil |
| 215 | } |
| 216 |
nothing calls this directly
no test coverage detected