Created
March 19, 2022 04:23
-
-
Save lyoungblood/bf54254d26594d59377279f9fcb6d11e to your computer and use it in GitHub Desktop.
Calculates recursive interest/folding using popular DeFi lending protocols
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/env ruby | |
# Copy this file as `recint` (or whatever you want) somewhere in your path and | |
# use it according to the usage notes below! | |
if ARGV.size < 4 | |
puts "Usage: recint <collateral_factor> <supply_apr> <supply_bonus> <borrow_apr> <borrow_bonus>" | |
puts | |
puts " Example - To calculate the net interest on recursively lending this asset:" | |
puts | |
puts " 80% collateral factor (CF)" | |
puts " 2.5% supply APR" | |
puts " 3.7% supply incentive APR" | |
puts " 3.2% borrow APR" | |
puts " 2.1% borrow incentive APR" | |
puts | |
puts " Use this command:" | |
puts | |
puts " recint 80 2.5 3.7 3.2 2.1" | |
puts | |
puts " Output:" | |
puts | |
puts " Base APR: -0.30%" | |
puts " Bonus APR: 26.90%" | |
puts " Total APR: 26.60%" | |
puts | |
exit 1 | |
end | |
# Gather input | |
cf, supply_apr, supply_bonus, borrow_apr, borrow_bonus = ARGV.map(&:to_f) | |
cf /= 100 | |
# Calculate recursive supply and borrow | |
total_supply = 1 / (1 - cf) | |
total_borrow = total_supply * cf | |
# Calculate base apr | |
base_supply_apr = total_supply * supply_apr | |
base_borrow_apr = total_borrow * borrow_apr | |
base_apr = base_supply_apr - base_borrow_apr | |
# Calculate bonus apr | |
bonus_supply_apr = total_supply * supply_bonus | |
bonus_borrow_apr = total_borrow * borrow_bonus | |
bonus_apr = bonus_supply_apr + bonus_borrow_apr | |
# Calculate total apr | |
total_apr = base_apr + bonus_apr | |
def percent(f) | |
'%.2f' % f + '%' | |
end | |
puts "Base APR: #{percent(base_apr)}" | |
puts "Bonus APR: #{percent(bonus_apr)}" | |
puts "Total APR: #{percent(total_apr)}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment