Class: Peatio::Tron::Blockchain
- Inherits:
-
Blockchain::Abstract
- Object
- Blockchain::Abstract
- Peatio::Tron::Blockchain
- Includes:
- Encryption
- Defined in:
- lib/peatio/tron/blockchain.rb
Constant Summary collapse
- DEFAULT_FEATURES =
{ case_sensitive: true, cash_addr_format: false }.freeze
- TOKEN_EVENT_IDENTIFIER =
'ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef'
Constants included from Encryption
Instance Method Summary collapse
- #configure(settings = {}) ⇒ Object
- #fetch_block!(block_number) ⇒ Object
-
#initialize(custom_features = {}) ⇒ Blockchain
constructor
A new instance of Blockchain.
- #latest_block_number ⇒ Object
- #load_balance_of_address!(address, currency_id) ⇒ Object
Methods included from Encryption
#abi_encode, #decode_hex, #encode_hex, #reformat_decode_address, #reformat_encode_address, #reformat_txid
Constructor Details
#initialize(custom_features = {}) ⇒ Blockchain
9 10 11 12 |
# File 'lib/peatio/tron/blockchain.rb', line 9 def initialize(custom_features = {}) @features = DEFAULT_FEATURES.merge(custom_features).slice(*SUPPORTED_FEATURES) @settings = {} end |
Instance Method Details
#configure(settings = {}) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/peatio/tron/blockchain.rb', line 14 def configure(settings = {}) # Clean client state during configure. @client = nil @trc10 = []; @trc20 = []; @trx = [] @settings.merge!(settings.slice(*SUPPORTED_SETTINGS)) @settings[:currencies]&.each do |c| if c.dig(:options, :trc10_token_id).present? @trc10 << c elsif c.dig(:options, :trc20_contract_address).present? @trc20 << c else @trx << c end end end |
#fetch_block!(block_number) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/peatio/tron/blockchain.rb', line 32 def fetch_block!(block_number) client.json_rpc(path: 'wallet/getblockbynum', params: { num: block_number }) .fetch('transactions', []).each_with_object([]) do |tx, txs_array| if %w[TransferContract TransferAssetContract].include? tx.dig('raw_data', 'contract')[0].fetch('type', nil) next if invalid_transaction?(tx) else tx = client.json_rpc(path: 'wallet/gettransactioninfobyid', params: { value: tx['txID'] }) next if tx.nil? || invalid_trc20_transaction?(tx) end txs = build_transaction(tx.merge('block_number' => block_number)).map do |ntx| Peatio::Transaction.new(ntx) end txs_array.append(*txs) end.yield_self { |txs_array| Peatio::Block.new(block_number, txs_array) } rescue Client::Error => e raise Peatio::Blockchain::ClientError, e end |
#latest_block_number ⇒ Object
53 54 55 56 57 58 |
# File 'lib/peatio/tron/blockchain.rb', line 53 def latest_block_number client.json_rpc(path: 'wallet/getblockbylatestnum', params: { num: 1 }) .fetch('block')[0]['block_header']['raw_data']['number'] rescue Client::Error => e raise Peatio::Blockchain::ClientError, e end |
#load_balance_of_address!(address, currency_id) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/peatio/tron/blockchain.rb', line 60 def load_balance_of_address!(address, currency_id) currency = @settings[:currencies].find { |c| c[:id] == currency_id.to_s } raise UndefinedCurrencyError unless currency if currency.dig(:options, :trc10_token_id).present? client.json_rpc(path: 'wallet/getaccount', params: { address: reformat_decode_address(address) } ).fetch('assetV2', []) .find { |a| a['key'] == currency.dig(:options, :trc10_token_id) } .try(:fetch, 'value', 0) elsif currency.dig(:options, :trc20_contract_address).present? client.json_rpc(path: 'wallet/triggersmartcontract', params: { owner_address: reformat_decode_address(address), contract_address: reformat_decode_address(currency.dig(:options, :trc20_contract_address)), function_selector: 'balanceOf(address)', parameter: abi_encode(reformat_decode_address(address)[2..42]) } ).fetch('constant_result')[0].hex else client.json_rpc(path: 'wallet/getaccount', params: { address: reformat_decode_address(address) } ).fetch('balance', nil) end.yield_self { |amount| convert_from_base_unit(amount.to_i, currency) } rescue Client::Error => e raise Peatio::Blockchain::ClientError, e end |