Class: Launchpad::IEO::Order
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Launchpad::IEO::Order
- 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
- .release_fund(percent = 0) ⇒ Object
- .tokens_ordered(ndigits: Launchpad::IEO::TOKENS_AMOUNT_PRECISION) ⇒ Object
Instance Method Summary collapse
- #add? ⇒ Boolean
- #as_json(_options = {}) ⇒ Object
- #base_currency ⇒ Object
- #calculate_contribution ⇒ Object
- #calculate_execute_refunded ⇒ Object
- #calculate_refund ⇒ Object
- #calculate_refunded ⇒ Object
- #calculate_release ⇒ Object
- #calculate_share ⇒ Object
- #cancel_transfer_params ⇒ Object
- #enqueue_execute_job ⇒ Object
- #enqueue_refund_job ⇒ Object
- #enqueue_released_job ⇒ Object
- #execute_transfer_params ⇒ Object
- #executed_without_commission ⇒ Object
- #generate_key ⇒ Object
- #lock_transfer_params ⇒ Object
- #perform_transfer(transfer_params) ⇒ Object
- #quote_currency ⇒ Object
- #ratio ⇒ Object
- #release_funds_params(funds) ⇒ Object
- #release_transfer_params ⇒ Object
- #sale_name ⇒ Object
- #tokens_ordered(ndigits: Launchpad::IEO::TOKENS_AMOUNT_PRECISION) ⇒ Object
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
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(={}) if [: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_currency ⇒ Object
302 303 304 |
# File 'app/models/launchpad/ieo/order.rb', line 302 def base_currency sale.currency_id end |
#calculate_contribution ⇒ Object
340 341 342 |
# File 'app/models/launchpad/ieo/order.rb', line 340 def calculate_contribution add? ? contribution + contribution * commission_rate : contribution end |
#calculate_execute_refunded ⇒ Object
431 432 433 |
# File 'app/models/launchpad/ieo/order.rb', line 431 def calculate_execute_refunded add? ? refunded + refunded * commission_rate : refunded end |
#calculate_refund ⇒ Object
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_refunded ⇒ Object
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_release ⇒ Object
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_share ⇒ Object
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_params ⇒ Object
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_job ⇒ Object
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_job ⇒ Object
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_job ⇒ Object
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_params ⇒ Object
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_commission ⇒ Object
294 295 296 |
# File 'app/models/launchpad/ieo/order.rb', line 294 def executed_without_commission add? ? executed : executed - commission_amount end |
#generate_key ⇒ Object
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_params ⇒ Object
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_currency ⇒ Object
306 307 308 |
# File 'app/models/launchpad/ieo/order.rb', line 306 def quote_currency sale_pair.quote_currency_id end |
#ratio ⇒ Object
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_params ⇒ Object
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_name ⇒ Object
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 |