Class: CoinSync::Importers::ArkVoting

Inherits:
Base
  • Object
show all
Defined in:
lib/coinsync/importers/ark_voting.rb

Defined Under Namespace

Classes: HistoryEntry

Constant Summary collapse

BASE_URL =
"https://explorer.dafty.net/api"
EPOCH_TIME =
Time.parse('2017-03-21 13:00 UTC')
ARK =
CryptoCurrency.new('ARK')

Instance Method Summary collapse

Methods inherited from Base

#can_build?, register_commands, register_importer, registered_commands

Constructor Details

#initialize(config, params = {}) ⇒ ArkVoting

Returns a new instance of ArkVoting.



31
32
33
34
# File 'lib/coinsync/importers/ark_voting.rb', line 31

def initialize(config, params = {})
  super
  @address = params['address']
end

Instance Method Details

#can_import?(type) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/coinsync/importers/ark_voting.rb', line 36

def can_import?(type)
  @address && [:balances, :transactions].include?(type)
end

#import_balancesObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# 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) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# 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) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# 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