github.com/ethersphere/bee/v2@v2.2.0/pkg/transaction/event_test.go (about) 1 // Copyright 2021 The Swarm Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package transaction_test 6 7 import ( 8 "errors" 9 "math/big" 10 "testing" 11 12 "github.com/ethereum/go-ethereum/common" 13 "github.com/ethereum/go-ethereum/core/types" 14 "github.com/ethersphere/bee/v2/pkg/transaction" 15 "github.com/ethersphere/bee/v2/pkg/util/abiutil" 16 "github.com/ethersphere/go-sw3-abi/sw3abi" 17 ) 18 19 var ( 20 erc20ABI = abiutil.MustParseABI(sw3abi.ERC20ABIv0_6_5) 21 ) 22 23 type transferEvent struct { 24 From common.Address 25 To common.Address 26 Value *big.Int 27 } 28 29 func newTransferLog(address, from, to common.Address, value *big.Int) *types.Log { 30 return &types.Log{ 31 Topics: []common.Hash{ 32 erc20ABI.Events["Transfer"].ID, 33 common.BytesToHash(from.Bytes()), 34 common.BytesToHash(to.Bytes()), 35 }, 36 Data: value.FillBytes(make([]byte, 32)), 37 Address: address, 38 } 39 } 40 41 func TestParseEvent(t *testing.T) { 42 t.Parallel() 43 44 from := common.HexToAddress("00") 45 to := common.HexToAddress("01") 46 value := big.NewInt(0) 47 48 t.Run("ok", func(t *testing.T) { 49 t.Parallel() 50 51 var event transferEvent 52 err := transaction.ParseEvent(&erc20ABI, "Transfer", &event, *newTransferLog(common.Address{}, from, to, value)) 53 if err != nil { 54 t.Fatal(err) 55 } 56 57 if event.From != from { 58 t.Fatalf("parsed wrong from. wanted %x, got %x", from, event.From) 59 } 60 61 if event.To != to { 62 t.Fatalf("parsed wrong to. wanted %x, got %x", to, event.To) 63 } 64 65 if value.Cmp(event.Value) != 0 { 66 t.Fatalf("parsed wrong value. wanted %d, got %d", value, event.Value) 67 } 68 }) 69 70 t.Run("no topic", func(t *testing.T) { 71 t.Parallel() 72 73 var event transferEvent 74 err := transaction.ParseEvent(&erc20ABI, "Transfer", &event, types.Log{ 75 Topics: []common.Hash{}, 76 }) 77 if !errors.Is(err, transaction.ErrNoTopic) { 78 t.Fatalf("expected error %v, got %v", transaction.ErrNoTopic, err) 79 } 80 }) 81 } 82 83 func TestFindSingleEvent(t *testing.T) { 84 t.Parallel() 85 86 contractAddress := common.HexToAddress("abcd") 87 from := common.HexToAddress("00") 88 to := common.HexToAddress("01") 89 value := big.NewInt(0) 90 91 t.Run("ok", func(t *testing.T) { 92 t.Parallel() 93 94 var event transferEvent 95 err := transaction.FindSingleEvent( 96 &erc20ABI, 97 &types.Receipt{ 98 Logs: []*types.Log{ 99 newTransferLog(from, to, from, value), // event from different contract 100 {Topics: []common.Hash{{}}, Address: contractAddress}, // different event from same contract 101 newTransferLog(contractAddress, from, to, value), 102 }, 103 Status: 1, 104 }, 105 contractAddress, 106 erc20ABI.Events["Transfer"], 107 &event, 108 ) 109 if err != nil { 110 t.Fatal(err) 111 } 112 113 if event.From != from { 114 t.Fatalf("parsed wrong from. wanted %x, got %x", from, event.From) 115 } 116 117 if event.To != to { 118 t.Fatalf("parsed wrong to. wanted %x, got %x", to, event.To) 119 } 120 121 if value.Cmp(event.Value) != 0 { 122 t.Fatalf("parsed wrong value. wanted %d, got %d", value, event.Value) 123 } 124 }) 125 126 t.Run("not found", func(t *testing.T) { 127 t.Parallel() 128 129 var event transferEvent 130 err := transaction.FindSingleEvent( 131 &erc20ABI, 132 &types.Receipt{ 133 Logs: []*types.Log{ 134 newTransferLog(from, to, from, value), // event from different contract 135 {Topics: []common.Hash{{}}, Address: contractAddress}, // different event from same contract 136 }, 137 Status: 1, 138 }, 139 contractAddress, 140 erc20ABI.Events["Transfer"], 141 &event, 142 ) 143 if !errors.Is(err, transaction.ErrEventNotFound) { 144 t.Fatalf("wanted error %v, got %v", transaction.ErrEventNotFound, err) 145 } 146 }) 147 148 t.Run("Reverted", func(t *testing.T) { 149 t.Parallel() 150 151 var event transferEvent 152 err := transaction.FindSingleEvent( 153 &erc20ABI, 154 &types.Receipt{Status: 0}, 155 contractAddress, 156 erc20ABI.Events["Transfer"], 157 &event, 158 ) 159 if !errors.Is(err, transaction.ErrTransactionReverted) { 160 t.Fatalf("wanted error %v, got %v", transaction.ErrTransactionReverted, err) 161 } 162 }) 163 }