Class: BSV::Wallet::Postgres::Broadcast

Inherits:
Sequel::Model
  • Object
show all
Includes:
Fetchable, BSV::Wallet::Pushable
Defined in:
lib/bsv/wallet/postgres/broadcast.rb

Constant Summary collapse

TERMINAL_STATUSES =

Broadcasts with these statuses are considered terminal — no further polling.

%w[
  SEEN_ON_NETWORK MINED IMMUTABLE
  REJECTED DOUBLE_SPEND_ATTEMPTED
].freeze
FETCH_STALENESS =

Minimum age (seconds) before a broadcast is eligible for status polling.

30

Instance Method Summary collapse

Instance Method Details

#fetch_argsObject



43
44
45
# File 'lib/bsv/wallet/postgres/broadcast.rb', line 43

def fetch_args
  { txid: action.dtxid }
end

#fetch_commandObject

— Fetchable contract —



39
40
41
# File 'lib/bsv/wallet/postgres/broadcast.rb', line 39

def fetch_command
  :get_tx_status
end

#needs_fetch?Boolean

Returns:

  • (Boolean)


47
48
49
50
51
52
# File 'lib/bsv/wallet/postgres/broadcast.rb', line 47

def needs_fetch?
  return false unless broadcast_at
  return false if TERMINAL_STATUSES.include?(tx_status)

  broadcast_at < Time.now - FETCH_STALENESS
end

#needs_push?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/bsv/wallet/postgres/broadcast.rb', line 33

def needs_push?
  broadcast_at.nil? && action&.raw_tx
end

#push_commandObject

— Pushable contract —



25
26
27
# File 'lib/bsv/wallet/postgres/broadcast.rb', line 25

def push_command
  :broadcast
end

#push_payloadObject



29
30
31
# File 'lib/bsv/wallet/postgres/broadcast.rb', line 29

def push_payload
  action.raw_tx
end

#write!(response) ⇒ Object

Update columns from a normalized Services response.

Parameters:

  • response (BSV::Network::ProtocolResponse)

    normalized response



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/bsv/wallet/postgres/broadcast.rb', line 59

def write!(response)
  data = response.data
  return unless data.is_a?(Hash)

  fields = {}
  fields[:broadcast_at] = Time.now if broadcast_at.nil?
  fields[:tx_status] = data[:tx_status] if data[:tx_status]
  fields[:arc_status] = data[:status] if data[:status]
  fields[:block_hash] = decode_hex(data[:block_hash]) if data[:block_hash]
  fields[:block_height] = data[:block_height] if data[:block_height]
  fields[:merkle_path] = decode_hex(data[:merkle_path]) if data[:merkle_path]
  fields[:extra_info] = data[:extra_info] if data[:extra_info]
  fields[:competing_txs] = Sequel.pg_array(data[:competing_txs]) if data[:competing_txs]

  update(fields) unless fields.empty?
end