/ethereum.rb

Ethereum library for the Ruby language

Primary LanguageRubyMIT LicenseMIT

Ethereum Ruby library - Ethereum.rb

Build Status security Dependency Status Code Climate

The goal of ethereum.rb is to make interacting with ethereum blockchain from ruby as easy as possible (but not easier).

Highlights

  • Simple syntax, programmer friendly
  • Deploy and interact with contracts on the blockchain
  • Contract - ruby object mapping
  • Compile Solidity contracts with solc compiler from ruby
  • Receive events from contract
  • Make direct json rpc calls to node from ruby application
  • Connect to node via IPC or HTTP
  • Helpful rake tasks for common actions

Installation

Before installing gem make sure you meet all prerequisites, especially that you have:

  • compatible ethereum node installed
  • compatible solidity compiler installed
  • wallet with some ethereum on it

Before you run a program check that the node is running and accounts you want to spend from are unlocked.

To install gem simply add this line to your application's Gemfile:

gem 'ethereum.rb'

And then execute:

$ bundle

Or install it yourself as:

$ gem install ethereum.eb

Basic Usage

You can create contract from solidity source and deploy it to the blockchain, with following code:

contract = Ethereum::Contract.create(file: "greeter.sol")
address = contract.deploy_and_wait("Hello from ethereum.rb!")

Deployment may take up to couple of minutes. Once deployed you can start interacting with contract, e.g. calling it's methods:

contract.call.greet # => "Hello from ethereum.rb!"

You can see example contract greeter here.

Creating contract

Compile multiple contracts at once

If you want to complie multiple contracts at once, you can create new instances using newly declared ruby clasess:

Ethereum::Contract.create(file: "mycontracts.sol", client: client)
contract = MyContract1.new
contract = contract.deploy_and_wait
contract2 = MyContract2.new
contract2 = contract.deploy_and_wait

All names used to name contract in solidity source will transalte to name of classes in ruby (camelized).

Note: If class of given name exist it will be undefined first to avoid name collision.

Get contract from blockchain

The other way to obtain contract instance is get one that already exist in the blockchain. To do so you need a contract name, contract address and ABI definition.

contract = Ethereum::Contract.create(name: "MyContract", address: "0x01a4d1A62F01ED966646acBfA8BB0b59960D06dd ", abi: abi)

Note that you need to specify contract name, that will be used to define new class in ruby, as it is not a part of ABI definition.

Alternatively you can obtain abi definition and name from contract source file:

contract = Ethereum::Contract.create(file: "MyContract.sol", address: "0x01a4d1A62F01ED966646acBfA8BB0b59960D06dd ")

If you want to create new contract, that is not yet deployed from ABI definition you will need also to supply binary code:

contract = Ethereum::Contract.create(name: "MyContract", abi: abi, code: "...")

Interacting with contract

Functions defined in a contract are exposed using the following conventions:

contract.transact.[function_name](params)
contract.transact_and_wait.[function_name](params)  
contract.call.[function_name](params)

Example Contract in Solidity

contract SimpleRegistry {

  mapping (bytes32 => string) public registry;

  function register(bytes32 key, string value) {
    registry[key] = value;
  }

  function get(bytes32 key) public constant returns(string) {
    return registry[key];
  }

}

For contract above here is how to access it's methods:

contract.transact_and_wait.register("performer", "Beastie Boys")

Will send transaction to the blockchain and wait for it to be mined.

contract.transact.register("performer", "Black Eyed Peas")

Will send transaction to the blockchain return instantly.

contract.call.get("performer") # => "Black Eyed Peas"

Will call method of the contract and return result. Note that no transaction need to be send to the network as method is read-only. On the other hand register method will change contract state, so you need to use transact or transact_and_wait to call it.

IPC Client Connection

By default methods interacting with contracts will use default Json RPC Client that will handle connection to ethereum node. Default client communicate via IPC. If you want to create custom client or use multiple clients you can create them yourself.

To create IPC client instance of simply create Ethereum::IpcClient:

client = Ethereum::IpcClient.new

You can also customize it with path to ipc file path and logging flag:

client = Ethereum::IpcClient.new("~/.parity/mycustom.ipc", false)

If no ipc file path given, IpcClient looks for ipc file in default locations for parity and geth. The second argument is optional. If it is true then logging is on.

By default logging is on and logs are saved in "/tmp/ethereum_ruby_http.log".

To create Http client use following:

client = Ethereum::HttpClient.new('http://localhost:8545')

You can supply client when creating a contract:

contract = Ethereum::Contract.create(client: client, ...)

You can also obtain default client:

client = Ethereum::Singleton.instance

Calling json rpc methods

Ethereum.rb allows you to interact directly with Ethereum node using json rpc api calls. Api calls translates directly to client methods. E.g. to call eth_gasPrice method:

client.eth_gas_price # => {"jsonrpc"=>"2.0", "result"=>"0x4a817c800", "id"=>1}

Note: methods are transated to underscore notation.

Full list of json rpc methods is available here

Utils rake tasks

There are couple of rake tasks to help in wallet maintenance, i.e.:

rake ethereum:contract:compile[path]            # Compile a contract / Compile and deploy contract
rake ethereum:transaction:byhash[id]            # Get info about transaction
rake ethereum:transaction:send[address,amount]  # Send [amount of] ether to an account

Debbuging

Logs from communication between ruby app and node are available under following path:

/tmp/ethereum_ruby_http.log

Roadmap

  • Rubydoc documentation
  • Signing transactions

Development

Run bin/console for an interactive prompt that will allow you to experiment.

Make sure rake ethereum:test:setup passes before running tests.

Then, run rake spec to run the tests.

Test that do send transactions to blockchain are marked with blockchain tag. Good practice is to run first fast tests that use no ether and only if they pass, run slow tests that do spend ether. To do that use the following line:

$ bundle exec rspec --tag ~blockchain && bundle exec rspec --tag blockchain

You need ethereum node up and running for tests to pass and it needs to be working on testnet (Ropsten).

Acknowledgements and license

This library has been forked from ethereum-ruby by DigixGlobal Pte Ltd (https://dgx.io).

The gem is available as open source under the terms of the MIT License.