class BlockchainNode::Model::Ethereum

Constants

DECIMALS_18

Public Instance Methods

accounts() click to toggle source
# File lib/blockchain-node/model/ethereum.rb, line 11
def accounts
  @client.personal_listAccounts
end
balanceOf(account) click to toggle source
# File lib/blockchain-node/model/ethereum.rb, line 15
def balanceOf(account)
  resp = @client.eth_getBalance(account, 'latest')
  hex_to_int(resp[:response]) / DECIMALS_18
end
blockchain() click to toggle source
# File lib/blockchain-node/model/ethereum.rb, line 7
def blockchain
  'ethereum'
end
highest_block() click to toggle source
# File lib/blockchain-node/model/ethereum.rb, line 37
def highest_block
  hex_to_int(@client.eth_blockNumber[:response])
end
send(from, to, ether) click to toggle source

sends a transaction. Returns the transaction ID

# 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) click to toggle source
# File lib/blockchain-node/model/ethereum.rb, line 41
def transactions_for_account(account, startBlock = nil, endBlock = nil)
  endBlock = highest_block if endBlock.nil?
  startBlock = endBlock - 1000 if startBlock.nil?
  account.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) == account || t["to"].try(:downcase) == account }
  end

  found_transactions
end
unlock(account, password, seconds = 30) click to toggle source
# File lib/blockchain-node/model/ethereum.rb, line 20
def unlock(account, password, seconds = 30)
  @client.personal_unlockAccount(account, password, seconds)
end
unlock_and_send(password, from, to, ether) click to toggle source
# 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

Private Instance Methods

hex_to_int(hex) click to toggle source
# File lib/blockchain-node/model/ethereum.rb, line 58
def hex_to_int(hex)
  Integer(hex)
end
int_to_hex(int) click to toggle source
# File lib/blockchain-node/model/ethereum.rb, line 62
def int_to_hex(int)
  '0x' + int.to_s(16)
end