Class: Launchpad::IEO::Sale

Inherits:
ApplicationRecord show all
Includes:
AASM
Defined in:
app/models/launchpad/ieo/sale.rb

Overview

TODO: We need to validate that currency exists on peatio side on sale creation step.

Constant Summary collapse

RESULTS =
%w[nothing listing].freeze
PUBLIC_STATES =
%w[preparing cancelled ongoing refunding distributing finished].freeze
EDITABLE_STATES =
{draft:     %w[name description owner_uid currency_id supply low_goal commission min_amount
              max_amount min_unit result starts_at finishes_at],
preparing: %w[name description starts_at finishes_at],
ongoing:   %w[finishes_at]}.freeze
TYPES =

fcfs - First Come, First Served algorithm.

%w[fcfs proportional].freeze
FEES_POLICY =

subtract - contribution * fees add - contribution + contribution * fees

%w[subtract add].freeze

Instance Method Summary collapse

Instance Method Details

#as_json(_options = {}) ⇒ Object



273
274
275
# File 'app/models/launchpad/ieo/sale.rb', line 273

def as_json(_options={})
  super(include: :pairs, methods: %i[collected_amount tokens_ordered ratio])
end

#fees_policyObject



269
270
271
# File 'app/models/launchpad/ieo/sale.rb', line 269

def fees_policy
  super&.inquiry
end

#full?Boolean

Finish fcfs sale when all tokens + min possible amount will be greater than total sale supply\ since it means that user would not be able to buy anymore.

Returns:

  • (Boolean)


257
258
259
# File 'app/models/launchpad/ieo/sale.rb', line 257

def full?
  tokens_ordered + min_amount >= supply
end

#list_currencyObject



281
282
283
284
285
286
# File 'app/models/launchpad/ieo/sale.rb', line 281

def list_currency
  Peatio::ManagementAPIV2::Client.new.update_currency(
    id:      currency_id,
    visible: true
  )
end

#notify_rangerObject



277
278
279
# File 'app/models/launchpad/ieo/sale.rb', line 277

def notify_ranger
  Peatio::MQ::Events.publish("public", "ieo", "tickers", sale: as_json)
end

#ratioObject



251
252
253
# File 'app/models/launchpad/ieo/sale.rb', line 251

def ratio
  (collected_amount / supply).round(Launchpad::IEO::RATIO_PRECISION, BigDecimal::ROUND_DOWN)
end

#resultObject



265
266
267
# File 'app/models/launchpad/ieo/sale.rb', line 265

def result
  super&.inquiry
end

#sales_to_jsonObject



288
289
290
291
292
293
294
295
# File 'app/models/launchpad/ieo/sale.rb', line 288

def sales_to_json
  attributes.merge!(
      collected_amount: collected_amount,
      tokens_ordered: tokens_ordered,
      ratio: ratio,
      pairs: pairs.as_json
      )
end

#tokens_ordered(ndigits: Launchpad::IEO::TOKENS_AMOUNT_PRECISION) ⇒ Object Also known as: collected_amount

Instance Methods =====================================================



240
241
242
243
244
245
246
# File 'app/models/launchpad/ieo/sale.rb', line 240

def tokens_ordered(ndigits: Launchpad::IEO::TOKENS_AMOUNT_PRECISION)
  if type.proportional?
    orders.executable.tokens_ordered(ndigits: ndigits)
  elsif type.fcfs?
    orders.active_and_prepared_and_completed.tokens_ordered(ndigits: ndigits)
  end
end

#typeObject



261
262
263
# File 'app/models/launchpad/ieo/sale.rb', line 261

def type
  super&.inquiry
end