Module: Paleth

Extended by:
Helpers
Defined in:
lib/paleth.rb,
lib/paleth/db.rb,
lib/paleth/eth.rb,
lib/paleth/net.rb,
lib/paleth/liar.rb,
lib/paleth/web3.rb,
lib/paleth/block.rb,
lib/paleth/rails.rb,
lib/paleth/helpers.rb,
lib/paleth/syncing.rb,
lib/paleth/version.rb,
lib/paleth/personal.rb,
lib/paleth/transaction.rb,
lib/paleth/transaction_receipt.rb

Defined Under Namespace

Modules: Helpers, Liar, Rails Classes: Block, Db, Eth, Net, Personal, SyncState, Transaction, TransactionReceipt, Web3

Constant Summary collapse

DEFAULT_HTTP_PROVIDER =
'http://localhost:8545'
UNITS =
%i(
  kwei
  ada
  mwei babbage
  gwei shannon
  szabo
  finney
  ether
  kether grand einstein
  mether
  gether
  tether
)
VERSION =
'0.0.1'

Class Method Summary collapse

Methods included from Helpers

address?, from_ascii, from_bigdecimal, from_decimal, from_wei, sha3, to_ascii, to_bigdecimal, to_decimal, to_hex, to_wei

Class Method Details

._dummy_web3Object

:nodoc:

In the web3.js library, those helpers are part of the web3 instance, but it doesn't seem very idiomatic ruby so we're going to have a dummy singleton instance of web3 to provide those services globally


14
15
16
# File 'lib/paleth/helpers.rb', line 14

def self._dummy_web3
  @__dummy_web3 ||= `new Web3(new Web3.providers.HttpProvider('http://dummy'))`
end

._web3_from(opts) ⇒ Object


54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/paleth.rb', line 54

def self._web3_from(opts)
  if opts.key? :web3
    opts[:web3]
  elsif opts.key? :http
    `new Web3(new Web3.providers.HttpProvider(#{opts[:http]}))`
  elsif `typeof(web3)` != 'undefined'
    `new Web3(web3.currentProvider)`
  else
    msg = "No provider given and no global 'web3' object. " +
          "Using http provider with default url: #{DEFAULT_HTTP_PROVIDER}"
    $console.warn msg
    `new Web3.providers.HttpProvider(#{DEFAULT_HTTP_PROVIDER})`
  end
end

.make(opts = {}) ⇒ Object

Create a Paleth web instance. Without any arguments, it'll try to initialize using `web3.currentProvider`, or you can initialize it manually by providing the following keyword arguments:

web3

An initialized web3 instance

http

The url to an ethereum json rpc server


50
51
52
# File 'lib/paleth.rb', line 50

def self.make(opts = {})
  Paleth::Web3.new(_web3_from(opts))
end

.testObject


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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/paleth.rb', line 69

def self.test
  p = make
  puts "Web3 API version = #{p.api_version}"
  %i(node ethereum network whisper).each do |item|
    p.version(item).then do |version|
      puts "#{item} version = #{version}"
    end.fail do |error|
      puts error
    end
  end

  puts "Connected: #{p.connected?}"
  puts "SHA3: #{sha3('Opal is cool')}"

  str = "Hello Opal !"
  hex = to_hex(str)
  puts to_ascii(hex)

  p.net.listening.then do |listening|
    puts "Currently listening: #{listening}"
  end.fail do |error|
    puts error
  end

  p.net.peer_count.then do |count|
    puts "Peer Count: #{count}"
  end.fail do |error|
    puts error
  end

  db = p.db('_test_')
  db.put_string('hello', 'world').then do
    db.get_string('hello').then do |result|
      puts "db.get('hello') => #{result}"
    end.fail do |error|
      puts error
    end
  end.fail do |error|
    puts error
  end

  puts "Default account = #{p.eth.}"
  puts "Default block = #{p.eth.default_block}"

  p.eth.coinbase.then do |coinbase|
    puts "Coinbase: #{coinbase}"
  end.fail do |error|
    puts error
  end

  p.eth.mining?.then do |result|
    puts "Mining? #{result}"
  end.fail do |error|
    puts error
  end

  p.eth.hashrate.then do |result|
    puts "Hashrate: #{result}"
  end.fail do |error|
    puts error
  end

  p.eth.gas_price.then do |result|
    puts "Gas Price: #{result}"
  end.fail do |error|
    puts error
  end

  p.eth.accounts.then do |result|
    puts "Acounts: #{result}"

    p.eth.balance(result[0]).then do |balance|
      puts "Account Balance: #{balance}"
    end.fail do |error|
      puts error
    end

  end.fail do |error|
    puts error
  end

  p.eth.block_number.then do |result|
    puts "Last Block number: #{result}"
  end.fail do |error|
    puts error
  end

  p.eth.syncing_state.then do |result|
    puts "Syncing state: #{result}"
  end.fail do |error|
    puts error
  end

  p.eth.syncing do |error, sync|
    puts "Syncing: (err=#{error}, (sync=#{sync}))"
  end

  addr = '0x6d5ae9dd8f1a2582deb1b096915313459f11ba70'
  p.eth.code(addr).then do |result|
    puts "Code: #{result[0..42]}"
  end.fail do |error|
    puts error
  end

  p.eth.block(:latest, true).then do |result|
    puts result
  end.fail do |error|
    puts error
  end

  hash = '0x8883605a5f08e5e8feb24cb1a30fc1a2d56d624bb22cc930145f70bb1bbc8c88'
  p.eth.transaction(hash).then do |result|
    puts result
  end.fail do |error|
    puts error
  end

  p.eth.transaction_receipt(hash).then do |result|
    puts result
  end.fail do |error|
    puts error
  end

  addr = '0x726c79ff9BcbB3A916A01423355D2bc779d03873'
  p.eth.transaction_count(addr).then do |result|
    puts "Transaction count for #{addr} = #{result}"
  end.fail do |error|
    puts error
  end


  data = to_hex "La chair est triste, helas !"
  p.eth.accounts.then do |accounts|
    puts "List accounts: #{accounts}"
    p.personal.sign(data, accounts[0]).then do |result|
      puts "Signed data = #{result}"
    end.fail do |error|
      puts error
    end
  end.fail do |error|
    puts error
  end

end