Skip to content

Commit 7f41d11

Browse files
authored
Refactor and remove unnecessary hex encode/decode (#185)
1 parent c87efb2 commit 7f41d11

11 files changed

+57
-32
lines changed

CHANGELOG.md

+11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
### Breaking Changes
6+
7+
- `Ethers.TxData` uses hex_decoded values instead of hex encoded ones
8+
9+
### Enhancements
10+
11+
- Update `Ethers` module to RPCfy eth_call request params
12+
- Removed unnecessary hex decode/encodes in requests
13+
314
## v0.6.3 (2025-01-21)
415

516
### Enahncements

lib/ethers.ex

+4-2
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,7 @@ defmodule Ethers do
681681
tx_params = TxData.to_map(tx_data, overrides)
682682

683683
case check_params(tx_params, :call) do
684-
:ok -> {:ok, tx_params, block}
684+
:ok -> {:ok, Transaction.to_rpc_map(tx_params), block}
685685
err -> err
686686
end
687687
end
@@ -704,9 +704,11 @@ defmodule Ethers do
704704
defp pre_process(contract_binary, overrides, :deploy = _action, opts) do
705705
{encoded_constructor, overrides} = Keyword.pop(overrides, :encoded_constructor)
706706

707+
encoded_constructor = encoded_constructor || ""
708+
707709
tx_params =
708710
Enum.into(overrides, %{
709-
data: "0x#{contract_binary}#{encoded_constructor}",
711+
data: contract_binary <> encoded_constructor,
710712
to: nil
711713
})
712714

lib/ethers/ccip_read.ex

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ defmodule Ethers.CcipRead do
5454
end)
5555
|> resolve_first(error) do
5656
data = ABI.TypeEncoder.encode([data, error.extra_data], [:bytes, :bytes])
57-
tx_data = %{tx_data | data: Utils.hex_encode(error.callback_function <> data)}
57+
tx_data = %{tx_data | data: error.callback_function <> data}
5858
Ethers.call(tx_data, opts)
5959
end
6060
end

lib/ethers/contract.ex

-2
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,6 @@ defmodule Ethers.Contract do
209209

210210
unquote(Macro.escape(selector))
211211
|> ABI.encode(args)
212-
|> Ethers.Utils.hex_encode(false)
213212
end
214213
end
215214
end
@@ -250,7 +249,6 @@ defmodule Ethers.Contract do
250249
|> Enum.map(fn {arg, type} -> Ethers.Utils.prepare_arg(arg, type) end)
251250

252251
ABI.encode(selector, args)
253-
|> Ethers.Utils.hex_encode()
254252
|> Ethers.TxData.new(selector, __default_address__(), __MODULE__)
255253
end
256254
end

lib/ethers/contract_helpers.ex

+4-1
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,10 @@ defmodule Ethers.ContractHelpers do
419419
end
420420

421421
defp maybe_read_contract_binary(:abi, abi) when is_list(abi), do: nil
422-
defp maybe_read_contract_binary(:abi, %{"bin" => bin}) when is_binary(bin), do: bin
422+
423+
defp maybe_read_contract_binary(:abi, %{"bin" => bin}) when is_binary(bin),
424+
do: Ethers.Utils.hex_decode!(bin)
425+
423426
defp maybe_read_contract_binary(:abi, map) when is_map(map), do: nil
424427
defp maybe_read_contract_binary(:abi, abi) when is_atom(abi), do: nil
425428

lib/ethers/multicall.ex

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ defmodule Ethers.Multicall do
2424
```
2525
"""
2626

27-
import Ethers.Utils, only: [hex_decode!: 1, human_arg: 2]
27+
import Ethers.Utils, only: [human_arg: 2]
2828

2929
alias Ethers.Contracts.Multicall3
3030
alias Ethers.TxData
@@ -98,7 +98,7 @@ defmodule Ethers.Multicall do
9898
def aggregate3_encode_data(data)
9999

100100
def aggregate3_encode_data({%TxData{data: data} = tx_data, opts}) do
101-
{fetch_address!(tx_data, opts), Keyword.get(opts, :allow_failure, true), hex_decode!(data)}
101+
{fetch_address!(tx_data, opts), Keyword.get(opts, :allow_failure, true), data}
102102
end
103103

104104
def aggregate3_encode_data(%TxData{} = tx_data), do: aggregate3_encode_data({tx_data, []})
@@ -163,7 +163,7 @@ defmodule Ethers.Multicall do
163163
def aggregate2_encode_data(data)
164164

165165
def aggregate2_encode_data({%TxData{data: data} = tx_data, opts}) do
166-
{fetch_address!(tx_data, opts), hex_decode!(data)}
166+
{fetch_address!(tx_data, opts), data}
167167
end
168168

169169
def aggregate2_encode_data(%TxData{} = tx_data), do: aggregate2_encode_data({tx_data, []})

lib/ethers/transaction.ex

+4-10
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,7 @@ defmodule Ethers.Transaction do
8484
def new(params) do
8585
case Map.fetch(params, :type) do
8686
{:ok, type} when type in @transaction_types ->
87-
input =
88-
if input_hex = Map.get(params, :input, Map.get(params, :data)) do
89-
Utils.hex_decode!(input_hex)
90-
end
91-
9287
params
93-
|> Map.put(:input, input)
9488
|> type.new()
9589
|> maybe_wrap_signed(params)
9690

@@ -308,13 +302,13 @@ defmodule Ethers.Transaction do
308302
t -> t
309303
end)
310304
|> Enum.map(fn
311-
{field, "0x" <> _ = value} ->
312-
{field, value}
313-
314305
{field, nil} ->
315306
{field, nil}
316307

317-
{field, input} when field in [:data, :input, :from, :to] ->
308+
{field, "0x" <> _ = value} when field in [:from, :to] ->
309+
{field, value}
310+
311+
{field, input} when field in [:data, :input] ->
318312
{field, Utils.hex_encode(input)}
319313

320314
{field, value} when is_integer(value) ->

lib/ethers/tx_data.ex

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ defmodule Ethers.TxData do
9090
alias Ethers.Utils
9191

9292
def inspect(%{selector: selector, data: data, default_address: default_address}, opts) do
93-
arguments = ABI.decode(selector, Utils.hex_decode!(data), :input)
93+
arguments = ABI.decode(selector, data, :input)
9494

9595
arguments_doc =
9696
Enum.zip([selector.types, input_names(selector), arguments])

test/ethers/counter_contract_test.exs

+18-7
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ defmodule Ethers.CounterContractTest do
1010
import Ethers.TestHelpers
1111

1212
alias Ethers.Event
13+
alias Ethers.Utils
1314

1415
alias Ethers.Contract.Test.CounterContract
1516

@@ -122,7 +123,7 @@ defmodule Ethers.CounterContractTest do
122123
test "calling view functions", %{address: address} do
123124
assert %Ethers.TxData{
124125
base_module: CounterContract,
125-
data: "0x6d4ce63c",
126+
data: Utils.hex_decode!("0x6d4ce63c"),
126127
selector: %ABI.FunctionSelector{
127128
function: "get",
128129
method_id: <<109, 76, 230, 60>>,
@@ -142,7 +143,9 @@ defmodule Ethers.CounterContractTest do
142143
end
143144

144145
test "sending transaction with state mutating functions", %{address: address} do
145-
{:ok, tx_hash} = CounterContract.set(101) |> Ethers.send_transaction(from: @from, to: address)
146+
{:ok, tx_hash} =
147+
CounterContract.set(101) |> Ethers.send_transaction(from: @from, to: address)
148+
146149
wait_for_transaction!(tx_hash)
147150

148151
{:ok, 101} = CounterContract.get() |> Ethers.call(to: address)
@@ -160,7 +163,9 @@ defmodule Ethers.CounterContractTest do
160163

161164
test "returns error if to address is not given" do
162165
assert {:error, :no_to_address} = CounterContract.get() |> Ethers.call()
163-
assert {:error, :no_to_address} = CounterContract.set(101) |> Ethers.send_transaction(from: @from)
166+
167+
assert {:error, :no_to_address} =
168+
CounterContract.set(101) |> Ethers.send_transaction(from: @from)
164169

165170
assert {:error, :no_to_address} =
166171
CounterContract.set(101) |> Ethers.send_transaction(from: @from, gas: 100)
@@ -203,7 +208,10 @@ defmodule Ethers.CounterContractTest do
203208
test "returns the params when called" do
204209
assert %Ethers.TxData{
205210
base_module: CounterContract,
206-
data: "0x60fe47b10000000000000000000000000000000000000000000000000000000000000065",
211+
data:
212+
Utils.hex_decode!(
213+
"0x60fe47b10000000000000000000000000000000000000000000000000000000000000065"
214+
),
207215
selector: %ABI.FunctionSelector{
208216
function: "set",
209217
method_id: <<96, 254, 71, 177>>,
@@ -223,7 +231,8 @@ defmodule Ethers.CounterContractTest do
223231
setup :deploy_counter_contract
224232

225233
test "can get the emitted event with the correct filter", %{address: address} do
226-
{:ok, tx_hash} = CounterContract.set(101) |> Ethers.send_transaction(from: @from, to: address)
234+
{:ok, tx_hash} =
235+
CounterContract.set(101) |> Ethers.send_transaction(from: @from, to: address)
227236

228237
wait_for_transaction!(tx_hash)
229238

@@ -245,7 +254,8 @@ defmodule Ethers.CounterContractTest do
245254
end
246255

247256
test "cat get the emitted events with get_logs! function", %{address: address} do
248-
{:ok, tx_hash} = CounterContract.set(101) |> Ethers.send_transaction(from: @from, to: address)
257+
{:ok, tx_hash} =
258+
CounterContract.set(101) |> Ethers.send_transaction(from: @from, to: address)
249259

250260
wait_for_transaction!(tx_hash)
251261

@@ -275,7 +285,8 @@ defmodule Ethers.CounterContractTest do
275285
end
276286

277287
test "can filter logs with from_block and to_block options", %{address: address} do
278-
{:ok, tx_hash} = CounterContract.set(101) |> Ethers.send_transaction(from: @from, to: address)
288+
{:ok, tx_hash} =
289+
CounterContract.set(101) |> Ethers.send_transaction(from: @from, to: address)
279290

280291
wait_for_transaction!(tx_hash)
281292

test/ethers/signer/json_rpc_test.exs

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ defmodule Ethers.Signer.JsonRPCTest do
22
use ExUnit.Case
33

44
alias Ethers.Signer
5+
alias Ethers.Utils
56

67
describe "sign_transaction/2" do
78
test "signs the transaction with the correct data" do
89
transaction = %Ethers.Transaction.Eip1559{
910
to: "0xFFcf8FDEE72ac11b5c542428B35EEF5769C409f0",
10-
input: "0x06fdde03",
11+
input: Utils.hex_decode!("0x06fdde03"),
1112
value: 0,
1213
chain_id: 31_337,
1314
nonce: 2918,
@@ -24,7 +25,7 @@ defmodule Ethers.Signer.JsonRPCTest do
2425
test "fails signing transaction with wrong from address" do
2526
transaction = %Ethers.Transaction.Eip1559{
2627
to: "0xFFcf8FDEE72ac11b5c542428B35EEF5769C409f0",
27-
input: "0x06fdde03",
28+
input: Utils.hex_decode!("0x06fdde03"),
2829
value: 0,
2930
chain_id: 31_337,
3031
nonce: 2918,

test/ethers_test.exs

+8-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ defmodule EthersTest do
1616

1717
import Ethers.TestHelpers
1818

19+
alias Ethers.Utils
20+
1921
alias Ethers.Contract.Test.HelloWorldContract
2022
alias Ethers.Contract.Test.HelloWorldWithDefaultAddressContract
2123
alias Ethers.ExecutionError
@@ -354,7 +356,7 @@ defmodule EthersTest do
354356
test "is included in the function calls when has default address" do
355357
assert %Ethers.TxData{
356358
base_module: HelloWorldWithDefaultAddressContract,
357-
data: "0xef5fb05b",
359+
data: Utils.hex_decode!("0xef5fb05b"),
358360
selector: %ABI.FunctionSelector{
359361
function: "sayHello",
360362
method_id: <<239, 95, 176, 91>>,
@@ -369,15 +371,18 @@ defmodule EthersTest do
369371
default_address: "0x1000bf6a479f320ead074411a4b0e7944ea8c9c1"
370372
} == HelloWorldWithDefaultAddressContract.say_hello()
371373

372-
assert %{data: "0xef5fb05b", to: "0x1000bf6a479f320ead074411a4b0e7944ea8c9c1"} ==
374+
assert %{
375+
data: Utils.hex_decode!("0xef5fb05b"),
376+
to: "0x1000bf6a479f320ead074411a4b0e7944ea8c9c1"
377+
} ==
373378
HelloWorldWithDefaultAddressContract.say_hello()
374379
|> Ethers.TxData.to_map([])
375380
end
376381

377382
test "is not included in the function calls when does not have default address" do
378383
assert %Ethers.TxData{
379384
base_module: HelloWorldContract,
380-
data: "0xef5fb05b",
385+
data: Utils.hex_decode!("0xef5fb05b"),
381386
selector: %ABI.FunctionSelector{
382387
function: "sayHello",
383388
method_id: <<239, 95, 176, 91>>,

0 commit comments

Comments
 (0)