Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
lpthong90 committed Mar 4, 2024
1 parent 2344151 commit 4894432
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 278 deletions.
4 changes: 2 additions & 2 deletions app/controllers/blocks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ def load_block
web3.get_block_by_number(params[:block_id].to_i, include_txs: true)
end
Block.from_json(data)
# rescue
# nil
rescue
nil
end
end

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/transactions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def load_transactions
def load_transaction
@transaction = begin
data = web3.get_transaction_by_hash(params[:hash])
Transaction.new.from_json(data.to_json)
Transaction.from_json(data)
rescue
nil
end
Expand Down
13 changes: 3 additions & 10 deletions app/models/address.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
class Address
include ActiveModel::API
include ActiveModel::Validations
include ActiveModel::Serializers::JSON

attr_accessor :address, :balance
class Address < ApplicationModel
attribute :address, :string
attribute :balance, :hex_integer

FORMAT_REGEX = /0x[a-fA-F0-9]{40}/

validates :address, presence: true
validates_format_of :address, with: FORMAT_REGEX

def balance=(value)
@balance = value.to_i(16) rescue 0
end

def eth_balance
balance.to_d * 1e-18
end
Expand Down
11 changes: 6 additions & 5 deletions app/models/application_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,8 @@ class ApplicationModel

include Turbo::Broadcastable

# include NormalizationConcern
include MyNormalizationConcern
include StringConcern

def attributes
instance_values
instance_values["attributes"]
end

def attributes=(hash)
Expand All @@ -21,4 +17,9 @@ def attributes=(hash)
send("#{new_key}=", value)
end
end

def self.from_json(data)
return unless data
self.new(**data)
end
end
92 changes: 29 additions & 63 deletions app/models/block.rb
Original file line number Diff line number Diff line change
@@ -1,77 +1,39 @@
class Block
include ActiveModel::API
include ActiveModel::Validations
include ActiveModel::Serializers::JSON

include Turbo::Broadcastable

include StringConcern

attr_accessor :baseFeePerGas, :difficulty, :extraData, :gasLimit, :gasUsed, :hash,
:logsBloom, :miner, :mixHash, :nonce, :number, :parentHash, :receiptsRoot,
:sha3Uncles, :size, :stateRoot, :timestamp, :transactionsRoot, :withdrawals,
:withdrawalsRoot, :transactions, :totalDifficulty, :uncles
class Block < ApplicationModel
attribute :baseFeePerGas, :hex_integer
attribute :difficulty, :hex_integer
attribute :extraData, :string
attribute :gasLimit, :hex_integer
attribute :gasUsed, :hex_integer
attribute :hash, :string
attribute :logsBloom, :string
attribute :miner, :string
attribute :mixHash, :string
attribute :nonce, :hex_integer
attribute :number, :hex_integer
attribute :parentHash, :string
attribute :receiptsRoot, :string
attribute :sha3Uncles, :string
attribute :size, :hex_integer
attribute :stateRoot, :string
attribute :timestamp, :hex_integer
attribute :transactionsRoot, :string
attribute :withdrawalsRoot, :string
attribute :totalDifficulty, :hex_integer

attr_accessor :withdrawals, :transactions, :uncles

NUMBER_FORMAT_REGEX = /-?\d+/

validates :hash, presence: true

def attributes
{
hash: hash,
number: number,
timestamp: timestamp,
miner: miner,
transactions: transactions,
withdrawals: withdrawals,
baseFeePerGas: baseFeePerGas,
totalDifficulty: totalDifficulty,
size: size
}
end

def totalDifficulty=(value)
@totalDifficulty = value.class == Integer ? value : value.to_i(16) rescue nil
end

def size=(value)
@size = value.class == Integer ? value : value.to_i(16)
end

def number=(value)
@number = value.class == Integer ? value : value.to_i(16)
end

def baseFeePerGas=(value)
@baseFeePerGas = value.class == Integer ? value : value.to_i(16)
end

def datetime
Time.at(self.timestamp)
end

def timestamp=(value)
@timestamp = value.class == Integer ? value : value.to_i(16)
end

def transactions=(value)
@transactions = value ? value : []
end

def broadcast_to_channel
ActionCable.server.broadcast('block_channel', { block: self })
end

def self.from_event(event)
from_json(event&.dig("params", "result"))
end

def self.from_json(data)
return unless data
Block.new(**data)
def datetime
Time.at(self.timestamp)
end


def seconds_ago
current_timestamp - self.timestamp
end
Expand All @@ -84,6 +46,10 @@ def gwei_baseFeePerGas
self.baseFeePerGas.to_d * 1e-9
end

def broadcast_to_channel
ActionCable.server.broadcast('block_channel', { block: self })
end

private
def current_timestamp
Time.now.to_i
Expand Down
44 changes: 0 additions & 44 deletions app/models/concerns/my_normalization_concern.rb

This file was deleted.

83 changes: 0 additions & 83 deletions app/models/concerns/normalization_concern.rb

This file was deleted.

9 changes: 0 additions & 9 deletions app/models/concerns/string_concern.rb

This file was deleted.

Loading

0 comments on commit 4894432

Please sign in to comment.