Among the design goals mentioned in the Ercoin’s whitepaper are:

  • simplicity;
  • low entry barrier;
  • easy integration;
  • fast confirmations;
  • companion software built around the blockchain, not on the blockchain.

To illustrate them, we can show a proof of concept of a serverless ebook store with delivery by email. An order is placed by making an Ercoin transfer, with transfer message containing the buyer’s email and the filename of the ebook (separated by space). The store monitors its Ercoin address, performing delivery immediately when there is an eligible confirmed transaction (typically in a few seconds after broadcast). Since the payments do not include any dynamically created data, ebook catalogue can be published via many conventional channels like social media, static websites and even printed leaflets and newspaper advertisements.

We implement Ercoin integration using only generic libraries, without any Ercoin- or Tendermint-specific SDK. Despite of that, the code (in Ruby) is only 42 lines long, including imports and blank lines. (This version does not include catching up on the missed transactions when the daemon goes back online, but it should be relatively easy to implement).

require 'bigdecimal'
require 'eventmachine'
require 'faye/websocket'
require 'json'
require 'mail'

CONFIG = JSON.parse(File.read('config.json'), {symbolize_names: true})

def send_book(path, email)
  mail = Mail.new do
    from CONFIG[:bookstore_email_address]
    to email
    subject path.sub_ext("").basename.to_s
    add_file path.to_s
  end
  mail.delivery_method :logger
  mail.deliver
end

def handle_tx(message, value)
  email, filename = message.split(nil, 2)
  return false unless value >= CONFIG[:price] && filename && URI::MailTo::EMAIL_REGEXP =~ email
  filepath = Pathname.new(Dir.pwd) / "books" / Pathname.new(filename).basename
  send_book(filepath, email) if filepath.exist?
end

EM.run {
  ws = Faye::WebSocket::Client.new("wss://#{CONFIG[:ercoin_node]}/websocket")

  ws.on :open do |event|
    ws.send(JSON.dump({jsonrpc: "2.0", id: 1, method: "subscribe", params: {query: "tm.event='Tx' AND tx.to='#{CONFIG[:bookstore_ercoin_address]}'"}}))
  end

  ws.on :message do |event|
    data = JSON.parse(event.data)
    next unless data.dig("result", "data", "type") == 'tendermint/event/Tx'
    events = data["result"]["events"]
    value = BigDecimal(events["tx.value"].first) / 10 ** 6
    message = Base64.decode64(events["tx.message"].first)
    handle_tx(message, value)
  end
}

Gemfile:

source 'https://rubygems.org'

gem 'faye-websocket'
gem 'mail'

The concept can of course be abstracted to a library that allows performing arbitrary reactions to Ercoin transactions.