michelson library / gem - test simulator / runtime for type-safe 'n' functional (crypto) contracts (in yes, it's just ruby)

(Gerald Bauer) #1

Hello,

  I've put together michelson [1] - a test simulator and runtime library / gem
that lets you write and run type-safe 'n' functional (crypto)
contracts in yes, it's just ruby.

   See the Let's Count [2] or the Let's Vote [3] examples to get started
with functional type-safe Liquidity (OCaml/ReasonML)-style programming in ruby.

```

···

######################################
# Let's Count - 0, 1, 2, 3
type :Storage, Integer

init [],
def storage()
  0
end

entry [Integer],
def inc( by, storage )
  [[], storage + by]
end

################################
## Test, Test, Test

storage = storage()
# => calling storage()...
# => returning:
# => 0
_, storage = inc( 2, storage )
# => calling inc( 2, 0 )...
# => returning:
# => [[], 2]
_, storage = inc( 1, storage )
# => calling inc( 1, 2 )...
# => returning:
# => [[], 3]


  or

type :Storage, Map‹String→Integer›

init [],
def storage()
  {"ocaml" => 0, "reason" => 0, "ruby" => 0}
end

entry [String],
def vote( choice, votes )
  amount = Current.amount
  if amount < 5.tz
    Current.failwith( "Not enough money, at least 5tz to vote" )
  else
    match Map.find(choice, votes), {
     None: ->() { Current.failwith( "Bad vote" ) },
     Some: ->(x) { votes = Map.add(choice, x + 1, votes); [[], votes] }}
  end
end

storage = storage()
#=> calling storage()...
#=> returning:
#=> {"ocaml"=>0, "reason"=>0, "ruby"=>0}
# _, storage = vote( "ruby", storage )
#=> calling vote( "ruby", {"ocaml"=>0, "reason"=>0, "ruby"=>0} )...
#=> RuntimeError: failwith - Not enough money, at least 5tz to vote

Current.amount = 10.tz

storage = storage()
#=> calling storage()...
#=> returning:
#=> {"ocaml"=>0, "reason"=>0, "ruby"=>0}
_, storage = vote( "ruby", storage )
#=> calling vote( "ruby", {"ocaml"=>0, "reason"=>0, "ruby"=>0} )...
#=> returning:
#=> [[], {"ocaml"=>0, "reason"=>0, "ruby"=>1}]
_, storage = vote( "reason", storage )
#=> calling vote( "reason", {"ocaml"=>0, "reason"=>0, "ruby"=>1} )...
#=> returning:
#=> [[], {"ocaml"=>0, "reason"=>1, "ruby"=>1}]
_, storage = vote( "python", storage )
#=> calling vote( "python", {"ocaml"=>0, "reason"=>1, "ruby"=>1} )...
#=> RuntimeError: failwith - Bad vote
```

   Happy (crypto) contract programming with (secure) ruby. Cheers. Prost.

[1] https://github.com/s6ruby/ruby-to-michelson/tree/master/michelson
[2] https://github.com/s6ruby/ruby-to-michelson/blob/master/contracts/counter.rb
[3] https://github.com/s6ruby/ruby-to-michelson/blob/master/contracts/vote.rb

0 Likes