Class: Mastercoin::SellingOffer

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

Defined Under Namespace

Classes: CannotDecodeSellingOfferException

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Message

decode, decode_from_compressed_public_key, #encode_to_compressed_public_key, #mangle_key_until_valid, probe, probe_and_read

Constructor Details

#initialize(options = {}) ⇒ SellingOffer

Returns a new instance of SellingOffer.



8
9
10
11
12
13
14
15
16
# File 'lib/mastercoin-ruby/selling_offer.rb', line 8

def initialize(options = {})
  options.reverse_merge!({time_limit: 10, transaction_fee: 20000})
  self.transaction_type = Mastercoin::TRANSACTION_SELL_FOR_BITCOIN
  self.currency_id = options[:currency_id]
  self.amount = options[:amount]
  self.bitcoin_amount = options[:bitcoin_amount]
  self.time_limit = options[:time_limit]
  self.transaction_fee = options[:transaction_fee]
end

Instance Attribute Details

#amountObject

14015bd586c0c7a28979ca294b114441f23bfc97be17cd6077b9e12e2709fec3



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

def amount
  @amount
end

#bitcoin_amountObject

14015bd586c0c7a28979ca294b114441f23bfc97be17cd6077b9e12e2709fec3



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

def bitcoin_amount
  @bitcoin_amount
end

#currency_idObject

14015bd586c0c7a28979ca294b114441f23bfc97be17cd6077b9e12e2709fec3



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

def currency_id
  @currency_id
end

#time_limitObject

14015bd586c0c7a28979ca294b114441f23bfc97be17cd6077b9e12e2709fec3



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

def time_limit
  @time_limit
end

#transaction_feeObject

14015bd586c0c7a28979ca294b114441f23bfc97be17cd6077b9e12e2709fec3



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

def transaction_fee
  @transaction_fee
end

#transaction_typeObject

14015bd586c0c7a28979ca294b114441f23bfc97be17cd6077b9e12e2709fec3



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

def transaction_type
  @transaction_type
end

Class Method Details

.decode_key_to_data(keys) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mastercoin-ruby/selling_offer.rb', line 18

def self.decode_key_to_data(keys)
  raise CannotDecodeSellingOfferException.new("Need an array of two public keys in order to decode Selling Offer") unless keys.is_a?(Array) || keys.count != 2

  key = Mastercoin::Util.sort_and_strip_keys(keys).join
  
  offer = SellingOffer.new
  offer.transaction_type = key[0..7].to_i(16)
  offer.currency_id = key[8..15].to_i(16)
  offer.amount = key[16..31].to_i(16)
  offer.bitcoin_amount = key[32..47].to_i(16)
  offer.time_limit = key[48..49].to_i(16)
  offer.transaction_fee = key[50..65].to_i(16)
  offer
end

Instance Method Details

#encode_data_to_keyObject



33
34
35
36
37
38
39
40
# File 'lib/mastercoin-ruby/selling_offer.rb', line 33

def encode_data_to_key
  raw = self.transaction_type.to_i.to_s(16).rjust(8,"0") + self.currency_id.to_i.to_s(16).rjust(8, "0") + self.amount.to_i.to_s(16).rjust(16, "0") + self.bitcoin_amount.to_i.to_s(16).rjust(16, "0") + self.time_limit.to_i.to_s(16).rjust(2,"0") + self.transaction_fee.to_i.to_s(16).rjust(16,"0")
  raw = raw.ljust(120,"0")
  keys = raw.chars.each_slice(60).map(&:join)
  keys.each_with_index.collect do |key, index|
    "#{(index + 1).to_s(16).rjust(2,"0")}#{key}"
  end
end

#explain(sending_address) ⇒ Object



42
43
44
# File 'lib/mastercoin-ruby/selling_offer.rb', line 42

def explain(sending_address)
  "Selling Offer from #{sending_address} of #{(self.amount / 1e8).to_f} #{Mastercoin::CURRENCY_IDS[self.currency_id.to_s]} for #{(self.bitcoin_amount / 1e8).to_f} Bitcoins. Time limit #{self.time_limit}. BTC Fee #{self.transaction_fee}"
end