Class: Istox::OrderBook

Inherits:
Object
  • Object
show all
Defined in:
lib/istox/helpers/order_book.rb

Class Method Summary collapse

Class Method Details

.allocation(soft_cap, total_supply, investments) ⇒ Object



4
5
6
7
8
9
10
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
50
51
52
53
54
55
# File 'lib/istox/helpers/order_book.rb', line 4

def allocation(soft_cap, total_supply, investments)
  
  # sort by token price desc, and id asc
  investments = investments.sort do |a, b|
    [b[:token_price], a[:id]] <=> [a[:token_price], b[:id]]
  end

  interests = investments
  if interests.count > 0
    cutoff_price = 0.0
    total_bid_token = 0.0
    total_allocated = 0.0
    total_unallocated = 0.0
    total_investment = 0.0
    cutoff_price = (soft_cap / total_supply).round(4)
    is_cutoff = false

    interests = interests.map do |item|
      bid_token = (item[:fiat_amount] / item[:token_price]).round(4)
      total_bid_token += bid_token
      allocated = 0.0
      unallocated = bid_token
      unless is_cutoff
        allocated = bid_token
        unallocated = 0.0
        if total_allocated + bid_token >= total_supply
          unallocated = total_allocated + bid_token - total_supply
          allocated = bid_token - unallocated
          cutoff_price = item[:token_price]
          is_cutoff = true
        end
        total_allocated += allocated
        total_unallocated += unallocated
      end
      { id: item[:id], fiat_amount: item[:fiat_amount], bid_price: item[:token_price], bid_token: bid_token.round(4), 
        allocated: allocated.round(4), unallocated: unallocated.round(4) }
    end

    interests = interests.map do |item|
      investment = item[:allocated] * cutoff_price
      total_investment += investment
      item.merge!(investment: investment)
    end
    
    return { total_supply: total_supply, total_investment: total_investment.round(4), 
        cutoff_price: cutoff_price.round(4), total_bid_token: total_bid_token.round(4), 
        total_allocated: total_allocated.round(4), total_unallocated: total_unallocated.round(4), 
        interests: interests }
  else
    raise 'Registration interest is empty'
  end
end