Signed-off-by: Adrian Hinz <adhinz@gmail.com>

This commit is contained in:
Adrian Hinz 2017-11-29 23:17:34 +01:00
parent fbc913d485
commit bb46a3ce69
20 changed files with 240 additions and 1 deletions

View File

@ -1,4 +1,21 @@
class HomeController < ApplicationController
def index
end
def seed
market = Market.where(name: 'BitBay').first
if market.blank?
market = Market.new(name: 'BitBay', url: 'https://bitbay.net')
market.save
end
bit_bay_currencies = [['BTC', 'Bitcoin'],['BCC', 'Bitcoin Cash'], ['ETH','Ethereum'], ['LSK', 'Lisk'], ['LTC', 'Litecoin'], ['DASH', 'Dash'], ['GAME', 'GameCredits']]
for bbc in bit_bay_currencies
curr = Currency.where('name = ?', bbc[0]).first
if curr.blank?
curr = Currency.new(name: bbc[0], description: bbc[1], market_id: market.id)
curr.save
end
end
end
end

View File

@ -0,0 +1,3 @@
class BitBayTrade < ApplicationRecord
belongs_to :currency
end

3
app/models/currency.rb Normal file
View File

@ -0,0 +1,3 @@
class Currency < ApplicationRecord
has_many :bit_bay_trades
end

2
app/models/market.rb Normal file
View File

@ -0,0 +1,2 @@
class Market < ApplicationRecord
end

View File

@ -0,0 +1,44 @@
#!/usr/bin/env ruby
# encoding: UTF-8
ENV["RAILS_ENV"] ||= "development"
require File.dirname(__FILE__) + "/../../config/environment"
require 'uri'
require 'net/http'
require 'json'
pause_interval = 60
start_index = '-1'
currency = Currency.where(name: 'GAME').first
while true
start_time = Time.now.to_i
bbt_index = BitBayTrade.where('currency_id = ?', currency.id).order('cast(tid as unsigned) DESC').first
unless bbt_index.blank?
start_index = bbt_index.tid
end
uri = "https://bitbay.net/API/Public/#{currency.name}PLN/trades.json?since=#{start_index}"
puts "start at #{start_index} index"
url = URI.parse(uri)
resp = Net::HTTP.get_response(url)
my_hash = JSON.parse(resp.body)
for mh in my_hash
bbt = BitBayTrade.where('currency_id = ? AND tid = ?', currency.id, mh['tid']).first
if bbt.blank?
BitBayTrade.create(currency_id: currency.id, tid: mh['tid'], date: Time.at(mh['date'].to_i), transaction_type: mh['type'], price: mh['price'], amount: mh['amount'])
#puts "#{mh['tid']} -#{mh['date']} - #{mh['price']} - #{mh['type']} - #{mh['amount']}"
end
end
end_time = Time.now.to_i
=begin
if end_time > start_time
diff_time = end_time - start_time
if diff_time < 60
puts "Sleep #{pause_interval - diff_time + 1} dla #{diff_time}"
sleep(pause_interval - diff_time + 1)
end
else
sleep(pause_interval)
end
=end
end

View File

@ -0,0 +1,13 @@
market = Market.where(name: 'BitBay').first
if market.blank?
market = Market.new(name: 'BitBay', url: 'https://bitbay.net')
market.save
end
bit_bay_currencies = [['BTC', 'Bitcoin'],['BCC', 'Bitcoin Cash'], ['ETH','Ethereum'], ['LSK', 'Lisk'], ['LTC', 'Litecoin'], ['DASH', 'Dash'], ['GAME', 'GameCredits']]
for bbc in bit_bay_currencies
curr = Currency.where('name = ?', bbc[0]).first
if curr.blank?
curr = Currency.new(name: bcc[0], description: bcc[1], market: market)
curr.save
end
end

View File

@ -15,7 +15,7 @@ default: &default
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: crypto
password: yX6txaHN8WenxrK2
host: localhost
host: 51.254.143.84
development:
<<: *default

View File

@ -1,5 +1,6 @@
Rails.application.routes.draw do
get 'home/index'
get 'home/seed'
root 'home#index'
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end

View File

@ -0,0 +1,10 @@
class CreateCurrencies < ActiveRecord::Migration[5.1]
def change
create_table :currencies do |t|
t.string :name
t.string :description
t.timestamps
end
end
end

View File

@ -0,0 +1,15 @@
class CreateBitBayTrades < ActiveRecord::Migration[5.1]
def change
create_table :bit_bay_trades do |t|
t.references :currency, foreign_key: true
t.string :tid
t.timestamp :date
t.string :type
t.float :price
t.float :amount
t.timestamps
end
add_index :bit_bay_trades, :tid
end
end

View File

@ -0,0 +1,6 @@
class AddIsActiveToCurrencies < ActiveRecord::Migration[5.1]
def change
add_column :currencies, :is_active, :boolean, default: true
add_index :currencies, :is_active
end
end

View File

@ -0,0 +1,12 @@
class CreateMarkets < ActiveRecord::Migration[5.1]
def change
create_table :markets do |t|
t.string :name
t.string :url
t.boolean :is_active, default: true
t.timestamps
end
add_index :markets, :is_active
end
end

View File

@ -0,0 +1,5 @@
class AddMarketToCurrencies < ActiveRecord::Migration[5.1]
def change
add_reference :currencies, :market, foreign_key: true
end
end

50
db/schema.rb Normal file
View File

@ -0,0 +1,50 @@
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20171129210447) do
create_table "bit_bay_trades", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.bigint "currency_id"
t.string "tid"
t.timestamp "date"
t.string "type"
t.float "price", limit: 24
t.float "amount", limit: 24
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["currency_id"], name: "index_bit_bay_trades_on_currency_id"
t.index ["tid"], name: "index_bit_bay_trades_on_tid"
end
create_table "currencies", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.string "name"
t.string "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "is_active", default: true
t.bigint "market_id"
t.index ["is_active"], name: "index_currencies_on_is_active"
t.index ["market_id"], name: "index_currencies_on_market_id"
end
create_table "markets", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.string "name"
t.string "url"
t.boolean "is_active", default: true
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["is_active"], name: "index_markets_on_is_active"
end
add_foreign_key "bit_bay_trades", "currencies"
add_foreign_key "currencies", "markets"
end

17
test/fixtures/bit_bay_trades.yml vendored Normal file
View File

@ -0,0 +1,17 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
currency: one
tid: MyString
date: 2017-11-29 21:41:04
type:
price: 1.5
amount: 1.5
two:
currency: two
tid: MyString
date: 2017-11-29 21:41:04
type:
price: 1.5
amount: 1.5

9
test/fixtures/currencies.yml vendored Normal file
View File

@ -0,0 +1,9 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
name: MyString
description: MyString
two:
name: MyString
description: MyString

11
test/fixtures/markets.yml vendored Normal file
View File

@ -0,0 +1,11 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
name: MyString
url: MyString
is_active: false
two:
name: MyString
url: MyString
is_active: false

View File

@ -0,0 +1,7 @@
require 'test_helper'
class BitBayTradeTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

View File

@ -0,0 +1,7 @@
require 'test_helper'
class CurrencyTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

View File

@ -0,0 +1,7 @@
require 'test_helper'
class MarketTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end