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+
1317 def self . event_signature ( event_name )
1418 "0x" + Digest ::Keccak256 . hexdigest ( event_name )
1519 end
@@ -153,14 +157,15 @@ def create_ethscription_transfers_from_events!
153157
154158 if event_type == Esip1EventSig
155159 begin
156- 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+ )
157164 rescue Eth ::Abi ::DecodingError
158165 next
159166 end
160167
161- next unless valid_bytes32? ( topics . third )
162-
163- target_ethscription = Ethscription . find_by ( transaction_hash : topics . third )
168+ target_ethscription = Ethscription . find_by ( transaction_hash : tx_hash )
164169
165170 if target_ethscription . present?
166171 ethscription_transfers . create! (
@@ -177,13 +182,14 @@ def create_ethscription_transfers_from_events!
177182 begin
178183 event_previous_owner = Eth ::Abi . decode ( [ 'address' ] , topics . second ) . first
179184 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+ )
180188 rescue Eth ::Abi ::DecodingError
181189 next
182190 end
183191
184- next unless valid_bytes32? ( topics . fourth )
185-
186- target_ethscription = Ethscription . find_by ( transaction_hash : topics . fourth )
192+ target_ethscription = Ethscription . find_by ( transaction_hash : tx_hash )
187193
188194 if target_ethscription . present?
189195 ethscription_transfers . create! (
@@ -285,10 +291,6 @@ def self.esip1_enabled?(block_number)
285291 block_number >= 17672762
286292 end
287293
288- def valid_bytes32? ( value )
289- /\A 0x[0-9a-f]{64}\z /i . match? ( value . to_s )
290- end
291-
292294 def self . contract_transfer_event_signatures ( block_number )
293295 [ ] . tap do |res |
294296 res << Esip1EventSig if esip1_enabled? ( block_number )
0 commit comments