Class: Paleth::Eth

Inherits:
Object
  • Object
show all
Includes:
Helpers, Liar
Defined in:
lib/paleth/eth.rb

Overview

Ethereum blockchain related methods

Instance Attribute Summary collapse

Instance 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

Methods included from Liar

#make_promise

Constructor Details

#initialize(core) ⇒ Eth

:nodoc:


18
19
20
# File 'lib/paleth/eth.rb', line 18

def initialize(core)
  @core = core
end

Instance Attribute Details

#coreObject (readonly)

:nodoc:


15
16
17
# File 'lib/paleth/eth.rb', line 15

def core
  @core
end

Instance Method Details

#accountsObject

Returns a promise which resolves to an array of account addresses


105
106
107
108
109
# File 'lib/paleth/eth.rb', line 105

def accounts
  make_promise(@core.web3.JS[:eth], 'getAccounts') do |result|
    Native(`result`)
  end
end

#balance(address, block = nil) ⇒ Object

Returns a promise which resolves to the balance of the account with the requested address at the given block. See default_block


118
119
120
121
122
123
# File 'lib/paleth/eth.rb', line 118

def balance(address, block = nil)
  args = [address, block].compact
  make_promise(@core.web3.JS[:eth], 'getBalance', *args) do |result|
    to_bigdecimal(result)
  end
end

#block(number, full = false) ⇒ Object

Returns a promise which resolve to a Block object representing the block with the requested number. number can be a block number, :earliest, :latest or :pending


143
144
145
146
147
# File 'lib/paleth/eth.rb', line 143

def block(number, full = false)
  make_promise(@core.web3.JS[:eth], 'getBlock', number, full) do |block|
    Paleth::Block.new(block)
  end
end

#block_numberObject

Returns a promise which resolves to the number of the most recent block


112
113
114
# File 'lib/paleth/eth.rb', line 112

def block_number
  make_promise(@core.web3.JS[:eth], 'getBlockNumber')
end

#block_transaction_count(number) ⇒ Object

Returns a promise which resolves to the number of transaction in the requested block. Number has the same meaning as in the #block method


159
160
161
# File 'lib/paleth/eth.rb', line 159

def block_transaction_count(number)
  make_promise(@core.web3.JS[:eth], 'getBlockTransactionCount', number)
end

#code(address, block = nil) ⇒ Object

Returns a promise which resolve to the code at the requested address. You can optionnaly provide a block number


135
136
137
138
# File 'lib/paleth/eth.rb', line 135

def code(address, block = nil)
  args = [address, block].compact
  make_promise(@core.web3.JS[:eth], 'getCode', *args)
end

#coinbaseObject

Returns a promise which resolve to the current coinbase


82
83
84
# File 'lib/paleth/eth.rb', line 82

def coinbase
  make_promise(@core.web3.JS[:eth], 'getCoinbase')
end

#default_accountObject

Get the default address for:

  • sendTransaction

  • call


26
27
28
# File 'lib/paleth/eth.rb', line 26

def 
  @core.web3.JS[:eth].JS[:defaultAccount]
end

#default_account=(addresss) ⇒ Object

Set the default account, see default_account.


31
32
33
# File 'lib/paleth/eth.rb', line 31

def default_account=(addresss)
  @core.web3.JS[:eth].JS[:defaultAccount] = addresss
end

#default_blockObject

Get the default block used for those calls

  • get_balance

  • get_code

  • get_transaction_count

  • get_storage_at

  • call

  • contract.my_method.call()

  • contract.my_method.estimate_gas()

Supported values are:

  • A block ID (Number)

  • :earliest, the genesis block

  • :latest, the head of the blockchain

  • :pending, the currently mined block


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

def default_block
  @core.web3.JS[:eth].JS[:defaultBlock]
end

#default_block=(block) ⇒ Object

Set the default block, see default_block


55
56
57
# File 'lib/paleth/eth.rb', line 55

def default_block=(block)
  @core.web3.JS[:eth].JS[:defaultBlock] = block
end

#gas_priceObject

Returns a promise which resolve to the current gas price as a BigNumber in wei


98
99
100
101
102
# File 'lib/paleth/eth.rb', line 98

def gas_price
  make_promise(@core.web3.JS[:eth], 'getGasPrice') do |result|
    to_bigdecimal(result)
  end
end

#hashrateObject

Returns a promise which resolve to the current hashrate if client is mining


93
94
95
# File 'lib/paleth/eth.rb', line 93

def hashrate
  make_promise(@core.web3.JS[:eth], 'getHashrate')
end

#mining?Boolean

Returns a promise which resolve true or false, depending on if the client is currently mining or not

Returns:

  • (Boolean)

88
89
90
# File 'lib/paleth/eth.rb', line 88

def mining?
  make_promise(@core.web3.JS[:eth], 'getMining')
end

#sign(address, data) ⇒ Object

Signs some data using the account with the provided address. The account has to be unlocked. Returns a promise which resolves to the signed data


198
199
200
201
202
# File 'lib/paleth/eth.rb', line 198

def sign(address, data)
  puts 'Warning: Eth#sign is deprecated and mostly does not work. ' \
       'Use Personal#sign'
  make_promise(@core.web3.JS[:eth], 'sign', address, data)
end

#storage_at(address, index, block = nil) ⇒ Object

Returns a promise which resolves to the value stored at the provided address with the given index. Optionnaly you can provide a block, or we'll use the default_block value


128
129
130
131
# File 'lib/paleth/eth.rb', line 128

def storage_at(address, index, block = nil)
  args = [address, index, block].compact
  make_promise(@core.web3.JS[:eth], 'getStorageAt', *args)
end

#syncing(&_) ⇒ Object

Takes a block which will be call with 2 arguments everytime the syncing status of the node changes:

  • error: nil or an error

  • sync:

    • true when sync starts

    • A SyncState object when sync is ongoing

    • false when it stops


74
75
76
77
78
79
# File 'lib/paleth/eth.rb', line 74

def syncing(&_)
  @core.web3.JS[:eth].JS.isSyncing do |error, sync|
    sync = Paleth::SyncState.new(sync) if sync && sync != true
    yield error, sync
  end
end

#syncing_stateObject

Returns a promis resolving to either false of a SyncState object


60
61
62
63
64
# File 'lib/paleth/eth.rb', line 60

def syncing_state
  make_promise(@core.web3.JS[:eth], 'getSyncing') do |result|
    result ? Paleth::SyncState.new(result) : result
  end
end

#transaction(hash) ⇒ Object

Returns a promise which resolve to the Transaction object with the requested hash


165
166
167
168
169
# File 'lib/paleth/eth.rb', line 165

def transaction(hash)
  make_promise(@core.web3.JS[:eth], 'getTransaction', hash) do |data|
    Paleth::Transaction.new data
  end
end

#transaction_count(address) ⇒ Object

Returns a promise which resolves to the number of transactions executed from the requested address


191
192
193
# File 'lib/paleth/eth.rb', line 191

def transaction_count(address)
  make_promise(@core.web3.JS[:eth], 'getTransactionCount', address)
end

#transaction_from_block(block, index) ⇒ Object

Returns a promise which resolves to the transaction object with the requested index in the requested block number, (or hash, or latest, …, see #block).


174
175
176
177
178
179
# File 'lib/paleth/eth.rb', line 174

def transaction_from_block(block, index)
  method = 'getTransactionFromBlock'
  make_promise(@core.web3.JS[:eth], method, block, index) do |data|
    Paleth::Transaction.new data
  end
end

#transaction_receipt(hash) ⇒ Object

Returns a promise which resolves to the TransactionReceipt object for the transaction with the requested hash


183
184
185
186
187
# File 'lib/paleth/eth.rb', line 183

def transaction_receipt(hash)
  make_promise(@core.web3.JS[:eth], 'getTransactionReceipt', hash) do |data|
    Paleth::TransactionReceipt.new data
  end
end

#uncle(number, index, full = false) ⇒ Object

Same as #block method, but get the uncle with the request index


150
151
152
153
154
# File 'lib/paleth/eth.rb', line 150

def uncle(number, index, full = false)
  make_promise(@core.web3.JS[:eth], 'getUncle', number, index, full) do |block|
    Paleth::Block.new(block)
  end
end