Module: Bitcoin::Wallet::AssetFeature

Included in:
UtxoDB
Defined in:
lib/bitcoin/wallet/asset_feature.rb

Defined Under Namespace

Modules: AssetType

Constant Summary collapse

KEY_PREFIX =
{
  asset_out_point: 'ao', # key: out_point, value AssetOutput
  asset_script_pubkey: 'as',     # key: asset_type, script_pubkey and out_point, value AssetOutput
  asset_height: 'ah',    # key: asset_type, block_height and out_point, value AssetOutput
}

Instance Method Summary collapse

Instance Method Details

#delete_token(utxo) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/bitcoin/wallet/asset_feature.rb', line 47

def delete_token(utxo)
  logger.info("UtxoDB#delete_token:#{utxo.inspect}")
  level_db.batch do
    out_point = Bitcoin::OutPoint.new(utxo.tx_hash, utxo.index)

    key = KEY_PREFIX[:asset_out_point] + out_point.to_payload.bth
    return unless level_db.contains?(key)
    asset_output = Bitcoin::Grpc::AssetOutput.decode(level_db.get(key).htb)
    level_db.delete(key)

    if utxo.script_pubkey
      key = KEY_PREFIX[:asset_script_pubkey] + [asset_output.asset_type].pack('C').bth + utxo.script_pubkey + out_point.to_payload.bth
      level_db.delete(key)
    end

    key = KEY_PREFIX[:asset_height] + [asset_output.asset_type].pack('C').bth + [utxo.block_height].pack('N').bth + out_point.to_payload.bth
    level_db.delete(key)
    return asset_output
  end
end

#get_asset_balance(asset_type, asset_id, account, current_block_height: 9999999, min: 0, max: 9999999, addresses: nil) ⇒ Object

Raises:

  • (NotImplementedError)


85
86
87
88
89
90
# File 'lib/bitcoin/wallet/asset_feature.rb', line 85

def get_asset_balance(asset_type, asset_id, , current_block_height: 9999999, min: 0, max: 9999999, addresses: nil)
  raise NotImplementedError.new("asset_type should not be nil.") unless asset_type
  raise ArgumentError.new('asset_id should not be nil') unless asset_id

  (asset_type, asset_id, , current_block_height: current_block_height, min: min, max: max).sum { |u| u.asset_quantity }
end

#list_uncolored_unspent_in_account(account, current_block_height: 9999999, min: 0, max: 9999999) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/bitcoin/wallet/asset_feature.rb', line 92

def (, current_block_height: 9999999, min: 0, max: 9999999)
  utxos = (, current_block_height: current_block_height, min: min, max: max)
  utxos.delete_if do |utxo|
    out_point = Bitcoin::OutPoint.new(utxo.tx_hash, utxo.index)
    level_db.contains?(KEY_PREFIX[:asset_out_point] + out_point.to_payload.bth)
  end
end

#list_unspent_assets(asset_type, asset_id, current_block_height: 9999999, min: 0, max: 9999999, addresses: nil) ⇒ Object

Raises:

  • (NotImplementedError)


68
69
70
71
72
73
74
75
76
77
# File 'lib/bitcoin/wallet/asset_feature.rb', line 68

def list_unspent_assets(asset_type, asset_id, current_block_height: 9999999, min: 0, max: 9999999, addresses: nil)
  raise NotImplementedError.new("asset_type should not be nil.") unless asset_type
  raise ArgumentError.new('asset_id should not be nil') unless asset_id

  if addresses
    list_unspent_assets_by_addresses(asset_type, asset_id, current_block_height, min: min, max: max, addresses: addresses)
  else
    list_unspent_assets_by_block_height(asset_type, asset_id, current_block_height, min: min, max: max)
  end
end

#list_unspent_assets_in_account(asset_type, asset_id, account, current_block_height: 9999999, min: 0, max: 9999999) ⇒ Object



79
80
81
82
83
# File 'lib/bitcoin/wallet/asset_feature.rb', line 79

def (asset_type, asset_id, , current_block_height: 9999999, min: 0 , max: 9999999)
  return [] unless 
  script_pubkeys = .watch_targets.map { |t| Bitcoin::Script.to_p2wpkh(t).to_payload.bth }
  list_unspent_assets_by_script_pubkeys(asset_type, asset_id, current_block_height, min: min, max: max, script_pubkeys: script_pubkeys)
end

#save_token(asset_type, asset_id, asset_quantity, utxo) ⇒ Object



14
15
16
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
# File 'lib/bitcoin/wallet/asset_feature.rb', line 14

def save_token(asset_type, asset_id, asset_quantity, utxo)
  logger.info("UtxoDB#save_token:#{[asset_type, asset_id, asset_quantity, utxo.inspect]}")
  level_db.batch do
    asset_output = Bitcoin::Grpc::AssetOutput.new(
      asset_type: asset_type,
      asset_id: asset_id,
      asset_quantity: asset_quantity,
      tx_hash: utxo.tx_hash,
      index: utxo.index,
      block_height: utxo.block_height
    )
    out_point = Bitcoin::OutPoint.new(utxo.tx_hash, utxo.index)
    payload = asset_output.to_proto.bth

    # out_point
    key = KEY_PREFIX[:asset_out_point] + out_point.to_payload.bth
    return if level_db.contains?(key)

    level_db.put(key, payload)

    # script_pubkey
    if utxo.script_pubkey
      key = KEY_PREFIX[:asset_script_pubkey] + [asset_type].pack('C').bth + utxo.script_pubkey + out_point.to_payload.bth
      level_db.put(key, payload)
    end

    # block_height
    key = KEY_PREFIX[:asset_height] + [asset_type].pack('C').bth + [utxo.block_height].pack('N').bth + out_point.to_payload.bth
    level_db.put(key, payload)
    return asset_output
  end
end