Skip to content

Commit 1e72271

Browse files
committed
accounts/abi: use unpackTuple to unpack event arguments
Events with just 1 argument fail before this change
1 parent 4e61ed0 commit 1e72271

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

accounts/abi/abi.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func (abi ABI) Unpack(v interface{}, name string, output []byte) (err error) {
8686
}
8787
return method.Outputs.Unpack(v, output)
8888
} else if event, ok := abi.Events[name]; ok {
89-
return event.Inputs.Unpack(v, output)
89+
return event.Inputs.unpackTuple(v, output)
9090
}
9191
return fmt.Errorf("abi: could not locate named method or event")
9292
}

accounts/abi/abi_test.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -621,14 +621,16 @@ func TestBareEvents(t *testing.T) {
621621
// TestUnpackEvent is based on this contract:
622622
// contract T {
623623
// event received(address sender, uint amount, bytes memo);
624+
// event receivedAddr(address sender);
624625
// function receive(bytes memo) external payable {
625626
// received(msg.sender, msg.value, memo);
627+
// receivedAddr(msg.sender);
626628
// }
627629
// }
628630
// When receive("X") is called with sender 0x00... and value 1, it produces this tx receipt:
629631
// receipt{status=1 cgas=23949 bloom=00000000004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000040200000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 logs=[log: b6818c8064f645cd82d99b59a1a267d6d61117ef [75fd880d39c1daf53b6547ab6cb59451fc6452d27caa90e5b6649dd8293b9eed] 000000000000000000000000376c47978271565f56deb45495afa69e59c16ab200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000158 9ae378b6d4409eada347a5dc0c180f186cb62dc68fcc0f043425eb917335aa28 0 95d429d309bb9d753954195fe2d69bd140b4ae731b9b5b605c34323de162cf00 0]}
630632
func TestUnpackEvent(t *testing.T) {
631-
const abiJSON = `[{"constant":false,"inputs":[{"name":"memo","type":"bytes"}],"name":"receive","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"anonymous":false,"inputs":[{"indexed":false,"name":"sender","type":"address"},{"indexed":false,"name":"amount","type":"uint256"},{"indexed":false,"name":"memo","type":"bytes"}],"name":"received","type":"event"}]`
633+
const abiJSON = `[{"constant":false,"inputs":[{"name":"memo","type":"bytes"}],"name":"receive","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"anonymous":false,"inputs":[{"indexed":false,"name":"sender","type":"address"},{"indexed":false,"name":"amount","type":"uint256"},{"indexed":false,"name":"memo","type":"bytes"}],"name":"received","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"sender","type":"address"}],"name":"receivedAddr","type":"event"}]`
632634
abi, err := JSON(strings.NewReader(abiJSON))
633635
if err != nil {
634636
t.Fatal(err)
@@ -656,6 +658,17 @@ func TestUnpackEvent(t *testing.T) {
656658
} else {
657659
t.Logf("len(data): %d; received event: %+v", len(data), ev)
658660
}
661+
662+
type ReceivedAddrEvent struct {
663+
Address common.Address
664+
}
665+
var receivedAddrEv ReceivedAddrEvent
666+
err = abi.Unpack(&receivedAddrEv, "receivedAddr", data)
667+
if err != nil {
668+
t.Error(err)
669+
} else {
670+
t.Logf("len(data): %d; received event: %+v", len(data), receivedAddrEv)
671+
}
659672
}
660673

661674
func TestABI_MethodById(t *testing.T) {

0 commit comments

Comments
 (0)