Rubyライブラリ"counterparty_ruby"を使ってみる(2/3)

f:id:yzono:20150116025537j:plain

はじめに

前回に引き続き@derosetechのcounterparty_rubyを使ってみます。

目次

  • アセットの作成 (Create an Issuance)
  • イベント結果のブロードキャスト (Create an Broadcast)
  • ギャンブル (Create an Broadcast and Bet)

アセットの作成 (Create an Issuance)

実装

Free Numericアドレスを作成するスクリプトを実装します。

/home/xcp/counterparty_ruby-master/demo_issuance.rb

require 'counterparty_ruby'

first_asset = Counterparty::Issuance.new( 
  source: 'mo1kgfktQfRaLMR5SvehDMHLa9Cu7xJp4m',
  asset: 'A15128863744940229000', 
  description: "Its party time!",
  divisible: false,
  quantity: 100 )

transaction_id = first_asset.save!                                          

puts "Transaction %s has been entered into the mempool" % transaction_id

実行

サインとブロードキャストも同時に処理してくれます。(便利)

$ ruby -I /home/xcp/counterparty_ruby-master/lib demo_issuance.rb 
Transaction 137d3ea55727e341d2949eec04bdf43eb816a9b3b60afc0e0eea06268e14cbce has been entered into the mempool

Blockscan(testnet)

https://testnet.blockscan.com/tx?txhash=137d3ea55727e341d2949eec04bdf43eb816a9b3b60afc0e0eea06268e14cbce

イベント結果のブロードキャスト (Create an Broadcast)

実装

「3月1日12AM UTCの金の価格は下落した」という結果をブロードキャストするスクリプトを実装します。

/home/xcp/counterparty_ruby-master/demo_issuance.rb

require 'counterparty_ruby'

gold_down = Counterparty::Broadcast.new(
  source: 'mo1kgfktQfRaLMR5SvehDMHLa9Cu7xJp4m', 
  fee_fraction: 0.05,
  text: "Price of gold, 12AM UTC March1. 1=inc 2=dec/const", 
  timestamp: 1418926641, 
  value: 2 )

tx_id = gold_down.save!

puts "Gold was broadcast down in transaction  %s" % tx_id

実行

$ ruby -I /home/xcp/counterparty_ruby-master/lib demo_broadcast.rb 
Gold was broadcast down in transaction  579210a6c0e7051a441389f14e4907740821a9cc50ee1e7874cdd0cab4df87a3

Blockscan(testnet)

https://testnet.blockscan.com/tx?txhash=579210a6c0e7051a441389f14e4907740821a9cc50ee1e7874cdd0cab4df87a3

ギャンブル (Create an Broadcast and Bet)

Gemインストール

sudo gem install activesupport

実装

require 'counterparty_ruby'
require 'active_support/time'
require "active_support/core_ext/numeric/time"

TEAM_BLUE_WINS = 1
TEAM_RED_WINS = 2

ALICE_ADDRESS = 'mfs4uCpPkWNTC5L3cWLRjFdgXJ9LP61Ztw'
JOHN_ADDRESS = 'mo1kgfktQfRaLMR5SvehDMHLa9Cu7xJp4m'

ORACLE_ADDRESS = 'mo1kgfktQfRaLMR5SvehDMHLa9Cu7xJp4m'

Counterparty.test!

broadcast_text = "Winner of game, %s. %s=red %s=blue" % [
  Time.now.strftime("%I%p %Z %b%-d"), TEAM_RED_WINS, TEAM_BLUE_WINS]

# Announce the availability of a Bet:
# NOTE: All times are in UTC
tx_init = Counterparty::Broadcast.new( source: ORACLE_ADDRESS, 
  value: Counterparty::Broadcast::OPEN_BROADCAST, timestamp: Time.now.to_i,
  text: broadcast_text, fee_fraction: 0.00, allow_unconfirmed_inputs: true ).save!

# Alice Bets on Blue:
tx_bet_on_blue = Counterparty::Bet.new(source: ALICE_ADDRESS, 
  feed_address: ORACLE_ADDRESS, bet_type: Counterparty::Bet::EQUAL, 
  deadline: 10.minutes.from_now.to_i, wager_quantity: 5, 
  counterwager_quantity: 1, expiration: 5, 
  target_value: TEAM_BLUE_WINS, leverage: Counterparty::Bet::LEVERAGE_BASIS,
  allow_unconfirmed_inputs: true ).save!

puts "Alice on Blue: %s" % tx_bet_on_blue

# John Bets on Red:
tx_bet_on_red = Counterparty::Bet.new(source: JOHN_ADDRESS, 
  feed_address: ORACLE_ADDRESS, bet_type: Counterparty::Bet::EQUAL, 
  deadline: 10.minutes.from_now.to_i, wager_quantity: 5, 
  counterwager_quantity: 1, expiration: 5,
  target_value: TEAM_RED_WINS, leverage: Counterparty::Bet::LEVERAGE_BASIS,
  allow_unconfirmed_inputs: true ).save!

puts "John on Red: %s" % tx_bet_on_red

# Close the broadcast : Team Blue wins!
tx_outcome = Counterparty::Broadcast.new( source: ORACLE_ADDRESS, 
  value: TEAM_BLUE_WINS, timestamp: 20.minutes.from_now.to_i, 
  fee_fraction: 0.00,
  text: broadcast_text, allow_unconfirmed_inputs: true ).save!

puts "Oracle says: %s" % tx_outcome

実行

$ ruby -I /home/xcp/counterparty_ruby-master/lib demo_bet.rb 
Alice on Blue: e39856dfe655c696568278a9e887e4e383a05ac89938418014b62b20ca3141c0
John on Red: f351b903ec0559d5529628e39eed2579b3616b2200134648ecdb8dc24bf5f159
Oracle says: aa54029ca6c0be5c4910a7c0b3f2f4cf6449778854ed734ba2fd51ac9f6a494e```

Blockscan(testnet)

https://testnet.blockscan.com/tx?txhash=e39856dfe655c696568278a9e887e4e383a05ac89938418014b62b20ca3141c0

https://testnet.blockscan.com/tx?txhash=f351b903ec0559d5529628e39eed2579b3616b2200134648ecdb8dc24bf5f159

https://testnet.blockscan.com/tx?txhash=aa54029ca6c0be5c4910a7c0b3f2f4cf6449778854ed734ba2fd51ac9f6a494e

まとめ

Blockscan on testnetが公開されたので確認作業がすごく楽になりました。 次回はcounterparty_rubyを使ってSmart Contractsをやります。

参考

Github brighton36/counterparty_ruby