Class: Cryptomarket::Websocket::WalletClient

Inherits:
AuthClient show all
Defined in:
lib/cryptomarket/websocket/wallet_client.rb

Overview

WalletClient connects via websocket to cryptomarket to get wallet information of the user. Uses SHA256 as auth method and authenticates automatically.

Instance Method Summary collapse

Methods inherited from AuthClient

#authenticate, #build_auth_payload, #connect, #connected?, #wait_authed

Methods inherited from ClientBase

#close, #connect, #connected?, #get_callback_for_response, #handle, #handle_good_response, #handle_notification, #handle_response, #on_close, #on_close=, #on_connect, #on_connect=, #on_error, #on_error=, #on_open, #request, #send_subscription, #send_unsubscription, #store_callback_and_send

Constructor Details

#initialize(api_key:, api_secret:, window: nil) ⇒ WalletClient

Creates a new client and authenticates it to the server

Params

String api_key

the user api key

String api_secret

the user api secret

Integer window

Maximum difference between the creation of the request and the moment of request processing in milliseconds. Max is 60_000. Defaul is 10_000



18
19
20
21
22
23
24
25
# File 'lib/cryptomarket/websocket/wallet_client.rb', line 18

def initialize(api_key:, api_secret:, window: nil)
  super(
    url: 'wss://api.exchange.cryptomkt.com/api/3/ws/wallet',
    api_key:,
    api_secret:,
    window:,
    subscription_keys: build_subscription_hash)
end

Instance Method Details

#build_subscription_hashObject



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/cryptomarket/websocket/wallet_client.rb', line 27

def build_subscription_hash
  transaction = 'transaction'
  balance = 'balance'
  { 'subscribe_transactions' => [transaction, Args::NotificationType::COMMAND],
    'unsubscribe_transactions' => [transaction, Args::NotificationType::COMMAND],
    'transaction_update' => [transaction, Args::NotificationType::UPDATE],

    'subscribe_wallet_balances' => [balance, Args::NotificationType::COMMAND],
    'unsubscribe_wallet_balances' => [balance, Args::NotificationType::COMMAND],
    'wallet_balances' => [balance, Args::NotificationType::SNAPSHOT],
    'wallet_balance_update' => [balance, Args::NotificationType::UPDATE] }
end

#get_transactions(callback:, tx_ids: nil, types: nil, subtypes: nil, statuses: nil, currencies: nil, from: nil, till: nil, id_from: nil, id_till: nil, order_by: nil, sort: nil, limit: nil, offset: nil, group_transactions: nil) ⇒ Object

Get the transaction history of the account Important:

- The list of supported transaction types may be expanded in future versions
- Some transaction subtypes are reserved for future use and do not purport to provide any functionality on the platform
- The list of supported transaction subtypes may be expanded in future versions

Requires the “Payment information” API key Access Right

api.exchange.cryptomkt.com/#get-transactions

Params

Proc callback

A Proc called with a list of transactions

Array[String] tx_ids

Optional. List of transaction identifiers to query

Array[String] types

Optional. List of types to query. valid types are: ‘DEPOSIT’, ‘WITHDRAW’, ‘TRANSFER’ and ‘SWAP’

Array[String] subtyes

Optional. List of subtypes to query. valid subtypes are: ‘UNCLASSIFIED’, ‘BLOCKCHAIN’, ‘AIRDROP’, ‘AFFILIATE’, ‘STAKING’, ‘BUY_CRYPTO’, ‘OFFCHAIN’, ‘FIAT’, ‘SUB_ACCOUNT’, ‘WALLET_TO_SPOT’, ‘SPOT_TO_WALLET’, ‘WALLET_TO_DERIVATIVES’, ‘DERIVATIVES_TO_WALLET’, ‘CHAIN_SWITCH_FROM’, ‘CHAIN_SWITCH_TO’ and ‘INSTANT_EXCHANGE’

Array[String] statuses

Optional. List of statuses to query. valid subtypes are: ‘CREATED’, ‘PENDING’, ‘FAILED’, ‘SUCCESS’ and ‘ROLLED_BACK’

+Array currencies

Optional. List of currencies ids.

String from

Optional. Optional. Interval initial value (inclusive). The value type depends on order_by

String till

Optional. Interval end value (inclusive). The value type depends on order_by

String id_from

Optional. Interval initial value when ordering by id. Min is 0

String id_till

Optional. Interval end value when ordering by id. Min is 0

String order_by

Optional. sorting parameter.‘created_at’, ‘updated_at’, ‘last_activity_at’ ‘or ’id’.

String sort

Optional. Sort direction. ‘ASC’ or ‘DESC’. Default is ‘DESC’

Integer limit

Optional. Transactions per query. Defaul is 100. Max is 1_000

Integer offset

Optional. Default is 0. Max is 100_000

bool group_transactions

Optional. Flag indicating whether the returned transactions will be parts of a single operation. Default is false



158
159
160
161
162
163
164
165
166
167
# File 'lib/cryptomarket/websocket/wallet_client.rb', line 158

def get_transactions( # rubocop:disable Metrics/ParameterLists
  callback:, tx_ids: nil, types: nil, subtypes: nil, statuses: nil, currencies: nil, from: nil, till: nil,
  id_from: nil, id_till: nil, order_by: nil, sort: nil, limit: nil, offset: nil, group_transactions: nil
)
  request('get_transactions', callback, {
            tx_ids:, types:, subtypes:, statuses:, currencies:,
            from:, till:, id_from:, id_till:, order_by:, sort:,
            limit:, offset:, group_transactions:
          })
end

#get_wallet_balance(currency:, callback:) ⇒ Object Also known as: get_wallet_balance_of_currency, get_wallet_balance_by_currency

Get the user’s wallet balance of a currency

Requires the “Payment information” API key Access Right

api.exchange.cryptomkt.com/#request-wallet-balance

Params

String currency

The currency code to query the balance

Proc callback

A Proc called with a user balance



119
120
121
122
123
124
125
126
127
128
129
# File 'lib/cryptomarket/websocket/wallet_client.rb', line 119

def get_wallet_balance(currency:, callback:)
  interceptor = lambda { |err, balance|
    unless err.nil?
      callback.call(err, nil)
      return
    end
    balance['currency'] = currency
    callback.call(err, balance)
  }
  request('wallet_balance', interceptor, { currency: })
end

#get_wallet_balances(callback:) ⇒ Object

Get the user’s wallet balance for all currencies with balance

api.exchange.cryptomkt.com/#request-wallet-balance

Params

Proc callback

A Proc called with a list of the user balances



105
106
107
# File 'lib/cryptomarket/websocket/wallet_client.rb', line 105

def get_wallet_balances(callback:)
  request('wallet_balances', callback)
end

#subscribe_to_transactions(callback:, result_callback: nil) ⇒ Object

A transaction notification occurs each time a transaction has been changed, such as creating a transaction, updating the pending state (e.g., the hash assigned) or completing a transaction

api.exchange.cryptomkt.com/#subscribe-to-transactions

Params

Proc callback

A Proc that recieves notifications as a list of reports, and the type of notification (only ‘update’)

Proc result_callback

Optional. A Proc of two arguments, An exception and a result, called either with the exception or with the result, a boolean value, indicating the success of the subscription



48
49
50
51
52
53
# File 'lib/cryptomarket/websocket/wallet_client.rb', line 48

def subscribe_to_transactions(callback:, result_callback: nil)
  interceptor = lambda { |notification, _type|
    callback.call(notification)
  }
  send_subscription('subscribe_transactions', interceptor, nil, result_callback)
end

#subscribe_to_wallet_balance(callback:, result_callback: nil) ⇒ Object

subscribe to a feed of the user’s wallet balances

only non-zero values are present

api.exchange.cryptomkt.com/#subscribe-to-wallet-balances

Params

Proc callback

A Proc that recieves notifications as a list of balances, and the type of notification (either ‘snapshot’ or ‘update’)

Proc result_callback

Optional. A Proc of two arguments, An exception and a result, called either with the exception or with the result, a boolean value, indicating the success of the subscription



76
77
78
79
80
81
82
83
84
85
# File 'lib/cryptomarket/websocket/wallet_client.rb', line 76

def subscribe_to_wallet_balance(callback:, result_callback: nil)
  interceptor = proc { |notification, type|
    if type == Args::NotificationType::SNAPSHOT
      callback.call(notification, type)
    else
      callback.call([notification], type)
    end
  }
  send_subscription('subscribe_wallet_balances', interceptor, nil, result_callback)
end

#unsubscribe_to_transactions(result_callback: nil) ⇒ Object

stop recieving the feed of transactions changes

api.exchange.cryptomkt.com/#subscribe-to-transactions

Params

Proc callback

Optional. A Proc of two arguments, An exception and a result, called either with the exception or with the result, a boolean value, indicating the success of the unsubscription



62
63
64
# File 'lib/cryptomarket/websocket/wallet_client.rb', line 62

def unsubscribe_to_transactions(result_callback: nil)
  send_unsubscription('unsubscribe_transactions', result_callback, nil)
end

#unsubscribe_to_wallet_balance(result_callback: nil) ⇒ Object

stop recieving the feed of balances changes

api.exchange.cryptomkt.com/#subscribe-to-wallet-balances

Params

Proc callback

Optional. A Proc of two arguments, An exception and a result, called either with the exception or with the result, a boolean value, indicating the success of the unsubscription



94
95
96
# File 'lib/cryptomarket/websocket/wallet_client.rb', line 94

def unsubscribe_to_wallet_balance(result_callback: nil)
  send_unsubscription('unsubscribe_wallet_balances', result_callback, nil)
end