Class: BlockchainNode::Model::Ethereum

Inherits:
Base
  • Object
show all
Defined in:
lib/blockchain-node/model/ethereum.rb

Constant Summary collapse

DECIMALS_18 =
1000000000000000000.0

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from BlockchainNode::Model::Base

Instance Method Details

#accountsObject



11
12
13
# File 'lib/blockchain-node/model/ethereum.rb', line 11

def accounts
  @client.personal_listAccounts
end

#balanceOf(account) ⇒ Object



15
16
17
18
# File 'lib/blockchain-node/model/ethereum.rb', line 15

def balanceOf()
  resp = @client.eth_getBalance(, 'latest')
  hex_to_int(resp[:response]) / DECIMALS_18
end

#blockchainObject



7
8
9
# File 'lib/blockchain-node/model/ethereum.rb', line 7

def blockchain
  'ethereum'
end

#highest_blockObject



37
38
39
# File 'lib/blockchain-node/model/ethereum.rb', line 37

def highest_block
  hex_to_int(@client.eth_blockNumber[:response])
end

#send(from, to, ether) ⇒ Object

sends a transaction. Returns the transaction ID



25
26
27
28
29
30
# File 'lib/blockchain-node/model/ethereum.rb', line 25

def send(from, to, ether)
  wei_to_send = (ether * DECIMALS_18).round
  value = '0x' + wei_to_send.to_s(16)
  tx = { from: from,  to: to,  value: value }
  @client.eth_sendTransaction(tx)[:response]
end

#transactions_for_account(account, startBlock = nil, endBlock = nil) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/blockchain-node/model/ethereum.rb', line 41

def (, startBlock = nil, endBlock = nil)
  endBlock = highest_block if endBlock.nil?
  startBlock = endBlock - 1000 if startBlock.nil?
  .downcase!

  found_transactions = []

  (startBlock..endBlock).each do |block|
    response = @client.eth_getBlockByNumber(int_to_hex(block), true)
    found_transactions += response["transactions"].select{ |t| t["from"].try(:downcase) ==  || t["to"].try(:downcase) ==  }
  end

  found_transactions
end

#unlock(account, password, seconds = 30) ⇒ Object



20
21
22
# File 'lib/blockchain-node/model/ethereum.rb', line 20

def unlock(, password, seconds = 30)
  @client.personal_unlockAccount(, password, seconds)
end

#unlock_and_send(password, from, to, ether) ⇒ Object



32
33
34
35
# File 'lib/blockchain-node/model/ethereum.rb', line 32

def unlock_and_send(password, from, to, ether)
  unlock(from, password, 10)
  send(from, to, ether)
end