Class: Peatio::Ripple::Wallet
- Inherits:
-
Wallet::Abstract
- Object
- Wallet::Abstract
- Peatio::Ripple::Wallet
- Defined in:
- lib/peatio/ripple/wallet.rb
Constant Summary collapse
- Error =
Class.new(StandardError)
Instance Method Summary collapse
-
#calculate_current_fee ⇒ Object
Returns fee in drops that is enough to process transaction in current ledger.
- #configure(settings = {}) ⇒ Object
- #create_address!(_setting) ⇒ Object
- #create_raw_address(options = {}) ⇒ Object
- #create_transaction!(transaction, options = {}) ⇒ Object
-
#initialize(settings = {}) ⇒ Wallet
constructor
A new instance of Wallet.
- #latest_block_number ⇒ Object
- #load_balance! ⇒ Object
- #sign_transaction(transaction, options = {}) ⇒ Object
Constructor Details
#initialize(settings = {}) ⇒ Wallet
Returns a new instance of Wallet.
7 8 9 |
# File 'lib/peatio/ripple/wallet.rb', line 7 def initialize(settings = {}) @settings = settings end |
Instance Method Details
#calculate_current_fee ⇒ Object
Returns fee in drops that is enough to process transaction in current ledger
97 98 99 100 101 |
# File 'lib/peatio/ripple/wallet.rb', line 97 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
11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/peatio/ripple/wallet.rb', line 11 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
25 26 27 28 29 30 |
# File 'lib/peatio/ripple/wallet.rb', line 25 def create_address!(_setting) { address: "#{@wallet[:address]}?dt=#{SecureRandom.random_number(10**6)}", secret: @wallet[:secret] } end |
#create_raw_address(options = {}) ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'lib/peatio/ripple/wallet.rb', line 32 def create_raw_address( = {}) secret = .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
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/peatio/ripple/wallet.rb', line 42 def create_transaction!(transaction, = {}) tx_blob = sign_transaction(transaction, ) client.json_rpc(:submit, [tx_blob]).yield_self do |result| = { 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.hash = result.fetch('tx_json').fetch('hash') else raise Error, "XRP withdrawal from #{@wallet.fetch(:address)} to #{transaction.to_address} failed. Message: #{}." end transaction end end |
#latest_block_number ⇒ Object
103 104 105 106 107 |
# File 'lib/peatio/ripple/wallet.rb', line 103 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
109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/peatio/ripple/wallet.rb', line 109 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
62 63 64 65 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 |
# File 'lib/peatio/ripple/wallet.rb', line 62 def sign_transaction(transaction, = {}) account_address = 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 .dig(:subtract_fee) transaction.amount = convert_from_base_unit(amount) unless transaction.amount == amount params = [{ secret: @wallet.fetch(:secret), tx_json: { Account: account_address, 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 #{account_address} to #{destination_address} failed: #{result}." end end end |