Method: OnChain::BlockChain.get_unspent_for_amount

Defined in:
lib/onchain/block_chain.rb

.get_unspent_for_amount(addresses, amount_in_satoshi) ⇒ Object



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
# File 'lib/onchain/block_chain.rb', line 78

def get_unspent_for_amount(addresses, amount_in_satoshi)
  
  unspents = []
  indexes = []
  amount_so_far = 0
  
  addresses.each_with_index do |address, index|

    if amount_so_far >= amount_in_satoshi
      break
    end
    
    unspent_outs = get_unspent_outs(address)
    
    unspent_outs.each do |spent|

      unspents << spent
      indexes << index
      
      amount_so_far = amount_so_far + spent[3].to_i
      if amount_so_far >= amount_in_satoshi
        break
      end
    end
  end
  
  change = amount_so_far - amount_in_satoshi 
  return unspents, indexes, change
  
end