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)
- DEFAULT_FEATURES =
{ skip_deposit_collection: false }.freeze
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(custom_features = {}) ⇒ Wallet
constructor
A new instance of Wallet.
- #latest_block_number ⇒ Object
- #load_balance! ⇒ Object
- #sign_transaction(transaction, options = {}) ⇒ Object
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_fee ⇒ Object
Returns fee in drops that is enough to process transaction in current ledger
100 101 102 103 104 |
# File 'lib/peatio/ripple/wallet.rb', line 100 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( = {}) 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
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/peatio/ripple/wallet.rb', line 45 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
106 107 108 109 110 |
# File 'lib/peatio/ripple/wallet.rb', line 106 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
112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/peatio/ripple/wallet.rb', line 112 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
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 95 96 97 |
# File 'lib/peatio/ripple/wallet.rb', line 65 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 |