Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

eth/chain: add sepolia chain id; docs #60

Merged
merged 4 commits into from
Jan 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .yardopts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--verbose --fail-on-warning --markup markdown --embed-mixins
4 changes: 2 additions & 2 deletions lib/eth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# Provides the `Eth` module.
# Provides the {Eth} module.
module Eth
end

# Loads the `Eth` module classes.
# Loads the {Eth} module classes.
require "eth/abi"
require "eth/api"
require "eth/address"
Expand Down
6 changes: 3 additions & 3 deletions lib/eth/abi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

require "eth/abi/type"

# Provides the `Eth` module.
# Provides the {Eth} module.
module Eth

# Provides a Ruby implementation of the Ethereum Applicatoin Binary Interface (ABI).
Expand Down Expand Up @@ -69,7 +69,7 @@ def encode(types, args)
# Encodes a specific value, either static or dynamic.
#
# @param type [Eth::Abi::Type] type to be encoded.
# @param arg [String, Number] value to be encoded.
# @param arg [String|Number] value to be encoded.
# @return [String] the encoded type.
# @raise [EncodingError] if value does not match type.
def encode_type(type, arg)
Expand Down Expand Up @@ -111,7 +111,7 @@ def encode_type(type, arg)
# Encodes primitive types.
#
# @param type [Eth::Abi::Type] type to be encoded.
# @param arg [String, Number] value to be encoded.
# @param arg [String|Number] value to be encoded.
# @return [String] the encoded primitive type.
# @raise [EncodingError] if value does not match type.
# @raise [ValueOutOfBounds] if value is out of bounds for type.
Expand Down
14 changes: 7 additions & 7 deletions lib/eth/abi/type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

# -*- encoding : ascii-8bit -*-

# Provides the `Eth` module.
# Provides the {Eth} module.
module Eth

# Provides a Ruby implementation of the Ethereum Applicatoin Binary Interface (ABI).
Expand All @@ -26,7 +26,7 @@ class Type
# Provides a specific parser error if type cannot be determined.
class ParseError < StandardError; end

# The base attribute, e.g., `string` or `bytes`
# The base attribute, e.g., `string` or `bytes`.
attr :base_type

# The sub-type attribute, e.g., `256` as size of an uint256.
Expand All @@ -36,7 +36,7 @@ class ParseError < StandardError; end
attr :dimensions

# Create a new Type object for base types, sub types, and dimensions.
# Should use the `Type.parse` parser.
# Should not be used; use {Type.parse} instead.
#
# @param base_type [String] the base-type attribute.
# @param sub_type [String] the sub-type attribute.
Expand Down Expand Up @@ -75,7 +75,7 @@ def parse(type)
@dimensions = dims.map { |x| x[1...-1].to_i }
end

# Creata new uint256 type used for size.
# Creates a new uint256 type used for size.
#
# @return [Eth::Abi::Type] a uint256 size type.
def self.size_type
Expand All @@ -85,7 +85,7 @@ def self.size_type
# Compares two types for their attributes.
#
# @param another_type [Eth::Abi::Type] another type to be compared.
# @return [Bool] true if all attributes match.
# @return [Boolean] true if all attributes match.
def ==(another_type)
base_type == another_type.base_type and
sub_type == another_type.sub_type and
Expand All @@ -111,9 +111,9 @@ def size
@size ||= s
end

# Helper to determine whether array is of dynamic size.
# Helpes to determine whether array is of dynamic size.
#
# @return [Bool] true if array is of dynamic size.
# @return [Boolean] true if array is of dynamic size.
def is_dynamic?
size.nil?
end
Expand Down
10 changes: 5 additions & 5 deletions lib/eth/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# Provides the `Eth` module.
# Provides the {Eth} module.
module Eth

# The `Eth::Address` class to handle checksummed Ethereum addresses.
# The {Eth::Address} class to handle checksummed Ethereum addresses.
class Address

# Provides a special checksum error if EIP-55 is violated.
Expand All @@ -24,7 +24,7 @@ class CheckSumError < StandardError; end
# The prefixed and checksummed Ethereum address.
attr_reader :address

# Constructor of the `Eth::Address` class. Creates a new hex
# Constructor of the {Eth::Address} class. Creates a new hex
# prefixed address.
#
# @param address [String] hex string representing an ethereum address.
Expand All @@ -38,9 +38,9 @@ def initialize(address)
end
end

# Check that the address is valid.
# Checks that the address is valid.
#
# @return [Bool] true if valid address.
# @return [Boolean] true if valid address.
def valid?
if !matches_any_format?
false
Expand Down
2 changes: 1 addition & 1 deletion lib/eth/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# Provides the `Eth` module.
# Provides the {Eth} module.
module Eth

# Provides the `Eth::Api` module grouping known RPC commands.
Expand Down
59 changes: 31 additions & 28 deletions lib/eth/chain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,90 +12,93 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# Provides the `Eth` module.
# Provides the {Eth} module.
module Eth

# Encapsulates `Chain` IDs and utilities for EIP-155 compatibility.
# ref: https://eips.ethereum.org/EIPS/eip-155
# Encapsulates {Eth::Chain} IDs and utilities for EIP-155 compatibility.
# Ref: https://eips.ethereum.org/EIPS/eip-155
module Chain
extend self

# Provides a special replay protection error if EIP-155 is violated.
class ReplayProtectionError < StandardError; end

# Chain ID for Ethereum mainnet
# Chain ID for Ethereum mainnet.
ETHEREUM = 1.freeze

# Chain ID for Expanse mainnet
# Chain ID for Expanse mainnet.
EXPANSE = 2.freeze

# Chain ID for Optimistic Ethereum mainnet
# Chain ID for Optimistic Ethereum mainnet.
OPTIMISM = 10.freeze

# Chain ID for Ethereum Classic mainnet
# Chain ID for Ethereum Classic mainnet.
CLASSIC = 61.freeze

# Chain ID for POA Network mainnet
# Chain ID for POA Network mainnet.
POA_NET = 99.freeze

# Chain ID for xDAI mainnet
# Chain ID for Gnosis mainnet.
XDAI = 100.freeze

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

# Chain ID for Morden (Ethereum) testnet
# Chain ID for Morden (Ethereum) testnet.
MORDEN = 2.freeze

# Chain ID for Ropsten testnet
# Chain ID for Ropsten testnet.
ROPSTEN = 3.freeze

# Chain ID for Rinkeby testnet
# Chain ID for Rinkeby testnet.
RINKEBY = 4.freeze

# Chain ID for Goerli testnet
# Chain ID for Goerli testnet.
GOERLI = 5.freeze

# Chain ID for Kotti testnet
# Chain ID for Kotti testnet.
KOTTI = 6.freeze

# Chain ID for Kovan testnet
# Chain ID for Kovan testnet.
KOVAN = 42.freeze

# Chain ID for Morden (Classic) testnet
# Chain ID for Morden (Classic) testnet.
MORDEN_CLASSIC = 62.freeze

# Chain ID for Mordor testnet
# Chain ID for Mordor testnet.
MORDOR = 63.freeze

# Chain ID for Optimistik Kovan testnet
# Chain ID for Optimistik Kovan testnet.
KOVAN_OPTIMISM = 69.freeze

# Chain ID for Arbitrum xDAI testnet
# Chain ID for Arbitrum xDAI testnet.
XDAI_ARBITRUM = 200.freeze

# Chain ID for Optimistic Goerli testnet
# Chain ID for Optimistic Goerli testnet.
GOERLI_OPTIMISM = 420.freeze

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

# Chain ID for the geth private network preset
# Chain ID for Sepolia testnet.
SEPOLIA = 11155111.freeze

# Chain ID for the geth private network preset.
PRIVATE_GETH = 1337.freeze

# Indicates wether the given `v` indicates a legacy chain value
# without EIP-155 replay protection.
#
# @param v [Integer] the signature's `v` value
# @return [Boolean] true if legacy value
# @param v [Integer] the signature's `v` value.
# @return [Boolean] true if legacy value.
def is_legacy?(v)
[27, 28].include? v
end

# Convert a given `v` value to an ECDSA recovery id for the given
# EIP-155 chain ID.
#
# @param v [Integer] the signature's `v` value
# @param v [Integer] the signature's `v` value.
# @param chain_id [Integer] the chain id the signature was generated on.
# @return [Integer] the recovery id corresponding to `v`.
# @raise [ReplayProtectionError] if the given `v` is invalid.
Expand Down Expand Up @@ -134,10 +137,10 @@ def to_v(recovery_id, chain_id = nil)
end

# Converst a `v` value into a chain ID. This does not work for legacy signatures
# with v < 36 that do not conform with EIP-155.
# with `v < 36` that do not conform with EIP-155.
#
# @param v [Integer] the signature's `v` value.
# @return [Integer] the chain id as per EIP-155 or nil if there is no replay protection.
# @return [Integer] the chain id as per EIP-155 or `nil` if there is no replay protection.
def to_chain_id(v)
return nil if v < 36
chain_id = (v - 35) / 2
Expand Down
19 changes: 10 additions & 9 deletions lib/eth/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# Provides the `Eth` module.
# Provides the {Eth} module.
module Eth

# Provides the `Eth::Client` super-class to connect to Ethereum
# Provides the {Eth::Client} super-class to connect to Ethereum
# network's RPC-API endpoints (IPC or HTTP).
class Client

Expand All @@ -28,13 +28,13 @@ class Client
# The connected network's client coinbase.
attr_accessor :default_account

# The transaction max priority fee per gas in Wei.
# The default transaction max priority fee per gas in Wei.
attr_accessor :max_priority_fee_per_gas

# The transaction max fee per gas in Wei.
# The default transaction max fee per gas in Wei.
attr_accessor :max_fee_per_gas

# The gas limit for the transaction.
# The default gas limit for the transaction.
attr_accessor :gas_limit

# Creates a new RPC-Client, either by providing an HTTP/S host or
Expand All @@ -50,7 +50,8 @@ def self.create(host)
raise ArgumentError, "Unable to detect client type!"
end

# Constructor for the Eth::Client super-class.
# Constructor for the {Eth::Client} super-class. Should not be used;
# use {Client.create} intead.
def initialize(_)
@id = 0
@max_priority_fee_per_gas = 0
Expand Down Expand Up @@ -95,7 +96,7 @@ def get_nonce(address)
# @param destination [Eth::Address] the destination address.
# @param amount [Integer] the transfer amount in Wei.
# @param sender_key [Eth::Key] the sender private key.
# @param legacy [Bool] enables legacy transactions (pre-EIP-1559).
# @param legacy [Boolean] enables legacy transactions (pre-EIP-1559).
# @return [String] the transaction hash.
def transfer_and_wait(destination, amount, sender_key = nil, legacy = false)
wait_for_tx(transfer(destination, amount, sender_key, legacy))
Expand All @@ -108,7 +109,7 @@ def transfer_and_wait(destination, amount, sender_key = nil, legacy = false)
# @param destination [Eth::Address] the destination address.
# @param amount [Integer] the transfer amount in Wei.
# @param sender_key [Eth::Key] the sender private key.
# @param legacy [Bool] enables legacy transactions (pre-EIP-1559).
# @param legacy [Boolean] enables legacy transactions (pre-EIP-1559).
# @return [String] the transaction hash.
def transfer(destination, amount, sender_key = nil, legacy = false)
params = {
Expand Down Expand Up @@ -159,7 +160,7 @@ def reset_id
# Checkes wether a transaction is mined or not.
#
# @param hash [String] the transaction hash.
# @return [Bool] true if included in a block.
# @return [Boolean] true if included in a block.
def is_mined_tx?(hash)
mined_tx = eth_get_transaction_by_hash hash
!mined_tx.nil? && !mined_tx["result"].nil? && !mined_tx["result"]["blockNumber"].nil?
Expand Down
5 changes: 3 additions & 2 deletions lib/eth/client/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

require "net/http"

# Provides the `Eth` module.
# Provides the {Eth} module.
module Eth

# Provides an HTTP/S-RPC client.
Expand All @@ -32,7 +32,8 @@ class Client::Http < Client
# Attribute indicator for SSL.
attr_reader :ssl

# Constructor for the HTTP Client.
# Constructor for the HTTP Client. Should not be used; use
# {Client.create} intead.
#
# @param host [String] an URI pointing to an HTTP RPC-API.
def initialize(host)
Expand Down
5 changes: 3 additions & 2 deletions lib/eth/client/ipc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

require "socket"

# Provides the `Eth` module.
# Provides the {Eth} module.
module Eth

# Provides an IPC-RPC client.
Expand All @@ -23,7 +23,8 @@ class Client::Ipc < Client
# The path of the IPC socket.
attr_accessor :path

# Constructor for the IPC Client.
# Constructor for the IPC Client. Should not be used; use
# {Client.create} intead.
#
# @param path [String] an URI pointing to an IPC RPC-API.
def initialize(path)
Expand Down
Loading