diff --git a/README.md b/README.md index 5527ba2..e5718d4 100644 --- a/README.md +++ b/README.md @@ -1,28 +1,150 @@ -# Peatio +# Peatio Core -Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/peatio`. To experiment with that code, run `bin/console` for an interactive prompt. - -TODO: Delete this and the text above, and describe your gem +Peatio Core is a part of Peatio cryptocurrency exchange platform exposed as a Ruby gem and implementing some authentication, database, AMQP and other critical stuff. ## Installation Add this line to your application's Gemfile: ```ruby -gem 'peatio' +gem 'peatio', '~> 2.4.0' ``` And then execute: - $ bundle + $ bundle install Or install it yourself as: $ gem install peatio +## Configuration variables + +| Name | Description | Default value | +|-------------------------|--------------------------------------|--------------------------------| +| `LOG_LEVEL` | Logger level | info | +| `RABBITMQ_HOST` | | 0.0.0.0 | +| `RABBITMQ_PORT` | | 5672 | +| `RABBITMQ_USER` | | guest | +| `RABBITMQ_PASSWORD` | | guest | +| `DATABASE_HOST` | | localhost | +| `DATABASE_USER` | | root | +| `DATABASE_PASS` | | Empty password used by default | +| `DATABASE_PORT` | | 3306 | +| `DATABASE_NAME` | | peatio_development | +| `RANGER_HOST` | WebSocket server binding host | 0.0.0.0 | +| `RANGER_PORT` | WebSocket server binding port | 8081 | +| `METRICS_HOST` | Thin Web server metrics binding host | 0.0.0.0 | +| `METRICS_PORT` | Thin Web server metrics binding port | 8082 | +| `JWT_ISSUER` | | | +| `JWT_AUDIENCE` | | | +| `JWT_ALGORITHM` | | RS256 | +| `JWT_DEFAULT_LEEWAY` | | | +| `JWT_ISSUED_AT_LEEWAY` | | | +| `JWT_EXPIRATION_LEEWAY` | | | +| `JWT_NOT_BEFORE_LEEWAY` | | | +| `JWT_PUBLIC_KEY` | Base64-encoded RSA public key | | +| `WEBSOCKET_HOST` | Used for testing | 0.0.0.0 | +| `WEBSOCKET_PORT` | Used for testing | 13579 | + ## Usage -TODO: Write usage instructions here +### Start Ranger WebSocket server + +Command: +``` +JWT_PUBLIC_KEY=RSA_PUBLIC_KEY_BASE64 bundle exec peatio service start ranger +``` + +Check out `bundle exec peatio service start ranger --help` for more information about available options. + +You also may need to adjust environment variables (see above). + +### Generate RSA keypair and print or save to file + +Command: +``` +bundle exec peatio security keygen --print +``` + +Result: +``` +-----BEGIN RSA PRIVATE KEY----- +MIIEpAIBAAKCAQEAv5QEw1nKyex1+MKMJM+CC1qhS6er3dpRZYxQvwK1l3shODgq +haAuJuxLK/RHzvzfsrtGtkmGv6wvJtsdM6muNQo8Jpqy9yXyY1d+HGq1sLgcI6LE ++/hU35JbwXkF5JT4oep0NCM4YNOlZB1Kd6DwdeHPk079DEMxpFo0KDiFmdnRakVc +dl4tki2ZUkeI5uNFFFUWjDsU39guIOIeKOeEANi08FBgpcfbUhM1a/i4VOHXBFfp +nk91lB1TANiy0rZlUhfIdT7/uiokDc06Fnd8XZErtxEiCXueNU9vRZLE/Rqms2Hm +ZKumY8aQ/nHFzw+DfxfjT72HuCH/8S1mSI19YQIDAQABAoIBAA0gDPt9SWFwK9zx +JzzWYqh4exh90k6OyIjWhimv+9T3AJ2ja3ZgNQlBoxzNzaWmuYS7Q+r1OqAjIc/d +rzB87YyAuQJEEwLPvL2FnwQ/Y1dKJdNjMW+54ca3dkXZDshMVIRzVabEyvYHSguD +3jg39IF/CQOko72VVO9Xpr8isDSME1zbfLDdA0K7O2wv1HrrvXvWc5ji9LtLnm3h +n+ru4VDNXSrtsQS44r/NeYcjZ72VC8JfTmCZMlHY4uwSrfOtXMHq1/wTE5dPlDA9 +6m49AzO6fPV8Q2kEW45XsHa3zZlcKi5vbrxqQoWKOUXbod/EL73OHyHmeK6QAbZl +DQtj2uECgYEA7OY+vVYj/dY2bRCWlfhfDtXHUbXOTE6jSfYYuCu9zmN+CILyS4j1 +GL5IrhF4QKdrsbxudkp3V2zP3CINjZfseP+6Di/fMc8XBHFwrk27jGcEqwGtG/ss +LLaQf4rEJ8JZcNLF30lHk+NzfMiW5pc0NAf0dS73ed2hmcnBRX0yrIU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nd8 +XZErtxEiCXueNU9vRZLE/Rqms2HmZKumY8aQ/nHFzw+DfxfjT72HuCH/8S1mSI19 +YQIDAQAB +-----END PUBLIC KEY----- +-----BASE64 ENCODED----- +LS0tLS1CRUdJTiBQVUJMSUMgS0VZLS0tLS0KTUlJQklqQU5CZ2txaGtpRzl3MEJBUUVGQUFPQ0FROEFNSUlCQ2dLQ0FRRUF2NVFFdzFuS3lleDErTUtNSk0rQwpDMXFoUzZlcjNkcFJaWXhRdndLMWwzc2hPRGdxaGFBdUp1eExLL1JIenZ6ZnNydEd0a21HdjZ3dkp0c2RNNm11Ck5RbzhKcHF5OXlYeVkxZCtIR3Exc0xnY0k2TEUrL2hVMzVKYndYa0Y1SlQ0b2VwME5DTTRZTk9sWkIxS2Q2RHcKZGVIUGswNzlERU14cEZvMEtEaUZtZG5SYWtWY2RsNHRraTJaVWtlSTV1TkZGRlVXakRzVTM5Z3VJT0llS09lRQpBTmkwOEZCZ3BjZmJVaE0xYS9pNFZPSFhCRmZwbms5MWxCMVRBTml5MHJabFVoZklkVDcvdWlva0RjMDZGbmQ4ClhaRXJ0eEVpQ1h1ZU5VOXZSWkxFL1JxbXMySG1aS3VtWThhUS9uSEZ6dytEZnhmalQ3Mkh1Q0gvOFMxbVNJMTkKWVFJREFRQUIKLS0tLS1FTkQgUFVCTElDIEtFWS0tLS0tCg== +``` + +Check out `bundle exec peatio security keygen --help` for more information. + +### Create database + +``` +bundle exec peatio db create +``` + +### Run database migrations + +This command will create important tables related to account management, trades and orders: `operations`, `orders`, `trades`. + +``` +bundle exec peatio db migrate +``` + +### Publish events to RabbitMQ (debugging purposes) + +Command: +``` +bundle exec peatio inject peatio_events +``` + +Result: +``` +D, [2019-12-31T16:20:07.391515 #38502] DEBUG -- : published event to public.global.tickers +D, [2019-12-31T16:20:07.391739 #38502] DEBUG -- : published event to public.eurusd.update +D, [2019-12-31T16:20:07.391857 #38502] DEBUG -- : published event to private.IDABC0000001.order +D, [2019-12-31T16:20:07.391961 #38502] DEBUG -- : published event to private.IDABC0000001.trade +D, [2019-12-31T16:20:07.392056 #38502] DEBUG -- : published event to private.IDABC0000002.trade +D, [2019-12-31T16:20:07.392170 #38502] DEBUG -- : published event to public.eurusd.trades +D, [2019-12-31T16:20:07.392266 #38502] DEBUG -- : published event to public.eurusd.ob-inc +D, [2019-12-31T16:20:07.392378 #38502] DEBUG -- : published event to public.eurusd.ob-snap +D, [2019-12-31T16:20:07.392473 #38502] DEBUG -- : published event to public.eurusd.ob-inc +D, [2019-12-31T16:20:07.392560 #38502] DEBUG -- : published event to public.eurusd.ob-inc +``` ## Development @@ -32,4 +154,4 @@ To install this gem onto your local machine, run `bundle exec rake install`. To ## Contributing -Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/peatio. +Bug reports and pull requests are welcome on GitHub at https://github.com/openware/peatio-core. diff --git a/Rakefile b/Rakefile index c92b11e..b6ae734 100644 --- a/Rakefile +++ b/Rakefile @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "bundler/gem_tasks" require "rspec/core/rake_task" diff --git a/lib/peatio/adapter_registry.rb b/lib/peatio/adapter_registry.rb index 98cd34e..a523c6a 100644 --- a/lib/peatio/adapter_registry.rb +++ b/lib/peatio/adapter_registry.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Peatio class AdapterRegistry Error = Class.new(StandardError) diff --git a/lib/peatio/auth/error.rb b/lib/peatio/auth/error.rb index 4dc15f1..693835f 100644 --- a/lib/peatio/auth/error.rb +++ b/lib/peatio/auth/error.rb @@ -1,5 +1,7 @@ +# frozen_string_literal: true + module Peatio::Auth - # Error repesent all errors that can be returned from Auth module. + # Error represent all errors that can be returned from Auth module. class Error < Peatio::Error # @return [String, JWT::*] Reason store underlying reason for given error. # @@ -11,7 +13,7 @@ def initialize(reason = nil) super( code: 2001, - text: "Authorization failed".tap { |t| t << ": #{reason}" if reason }, + text: %[Authorization failed#{": #{reason}" if reason.present?}] ) end end diff --git a/lib/peatio/auth/jwt_authenticator.rb b/lib/peatio/auth/jwt_authenticator.rb index 39bebde..8cd28d9 100644 --- a/lib/peatio/auth/jwt_authenticator.rb +++ b/lib/peatio/auth/jwt_authenticator.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "jwt" require_relative "error" diff --git a/lib/peatio/block.rb b/lib/peatio/block.rb index 60153f3..9eb5be1 100644 --- a/lib/peatio/block.rb +++ b/lib/peatio/block.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Peatio #:nodoc: # This class represents blockchain block which contains transactions. diff --git a/lib/peatio/blockchain/abstract.rb b/lib/peatio/blockchain/abstract.rb index e0ab7e6..02a6b11 100644 --- a/lib/peatio/blockchain/abstract.rb +++ b/lib/peatio/blockchain/abstract.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Peatio #:nodoc: module Blockchain #:nodoc: diff --git a/lib/peatio/blockchain/error.rb b/lib/peatio/blockchain/error.rb index 11f5a15..3fda5ff 100644 --- a/lib/peatio/blockchain/error.rb +++ b/lib/peatio/blockchain/error.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Peatio module Blockchain Error = Class.new(StandardError) diff --git a/lib/peatio/blockchain/registry.rb b/lib/peatio/blockchain/registry.rb index 8ff096b..6db188c 100644 --- a/lib/peatio/blockchain/registry.rb +++ b/lib/peatio/blockchain/registry.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "peatio/adapter_registry" module Peatio diff --git a/lib/peatio/command/base.rb b/lib/peatio/command/base.rb index be780cb..f71ca2c 100644 --- a/lib/peatio/command/base.rb +++ b/lib/peatio/command/base.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Peatio::Command class Base < Clamp::Command def say(str) diff --git a/lib/peatio/command/db.rb b/lib/peatio/command/db.rb index 217a1ee..4a41646 100644 --- a/lib/peatio/command/db.rb +++ b/lib/peatio/command/db.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Peatio::Command::DB class Create < Peatio::Command::Base def execute diff --git a/lib/peatio/command/inject.rb b/lib/peatio/command/inject.rb index 1adc236..62970bf 100644 --- a/lib/peatio/command/inject.rb +++ b/lib/peatio/command/inject.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Peatio::Command class Inject < Peatio::Command::Base class PeatioEvents < Peatio::Command::Base diff --git a/lib/peatio/command/root.rb b/lib/peatio/command/root.rb index ed2c6f0..5dda4e0 100644 --- a/lib/peatio/command/root.rb +++ b/lib/peatio/command/root.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "peatio/command/base" require "peatio/command/service" require "peatio/command/db" diff --git a/lib/peatio/error.rb b/lib/peatio/error.rb index fd8d16c..fa1a88a 100644 --- a/lib/peatio/error.rb +++ b/lib/peatio/error.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class Peatio::Error < ::StandardError @@default_code = 2000 diff --git a/lib/peatio/executor.rb b/lib/peatio/executor.rb index 0a3f448..7bb8967 100644 --- a/lib/peatio/executor.rb +++ b/lib/peatio/executor.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "mysql2" require "benchmark" diff --git a/lib/peatio/security/key_generator.rb b/lib/peatio/security/key_generator.rb index 9f872d4..0ed2589 100644 --- a/lib/peatio/security/key_generator.rb +++ b/lib/peatio/security/key_generator.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'fileutils' module Peatio::Security diff --git a/lib/peatio/sql/client.rb b/lib/peatio/sql/client.rb index 3878599..181e607 100644 --- a/lib/peatio/sql/client.rb +++ b/lib/peatio/sql/client.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Peatio::Sql class Client attr_accessor :client, :config diff --git a/lib/peatio/sql/schema.rb b/lib/peatio/sql/schema.rb index 43ed239..7602817 100644 --- a/lib/peatio/sql/schema.rb +++ b/lib/peatio/sql/schema.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Peatio::Sql class Schema attr_accessor :client @@ -30,7 +32,7 @@ def create_tables(options = {}) statements << "DROP TABLE IF EXISTS `orders`;" if options[:drop_if_exists] statements << <