Class: Bitcoin::Wallet::AssetHandler

Inherits:
Concurrent::Actor::RestartingContext
  • Object
show all
Defined in:
lib/bitcoin/wallet/asset_handler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(spv, publisher) ⇒ AssetHandler

Returns a new instance of AssetHandler.



8
9
10
11
12
13
14
15
# File 'lib/bitcoin/wallet/asset_handler.rb', line 8

def initialize(spv, publisher)
  publisher << [:subscribe, Bitcoin::Grpc::EventUtxoSpent]
  publisher << [:subscribe, Bitcoin::Grpc::WatchAssetIdAssignedRequest]
  @publisher = publisher
  @utxo_db = spv.wallet.utxo_db
  @logger = Bitcoin::Logger.create(:debug)
  @watchings = []
end

Instance Attribute Details

#publisherObject (readonly)

Returns the value of attribute publisher.



6
7
8
# File 'lib/bitcoin/wallet/asset_handler.rb', line 6

def publisher
  @publisher
end

#utxo_dbObject (readonly)

Returns the value of attribute utxo_db.



6
7
8
# File 'lib/bitcoin/wallet/asset_handler.rb', line 6

def utxo_db
  @utxo_db
end

#watchingsObject (readonly)

Returns the value of attribute watchings.



6
7
8
# File 'lib/bitcoin/wallet/asset_handler.rb', line 6

def watchings
  @watchings
end

Instance Method Details

#on_message(message) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/bitcoin/wallet/asset_handler.rb', line 17

def on_message(message)
  case message
  when Bitcoin::Grpc::WatchTokenRequest
    watchings << message
  when Bitcoin::Grpc::EventUtxoSpent
    tx = Bitcoin::Tx.parse_from_payload(message.tx_payload.htb)
    log(::Logger::DEBUG, "tx=#{tx}, open_assets?=#{tx.open_assets?}")
    utxo_db.delete_token(message.utxo) if tx.open_assets?
  when Bitcoin::Grpc::WatchAssetIdAssignedRequest
    tx = Bitcoin::Tx.parse_from_payload(message.tx_payload.htb)
    log(::Logger::DEBUG, "tx=#{tx}, open_assets?=#{tx.open_assets?}")
    case
    when tx.open_assets?
      outputs = Bitcoin::Grpc::OapService.outputs_with_open_asset_id(message.tx_hash)
      begin
        puts outputs
        if outputs
          outputs.each do |output|
            asset_id = output['asset_id']
            next unless asset_id

            asset_id_as_hex = Bitcoin::Base58.decode(asset_id)
            asset_quantity = output['asset_quantity']
            oa_output_type = output['oa_output_type']

            out_point = Bitcoin::OutPoint.new(tx.tx_hash, output['n'])
            utxo = utxo_db.get_utxo(out_point)
            next unless utxo

            asset_output = utxo_db.save_token(AssetFeature::AssetType::OPEN_ASSETS, asset_id_as_hex, asset_quantity, utxo)
            next unless asset_output

            item_to_delete = []
            watchings.select { |item| item.asset_id == asset_id }.each do |item|
              if oa_output_type == 'issuance'
                publisher << Bitcoin::Grpc::EventTokenIssued.new(request_id: item.id, asset: asset_output)
              else
                publisher << Bitcoin::Grpc::EventTokenTransfered.new(request_id: item.id, asset: asset_output)
              end
              item_to_delete << item
            end
            item_to_delete.each { |item| watchings.delete(item) }
          end
        else
          raise 'can not get asset_id'
        end
      rescue => e
        log(::Logger::DEBUG, e.message)
        log(::Logger::DEBUG, e.backtrace)
        task = Concurrent::TimerTask.new(execution_interval: 60) do
          self << message
          task.shutdown
        end
        task.execute
      end
    else
    end
  end
end