Skip to content

AergoLite﹢Ruby

Bernardo Ramos edited this page Nov 30, 2020 · 1 revision

For Ruby we use the sqlite3-ruby wrapper

First install AergoLite on your computer.

Then install the wrapper with this line:

sudo gem install sqlite3 -- --with-sqlite3-include=/usr/local/include \
  --with-sqlite3-lib=/usr/local/lib/aergolite/

Usage

Example code:

require "sqlite3"

# Open a database
uri = "file:path/to/app.db?blockchain=on&discovery=local:4329&password=test"
db = SQLite3::Database.new uri

# Print the status
p db.get_first_value("pragma blockchain_status")

# Wait until the db can be accessed
while true
  break if db.get_first_value("pragma db_is_ready")
  sleep 1
end
p "the database is ready"

db.execute("INSERT INTO students (name, email, grade, blog) 
            VALUES (?, ?, ?, ?)", ["Jane", "[email protected]", "A", "http://blog.janedoe.com"])

db.execute("select * from students") do |row|
  p row
end

Signing a transaction from the blockchain admin:

db.create_function "on_sign_transaction", 1 do |func, data|
  signature = sign(data, privkey)
  func.result = hex(pubkey) + ":" + hex(signature)
end

Receiving a notification of a processed transaction:

db.create_function "transaction_notification", 2 do |func, nonce, status|
  p "transaction " + str(nonce) + ": " + status
end

Receiving notification of local db update:

db.create_function "update_notification", 0 do |func|
  p "update received"
end
Clone this wiki locally