Skip to content

Commit eb676f4

Browse files
authored
Merge pull request #72 from alisinabh/fix-integer-encoding
Fix integer encoding in parameters of transactions
2 parents 465a839 + 713a19a commit eb676f4

File tree

4 files changed

+34
-5
lines changed

4 files changed

+34
-5
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
### Bug fixes
6+
7+
- Encode integers to hex even when they are part of params
8+
39
## v0.2.0 (2024-01-01)
410

511
### New Features
@@ -12,7 +18,7 @@
1218

1319
## v0.1.3 (2023-12-26)
1420

15-
## Bug fixes
21+
### Bug fixes
1622

1723
- unsized integer encoding to hex will now raise if given negative numbers.
1824
- `Utils.date_to_block_number/3` going to negative block numbers issue fixed.

lib/ethers/tx_data.ex

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,12 @@ defmodule Ethers.TxData do
3737
end
3838

3939
def to_map(tx_map, overrides) when is_map(tx_map) do
40-
Enum.map(overrides, fn
40+
overrides
41+
|> Enum.into(tx_map)
42+
|> Map.new(fn
4143
{k, v} when is_integer(v) -> {k, Ethers.Utils.integer_to_hex(v)}
4244
kv -> kv
4345
end)
44-
|> Enum.into(tx_map)
4546
end
4647

4748
defp get_tx_map(%{selector: %{type: :function}} = tx_data) do

test/ethers_test.exs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,24 @@ defmodule EthersTest do
314314
assert {:ok, "hello local signer"} =
315315
Ethers.call(HelloWorldContract.say_hello(), to: address)
316316
end
317+
318+
test "converts all integer params and overrides to hex" do
319+
assert {:ok, _tx_hash} =
320+
Ethers.send(
321+
%{value: 1000},
322+
rpc_client: Ethers.TestRPCModule,
323+
from: @from,
324+
to: "0x95cED938F7991cd0dFcb48F0a06a40FA1aF46EBC",
325+
rpc_opts: [send_params_to_pid: self()]
326+
)
327+
328+
assert_receive %{
329+
from: "0x90f8bf6a479f320ead074411a4b0e7944ea8c9c1",
330+
gas: "0x119",
331+
to: "0x95cED938F7991cd0dFcb48F0a06a40FA1aF46EBC",
332+
value: "0x3E8"
333+
}
334+
end
317335
end
318336

319337
describe "sign_transaction/2" do

test/support/test_rpc_module.ex

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@ defmodule Ethers.TestRPCModule do
77
{:ok, "0x100"}
88
end
99

10-
def eth_send_transaction(_params, _opts) do
11-
{:ok, "tx_hash"}
10+
def eth_send_transaction(params, opts) do
11+
if pid = opts[:send_params_to_pid] do
12+
send(pid, params)
13+
end
14+
15+
{:ok, opts[:tx_hash] || "tx_hash"}
1216
end
1317

1418
def eth_call(params, block, opts) do

0 commit comments

Comments
 (0)