Sha256: 32691a215646ffd3606dc87e7a196e9e570e443a4a4123ff2a4e1d1974ac4f9e

Contents?: true

Size: 1.7 KB

Versions: 2

Compression:

Stored size: 1.7 KB

Contents

# frozen_string_literal: true

module Capwatch
  class Fund
    attr_accessor :provider, :config, :coins, :positions

    def initialize(provider:, config:)
      @provider = provider
      @config = config
      @positions = config.positions
      @coins = config.coins
      build
    end

    def [](symbol)
      coins.find { |coin| coin.symbol == symbol }
    end

    def value_btc
      coins.map(&:value_btc).sum
    end

    def value_usd
      coins.map(&:value_usd).sum
    end

    def value_eth
      coins.map(&:value_eth).sum
    end

    def percent_change_1h
      coins.map { |coin| coin.percent_change_1h * coin.distribution }.sum
    end

    def percent_change_24h
      coins.map { |coin| coin.percent_change_24h * coin.distribution }.sum
    end

    def percent_change_7d
      coins.map { |coin| coin.percent_change_7d * coin.distribution }.sum
    end

    def build
      assign_quantity
      assign_prices
      distribution
    end

    def assign_quantity
      coins.each do |coin|
        coin.quantity = positions[coin.symbol]
      end
    end

    def assign_prices
      coins.each do |coin|
        provider.update_coin(coin)
      end
    end

    def distribution
      coins.each do |coin|
        coin.distribution = coin.value_btc / value_btc
      end
    end

    def serialize
      coins.map { |coin| coin.serialize }.to_json
    end

    def fund_totals
      {
        value_usd: value_usd,
        value_btc: value_btc,
        value_eth: value_eth,
        percent_change_24h: percent_change_24h,
        percent_change_7d: percent_change_7d
      }
    end

    def console_table
      Console.new(name = config.name, body = serialize, totals = fund_totals).draw_table
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
capwatch-0.2.5 lib/capwatch/fund.rb
capwatch-0.2.4 lib/capwatch/fund.rb