Class: Launchpad::IEO::Order

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

Overview

TODO: Add validation that orders are created only for ongoing sales. TODO: Add attr_readonly for all models.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.release_fund(percent = 0) ⇒ Object



257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'app/models/launchpad/ieo/order.rb', line 257

def release_fund(percent = 0)
  return if percent.to_d == 0

  all.each do |order|
    next unless order.tokens_locked.positive?

    funds = (order.tokens_locked + order.tokens_received) * percent.to_d
    order.perform_transfer(order.release_funds_params(funds))
    order.tokens_locked   -= funds
    order.tokens_received += funds
    order.save!
  end
end

.tokens_ordered(ndigits: Launchpad::IEO::TOKENS_AMOUNT_PRECISION) ⇒ Object



248
249
250
251
252
253
254
255
# File 'app/models/launchpad/ieo/order.rb', line 248

def tokens_ordered(ndigits: Launchpad::IEO::TOKENS_AMOUNT_PRECISION)
  # Include SalePair for accessing price.
  if first&.add?
    all.includes(:sale_pair).sum("contribution / ieo_sale_pairs.price").round(ndigits).to_d
  else
    all.includes(:sale_pair).sum("(1 - commission_rate) * contribution / ieo_sale_pairs.price").round(ndigits).to_d
  end
end

Instance Method Details

#add?Boolean

Returns:

  • (Boolean)


336
337
338
# File 'app/models/launchpad/ieo/order.rb', line 336

def add?
  sale.fees_policy.add?
end

#as_json(_options = {}) ⇒ Object



276
277
278
279
280
281
282
283
284
# File 'app/models/launchpad/ieo/order.rb', line 276

def as_json(_options={})
  if _options[:uid]
    super(except:  [:transfer_keys, :uid],
      methods: %i[tokens_ordered base_currency quote_currency ratio sale_name]).merge(sale_id: sale.id)
  else
    super(except:  :transfer_keys,
      methods: %i[tokens_ordered base_currency quote_currency ratio sale_name]).merge(sale_id: sale.id)
  end
end

#base_currencyObject



302
303
304
# File 'app/models/launchpad/ieo/order.rb', line 302

def base_currency
  sale.currency_id
end

#calculate_contributionObject



340
341
342
# File 'app/models/launchpad/ieo/order.rb', line 340

def calculate_contribution
  add? ? contribution + contribution * commission_rate : contribution
end

#calculate_execute_refundedObject



431
432
433
# File 'app/models/launchpad/ieo/order.rb', line 431

def calculate_execute_refunded
  add? ? refunded + refunded * commission_rate : refunded
end

#calculate_refundObject



326
327
328
329
# File 'app/models/launchpad/ieo/order.rb', line 326

def calculate_refund
  # This logic may become complex later.
  self.refunded = calculate_contribution
end

#calculate_refundedObject



331
332
333
334
# File 'app/models/launchpad/ieo/order.rb', line 331

def calculate_refunded
  self.refunded = contribution - executed
  self.refunded = calculate_execute_refunded
end

#calculate_releaseObject



344
345
346
347
# File 'app/models/launchpad/ieo/order.rb', line 344

def calculate_release
  self.tokens_received += tokens_locked 
  self.tokens_locked = 0
end

#calculate_shareObject



315
316
317
318
319
320
321
322
323
324
# File 'app/models/launchpad/ieo/order.rb', line 315

def calculate_share
  self.executed = contribution * ratio
  self.refunded = contribution - executed

  self.commission_amount = executed * commission_rate

  tokens_ordered = (executed_without_commission / sale_pair.price).round(Launchpad::IEO::TOKENS_AMOUNT_PRECISION, BigDecimal::ROUND_DOWN)
  self.tokens_locked = tokens_ordered * sale.lockup_percentage.to_d
  self.tokens_received = tokens_ordered * (1 - sale.lockup_percentage.to_d)
end

#cancel_transfer_paramsObject



460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
# File 'app/models/launchpad/ieo/order.rb', line 460

def cancel_transfer_params
  quote_currency_type = fetch_currency(sale_pair.quote_currency_id)
                        .fetch(:type)
                        .to_sym

  operations =
    [
      { # Debit user locked liabilities & credit user main liabilities with quote currency.
        currency:    sale_pair.quote_currency_id,
        amount:      refunded,
        account_src: {code: Launchpad::IEO::LIABILITY_CODES[quote_currency_type][:locked],
                      uid:  uid},
        account_dst: {code: Launchpad::IEO::LIABILITY_CODES[quote_currency_type][:main],
                      uid:  uid}
      }
    ]
  if add?
    # Debit user locked liabilities & credit main revenues with quote currency.
    operations << {
      currency:    sale_pair.quote_currency_id,
      amount:      commission_amount,
      account_src: {code: Launchpad::IEO::LIABILITY_CODES[quote_currency_type][:locked],
                    uid: uid},
      account_dst: {code: Launchpad::IEO::REVENUE_CODES[quote_currency_type][:main]}
    }
  end

  operations.delete_if { |op| op[:amount].zero? }

  {
    key:         "IEO-cancel-#{id}",
    category:    :purchases,
    description: "Cancel IEO order #{id}",
    operations:  operations
  }
end

#enqueue_execute_jobObject



530
531
532
# File 'app/models/launchpad/ieo/order.rb', line 530

def enqueue_execute_job
  Launchpad::IEO::OrderExecuteWorker.perform_async(to_sgid(for: "order_execute"))
end

#enqueue_refund_jobObject



538
539
540
# File 'app/models/launchpad/ieo/order.rb', line 538

def enqueue_refund_job
  Launchpad::IEO::OrderRefundWorker.perform_async(to_sgid(for: "order_refund"))
end

#enqueue_released_jobObject



534
535
536
# File 'app/models/launchpad/ieo/order.rb', line 534

def enqueue_released_job
  Launchpad::IEO::OrderReleaseWorker.perform_async(to_sgid(for: "order_release"))
end

#execute_transfer_paramsObject



372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
# File 'app/models/launchpad/ieo/order.rb', line 372

def execute_transfer_params
  base_currency_type = fetch_currency(sale.currency_id)
                        .fetch(:type)
                        .to_sym
  quote_currency_type = fetch_currency(sale_pair.quote_currency_id)
                        .fetch(:type)
                        .to_sym

  operations =
    [
      { # Debit seller main liabilities & credit user locked liabilities with base currency.
        currency:    sale.currency_id,
        amount:      tokens_locked,
        account_src: {code: Launchpad::IEO::LIABILITY_CODES[base_currency_type][:main],
                      uid:  sale.owner_uid},
        account_dst: {code: Launchpad::IEO::LIABILITY_CODES[base_currency_type][:locked],
                      uid:  uid}
      },
      { # Debit seller main liabilities & credit user main liabilities with base currency.
        currency:    sale.currency_id,
        amount:      tokens_received,
        account_src: {code: Launchpad::IEO::LIABILITY_CODES[base_currency_type][:main],
                      uid:  sale.owner_uid},
        account_dst: {code: Launchpad::IEO::LIABILITY_CODES[base_currency_type][:main],
                      uid:  uid}
      },
      { # Debit user locked liabilities & credit seller main liabilities with quote currency.
        currency:    sale_pair.quote_currency_id,
        amount:      executed_without_commission,
        account_src: {code: Launchpad::IEO::LIABILITY_CODES[quote_currency_type][:locked],
                      uid:  uid},
        account_dst: {code: Launchpad::IEO::LIABILITY_CODES[quote_currency_type][:main],
                      uid:  sale.owner_uid}
      },
      { # Debit user locked liabilities & credit user main liabilities with quote currency.
        currency:    sale_pair.quote_currency_id,
        amount:      calculate_execute_refunded,
        account_src: {code: Launchpad::IEO::LIABILITY_CODES[quote_currency_type][:locked],
                      uid:  uid},
        account_dst: {code: Launchpad::IEO::LIABILITY_CODES[quote_currency_type][:main],
                      uid:  uid}
      },
      { # Debit user locked liabilities & credit main revenues with quote currency.
        currency:    sale_pair.quote_currency_id,
        amount:      commission_amount,
        account_src: {code: Launchpad::IEO::LIABILITY_CODES[quote_currency_type][:locked],
                      uid:  uid},
        account_dst: {code: Launchpad::IEO::REVENUE_CODES[quote_currency_type][:main]}
      }
    ].delete_if { |op| op[:amount].zero? }

  {
    key:         "IEO-execute-#{id}",
    category:    :purchases,
    description: "Execute IEO order #{id}",
    operations:  operations
  }
end

#executed_without_commissionObject



294
295
296
# File 'app/models/launchpad/ieo/order.rb', line 294

def executed_without_commission
  add? ? executed : executed - commission_amount
end

#generate_keyObject



521
522
523
# File 'app/models/launchpad/ieo/order.rb', line 521

def generate_key
  "IEO-fund-release-#{id}-#{transfer_keys.grep(/IEO-fund-release-/).size}"
end

#lock_transfer_paramsObject



349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# File 'app/models/launchpad/ieo/order.rb', line 349

def lock_transfer_params
  type = fetch_currency(sale_pair.quote_currency_id)
          .fetch(:type)
          .to_sym

  operations =
    [
      {
        currency:    sale_pair.quote_currency_id,
        amount:      calculate_contribution,
        account_src: {code: Launchpad::IEO::LIABILITY_CODES[type][:main],   uid: uid},
        account_dst: {code: Launchpad::IEO::LIABILITY_CODES[type][:locked], uid: uid}
      }
    ].delete_if { |op| op[:amount].zero? }

  {
    key:         "IEO-lock-#{id}",
    category:    "purchases",
    description: "Lock funds for IEO order #{id}",
    operations:  operations
  }
end

#perform_transfer(transfer_params) ⇒ Object



525
526
527
528
# File 'app/models/launchpad/ieo/order.rb', line 525

def perform_transfer(transfer_params)
  Peatio::ManagementAPIV2::Client.new.create_transfer(transfer_params)
  transfer_keys << transfer_params[:key]
end

#quote_currencyObject



306
307
308
# File 'app/models/launchpad/ieo/order.rb', line 306

def quote_currency
  sale_pair.quote_currency_id
end

#ratioObject



310
311
312
313
# File 'app/models/launchpad/ieo/order.rb', line 310

def ratio
  sale_ratio = sale.ratio < 1 ? 1 : sale.ratio
  (1 / sale_ratio).to_d.round(Launchpad::IEO::RATIO_PRECISION, BigDecimal::ROUND_DOWN)
end

#release_funds_params(funds) ⇒ Object



497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
# File 'app/models/launchpad/ieo/order.rb', line 497

def release_funds_params(funds)
  base_currency_type = fetch_currency(sale.currency_id)
                         .fetch(:type)
                         .to_sym
  operations =
      [
        { # Debit user locked liabilities & credit user main liabilities with base currency.
          currency:    sale.currency_id,
          amount:      funds,
          account_src: {code: Launchpad::IEO::LIABILITY_CODES[base_currency_type][:locked],
                        uid:  uid},
          account_dst: {code: Launchpad::IEO::LIABILITY_CODES[base_currency_type][:main],
                        uid:  uid}
        }
      ].delete_if { |op| op[:amount].zero? }

  {
      key:         generate_key,
      category:    :purchases,
      description: "Release IEO order locked funds #{id}",
      operations:  operations
  }
end

#release_transfer_paramsObject



435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
# File 'app/models/launchpad/ieo/order.rb', line 435

def release_transfer_params
  base_currency_type = fetch_currency(sale.currency_id)
                        .fetch(:type)
                        .to_sym

  operations =
    [
      { # Debit user locked liabilities & credit user main liabilities with base currency.
        currency:    sale.currency_id,
        amount:      tokens_locked,
        account_src: {code: Launchpad::IEO::LIABILITY_CODES[base_currency_type][:locked],
                      uid:  uid},
        account_dst: {code: Launchpad::IEO::LIABILITY_CODES[base_currency_type][:main],
                      uid:  uid}
      }
    ].delete_if { |op| op[:amount].zero? }

  {
    key:         "IEO-release-#{id}",
    category:    :purchases,
    description: "Release IEO order #{id}",
    operations:  operations
  }
end

#sale_nameObject



298
299
300
# File 'app/models/launchpad/ieo/order.rb', line 298

def sale_name
  sale.name
end

#tokens_ordered(ndigits: Launchpad::IEO::TOKENS_AMOUNT_PRECISION) ⇒ Object



286
287
288
289
290
291
292
# File 'app/models/launchpad/ieo/order.rb', line 286

def tokens_ordered(ndigits: Launchpad::IEO::TOKENS_AMOUNT_PRECISION)
  if add?
    (contribution / price).round(ndigits)
  else
    ((1 - commission_rate) * contribution / price).round(ndigits)
  end
end