beowulf-ruby

beowulf-ruby is the official Beowulf library for Ruby.

Quick Start

Add the gem to your Gemfile:

# MainNet
gem 'beowulf-ruby'

# TestNet
gem 'beowulf-ruby-testnet'

Then:

$ bundle install

If you don't have bundler, see the next section.

Prerequisites

minimum ruby version: 2.2

Linux

$ sudo apt-get install ruby-full git openssl libssl1.0.0 libssl-dev
$ gem install bundler

macOS

$ gem install bundler

Usage

require 'beowulf'

api = Beowulf::Api.new
api.get_dynamic_global_properties do |properties|
  properties.head_block_number
end
=> 2307597

... or ...

require 'beowulf'

api = Beowulf::Api.new
response = api.get_dynamic_global_properties
response.result.head_block_number
=> 2307597

Example

require 'beowulf'

# 0. Init
## MainNet: https://bw.beowulfchain.com/rpc
## TestNet: https://testnet-bw.beowulfchain.com/rpc
api = Beowulf::Api.new(url: 'http://localhost:8376/rpc') # Replace this url with your node url

# 1. get_version
version = api.get_version
puts version.to_json

# 2. get_block
bk = api.get_blocks(1)
puts bk.to_json

# 3. get_config
cfg = api.get_config
puts cfg.to_json
puts cfg.result.BEOWULF_INIT_SUPPLY

# 4. get_accounts
acc = api.get_accounts(["beowulf2", "beowulf3"])
puts acc.to_json

# 5. get_transaction
gtx = api.get_transaction("e725f75544dbeea7a017250bd0186ca247b24724")
puts gtx.to_json

# 6. Transfer native coin
## 6.1. Transfer BWF from alice to bob
tx = Beowulf::Transaction.new(url: 'https://bw.beowulfchain.com/rpc', wif: 'alice Private-Key Here')
transfer = {
    type: :transfer,
    from: 'alice',
    to: 'bob',
    amount: '100.00000 BWF',
    fee: '0.01000 W',
    memo: 'alice to bob'
}
tx.operations << transfer
tx_resp = tx.process(true)
puts tx_resp.to_json
puts "tx_resp.result.id:", tx_resp.result.id

## 6.2. Transfer W from alice to bob
tx = Beowulf::Transaction.new(url: 'https://bw.beowulfchain.com/rpc', wif: 'alice Private-Key Here')
transfer = {
    type: :transfer,
    from: 'alice',
    to: 'bob',
    amount: '1.00000 W',
    fee: '0.01000 W',
    memo: 'alice to bob'
}
tx.operations << transfer
tx_resp = tx.process(true)
puts tx_resp.to_json
puts "tx_resp.result.id:", tx_resp.result.id

# 7. Transfer token
## Transfer token KNOW from alice to bob
tx = Beowulf::Transaction.new(url: 'https://bw.beowulfchain.com/rpc', wif: 'alice Private-Key Here')
transfer = {
    type: :transfer,
    from: 'alice',
    to: 'bob',
    amount: '100.00000 KNOW',
    fee: '0.01000 W',
    memo: 'alice to bob'
}
tx.operations << transfer
tx_resp = tx.process(true)
puts tx_resp.to_json
puts "tx_resp.result.id:", tx_resp.result.id


# 8. Create account
## 8.1. GenKeys
wl = Beowulf::Wallet.new(name: "new-account-name")
wl.gen_keys
puts "wl.private_key:", wl.private_key
puts "wl.public_key:", wl.public_key

## 8.2. AccountCreate
tx = Beowulf::Transaction.new(url: 'https://bw.beowulfchain.com/rpc', wif: 'creator Private-Key Here')
owner = {
    weight_threshold: 1,
    account_auths: [],
    key_auths: [[wl.public_key, 1]]
}
 = {
    type: :account_create,
    fee: '0.10000 W',
    creator: 'creator',
    new_account_name: wl.name,
    owner: owner,
    json_metadata: ''
}
tx.operations << 
tx_resp = tx.process(true)
puts "tx_resp:", tx_resp.to_json
puts "tx_resp.result.id:", tx_resp.result.id

## 8.3. Write file wallet.
wallet_path = "/path/to/folder/save/wallet"
password = "your_password"
wl.save_wallet_file(wallet_path, "", password)

## 8.4. Load file wallet.
sleep(2)
wallet_path_file = File.join(wallet_path, "new-account-name-wallet.json")
wl2 = Beowulf::Wallet.new
wl2.read_wallet_file(wallet_path_file, password)
puts "wl2.private_key:", wl2.private_key
puts "wl2.public_key:", wl2.public_key

Failover

Beowulf supports failover for situations where a node has, for example, become unresponsive. When creating a new instance of ::Api and ::Transaction, you may provide a list of alternative nodes, or leave them out to use the default list. For example:

options = {
  url: 'https://api.yournodedomain.com',
  failover_urls: [
    'https://api.otherdomain1.com',
    'https://api.otherdomain2.com'
  ]
}

api = Beowulf::Api.new(options)

License

MIT, see the LICENSE file.