The problem
From labs/9-kelloggcoin.rb in your copy of ruby-intro:
Cryptocurrency is all the rage. You have decided to create your own, KelloggCoin, that will massively disrupt financial markets at the Global Hub.
Don't change the following code, which represents a series of transactions, each of which represents a transfer of KelloggCoin from one user to another. Transactions with no from_user are from the "ICO" (Initial Coin Offering).
blockchain = [
{ "from_user" => nil, "to_user" => "ben", "amount" => 20000 },
{ "from_user" => nil, "to_user" => "brian", "amount" => 20000 },
{ "from_user" => "ben", "to_user" => "evan", "amount" => 9000 },
{ "from_user" => "brian", "to_user" => "anthony", "amount" => 7000 },
{ "from_user" => "evan", "to_user" => "anthony", "amount" => 400 },
{ "from_user" => "ben", "to_user" => "anthony", "amount" => 1500 },
{ "from_user" => "anthony", "to_user" => "ben", "amount" => 4500 },
{ "from_user" => "anthony", "to_user" => "evan", "amount" => 1750 }
]
Write code that returns the number of KelloggCoin that each user has in their KelloggCoin "wallet". It should print out:
Ben's KelloggCoin balance is 14000 Brian's KelloggCoin balance is 13000 Evan's KelloggCoin balance is 10350 Anthony's KelloggCoin balance is 2650
Run it from the top folder of the repo:
bin/rails runner labs/9-kelloggcoin.rb
Events and state
Look at what the blockchain stores, and what it doesn't. It's a list of transactions, each one something that happened: Ben sent Evan 9,000. Nowhere does it say how much anyone has.
Each transaction is an event. A balance is state: where things stand right now. You get the state by replaying every event, in order, from the beginning. Same idea as the craps replay: a list of events in, a known result out. A bank account, an inventory count, the score of a game: the same shape, everywhere.
The solution
The goal is to track the changes to each user's balance, then display where each one ends up. Here's one possible solution.
Before the ICO, nobody has any KelloggCoin, so every balance starts at 0. We can't track changes to a balance unless the balance exists:
# create 4 variables to represent each user's balance
bens_balance = 0
brians_balance = 0
evans_balance = 0
anthonys_balance = 0
Next, loop through the transactions. For each to_user, add the amount to their balance; for each from_user, subtract it.
for transaction in blockchain
# each "transaction" in the "blockchain" array is a Hash
# if the user is the "from_user", decrement their balance
if transaction["from_user"] == "ben"
bens_balance = bens_balance - transaction["amount"]
elsif transaction["from_user"] == "brian"
brians_balance = brians_balance - transaction["amount"]
elsif transaction["from_user"] == "evan"
evans_balance = evans_balance - transaction["amount"]
elsif transaction["from_user"] == "anthony"
anthonys_balance = anthonys_balance - transaction["amount"]
end
# if the user is the "to_user", increment their balance
if transaction["to_user"] == "ben"
bens_balance = bens_balance + transaction["amount"]
elsif transaction["to_user"] == "brian"
brians_balance = brians_balance + transaction["amount"]
elsif transaction["to_user"] == "evan"
evans_balance = evans_balance + transaction["amount"]
elsif transaction["to_user"] == "anthony"
anthonys_balance = anthonys_balance + transaction["amount"]
end
end
The ICO transactions have nil as the from_user. nil isn't equal to any of the four names, so none of the subtractions run: coins come from nowhere, which is what an ICO is.
Lastly, display the final balances:
puts "Ben's KelloggCoin balance is #{bens_balance}"
puts "Brian's KelloggCoin balance is #{brians_balance}"
puts "Evan's KelloggCoin balance is #{evans_balance}"
puts "Anthony's KelloggCoin balance is #{anthonys_balance}"
Go further: check your own answer
The lab file asks you to compare each balance to the expected number, print PASS or FAIL, and raise an error when it's wrong. Reading four numbers by eye works today; a check works every time you change the code. Same shape as the craps replay: a list of what you expect, a loop, and a check.
checks = [
{ "name" => "Ben", "balance" => bens_balance, "expected" => 14000 },
{ "name" => "Brian", "balance" => brians_balance, "expected" => 13000 },
{ "name" => "Evan", "balance" => evans_balance, "expected" => 10350 },
{ "name" => "Anthony", "balance" => anthonys_balance, "expected" => 2650 }
]
for check in checks
if check["balance"] == check["expected"]
puts "PASS: #{check["name"]} #{check["balance"]}"
else
raise "FAIL: #{check["name"]} expected #{check["expected"]}, got #{check["balance"]}"
end
end
To see it fail, swap a - for a + in one of the subtractions and run it again.
Final code
# 1. create 4 variables to represent each user's balance
bens_balance = 0
brians_balance = 0
evans_balance = 0
anthonys_balance = 0
# 2. loop through the "blockchain" transactions: replay every event, in order
for transaction in blockchain
# each "transaction" in the "blockchain" array is a Hash
# 3. if the user is the "from_user", decrement their balance
if transaction["from_user"] == "ben"
bens_balance = bens_balance - transaction["amount"]
elsif transaction["from_user"] == "brian"
brians_balance = brians_balance - transaction["amount"]
elsif transaction["from_user"] == "evan"
evans_balance = evans_balance - transaction["amount"]
elsif transaction["from_user"] == "anthony"
anthonys_balance = anthonys_balance - transaction["amount"]
end
# 4. if the user is the "to_user", increment their balance
if transaction["to_user"] == "ben"
bens_balance = bens_balance + transaction["amount"]
elsif transaction["to_user"] == "brian"
brians_balance = brians_balance + transaction["amount"]
elsif transaction["to_user"] == "evan"
evans_balance = evans_balance + transaction["amount"]
elsif transaction["to_user"] == "anthony"
anthonys_balance = anthonys_balance + transaction["amount"]
end
end
# 5. finally, print out the result: the state after every event
puts "Ben's KelloggCoin balance is #{bens_balance}"
puts "Brian's KelloggCoin balance is #{brians_balance}"
puts "Evan's KelloggCoin balance is #{evans_balance}"
puts "Anthony's KelloggCoin balance is #{anthonys_balance}"
# 6. go further: check each balance against the expected number
checks = [
{ "name" => "Ben", "balance" => bens_balance, "expected" => 14000 },
{ "name" => "Brian", "balance" => brians_balance, "expected" => 13000 },
{ "name" => "Evan", "balance" => evans_balance, "expected" => 10350 },
{ "name" => "Anthony", "balance" => anthonys_balance, "expected" => 2650 }
]
for check in checks
if check["balance"] == check["expected"]
puts "PASS: #{check["name"]} #{check["balance"]}"
else
raise "FAIL: #{check["name"]} expected #{check["expected"]}, got #{check["balance"]}"
end
end
Output:
Ben's KelloggCoin balance is 14000
Brian's KelloggCoin balance is 13000
Evan's KelloggCoin balance is 10350
Anthony's KelloggCoin balance is 2650
PASS: Ben 14000
PASS: Brian 13000
PASS: Evan 10350
PASS: Anthony 2650
Congrats! You're on your way to mastery of the programming fundamentals: data types, variables, conditional logic, arrays, hashes, and loops!
Challenge!
The above solution gets the job done and there's no need to go any further. However, if this solution is leaving you a bit dissatisfied, then you're starting to think like a programmer!
Why might you be feeling dissatisfied? What are the drawbacks?
Well, this solution only works with these 4 specific users. What if someone new joined the blockchain? We would need to duplicate a lot of the code for every new user. Is there a way to write it that doesn't depend on knowing who the users are, or how many there are? A hint: a hash can hold a balance for each user, with the user's name as the key.
See if you can challenge yourself!