Class: Istox::OrderBookProrate

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

Class Method Summary collapse

Class Method Details

.allocation(token_price:, min_investment:, bid_block:, invest_step:, hard_cap:, investments:, whitelists: [], include_refund_amount: false, rollover_investments: []) ⇒ Object

rubocop:disable Metrics/AbcSize,Metrics/PerceivedComplexity



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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/istox/helpers/order_book_prorate.rb', line 4

def allocation(token_price:, min_investment:, bid_block:, # rubocop:disable Metrics/AbcSize,Metrics/PerceivedComplexity
               invest_step:, hard_cap:, investments:, whitelists: [], include_refund_amount: false,
               rollover_investments: [])

  # make sure is hashes
  original_investments = investments.as_json.map(&:symbolize_keys)
  whitelists = whitelists.as_json.map(&:symbolize_keys)
  investments = investments.as_json.map(&:symbolize_keys)
  rollover_investments = rollover_investments.as_json.map(&:symbolize_keys)

  ## rollover investment is guaranteed, so it should decrease the hard cap
  rollover_investments.map do |rollover_investment|
    hard_cap = ::Istox::FMath.sub(hard_cap, rollover_investment[:fiat_amount])
  end

  # process whitelist
  whitelists.each do |whitelist|
    whitelisted_investments = investments.select { |investment1| investment1[:account_id] == whitelist[:account_id] }

    next unless whitelisted_investments.length.positive?

    current_guarenteed_amount = ::BigDecimal.new(whitelist[:guaranteed_allocation].to_s)

    whitelisted_investments.each do |whitelisted_investment|
      granted_amount = [::BigDecimal.new(whitelisted_investment[:fiat_amount].to_s), current_guarenteed_amount].min

      next unless granted_amount.positive?

      current_guarenteed_amount = ::Istox::FMath.sub(current_guarenteed_amount, granted_amount).to_d

      hard_cap = ::Istox::FMath.sub(hard_cap, granted_amount)
      whitelisted_investment[:fiat_amount] = Istox::FMath.sub(whitelisted_investment[:fiat_amount], granted_amount)
      whitelisted_investment[:is_vip] = true
      whitelisted_investment[:granted_amount] = granted_amount.to_s
    end
  end

  # sort by id asc
  interests = investments.sort do |a, b|
    a[:id] <=> b[:id]
  end

  ## check whether each investment already had earlier investment from the same investor
  interests = interests.map do |interest|
    earlier_interest = interests.find do |i|
      i[:account_id] == interest[:account_id] &&
        i[:id] != interest[:id] && i[:id].to_i < interest[:id].to_i
    end

    interest[:has_earlier_interest] = earlier_interest.present?

    interest
  end

  total_interests = 0

  max_allowed_investor = Istox::FMath.round_down(::Istox::FMath.div(hard_cap, min_investment), 0).to_i

  allowed_investor_ids = []
  interests.each_with_index.map do |interest|
    if !allowed_investor_ids.include?(interest[:account_id]) &&
       interest[:fiat_amount].to_d.positive? && allowed_investor_ids.length < max_allowed_investor
      allowed_investor_ids.push(interest[:account_id])
      interest[:to_be_set_zero] = false
      total_interests = ::Istox::FMath.add(total_interests, interest[:fiat_amount])
    elsif allowed_investor_ids.include?(interest[:account_id]) && interest[:fiat_amount].to_d.positive?
      interest[:to_be_set_zero] = false
      total_interests = ::Istox::FMath.add(total_interests, interest[:fiat_amount])
    else
      interest[:to_be_set_zero] = true
    end
  end

  # only need to handle when oversubscribe
  if total_interests.to_d > hard_cap.to_s.to_d

    # prorate the interests
    interests = interests.each_with_index.map do |interest|
      investment = if interest[:fiat_amount].to_d.positive? && !interest[:to_be_set_zero]
                     result = ::Istox::FMath.round_down(::Istox::FMath.mul(::Istox::FMath.div(interest[:fiat_amount], total_interests),
                                                                           hard_cap), 0)

                     result = result.to_d - result.to_d.modulo(invest_step.to_s.to_d)

                     # only set to min investment if is not vip
                     if result < min_investment.to_d && interest[:is_vip].blank? && !interest[:has_earlier_interest]
                       result = min_investment.to_d
                     end

                     result
                   else
                     ::BigDecimal.new('0').to_s
                   end

      {
        id: interest[:id],
        is_vip: interest[:is_vip],
        granted_amount: interest[:granted_amount],
        has_earlier_interest: interest[:has_earlier_interest],
        investment: investment.to_s
      }
    end

    new_total_interests = '0'
    interests.each do |interest|
      new_total_interests = ::Istox::FMath.add(new_total_interests, interest[:investment])
    end

    # need to do adjustment if summation of new rounded values still larger than
    if new_total_interests.to_d > hard_cap.to_s.to_d
      total_deducting = ::Istox::FMath.sub(new_total_interests, hard_cap)
      interests.reverse_each do |interest|
        next unless interest[:investment].to_d > min_investment.to_d || interest[:is_vip] || interest[:has_earlier_interest]

        # allow to deduct to zero if is vip
        deductable = if interest[:is_vip] || interest[:has_earlier_interest]
                       interest[:investment]
                     else
                       ::Istox::FMath.sub(interest[:investment], min_investment)
                     end

        if deductable.to_d > total_deducting.to_d
          interest[:investment] = ::Istox::FMath.sub(interest[:investment], total_deducting)
          total_deducting = 0
        else
          interest[:investment] = ::Istox::FMath.sub(interest[:investment], deductable)
          total_deducting = ::Istox::FMath.sub(total_deducting, deductable)
        end
      end
    end

    final_interests = interests.map do |interest|
      interest[:investment] = ::Istox::FMath.add(interest[:investment], interest[:granted_amount]) if interest[:granted_amount].present? &&
                                                                                                      interest[:granted_amount].to_d.positive?
      interest.delete(:is_vip)
      interest.delete(:granted_amount)
      interest.delete(:has_earlier_interest)

      if include_refund_amount
        original_investment = original_investments.find { |original_investment1| original_investment1[:id] == interest[:id] }
        interest[:refund_amount] = original_investment.present? ? ::Istox::FMath.sub(original_investment[:fiat_amount], interest[:investment]) : '0'
      end
      interest
    end

  else

    final_interests = interests.each_with_index.map do |interest|
      final_amount = if interest[:is_vip]
                       vip_fiat_amount = !interest[:to_be_set_zero] ? interest[:fiat_amount] : '0'
                       ::Istox::FMath.add(vip_fiat_amount, interest[:granted_amount])
                     else
                       !interest[:to_be_set_zero] ? interest[:fiat_amount] : '0'
                     end

      result = {
        id: interest[:id],
        investment: ::BigDecimal.new(final_amount.to_s).to_s

      }

      if include_refund_amount
        original_investment = original_investments.find { |original_investment1| original_investment1[:id] == interest[:id] }
        refund_amount = ::Istox::FMath.sub(original_investment[:fiat_amount], final_amount)
        result[:refund_amount] = refund_amount
      end

      result
    end
  end

  transform_rollover_investments = rollover_investments.map do |rollover_investment|
    rollover_investment[:investment] = ::BigDecimal.new(rollover_investment[:fiat_amount].to_s).to_s
    rollover_investment[:refund_amount] = ::BigDecimal.new(0).to_s if include_refund_amount

    rollover_investment.select { |k, _v| %i[id investment refund_amount].include?(k) }
  end

  {
    cutoff_price: token_price.to_s,
    interests: final_interests.concat(transform_rollover_investments)
  }
end