Initial commit
This commit is contained in:
55
lib/peatio/opendax_cloud/client.rb
Normal file
55
lib/peatio/opendax_cloud/client.rb
Normal file
@@ -0,0 +1,55 @@
|
||||
module OpendaxCloud
|
||||
class Client
|
||||
Error = Class.new(StandardError)
|
||||
|
||||
class ConnectionError < Error; end
|
||||
class MissingEnvError < Error; end
|
||||
|
||||
def initialize(endpoint, idle_timeout: 5)
|
||||
@platform_id = ENV.fetch('PLATFORM_ID') do
|
||||
raise MissingEnvError, :platform_id
|
||||
end
|
||||
|
||||
@endpoint = URI.parse(endpoint)
|
||||
@private_key = OpenSSL::PKey.read(Base64.urlsafe_decode64(ENV.fetch('PEATIO_JWT_PRIVATE_KEY')))
|
||||
@path = @endpoint.path.empty? ? "/" : @endpoint.path
|
||||
@idle_timeout = idle_timeout
|
||||
end
|
||||
|
||||
def rest_api(verb, path, data = nil)
|
||||
args = [@endpoint.to_s + path]
|
||||
jwt = JWT.encode(data, @private_key, 'RS256')
|
||||
|
||||
headers = { 'Content-Type' => 'application/json',
|
||||
'Authorization' => 'Bearer ' + jwt,
|
||||
'Accept' => 'application/json',
|
||||
'PlatformID' => @platform_id }
|
||||
|
||||
if data.present?
|
||||
if %i[post put patch].include?(verb)
|
||||
args << data.compact.to_json << headers
|
||||
else
|
||||
args << data.compact << headers
|
||||
end
|
||||
else
|
||||
args << data << headers
|
||||
end
|
||||
|
||||
response = connection.send(verb, *args)
|
||||
response.assert_success!
|
||||
JSON.parse(response.body)
|
||||
rescue Faraday::Error => e
|
||||
raise ConnectionError, e
|
||||
rescue StandardError => e
|
||||
raise Error, e
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def connection
|
||||
@connection ||= Faraday.new(@endpoint) do |f|
|
||||
f.adapter :net_http_persistent, pool_size: 5, idle_timeout: @idle_timeout
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
67
lib/peatio/opendax_cloud/wallet.rb
Normal file
67
lib/peatio/opendax_cloud/wallet.rb
Normal file
@@ -0,0 +1,67 @@
|
||||
module OpendaxCloud
|
||||
class Wallet < Peatio::Wallet::Abstract
|
||||
Error = Class.new(StandardError)
|
||||
DEFAULT_FEATURES = { skip_deposit_collection: true }.freeze
|
||||
|
||||
def initialize(custom_features = {})
|
||||
@features = DEFAULT_FEATURES.merge(custom_features).slice(*SUPPORTED_FEATURES)
|
||||
@settings = {}
|
||||
end
|
||||
|
||||
def configure(settings = {})
|
||||
# Clean client state during configure.
|
||||
@client = nil
|
||||
|
||||
@settings.merge!(settings.slice(*SUPPORTED_SETTINGS))
|
||||
|
||||
@wallet = @settings.fetch(:wallet) do
|
||||
raise Peatio::Wallet::MissingSettingError, :wallet
|
||||
end.slice(:uri, :address)
|
||||
|
||||
@currency = @settings.fetch(:currency) do
|
||||
raise Peatio::Wallet::MissingSettingError, :currency
|
||||
end.slice(:id, :base_factor, :options)
|
||||
end
|
||||
|
||||
def create_address!(_options = {})
|
||||
response = client.rest_api(:post, '/address/new', {
|
||||
currency_id: currency_id
|
||||
})
|
||||
|
||||
{ address: response['address'], details: response.except('address') }
|
||||
rescue OpendaxCloud::Client::Error => e
|
||||
raise Peatio::Wallet::ClientError, e
|
||||
end
|
||||
|
||||
def create_transaction!(transaction)
|
||||
response = client.rest_api(:post, '/tx/send', {
|
||||
currency_id: currency_id,
|
||||
to: transaction.to_address,
|
||||
amount: transaction.amount,
|
||||
options: transaction.options
|
||||
})
|
||||
transaction.options = response['options']
|
||||
transaction
|
||||
rescue OpendaxCloud::Client::Error => e
|
||||
raise Peatio::Wallet::ClientError, e
|
||||
end
|
||||
|
||||
def load_balance!
|
||||
response = client.rest_api(:post, '/address/balance', {
|
||||
currency_id: currency_id
|
||||
}.compact).fetch('balance')
|
||||
|
||||
response.to_d
|
||||
rescue OpendaxCloud::Client::Error => e
|
||||
raise Peatio::Wallet::ClientError, e
|
||||
end
|
||||
|
||||
def currency_id
|
||||
@currency.fetch(:id)
|
||||
end
|
||||
|
||||
def client
|
||||
@client ||= Client.new(@wallet.fetch(:uri), idle_timeout: 1)
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user