Skip to content

Commit 2389c5c

Browse files
authored
chore: run rufo, add docs (#332)
* gem: run rufo * spec: add tests for event spec * eth/tx: add docs for eip7702 * eth/chain: add hoodi testnet * eth: add more docs * eth/tx: fix tests for 7702 * gem: run rufo
1 parent 80bad8a commit 2389c5c

File tree

8 files changed

+82
-15
lines changed

8 files changed

+82
-15
lines changed

lib/eth/abi/event.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ def signature(interface)
4444
"#{name}(#{types.join(",")})"
4545
end
4646

47+
# Gets the input type for events.
48+
#
49+
# @param input [Hash] events input.
50+
# @return [String] input type.
4751
def type(input)
4852
if input["type"] == "tuple"
4953
"(#{input["components"].map { |c| type(c) }.join(",")})"

lib/eth/address.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ module Eth
1717

1818
# The {Eth::Address} class to handle checksummed Ethereum addresses.
1919
class Address
20+
21+
# The literal zero address 0x0.
2022
ZERO = "0x0000000000000000000000000000000000000000"
2123

2224
# Provides a special checksum error if EIP-55 is violated.

lib/eth/chain.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,16 @@ class ReplayProtectionError < StandardError; end
149149
# Chain ID for Arbitrum Goerli testnet.
150150
GOERLI_ARBITRUM = 421613.freeze
151151

152+
# Chain ID for Hoodi testnet.
153+
HOODI = 560048.freeze
154+
152155
# Chain ID for Sepolia testnet.
153156
SEPOLIA = 11155111.freeze
154157

155158
# Chain ID for Holesovice testnet.
156159
HOLESOVICE = 11166111.freeze
160+
161+
# Chain ID for Holesovice testnet.
157162
HOLESKY = HOLESOVICE
158163

159164
# Chain ID for the geth private network preset.

lib/eth/contract/event.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

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

17+
# Provides the {Eth} module.
1718
module Eth
1819
# Provide classes for contract event.
1920
class Contract::Event
@@ -73,7 +74,6 @@ def set_address(address)
7374
@address = address ? Eth::Address.new(address).address : nil
7475
end
7576

76-
7777
# Decodes event parameters from logs.
7878
#
7979
# @param topics [Array<String>] The list of log topics, including the event selector.
@@ -90,8 +90,8 @@ def decode_params(topics, data = "0x")
9090
result
9191
end,
9292
**Hash[non_indexed_inputs.map { _1["name"] }.zip(
93-
Eth::Abi.decode(non_indexed_inputs.map { |i| i["type"] }, data)
94-
)],
93+
Eth::Abi.decode(non_indexed_inputs.map { |i| i["type"] }, data)
94+
)],
9595
}
9696
end
9797

@@ -107,4 +107,3 @@ def type_name(x)
107107
end
108108
end
109109
end
110-
# Provides the {Eth} module.

lib/eth/tx.rb

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ class ParameterError < TypeError; end
7373
# The EIP-1559 transaction type is 2.
7474
TYPE_1559 = 0x02.freeze
7575

76+
# The EIP-4844 transaction type is 3.
77+
TYPE_4844 = 0x03.freeze
78+
7679
# The EIP-7702 transaction type is 4.
7780
TYPE_7702 = 0x04.freeze
7881

@@ -84,18 +87,23 @@ class ParameterError < TypeError; end
8487

8588
# Creates a new transaction of any type for given parameters and chain ID.
8689
# Required parameters are (optional in brackets):
87-
# - EIP-7702: chain_id, nonce, priority_fee, max_gas_fee, gas_limit, authorizations(, from, to,
88-
# value, data, access_list)
8990
# - EIP-1559: chain_id, nonce, priority_fee, max_gas_fee, gas_limit(, from, to,
9091
# value, data, access_list)
9192
# - EIP-2930: chain_id, nonce, gas_price, gas_limit, access_list(, from, to,
9293
# value, data)
94+
# - EIP-7702: chain_id, nonce, priority_fee, max_gas_fee, gas_limit, authorizations(, from, to,
95+
# value, data, access_list)
9396
# - Legacy: nonce, gas_price, gas_limit(, from, to, value, data)
9497
#
9598
# @param params [Hash] all necessary transaction fields.
9699
# @param chain_id [Integer] the EIP-155 Chain ID (legacy transactions only).
97100
def new(params, chain_id = Chain::ETHEREUM)
98101

102+
# if we deal with blobs, attempt EIP-4844 (not implemented)
103+
unless params[:max_fee_per_blob_gas].nil?
104+
raise NotimplementedError, "EIP-4844 blob transactions are not implemented"
105+
end
106+
99107
# if we deal with authorizations, attempt EIP-7702
100108
unless params[:authorization_list].nil?
101109
params[:chain_id] = chain_id if params[:chain_id].nil?
@@ -129,10 +137,6 @@ def decode(hex)
129137
type = hex[0, 2].to_i(16)
130138

131139
case type
132-
when TYPE_7702
133-
134-
# EIP-1559 transaction (type 2)
135-
return Tx::Eip7702.decode hex
136140
when TYPE_1559
137141

138142
# EIP-1559 transaction (type 2)
@@ -141,6 +145,14 @@ def decode(hex)
141145

142146
# EIP-2930 transaction (type 1)
143147
return Tx::Eip2930.decode hex
148+
when TYPE_4844
149+
150+
# EIP-4844 transaction (type 3)
151+
raise NotimplementedError, "EIP-4844 blob transactions are not implemented"
152+
when TYPE_7702
153+
154+
# EIP-7702 transaction (type 4)
155+
return Tx::Eip7702.decode hex
144156
else
145157

146158
# Legacy transaction if first byte is RLP (>= 192)
@@ -159,8 +171,6 @@ def decode(hex)
159171
# @raise [TransactionTypeError] if the transaction type is unknown.
160172
def unsigned_copy(tx)
161173
case tx.type
162-
when TYPE_7702
163-
return Tx::Eip7702.unsigned_copy tx
164174
when TYPE_1559
165175

166176
# EIP-1559 transaction (type 2)
@@ -169,6 +179,14 @@ def unsigned_copy(tx)
169179

170180
# EIP-2930 transaction (type 1)
171181
return Tx::Eip2930.unsigned_copy tx
182+
when TYPE_4844
183+
184+
# EIP-4844 transaction (type 3)
185+
raise NotimplementedError, "EIP-4844 blob transactions are not implemented"
186+
when TYPE_7702
187+
188+
# EIP-7702 transaction (type 4)
189+
return Tx::Eip7702.unsigned_copy tx
172190
when TYPE_LEGACY
173191

174192
# Legacy transaction ("type 0")

lib/eth/tx/eip7702.rb

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,19 @@ module Tx
2222
# types and envelopes.
2323
# Ref: https://eips.ethereum.org/EIPS/eip-7702
2424
class Eip7702
25+
26+
# Provides an EIP-7702 authorization that store the address to
27+
# code which the signer desires to execute in the context of their EOA.
2528
class Authorization
29+
30+
# The EIP-155 Chain ID.
31+
# Ref: https://eips.ethereum.org/EIPS/eip-155
2632
attr_reader :chain_id
2733

34+
# The authority addess.
2835
attr_reader :address
2936

37+
# The transaction nonce.
3038
attr_reader :nonce
3139

3240
# The signature's y-parity byte (not v).
@@ -38,6 +46,14 @@ class Authorization
3846
# The signature `s` value.
3947
attr_reader :signature_s
4048

49+
# Create a type-4 (EIP-7702) authorization object that
50+
# can be prepared for type-4 transactions.
51+
# Ref: https://eips.ethereum.org/EIPS/eip-7702
52+
#
53+
# @param fields [Hash] all necessary transaction fields.
54+
# @option fields [Integer] :chain_id the chain ID.
55+
# @option fields [Eth::Address] :address the authority address.
56+
# @option fields [Integer] :nonce the transaction nonce.
4157
def initialize(fields)
4258
@chain_id = fields[:chain_id].to_i
4359
@address = fields[:address].to_s
@@ -75,6 +91,9 @@ def sign(key)
7591
return hash
7692
end
7793

94+
# Encodes the unsigned authorization payload required for signing.
95+
#
96+
# @return [String] an RLP-encoded, unsigned, enveloped EIP-7702 transaction.
7897
def unsigned_encoded
7998
authorization_data = []
8099
authorization_data.push Util.serialize_int_to_big_endian @chain_id
@@ -83,10 +102,16 @@ def unsigned_encoded
83102
Rlp.encode authorization_data
84103
end
85104

105+
# Gets the sign-hash required to sign.
106+
#
107+
# @return [String] a Keccak-256 hash.
86108
def unsigned_hash
87109
Util.keccak256 unsigned_encoded
88110
end
89111

112+
# Gets the raw serialized authorization data.
113+
#
114+
# @return [Bytes] raw serialized authorization data.
90115
def raw
91116
authorization_data = []
92117
authorization_data.push Util.serialize_int_to_big_endian @chain_id
@@ -99,6 +124,9 @@ def raw
99124
authorization_data
100125
end
101126

127+
# Compares two authorization data objects.
128+
#
129+
# @return [Bool] true if objects are same and share same state.
102130
def ==(o)
103131
o.class == self.class && o.state == state
104132
end
@@ -139,7 +167,7 @@ def state
139167
# Ref: https://eips.ethereum.org/EIPS/eip-2930
140168
attr_reader :access_list
141169

142-
# The list of authorizations (a list of Eth::Tx::Eip7702::Authorization instances)
170+
# The list of of {Authorization} instances
143171
attr_reader :authorization_list
144172

145173
# The signature's y-parity byte (not v).

spec/eth/chain_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
expect(Chain::MUMBAI).to eq 80001
4949
expect(Chain::RINKEBY_ARBITRUM).to eq 421611
5050
expect(Chain::GOERLI_ARBITRUM).to eq 421613
51+
expect(Chain::HOODI).to eq 560048
5152
expect(Chain::SEPOLIA).to eq 11155111
5253
expect(Chain::HOLESOVICE).to eq 11166111
5354
expect(Chain::HOLESKY).to eq 11166111

spec/eth/contract/event_spec.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,9 @@
103103
{ "indexed" => true, "name" => "collection", "type" => "address" },
104104
{ "indexed" => true, "name" => "tokenId", "type" => "uint256" },
105105
{ "indexed" => true, "name" => "ipfsHash", "type" => "bytes32" },
106-
{ "indexed" => false, "name" => "to", "type" => "address" }
107-
]
106+
{ "indexed" => false, "name" => "to", "type" => "address" },
107+
],
108+
"type" => "tuple",
108109
})
109110
end
110111

@@ -126,6 +127,15 @@
126127
"0x000000000000000000000000f0d00750656f12ab7550bf5039d74691f9e461f0"
127128
end
128129

130+
it "correctly serves accessors" do
131+
expect(event.name).to eq "TokenMint"
132+
expect(event.input_types).to eq ["address", "uint256", "bytes32", "address"]
133+
expect(event.inputs).to eq ["collection", "tokenId", "ipfsHash", "to"]
134+
expect(event.event_string).to eq "TokenMint(address,uint256,bytes32,address)"
135+
expect(event.signature).to eq Util.remove_hex_prefix topics.first
136+
expect(event.address).to be_nil
137+
end
138+
129139
it "correctly decodes the event parameters" do
130140
decoded = event.decode_params(
131141
topics,

0 commit comments

Comments
 (0)