# frozen_string_literal: true module Cryptum # This module is used to Interact with the APIs module API # Module specifically related to orders history retrieval. module Portfolio # Obtain latest order history public_class_method def self.get(opts = {}) option_choice = opts[:option_choice] env = opts[:env] crypto = opts[:crypto] fiat = opts[:fiat] fiat_portfolio_file = opts[:fiat_portfolio_file] event_notes = opts[:event_notes] # Retrieve Exchange Rates exchange_rates_resp = Cryptum::API::ExchangeRates.get( option_choice: option_choice, env: env ) exchange_rates = exchange_rates_resp[:data][:rates] portfolio_complete_arr = Cryptum::API::Rest.call( option_choice: option_choice, env: env, http_method: :GET, api_call: '/accounts', event_notes: event_notes ) portfolio_complete_arr ||= [] all_products = portfolio_complete_arr.select do |products| products if products[:balance].to_f.positive? end total_holdings = 0.00 all_products.each do |product| currency = product[:currency].to_sym this_exchange_rate = exchange_rates[currency].to_f total_holdings += product[:balance].to_f / this_exchange_rate end crypto_portfolio = portfolio_complete_arr.select do |product| product if product[:currency] == crypto end fiat_portfolio = portfolio_complete_arr.select do |product| product if product[:currency] == fiat end fiat_portfolio.last[:total_holdings] = format( '%0.8f', total_holdings ) File.write( fiat_portfolio_file, JSON.pretty_generate(fiat_portfolio) ) crypto_portfolio rescue Interrupt, StandardError => e Cryptum::Log.append(level: :error, msg: e, which_self: self) end # Display Usage for this Module public_class_method def self.help puts "USAGE: fees = #{self}.get( env: 'required - Coinbase::Option::Environment.get Object', option_choice: 'required - Coinbase::Option::Choice Object' ) " end end end end