Hello,
a little weekend experiment - let's (re)build the billion dollar
ethereum world computer from scratch (zero) using a 25-year-old
contract language - ruby - and ye good 40-year-old SQL databases.
See a first live converted gold mine / token contract in universum
e.g. token.rb [1]
require 'universum'
class Token < Contract
class Transfer < Event
def initialize( from:, to:, value: )
@from, @to, @value = from, to, value
end
end
class Approval < Event
def initialize( owner:, spender:, value: )
@owner, @spender, @value = owner, spender, value
end
end
def initialize( name:, symbol:, decimals:, initial_supply: )
@name = name
@symbol = symbol
@decimals = decimals
@total_supply = initial_supply * (10 ** decimals)
@balances = Hash.new(0) ## note: special hash (default value is 0
and NOT nil)
@balances[msg.sender] = @total_supply
@allowed = {}
end
def balance_of( owner: )
@balances[owner]
end
def transfer( to:, value: )
if assert( @balances[msg.sender] >= value ) &&
assert( @balances[to] + value >= @balances[to] )
@balances[msg.sender] -= value # Subtract from the sender
@balances[to] += value # Add the same to the recipient
log Transfer.new( from: msg.sender, to: to, value: value )
true
else
false
end
end
....
Let's put ruby on ~~rails~~ the blockchain :-). And the proof-of-pudding.
Some test assertions in token_test.rb [2] thanks to minitest:
class TestToken < Minitest::Test
def setup
@token = Token.new(
name: 'Your Crypto Token',
symbol: 'YOU',
decimals: 8,
initial_supply: 1_000_000
)
end
def test_transfer
assert_equal 100_000_000_000_000, @token.balance_of( owner: '0x0000' )
assert_equal 0, @token.balance_of( owner: '0x1111' )
assert @token.transfer( to: '0x1111', value: 100 )
assert_equal 100, @token.balance_of( owner: '0x1111' )
assert @token.transfer( to: '0x2222', value: 200 )
assert_equal 200, @token.balance_of( owner: '0x2222' )
assert_equal 99_999_999_999_700, @token.balance_of( owner: '0x0000' )
end
#...
end
Let's build the next world (blockchain) computer with Ruby :-).
Universum is the new Ethereum (2.0). Join in.
Cheers. Prost.
[1] https://github.com/openblockchains/universe/blob/master/tokens/token.rb.
[2] https://github.com/openblockchains/universe/blob/master/tokens/token_test.rb