Class: Peatio::Electrum::Wallet

Inherits:
Wallet::Abstract
  • Object
show all
Defined in:
lib/peatio/electrum/wallet.rb

Overview

Constant Summary collapse

DEFAULT_FEATURES =
{ skip_deposit_collection: false }.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(custom_features = {}) ⇒ Wallet

Returns a new instance of Wallet.



13
14
15
16
# File 'lib/peatio/electrum/wallet.rb', line 13

def initialize(custom_features = {})
  @features = DEFAULT_FEATURES.merge(custom_features).slice(*SUPPORTED_FEATURES)
  @settings = {}
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



9
10
11
# File 'lib/peatio/electrum/wallet.rb', line 9

def client
  @client
end

Instance Method Details

#configure(settings = {}) ⇒ Hash

Note:

Be careful with your wallet state after configure. Clean everything what could be related to other wallet configuration. E.g. client state.

Merges given configuration parameters with defined during initialization and returns the result.

With :address required key other settings could be customized using Wallet#settings.

Parameters:

  • settings (Hash) (defaults to: {})

    configurations to use.

Options Hash (settings):

  • :wallet (Hash)

    Wallet settings for performing API calls.

  • :currencies (Array<Hash>)

    List of currency hashes with :id,:base_factor,:options(deprecated) keys. Custom keys could be added by defining them in Currency #options.

Returns:

  • (Hash)

    merged settings.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/peatio/electrum/wallet.rb', line 34

def configure(settings = {})
  @settings.merge!(settings.slice(*SUPPORTED_SETTINGS))

  @wallet = @settings.fetch(:wallet) do
    raise Peatio::Wallet::MissingSettingError, :wallet
  end.slice(:uri, :address, :secret)

  @currency = @settings.fetch(:currency) do
    raise Peatio::Wallet::MissingSettingError, :currency
  end.slice(:id, :base_factor, :options)

  unless @settings[:wallet][:uri]
    raise Peatio::Wallet::MissingSettingError, 'Missing uri in wallet'
  end

  @client = Client.new(@settings[:wallet][:uri])
end

#create_address!(_options = {}) ⇒ Hash

Performs API call for address creation and returns it.

Examples:

{ address: :fake_address,
  secret:  :changeme,
  details: { uid: .member.uid } }

Parameters:

  • options (Hash)

Returns:

  • (Hash)

    newly created blockchain address.

Raises:

  • (Peatio::Wallet::ClientError)

    if error was raised on wallet API call.



66
67
68
69
70
71
72
# File 'lib/peatio/electrum/wallet.rb', line 66

def create_address!(_options = {})
  {
    address: client.create_address
  }
rescue Peatio::Electrum::Client::Error => e
  raise Peatio::Wallet::ClientError, e
end

#create_transaction!(transaction, _options = {}) ⇒ Peatio::Transaction

Performs API call for creating transaction and returns updated transaction.

to_address, amount & currency_id.

Parameters:

  • transaction (Peatio::Transaction)

    transaction with defined

  • options (Hash)

Returns:

  • (Peatio::Transaction)

    transaction with updated hash.

Raises:

  • (Peatio::Wallet::ClientError)

    if error was raised on wallet API call.



94
95
96
97
98
99
100
101
# File 'lib/peatio/electrum/wallet.rb', line 94

def create_transaction!(transaction, _options = {})
  tx = client.payto(transaction.to_address, transaction.amount, password: @settings[:wallet][:secret])['hex']
  txid = client.broadcast(tx)
  transaction.hash = txid
  transaction
rescue Peatio::Electrum::Client::Error => e
  raise Peatio::Wallet::ClientError, e
end

#load_balance!BigDecimal

Note:

Optional. Don’t override this method if your blockchain

Fetches address balance of specific currency.

doesn’t provide functionality to get balance by address.

if error was raised on wallet API call ClientError is raised. if wallet API call was successful but we can’t detect balance for address Error is raised.

Returns:

  • (BigDecimal)

    the current address balance.

Raises:

  • (Peatio::Wallet::ClientError)


114
115
116
117
118
# File 'lib/peatio/electrum/wallet.rb', line 114

def load_balance!
  client.get_balance
rescue Peatio::Electrum::Client::Error => e
  raise Peatio::Wallet::ClientError, e
end