Class: Radiator::Transaction

Inherits:
Object
  • Object
show all
Includes:
ChainConfig, Utils
Defined in:
lib/radiator/transaction.rb

Overview

  • graphenej:

    * https://github.com/kenCode-de/graphenej/blob/master/graphenej/src/main/java/de/bitsharesmunich/graphenej/Transaction.java#L142

Constant Summary

VALID_OPTIONS =
%w(
  wif private_key ref_block_num ref_block_prefix expiration
  chain
).map(&:to_sym)

Constants included from ChainConfig

ChainConfig::EXPIRE_IN_SECS, ChainConfig::EXPIRE_IN_SECS_PROPOSAL, ChainConfig::NETWORKS_GOLOS_ADDRESS_PREFIX, ChainConfig::NETWORKS_GOLOS_CHAIN_ID, ChainConfig::NETWORKS_GOLOS_CORE_ASSET, ChainConfig::NETWORKS_GOLOS_DEBT_ASSET, ChainConfig::NETWORKS_GOLOS_DEFAULT_NODE, ChainConfig::NETWORKS_GOLOS_VEST_ASSET, ChainConfig::NETWORKS_STEEM_ADDRESS_PREFIX, ChainConfig::NETWORKS_STEEM_CHAIN_ID, ChainConfig::NETWORKS_STEEM_CORE_ASSET, ChainConfig::NETWORKS_STEEM_DEBT_ASSET, ChainConfig::NETWORKS_STEEM_DEFAULT_NODE, ChainConfig::NETWORKS_STEEM_VEST_ASSET, ChainConfig::NETWORKS_TEST_ADDRESS_PREFIX, ChainConfig::NETWORKS_TEST_CHAIN_ID, ChainConfig::NETWORKS_TEST_CORE_ASSET, ChainConfig::NETWORKS_TEST_DEBT_ASSET, ChainConfig::NETWORKS_TEST_DEFAULT_NODE, ChainConfig::NETWORKS_TEST_VEST_ASSET, ChainConfig::NETWORK_CHAIN_IDS

Instance Method Summary collapse

Methods included from Utils

#debug, #error, #extract_signatures, #hexlify, #pakArr, #pakC, #pakHash, #pakI, #pakL!, #pakS, #pakStr, #pakc, #paks, #send_log, #unhexlify, #varint, #warning

Constructor Details

#initialize(options = {}) ⇒ Transaction



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
# File 'lib/radiator/transaction.rb', line 18

def initialize(options = {})
  options = options.dup
  options.each do |k, v|
    k = k.to_sym
    if VALID_OPTIONS.include?(k.to_sym)
      options.delete(k)
      send("#{k}=", v)
    end
  end
  
  @logger = options[:logger] || Radiator.logger
  @chain ||= :steem
  @chain = @chain.to_sym
  @chain_id = chain_id options[:chain_id]
  @url = options[:url] || url
  @operations = options[:operations] || []
  
  unless NETWORK_CHAIN_IDS.include? @chain_id
    warning "Unknown chain id: #{@chain_id}"
  end
  
  if !!wif && !!private_key
    raise TransactionError, "Do not pass both wif and private_key.  That's confusing."
  end
  
  if !!wif
    @private_key = Bitcoin::Key.from_base58 wif
  end
  
  @expiration ||= nil
  @immutable_expiration = !!@expiration
  
  options = options.merge(url: @url, chain: @chain)
  @api = Api.new(options)
  @network_broadcast_api = NetworkBroadcastApi.new(options)
end

Instance Method Details

#chain_id(chain_id = nil) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/radiator/transaction.rb', line 55

def chain_id(chain_id = nil)
  return chain_id if !!chain_id
  
  case chain.to_s.downcase.to_sym
  when :steem then NETWORKS_STEEM_CHAIN_ID
  when :golos then NETWORKS_GOLOS_CHAIN_ID
  when :test then NETWORKS_TEST_CHAIN_ID
  end
end

#operationsObject



97
98
99
100
101
102
103
104
# File 'lib/radiator/transaction.rb', line 97

def operations
  @operations = @operations.map do |op|
    case op
    when Operation then op
    else; Operation.new(op)
    end
  end
end

#operations=(operations) ⇒ Object



106
107
108
# File 'lib/radiator/transaction.rb', line 106

def operations=(operations)
  @operations = operations
end

#process(broadcast = false) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/radiator/transaction.rb', line 73

def process(broadcast = false)
  prepare
  
  if broadcast
    loop do
      response = @network_broadcast_api.broadcast_transaction_synchronous(payload)
      
      if !!response.error
        parser = ErrorParser.new(response)
        
        if parser.can_reprepare?
          debug "Error code: #{parser}, repreparing transaction ..."
          prepare
          redo 
        end
      end
      
      return response
    end
  else
    self
  end
end

#urlObject



65
66
67
68
69
70
71
# File 'lib/radiator/transaction.rb', line 65

def url
  case chain.to_s.downcase.to_sym
  when :steem then NETWORKS_STEEM_DEFAULT_NODE
  when :golos then NETWORKS_GOLOS_DEFAULT_NODE
  when :test then NETWORKS_TEST_DEFAULT_NODE
  end
end