Last active
August 29, 2016 00:29
-
-
Save findkiko/027494fc235f27fea47cf498d190c6f0 to your computer and use it in GitHub Desktop.
Loop-free many-to-many (bitcoin) accounting in Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
tx = { | |
"inputs" => [ | |
{ | |
"address" => "abc", | |
"value" => 6 | |
}, | |
{ | |
"address" => "def", | |
"value" => 4 | |
}, | |
{ | |
"address" => "ghi", | |
"value" => 3 | |
} | |
], | |
"outputs" => [ | |
{ | |
"address" => "jkl", | |
"value" => 2 | |
}, | |
{ | |
"address" => "mno", | |
"value" => 2 | |
}, | |
{ | |
"address" => "pqr", | |
"value" => 1 | |
} | |
] | |
} | |
my_wallet = [ "abc", "mno", "xyz" ] | |
inbound = tx["outputs"] | |
.select { |index| my_wallet.include?(index["address"]) } | |
.map { |pair| pair["value"] } | |
.reduce(:+) | |
outbound = tx["inputs"] | |
.select { |index| my_wallet.include?(index["address"]) } | |
.map { |pair| pair["value"] } | |
.reduce(:+) | |
xfer = inbound - outbound | |
puts xfer.to_s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment