Guest User

Untitled

a guest
Dec 14th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.87 KB | None | 0 0
  1. class DashboardsController < ApplicationController
  2. before_action :top_five_traded, :latest_news, :top5_winners, :top5_losers
  3. before_action :set_portfolio, only: [:dashboard, :portfolio_overview]
  4.  
  5. def dashboard
  6. end
  7.  
  8.  
  9.  
  10. private
  11.  
  12. def portfolio_overview
  13. # {"Bitcoin"=>{"BTC"=>3443.67}, "Ethereum"=>{"ETH"=>200.04}}
  14. live_prices = crypto_service.call_current_prices
  15.  
  16.  
  17. # ---- THIS IS WHERE WE CALCULATE THE CURRENT PRICE FOR EVERY
  18. # ---- CRYPTOCURRENCY OWNED BY THE USER
  19. portfolio_price_hash = {}
  20. portfolio_price_hash_of_hashes = {}
  21.  
  22. @portfolios.each do |portfolio|
  23. portfolio_price_hash[portfolio.cryptocurrency.ticker_code] = live_prices[portfolio.cryptocurrency.ticker_name.capitalize][portfolio.cryptocurrency.ticker_code]
  24. # {"Bitcoin" => {"BTC"=>3443.67}, "Ethereum" => {"ETH"=>200.04}}
  25. portfolio_price_hash_of_hashes[portfolio.cryptocurrency.ticker_name] = portfolio_price_hash
  26. end
  27.  
  28. # ---- THIS IS WHERE WE CALCULATE THE CURRENT VALUE HELD BY EVERY
  29. # ---- CRYPTOCURRENCY OWNED BY THE USER
  30.  
  31. ind_price_crypto_hash = {}
  32.  
  33. @portfolios.each do |portfolio|
  34. live_price_per_crypto = portfolio_price_hash_of_hashes[portfolio.cryptocurrency.ticker_name][portfolio.cryptocurrency.ticker_code]
  35. crypto_armound_held = portfolio.crypto_amount_held
  36. ind_price_crypto_hash[portfolio.cryptocurrency.ticker_code] = live_price_per_crypto * crypto_armound_held
  37. end
  38.  
  39. # ---- THIS IS WHERE WE CALCULATE THE CURRENT
  40. # ---- THE CURRENT PROFIT / LOSS
  41.  
  42. profit_or_loss_array = []
  43. profit_or_loss_hash_of_arrays = {}
  44.  
  45. @portfolios.each do |portfolio|
  46. portfolio.cryptocurrency.trades.buy.each do |trade|
  47. price_paid = trade.fiat_amount_cents
  48. price_now = portfolio_price_hash_of_hashes[trade.cryptocurrency.ticker_name][trade.cryptocurrency.ticker_code] * trade.cryptocurrency_amount
  49. profit_or_loss_array << price_now - price_paid
  50. profit_or_loss_hash_of_arrays[trade.cryptocurrency.ticker_name] = profit_or_loss_array
  51. end
  52. portfolio.cryptocurrency.trades.sell.each do |trade|
  53. price_paid = trade.fiat_amount_cents
  54. price_now = portfolio_price_hash_of_hashes[trade.cryptocurrency.ticker_name][trade.cryptocurrency.ticker_code] * trade.cryptocurrency_amount
  55. profit_or_loss_array << price_now - price_paid
  56. # THIS RETURNS SOMETHING LIKE THE BELOW EXAMPLE
  57. # {"Ripple"=>[-0.999985055e5, -0.3988044e3, -0.11999994022e7, -0.3399487e7, -0.1999994246e6],
  58. # "Stellar"=>[-0.999985055e5, -0.3988044e3, -0.11999994022e7, -0.3399487e7, -0.1999994246e6],
  59. # "Cardano"=>[-0.999985055e5, -0.3988044e3, -0.11999994022e7, -0.3399487e7, -0.1999994246e6]}
  60. profit_or_loss_hash_of_arrays[trade.cryptocurrency.ticker_name] = profit_or_loss_array
  61. end
  62. end
  63. end
  64.  
  65. def top_five_traded
  66. # this returns an hash with the top5 most traded by volume
  67. # across all the different exchanges
  68. # Also, it comes sorted from the API
  69. # ex: {:Bitcoin=>"BTC", :Ethereum=>"ETH", :EOS=>"EOS", :XRP=>"XRP", :ZCash=>"ZEC"}
  70. @top_five = crypto_service.call_top5_traded
  71. end
  72.  
  73. def latest_news
  74. # This returns an array of hashes with news articles
  75. @latest_news = crypto_service.call_latest_news
  76. end
  77.  
  78. def top5_winners
  79. # this returns an hash like this
  80. # {"REP"=>8.108108108108116, "WAVES"=>6.535947712418292, "BNB"=>4.228486646884287, "BAT"=>2.183984116479158, "QTUM"=>1.8749999999999878}
  81. @top5_winners = crypto_service.call_top5_winners
  82. end
  83.  
  84. def top5_losers
  85. # this returns an hash like this
  86. # {"DIG"=>-22.26890756302521, "BCH"=>-4.433786825878439, "ZEC"=>-4.110072323161049, "XLM"=>-3.729401561144838, "LSK"=>-3.361344537815129}
  87. @top5_losers = crypto_service.call_top5_losers
  88. end
  89.  
  90. def crypto_service
  91. # API MEMOIZATION CODE
  92. @crypto_service ||= CryptoCompareService.new
  93. end
  94.  
  95. def set_portfolio
  96. @portfolios = current_user.portfolios
  97. end
  98. end
Add Comment
Please, Sign In to add comment