Skip to content

Instantly share code, notes, and snippets.

@douglasnomizo
Created December 10, 2014 06:26
Show Gist options
  • Save douglasnomizo/d2b6bc0dc073ae30aa81 to your computer and use it in GitHub Desktop.
Save douglasnomizo/d2b6bc0dc073ae30aa81 to your computer and use it in GitHub Desktop.
# 1. Transaction Fee Caculation
# We have a ladder fee structure. Please write a function to calculate total transaction fee we can get given the trading volume.
# Volume Fee
# 0 - 10 btc  2%
# 10 - 50 btc 1%
# 50 - 100 btc 0.5%
# 100 - 1000 btc 0.25%
# 1000 - 5000 btc 0.1%
# 5000 - 10000 btc 0.05%
# >= 10000 btc 0%
# examples:
# f(5) = 5*0.02 = 0.1
# f(10) = 10*0.02 = 0.2
# f(20) = 10*0.02 + 10*0.01 = 0.3
# f(60) = 10*0.02 + 40*0.01 + 10*0.005 = 0.65
require 'bigdecimal'
class FeeCalculator
BITCOIN_TAX = {
10000..Float::INFINITY => 0,
5001..10000 => 0.05,
1001..5000 => 0.1,
101..1000 => 0.25,
51..100 => 0.5,
11..50 => 1,
1..10 => 2
}
def self.for(bitcoins)
return self.tax_for(bitcoins)
end
private
def self.tax_for(bitcoins)
total = BigDecimal(0)
BITCOIN_TAX.each do |range, tax|
while range.include? bitcoins and bitcoins > 0 do
total += (bitcoins - range.min + 1) * (tax / 100.0)
bitcoins = range.min - 1
end
end
total
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment