Class: Launchpad::API::V2::Private::IEO::OrdersController

Inherits:
BaseController show all
Defined in:
app/controllers/launchpad/api/v2/private/ieo/orders_controller.rb

Instance Method Summary collapse

Methods included from JWTPayload

#email, #jwt_payload, #role, #uid

Methods included from ExceptionHandlers

included

Methods included from Response

#controller_namespace, #error_response, #errors_response, #json_response, #not_found

Instance Method Details

#createObject

POST /ieo/orders TODO: Improve handling of Management API exceptions.



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
# File 'app/controllers/launchpad/api/v2/private/ieo/orders_controller.rb', line 11

def create
  sale = Launchpad::IEO::Sale.public_states.find(order_params[:sale])
  unless sale.ongoing?
    error_response("order.sale_not_active", 422)
    return
  end

  sale_pair = Launchpad::IEO::SalePair.find_by!(sale_id: sale.id, quote_currency_id: order_params[:quote_unit])

  begin
    response = Peatio::ManagementAPIV2::Client.new.balance(
      uid:      uid,
      currency: order_params[:quote_unit]
    )
  rescue ManagementAPIV2::Exception
    error_response("order.user_balance_checking_failed", 503)
    return
  end

  if response[:balance].to_d < contribution(sale, order_params[:contribution].to_d)
    error_response("order.insufficient_balance", 422)
    return
  end

  order = Launchpad::IEO::Order.create!(
    sale_pair_id: sale_pair.id,
    uid:          uid,
    contribution: order_params[:contribution].to_d
  )

  begin
    order.dispatch!

    json_response(order, 201)
  rescue ManagementAPIV2::Exception
    order.reject!
    error_response("order.transfers_creation_failed", 422)
  end
end

#destroyObject

DELETE /ieo/orders/:id



71
72
73
74
75
76
77
78
79
80
81
# File 'app/controllers/launchpad/api/v2/private/ieo/orders_controller.rb', line 71

def destroy
  order = Launchpad::IEO::Order.find_by!(uid: uid, id: params[:id])

  unless order.active?
    error_response("order.non_active", 422)
    return
  end

  order.cancel!
  json_response(order, 200)
end

#indexObject

GET /ieo/orders



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/controllers/launchpad/api/v2/private/ieo/orders_controller.rb', line 52

def index
  ransack_params = {
    uid_eq:                         uid,
    state_in:                       params[:state],
    sale_pair_sale_id_eq:           params[:sale],
    sale_pair_quote_currency_id_eq: params[:quote_unit],
    sale_name_eq:                   params[:search]
  }
  ransack_params.merge!(created_at_gteq: Time.at(params[:time_from].to_i))\
      unless params[:time_from].nil?
  ransack_params.merge!(created_at_lteq: Time.at(params[:time_to].to_i))\
      unless params[:time_to].nil?

  orders = Launchpad::IEO::Order.ransack(ransack_params)
  orders.sorts = "id desc"
  json_response(paginate(orders.result.includes(:sale, :sale_pair)), 200)
end