class CoinSync::Importers::ArkVoting

Constants

ARK
BASE_URL
EPOCH_TIME

Public Class Methods

new(config, params = {}) click to toggle source
Calls superclass method
# File lib/coinsync/importers/ark_voting.rb, line 31
def initialize(config, params = {})
  super
  @address = params['address']
end

Public Instance Methods

can_import?(type) click to toggle source
# File lib/coinsync/importers/ark_voting.rb, line 36
def can_import?(type)
  @address && [:balances, :transactions].include?(type)
end
import_balances() click to toggle source
# File lib/coinsync/importers/ark_voting.rb, line 72
def import_balances
  response = make_request('/getAccount', address: @address)

  case response
  when Net::HTTPSuccess
    json = JSON.parse(response.body)

    if json['success'] != true || !json['balance']
      raise "Ark importer: Invalid response: #{response.body}"
    end

    [Balance.new(ARK, available: BigDecimal.new(json['balance']) / 100_000_000)]
  when Net::HTTPBadRequest
    raise "Ark importer: Bad request: #{response}"
  else
    raise "Ark importer: Bad response: #{response}"
  end
end
import_transactions(filename) click to toggle source
# File lib/coinsync/importers/ark_voting.rb, line 40
def import_transactions(filename)
  offset = 0
  limit = 50
  transactions = []

  loop do
    response = make_request('/getTransactionsByAddress', address: @address, limit: limit, offset: offset)

    case response
    when Net::HTTPSuccess
      json = JSON.parse(response.body)

      if json['success'] != true || !json['transactions']
        raise "Ark importer: Invalid response: #{response.body}"
      end

      break if json['transactions'].empty?

      rewards = json['transactions'].select { |tx| tx['senderDelegate'] }
      transactions.concat(rewards)

      offset += limit
    when Net::HTTPBadRequest
      raise "Ark importer: Bad request: #{response}"
    else
      raise "Ark importer: Bad response: #{response}"
    end
  end

  File.write(filename, JSON.pretty_generate(transactions) + "\n")
end
read_transaction_list(source) click to toggle source
# File lib/coinsync/importers/ark_voting.rb, line 91
def read_transaction_list(source)
  json = JSON.parse(source.read)
  transactions = []

  json.each do |hash|
    entry = HistoryEntry.new(hash)

    transactions << Transaction.new(
      exchange: 'Ark voting',
      time: entry.timestamp,
      bought_amount: entry.amount,
      bought_currency: ARK,
      sold_amount: BigDecimal.new(0),
      sold_currency: FiatCurrency.new(nil)
    )
  end

  transactions.reverse
end

Private Instance Methods

make_request(path, params = {}) click to toggle source
# File lib/coinsync/importers/ark_voting.rb, line 113
def make_request(path, params = {})
  url = URI(BASE_URL + path)
  url.query = URI.encode_www_form(params)

  Request.get(url)
end