Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 130 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -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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5uNFFFUWjDsU39guIOIeKOeE
ANi08FBgpcfbUhM1a/i4VOHXBFfpnk91lB1TANiy0rZlUhfIdT7/uiokDc06Fnd8
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

Expand All @@ -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.
2 changes: 2 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require "bundler/gem_tasks"
require "rspec/core/rake_task"

Expand Down
2 changes: 2 additions & 0 deletions lib/peatio/adapter_registry.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Peatio
class AdapterRegistry
Error = Class.new(StandardError)
Expand Down
6 changes: 4 additions & 2 deletions lib/peatio/auth/error.rb
Original file line number Diff line number Diff line change
@@ -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.
#
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions lib/peatio/auth/jwt_authenticator.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require "jwt"

require_relative "error"
Expand Down
2 changes: 2 additions & 0 deletions lib/peatio/block.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Peatio #:nodoc:

# This class represents blockchain block which contains transactions.
Expand Down
2 changes: 2 additions & 0 deletions lib/peatio/blockchain/abstract.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Peatio #:nodoc:
module Blockchain #:nodoc:

Expand Down
2 changes: 2 additions & 0 deletions lib/peatio/blockchain/error.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Peatio
module Blockchain
Error = Class.new(StandardError)
Expand Down
2 changes: 2 additions & 0 deletions lib/peatio/blockchain/registry.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require "peatio/adapter_registry"

module Peatio
Expand Down
2 changes: 2 additions & 0 deletions lib/peatio/command/base.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Peatio::Command
class Base < Clamp::Command
def say(str)
Expand Down
2 changes: 2 additions & 0 deletions lib/peatio/command/db.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Peatio::Command::DB
class Create < Peatio::Command::Base
def execute
Expand Down
2 changes: 2 additions & 0 deletions lib/peatio/command/inject.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Peatio::Command
class Inject < Peatio::Command::Base
class PeatioEvents < Peatio::Command::Base
Expand Down
2 changes: 2 additions & 0 deletions lib/peatio/command/root.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require "peatio/command/base"
require "peatio/command/service"
require "peatio/command/db"
Expand Down
2 changes: 2 additions & 0 deletions lib/peatio/error.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class Peatio::Error < ::StandardError
@@default_code = 2000

Expand Down
2 changes: 2 additions & 0 deletions lib/peatio/executor.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require "mysql2"
require "benchmark"

Expand Down
2 changes: 2 additions & 0 deletions lib/peatio/security/key_generator.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'fileutils'

module Peatio::Security
Expand Down
2 changes: 2 additions & 0 deletions lib/peatio/sql/client.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Peatio::Sql
class Client
attr_accessor :client, :config
Expand Down
8 changes: 5 additions & 3 deletions lib/peatio/sql/schema.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Peatio::Sql
class Schema
attr_accessor :client
Expand Down Expand Up @@ -30,7 +32,7 @@ def create_tables(options = {})

statements << "DROP TABLE IF EXISTS `orders`;" if options[:drop_if_exists]
statements << <<EOF
CREATE TABLE IF NOT EXISTS`orders` (
CREATE TABLE IF NOT EXISTS `orders` (
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`uid` INT(11) UNSIGNED NOT NULL,
`bid` VARCHAR(5) NOT NULL,
Expand All @@ -44,7 +46,7 @@ def create_tables(options = {})
`created_at` DATETIME NOT NULL,
`updated_at` DATETIME NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
) ENGINE = InnoDB DEFAULT CHARSET=utf8;
EOF

statements << "DROP TABLE IF EXISTS `trades`;" if options[:drop_if_exists]
Expand All @@ -61,7 +63,7 @@ def create_tables(options = {})
`created_at` datetime NOT NULL,
`updated_at` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
) ENGINE = InnoDB DEFAULT CHARSET=utf8;
EOF
statements.each do |statement|
puts statement
Expand Down
2 changes: 2 additions & 0 deletions lib/peatio/transaction.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'active_support/concern'
require 'active_support/core_ext/string/inquiry'
require 'active_support/core_ext/object/blank'
Expand Down
2 changes: 2 additions & 0 deletions lib/peatio/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Peatio
VERSION = "2.4.0-alpha"
end
2 changes: 2 additions & 0 deletions lib/peatio/wallet/abstract.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Peatio
module Wallet
# @abstract Represents basic wallet interface.
Expand Down
2 changes: 2 additions & 0 deletions lib/peatio/wallet/error.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Peatio
module Wallet
Error = Class.new(StandardError)
Expand Down
2 changes: 2 additions & 0 deletions lib/peatio/wallet/registry.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require "peatio/adapter_registry"

module Peatio
Expand Down
1 change: 0 additions & 1 deletion spec/helpers/socket_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# encoding: UTF-8
# frozen_string_literal: true

require "em-spec/rspec"
Expand Down
2 changes: 2 additions & 0 deletions spec/peatio/adapter_registry_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

WalletAdapter = Class.new(Peatio::Blockchain::Abstract)
BlockchainAdapter = Class.new(Peatio::Wallet::Abstract)

Expand Down
2 changes: 2 additions & 0 deletions spec/peatio/auth/jwt_authenticator_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

describe Peatio::Auth::JWTAuthenticator do

let(:rsa_private) { OpenSSL::PKey::RSA.generate(2048) }
Expand Down
2 changes: 2 additions & 0 deletions spec/peatio/ranger/web_socket_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require "em-spec/rspec"
require "bunny-mock"

Expand Down
2 changes: 2 additions & 0 deletions spec/peatio/security/key_generator_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

describe Peatio::Security::KeyGenerator do
context "generate key pair" do
let(:key_pair) { Peatio::Security::KeyGenerator.new}
Expand Down
2 changes: 2 additions & 0 deletions spec/peatio/transaction_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'pry-byebug'

describe Peatio::Transaction do
Expand Down
2 changes: 2 additions & 0 deletions spec/peatio_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

describe Peatio do
it "has a version number" do
expect(Peatio::VERSION).not_to be nil
Expand Down
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require "bundler/setup"
require "peatio"

Expand Down