Skip to content

Commit

Permalink
eth: some docs and cleanups (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
q9f authored May 30, 2022
1 parent 53d7f36 commit 1f41b24
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 17 deletions.
7 changes: 0 additions & 7 deletions lib/eth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,7 @@ module Eth
require "eth/chain"
require "eth/constant"
require "eth/contract"
require "eth/contract/event"
require "eth/contract/function"
require "eth/contract/function_input"
require "eth/contract/function_output"
require "eth/contract/initializer"
require "eth/client"
require "eth/client/http"
require "eth/client/ipc"
require "eth/eip712"
require "eth/key"
require "eth/rlp"
Expand Down
8 changes: 6 additions & 2 deletions lib/eth/chain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ class ReplayProtectionError < StandardError; end
# Chain ID for Gnosis mainnet.
XDAI = 100.freeze

# Chain ID for the Polygon Matic mainnet.
MATIC = 137.freeze

# Chain ID for Arbitrum mainnet.
ARBITRUM = 42161.freeze

Expand Down Expand Up @@ -77,6 +80,9 @@ class ReplayProtectionError < StandardError; end
# Chain ID for Optimistic Goerli testnet.
GOERLI_OPTIMISM = 420.freeze

# Chain ID for the Polygon Mumbai testnet.
MUMBAI = 80001.freeze

# Chain ID for Arbitrum Rinkeby testnet.
RINKEBY_ARBITRUM = 421611.freeze

Expand All @@ -85,8 +91,6 @@ class ReplayProtectionError < StandardError; end

# Chain ID for the geth private network preset.
PRIVATE_GETH = 1337.freeze
MATIC = 137.freeze
MUMBAI = 80001.freeze

# Indicates wether the given `v` indicates a legacy chain value
# without EIP-155 replay protection.
Expand Down
10 changes: 7 additions & 3 deletions lib/eth/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def deploy(contract, *args, **kwargs)
})
end
unless kwargs[:sender_key].nil?
# use the provided key as sender and signer
# Uses the provided key as sender and signer
params.merge!({
from: kwargs[:sender_key].address,
nonce: get_nonce(kwargs[:sender_key].address),
Expand All @@ -212,7 +212,7 @@ def deploy(contract, *args, **kwargs)
tx.sign kwargs[:sender_key]
return eth_send_raw_transaction(tx.hex)["result"]
else
# use the default account as sender and external signer
# Uses the default account as sender and external signer
params.merge!({
from: default_account,
nonce: get_nonce(default_account),
Expand Down Expand Up @@ -413,7 +413,7 @@ def call_raw(contract, func, *args, **kwargs)
})
end
unless kwargs[:sender_key].nil?
# use the provided key as sender and signer
# Uses the provided key as sender and signer
params.merge!({
from: kwargs[:sender_key].address,
nonce: get_nonce(kwargs[:sender_key].address),
Expand Down Expand Up @@ -477,3 +477,7 @@ def marshal(params)
end
end
end

# Load the client/* libraries
require "eth/client/http"
require "eth/client/ipc"
7 changes: 7 additions & 0 deletions lib/eth/contract.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,10 @@ def parse_abi(abi)
end
end
end

# Load the contract/* libraries
require "eth/contract/event"
require "eth/contract/function"
require "eth/contract/function_input"
require "eth/contract/function_output"
require "eth/contract/initializer"
1 change: 1 addition & 0 deletions lib/eth/contract/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

# Provides the {Eth} module.
module Eth

# Provide classes for contract event.
class Contract::Event
attr_accessor :name, :signature, :input_types, :inputs, :event_string, :address
Expand Down
7 changes: 4 additions & 3 deletions lib/eth/contract/function.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

# Provides the {Eth} module.
module Eth

# Provides the methods for smart contract function.
class Contract::Function
attr_accessor :name, :inputs, :outputs, :signature, :constant, :function_string
Expand All @@ -36,7 +37,7 @@ def initialize(data)
@signature = self.class.encoded_function_signature(@function_string)
end

# Create function strings.
# Creates function strings.
#
# @param name [String] function name.
# @param inputs [Array<Eth::Contract::FunctionInput>] function input class list.
Expand All @@ -45,12 +46,12 @@ def self.calc_signature(name, inputs)
"#{name}(#{inputs.collect { |x| x.type }.join(",")})"
end

# encode function signature.
# Encodes a function signature.
#
# @param signature [String] function signature.
# @return [String] encoded function signature string.
def self.encoded_function_signature(signature)
Digest::Keccak.hexdigest(signature, 256)[0..7]
Util.bin_to_hex Util.keccak256(signature)[0..3]
end
end
end
3 changes: 2 additions & 1 deletion lib/eth/contract/function_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

# Provides the {Eth} module.
module Eth

# Provide classes for contract function input.
class Contract::FunctionInput
attr_accessor :type, :name
Expand All @@ -28,7 +29,7 @@ def initialize(data)
@name = data["name"]
end

# Returns types like uint256
# Returns complete types with subtypes, e.g., `uint256`.
def type
@type.base_type + @type.sub_type
end
Expand Down
5 changes: 5 additions & 0 deletions lib/eth/contract/function_output.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,20 @@

# Provides the {Eth} module.
module Eth

# Provide classes for contract function output.
class Contract::FunctionOutput
attr_accessor :type, :name

# Constructor of the {Eth::Contract::FunctionOutput} class.
#
# @param data [Hash] contract abi data.
def initialize(data)
@type = Eth::Abi::Type.parse(data["type"])
@name = data["name"]
end

# Returns complete types with subtypes, e.g., `uint256`.
def type
@type.base_type + @type.sub_type
end
Expand Down
3 changes: 2 additions & 1 deletion lib/eth/contract/initializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

# Provides the {Eth} module.
module Eth

# Provide classes for contract initializer.
class Contract::Initializer
attr_accessor :contracts, :file
Expand All @@ -36,7 +37,7 @@ def initialize(file)
end
end

# Build and return all contracts.
# Builds and returns all contracts.
def build_all
@contracts.each do |contract|
contract.build
Expand Down

0 comments on commit 1f41b24

Please sign in to comment.