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
56
57
# 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

  total_supply = ::BigDecimal.new(total_supply.to_s)

  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 = ::Istox::FMath.round_down(::Istox::FMath.div(soft_cap, total_supply), 2)
    is_cutoff = false

    interests = interests.map do |item|
      bid_token = ::Istox::FMath.div(item[:fiat_amount], item[:token_price])
      total_bid_token = ::Istox::FMath.add(total_bid_token, bid_token)
      allocated = 0.0
      unallocated = bid_token
      unless is_cutoff
        allocated = bid_token
        unallocated = 0.0
        if ::BigDecimal.new(::Istox::FMath.add(total_allocated, bid_token)) >= total_supply
          unallocated = ::Istox::FMath.sub(::Istox::FMath.add(total_allocated, bid_token), total_supply)
          allocated = ::Istox::FMath.sub(bid_token, unallocated)
          cutoff_price = item[:token_price]
          is_cutoff = true
        end
        total_allocated = ::Istox::FMath.add(total_allocated, allocated)
        total_unallocated = ::Istox::FMath.add(total_unallocated, unallocated)
      end
      { id: item[:id], fiat_amount: item[:fiat_amount], bid_price: item[:token_price], bid_token: bid_token, 
        allocated: allocated, unallocated: unallocated }
    end

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