Class: Peatio::Ripple::Wallet

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

Constant Summary collapse

Error =
Class.new(StandardError)
DEFAULT_FEATURES =
{ skip_deposit_collection: false }.freeze

Instance Method Summary collapse

Constructor Details

#initialize(custom_features = {}) ⇒ Wallet

Returns a new instance of Wallet.


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

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

Instance Method Details

#calculate_current_feeObject

Returns fee in drops that is enough to process transaction in current ledger


101
102
103
104
105
# File 'lib/peatio/ripple/wallet.rb', line 101

def calculate_current_fee
  client.json_rpc(:fee, {}).yield_self do |result|
    result.dig('drops', 'open_ledger_fee').to_i
  end
end

#configure(settings = {}) ⇒ Object


14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/peatio/ripple/wallet.rb', line 14

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, :secret)

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

#create_address!(_setting) ⇒ Object


28
29
30
31
32
33
# File 'lib/peatio/ripple/wallet.rb', line 28

def create_address!(_setting)
  {
    address: "#{@wallet[:address]}?dt=#{SecureRandom.random_number(10**6)}",
    secret: @wallet[:secret]
  }
end

#create_raw_address(options = {}) ⇒ Object


35
36
37
38
39
40
41
42
43
# File 'lib/peatio/ripple/wallet.rb', line 35

def create_raw_address(options = {})
  secret = options.fetch(:secret) { PasswordGenerator.generate(64) }
  result = client.json_rpc(:wallet_propose, [{ passphrase: secret }])

  result.slice('key_type', 'master_seed', 'master_seed_hex',
                'master_key', 'public_key', 'public_key_hex')
        .merge(address: normalize_address(result.fetch('account_id')), secret: secret)
        .symbolize_keys
end

#create_transaction!(transaction, options = {}) ⇒ Object


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/peatio/ripple/wallet.rb', line 45

def create_transaction!(transaction, options = {})
  tx_blob = sign_transaction(transaction, options)
  client.json_rpc(:submit, [tx_blob]).yield_self do |result|
    error_message = {
      message: result.fetch('engine_result_message'),
      status: result.fetch('engine_result')
    }

    # TODO: It returns provision results. Transaction may fail or success
    # than change status to opposite one before ledger is final.
    # Need to set special status and recheck this transaction status
    if result['engine_result'].to_s == 'tesSUCCESS' && result['status'].to_s == 'success'
      transaction.currency_id = 'xrp' if transaction.currency_id.blank?
      transaction.hash = result.fetch('tx_json').fetch('hash')
    else
      raise Error, "XRP withdrawal from #{@wallet.fetch(:address)} to #{transaction.to_address} failed. Message: #{error_message}."
    end
    transaction
  end
end

#latest_block_numberObject


107
108
109
110
111
# File 'lib/peatio/ripple/wallet.rb', line 107

def latest_block_number
  client.json_rpc(:ledger, [{ ledger_index: 'validated' }]).fetch('ledger_index')
rescue Client::Error => e
  raise Peatio::Blockchain::ClientError, e
end

#load_balance!Object


113
114
115
116
117
118
119
120
121
122
123
# File 'lib/peatio/ripple/wallet.rb', line 113

def load_balance!
  client.json_rpc(:account_info,
                  [account: normalize_address(@wallet.fetch(:address)), ledger_index: 'validated', strict: true])
                  .fetch('account_data')
                  .fetch('Balance')
                  .to_d
                  .yield_self { |amount| convert_from_base_unit(amount) }

rescue Client::Error => e
  raise Peatio::Wallet::ClientError, e
end

#sign_transaction(transaction, options = {}) ⇒ Object


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/peatio/ripple/wallet.rb', line 66

def sign_transaction(transaction, options = {})
   = normalize_address(@wallet[:address])
  destination_address = normalize_address(transaction.to_address)
  destination_tag = destination_tag_from(transaction.to_address)
  fee = calculate_current_fee

  amount = convert_to_base_unit(transaction.amount)

  # Subtract fees from initial deposit amount in case of deposit collection
  amount -= fee if options.dig(:subtract_fee)
  transaction.amount = convert_from_base_unit(amount) unless transaction.amount == amount

    params = [{
    secret: @wallet.fetch(:secret),
    tx_json: {
      Account:            ,
      Amount:             amount.to_s,
      Fee:                fee.to_s,
      Destination:        destination_address,
      DestinationTag:     destination_tag,
      TransactionType:    'Payment',
      LastLedgerSequence: latest_block_number + 4
      }
    }]

  client.json_rpc(:sign, params).yield_self do |result|
    if result['status'].to_s == 'success'
      { tx_blob: result['tx_blob'] }
    else
      raise Error, "XRP sign transaction from #{} to #{destination_address} failed: #{result}."
    end
  end
end