11
22class EthTransaction < ApplicationRecord
33 belongs_to :eth_block , foreign_key : :block_number , primary_key : :block_number , optional : true ,
4- inverse_of : :eth_transaction
4+ inverse_of : :eth_block
55 has_one :ethscription , foreign_key : :transaction_hash , primary_key : :transaction_hash ,
6- inverse_of : :eth_transaction , dependent : :destroy
7-
6+ inverse_of : :eth_transaction
87 has_many :ethscription_transfers , foreign_key : :transaction_hash ,
9- primary_key : :transaction_hash , dependent : :destroy , inverse_of : :eth_transaction
8+ primary_key : :transaction_hash , inverse_of : :eth_transaction
9+ has_many :ethscription_ownership_versions , foreign_key : :transaction_hash ,
10+ primary_key : :transaction_hash , inverse_of : :eth_transaction
1011
1112 attr_accessor :transfer_index
1213
14+ scope :newest_first , -> { order ( block_number : :desc , transaction_index : :desc ) }
15+ scope :oldest_first , -> { order ( block_number : :asc , transaction_index : :asc ) }
16+
17+ def self . event_signature ( event_name )
18+ "0x" + Digest ::Keccak256 . hexdigest ( event_name )
19+ end
20+
21+ CreateEthscriptionEventSig = event_signature ( "ethscriptions_protocol_CreateEthscription(address,string)" )
22+ Esip2EventSig = event_signature ( "ethscriptions_protocol_TransferEthscriptionForPreviousOwner(address,address,bytes32)" )
23+ Esip1EventSig = event_signature ( "ethscriptions_protocol_TransferEthscription(address,bytes32)" )
24+
1325 def possibly_relevant?
1426 status != 0 &&
1527 ( possibly_creates_ethscription? || possibly_transfers_ethscription? )
@@ -104,7 +116,7 @@ def ethscription_creation_events
104116 return [ ] unless EthTransaction . esip3_enabled? ( block_number )
105117
106118 ordered_events . select do |log |
107- EthTransaction . contracts_create_ethscription_event_sig == log [ 'topics' ] . first
119+ CreateEthscriptionEventSig == log [ 'topics' ] . first
108120 end
109121 end
110122
@@ -143,16 +155,17 @@ def create_ethscription_transfers_from_events!
143155 topics = log [ 'topics' ]
144156 event_type = topics . first
145157
146- if event_type == EthTransaction . esip1_transfer_event_signature
158+ if event_type == Esip1EventSig
147159 begin
148- event_to = Eth ::Abi . decode ( [ 'address' ] , topics . third ) . first
160+ event_to = Eth ::Abi . decode ( [ 'address' ] , topics . second ) . first
161+ tx_hash = Eth ::Util . bin_to_prefixed_hex (
162+ Eth ::Abi . decode ( [ 'bytes32' ] , topics . third ) . first
163+ )
149164 rescue Eth ::Abi ::DecodingError
150165 next
151166 end
152167
153- next unless valid_bytes32? ( topics . third )
154-
155- target_ethscription = Ethscription . find_by ( transaction_hash : topics . third )
168+ target_ethscription = Ethscription . find_by ( transaction_hash : tx_hash )
156169
157170 if target_ethscription . present?
158171 ethscription_transfers . create! (
@@ -165,17 +178,18 @@ def create_ethscription_transfers_from_events!
165178 } . merge ( transfer_attrs )
166179 )
167180 end
168- elsif event_type == EthTransaction . esip2_transfer_event_signature
181+ elsif event_type == Esip2EventSig
169182 begin
170183 event_previous_owner = Eth ::Abi . decode ( [ 'address' ] , topics . second ) . first
171184 event_to = Eth ::Abi . decode ( [ 'address' ] , topics . third ) . first
185+ tx_hash = Eth ::Util . bin_to_prefixed_hex (
186+ Eth ::Abi . decode ( [ 'bytes32' ] , topics . fourth ) . first
187+ )
172188 rescue Eth ::Abi ::DecodingError
173189 next
174190 end
175191
176- next unless valid_bytes32? ( topics . fourth )
177-
178- target_ethscription = Ethscription . find_by ( transaction_hash : topics . fourth )
192+ target_ethscription = Ethscription . find_by ( transaction_hash : tx_hash )
179193
180194 if target_ethscription . present?
181195 ethscription_transfers . create! (
@@ -277,40 +291,11 @@ def self.esip1_enabled?(block_number)
277291 block_number >= 17672762
278292 end
279293
280- def valid_bytes32? ( value )
281- /\A 0x[0-9a-f]{64}\z /i . match? ( value . to_s )
282- end
283-
284294 def self . contract_transfer_event_signatures ( block_number )
285295 [ ] . tap do |res |
286- res << esip1_transfer_event_signature if esip1_enabled? ( block_number )
287- res << esip2_transfer_event_signature if esip2_enabled? ( block_number )
288- end
289- end
290-
291- class << self
292- extend Memoist
293-
294- def contracts_create_ethscription_event_sig
295- "0x" + Digest ::Keccak256 . hexdigest (
296- "ethscriptions_protocol_CreateEthscription(address,string)"
297- )
298- end
299- memoize :contracts_create_ethscription_event_sig
300-
301- def esip2_transfer_event_signature
302- "0x" + Digest ::Keccak256 . hexdigest (
303- "ethscriptions_protocol_TransferEthscriptionForPreviousOwner(address,address,bytes32)"
304- )
305- end
306- memoize :esip2_transfer_event_signature
307-
308- def esip1_transfer_event_signature
309- "0x" + Digest ::Keccak256 . hexdigest (
310- "ethscriptions_protocol_TransferEthscription(address,bytes32)"
311- )
296+ res << Esip1EventSig if esip1_enabled? ( block_number )
297+ res << Esip2EventSig if esip2_enabled? ( block_number )
312298 end
313- memoize :esip1_transfer_event_signature
314299 end
315300
316301 def self . prune_transactions
0 commit comments