Class: AtmEth::Ethereum
- Inherits:
-
Object
- Object
- AtmEth::Ethereum
- Defined in:
- lib/atm/eth/ethereum.rb
Instance Method Summary collapse
- #generate_wallet ⇒ Object
- #get_balance(address) ⇒ Object
- #get_transaction_status(tx_hash) ⇒ Object
-
#initialize(provider_url) ⇒ Ethereum
constructor
A new instance of Ethereum.
- #send_transaction_from_wallet(wallet, to, value_in_wei) ⇒ Object
Constructor Details
#initialize(provider_url) ⇒ Ethereum
Returns a new instance of Ethereum.
10 11 12 |
# File 'lib/atm/eth/ethereum.rb', line 10 def initialize(provider_url) @provider_url = provider_url end |
Instance Method Details
#generate_wallet ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/atm/eth/ethereum.rb', line 14 def generate_wallet key = SecureRandom.hex(32) private_key = "0x#{key}" address = generate_address_from_private_key(private_key) { address: address, private_key: private_key } end |
#get_balance(address) ⇒ Object
40 41 42 43 44 45 |
# File 'lib/atm/eth/ethereum.rb', line 40 def get_balance(address) uri = URI("#{@provider_url}?module=account&action=balance&address=#{address}&tag=latest") response = Net::HTTP.get(uri) balance_wei = JSON.parse(response)['result'] wei_to_ether(balance_wei) end |
#get_transaction_status(tx_hash) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/atm/eth/ethereum.rb', line 47 def get_transaction_status(tx_hash) uri = URI("#{@provider_url}?module=proxy&action=eth_getTransactionReceipt&txhash=#{tx_hash}") response = Net::HTTP.get(uri) receipt = JSON.parse(response)['result'] if receipt.nil? 'Pending' else receipt['status'] == '0x1' ? 'Successful' : 'Failed' end end |
#send_transaction_from_wallet(wallet, to, value_in_wei) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/atm/eth/ethereum.rb', line 22 def send_transaction_from_wallet(wallet, to, value_in_wei) nonce = get_nonce(wallet[:address]) gas_price = estimate_gas_price transaction_data = { nonce: nonce, gasPrice: gas_price, gasLimit: '0x5208', to: to, value: value_in_wei, data: '' } signed_transaction = sign_transaction(transaction_data, wallet[:private_key]) send_raw_transaction(signed_transaction) end |