Class: Mastercoin::ExodusPayment

Inherits:
Object
  • Object
show all
Defined in:
lib/mastercoin-ruby/exodus_payment.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#addressObject

Returns the value of attribute address.



4
5
6
# File 'lib/mastercoin-ruby/exodus_payment.rb', line 4

def address
  @address
end

#bonus_boughtObject

Returns the value of attribute bonus_bought.



4
5
6
# File 'lib/mastercoin-ruby/exodus_payment.rb', line 4

def bonus_bought
  @bonus_bought
end

#coins_boughtObject

Returns the value of attribute coins_bought.



4
5
6
# File 'lib/mastercoin-ruby/exodus_payment.rb', line 4

def coins_bought
  @coins_bought
end

#time_includedObject

Returns the value of attribute time_included.



4
5
6
# File 'lib/mastercoin-ruby/exodus_payment.rb', line 4

def time_included
  @time_included
end

#txObject

Returns the value of attribute tx.



4
5
6
# File 'lib/mastercoin-ruby/exodus_payment.rb', line 4

def tx
  @tx
end

Class Method Details

.from_address(address) ⇒ Object

This is a very slow and probably very inefficient way to calculate the coins bought TODO: Please rewrite



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
# File 'lib/mastercoin-ruby/exodus_payment.rb', line 64

def self.from_address(address)
  buying = ExodusPayment.new
  @used = {}
  @rejected_tx = []

  buying.address = address

  buying.coins_bought = 0
  buying.bonus_bought = 0

  store = Mastercoin.storage
  txouts = store.get_txouts_for_address(address)

  # 1. Get all outputs for an address
  # 2. Check to see if this ouput has a next input for the Exodus address
  #    A. Get the tx for the next input if any exist
  #    B. Check if the tx has any outputs with the Exodus address
  # 3. If so find which input did the total best payments to Exodus
  # 4. Check the inputs for Exodus output and award the one with the highest total

  txouts.each do |txout|
    Mastercoin.log.debug("Checking txout: #{txout.to_hash(with_address: true)}")
    input = txout.get_next_in

    if input
      tx = input.get_tx
      next if @rejected_tx.include?(tx.hash)

      block_time = store.get_block_by_tx(tx.hash).time

      if tx.get_block.depth > Mastercoin::END_BLOCK
        Mastercoin.log.debug("Transaction after end date: Rejecting")
        @rejected_tx << tx.hash
        next
      end

      addresses = tx.outputs.collect{|x| x.to_hash(with_address: true)["address"] }

      unless addresses.include?(Mastercoin::EXODUS_ADDRESS)
        Mastercoin.log.debug("TX #{tx.hash} does not include transaction to Exodus")
        @rejected_tx << tx.hash
        next
      else
        Mastercoin.log.debug("TX #{tx.hash} is a transaction to Exodus")
      end

      highest_input = ExodusPayment.highest_output_for_tx(tx)

      Mastercoin.log.debug("Highest input for #{tx.hash} is #{highest_input}")

      # Get all the inputs from this transaction and see which has the higest one. the Funds belong to the input with the highest value
      tx.out.each do |output|
        if output.get_addresses.flatten.include?(Mastercoin::EXODUS_ADDRESS) && !@used.keys.include?(tx.hash)
          Mastercoin.log.debug("TX #{tx.hash} is not inside our used tx hash: #{@used.keys}")

          unless txout.get_address == highest_input
            Mastercoin.log.debug("This is not the highest input; can't give the coins. #{txout.get_address} we needed #{highest_input}")
            next
          else
          end

          @used[tx.hash] = highest_input

          btc_amount = (output.value / 1e8)
          bought = btc_amount * 100
          buying.coins_bought += bought
          date_difference = (Mastercoin::END_TIME.to_i - block_time.to_i) / 60.0 / 60 / 24 / 7
          if date_difference > 0
            bonus = (btc_amount * 100 * (date_difference * 0.1))

            buying.bonus_bought += sprintf("%0.08f", bonus).to_f
          end
        else
          Mastercoin.log.debug("This is not the Exodus output; probably change address")
        end
      end
    end
  end
  return buying
end

.from_transaction(hash) ⇒ Object



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
# File 'lib/mastercoin-ruby/exodus_payment.rb', line 18

def self.from_transaction(hash) 
  buying = ExodusPayment.new
  buying.coins_bought = 0
  buying.bonus_bought = 0

  store = Mastercoin.storage
  tx = store.get_tx(hash)
  raise TransactionNotFoundException.new("Could not find the given transaction with #{hash}. Perhaps your blockchain is not up-to-date?") unless tx
  buying.tx = tx
  block_time = store.get_block_by_tx(tx.hash).time
  buying.time_included = block_time
  highest = ExodusPayment.highest_output_for_tx(tx)
  buying.address = highest

  exodus_output = tx.outputs.find{|x| x.to_hash(with_address:true)["address"] == Mastercoin::EXODUS_ADDRESS}

  if tx.get_block.depth <= Mastercoin::END_BLOCK
    btc_amount = (exodus_output.value / 1e8)
    bought =  btc_amount * 100
    buying.coins_bought += bought
    date_difference = (Mastercoin::END_TIME.to_i - block_time.to_i) / 60.0 / 60 / 24 / 7
    if date_difference > 0
      bonus = (btc_amount * 100 * (date_difference * 0.1))

      buying.bonus_bought += sprintf("%0.08f", bonus).to_f
    end
  end
  return buying
end

.highest_output_for_tx(tx) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/mastercoin-ruby/exodus_payment.rb', line 48

def self.highest_output_for_tx(tx)
  result = {}
  output_hash = tx.in.collect{|x| x.get_prev_out.to_hash(with_address: true) }

  output_hash.each do |output|
    address = output['address']
    result[address] ||= 0
    result[address] += output['value'].to_f
  end

  highest_input = result.sort{|x,y| y[1] <=> x[1]}
  highest_input = highest_input[0][0]
end

Instance Method Details

#to_jsonObject



10
11
12
# File 'lib/mastercoin-ruby/exodus_payment.rb', line 10

def to_json
  {coins_bought: self.coins_bought, bonus_bought: self.bonus_bought}.to_json
end

#to_sObject



6
7
8
# File 'lib/mastercoin-ruby/exodus_payment.rb', line 6

def to_s
  "Bought #{self.coins_bought} Mastercoins and got a #{self.bonus_bought} Mastercoins extra."
end

#total_amountObject



14
15
16
# File 'lib/mastercoin-ruby/exodus_payment.rb', line 14

def total_amount
  self.coins_bought + self.bonus_bought
end