github.com/lmittmann/w3@v0.20.0/event_test.go (about) 1 package w3_test 2 3 import ( 4 "math/big" 5 "strconv" 6 "testing" 7 8 "github.com/ethereum/go-ethereum/common" 9 "github.com/ethereum/go-ethereum/core/types" 10 "github.com/google/go-cmp/cmp" 11 "github.com/google/go-cmp/cmp/cmpopts" 12 "github.com/lmittmann/w3" 13 ) 14 15 func TestNewEvent(t *testing.T) { 16 tests := []struct { 17 Signature string 18 WantEvent *w3.Event 19 }{ 20 { 21 Signature: "Transfer(address,address,uint256)", 22 WantEvent: &w3.Event{ 23 Signature: "Transfer(address,address,uint256)", 24 Topic0: w3.H("0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"), 25 }, 26 }, 27 { 28 Signature: "Transfer(address from, address to, uint256 value)", 29 WantEvent: &w3.Event{ 30 Signature: "Transfer(address,address,uint256)", 31 Topic0: w3.H("0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"), 32 }, 33 }, 34 { 35 Signature: "Approval(address,address,uint256)", 36 WantEvent: &w3.Event{ 37 Signature: "Approval(address,address,uint256)", 38 Topic0: w3.H("0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925"), 39 }, 40 }, 41 { 42 Signature: "Approval(address owner, address spender, uint256 value)", 43 WantEvent: &w3.Event{ 44 Signature: "Approval(address,address,uint256)", 45 Topic0: w3.H("0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925"), 46 }, 47 }, 48 } 49 50 for i, test := range tests { 51 t.Run(strconv.Itoa(i), func(t *testing.T) { 52 gotEvent, err := w3.NewEvent(test.Signature) 53 if err != nil { 54 t.Fatalf("Failed to create new FUnc: %v", err) 55 } 56 57 if diff := cmp.Diff(test.WantEvent, gotEvent, 58 cmpopts.IgnoreUnexported(w3.Event{}), 59 cmpopts.IgnoreFields(w3.Event{}, "Args"), 60 ); diff != "" { 61 t.Fatalf("(-want, +got)\n%s", diff) 62 } 63 }) 64 } 65 } 66 67 func TestEventDecodeArgs(t *testing.T) { 68 tests := []struct { 69 Event *w3.Event 70 Log *types.Log 71 Args []any 72 WantArgs []any 73 }{ 74 { 75 Event: w3.MustNewEvent("Transfer(address,address,uint256)"), 76 Log: &types.Log{ 77 Topics: []common.Hash{ 78 w3.H("0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"), 79 }, 80 Data: w3.B("0x" + 81 "000000000000000000000000000000000000000000000000000000000000c0fe" + 82 "000000000000000000000000000000000000000000000000000000000000dead" + 83 "000000000000000000000000000000000000000000000000000000000000002a"), 84 }, 85 Args: []any{new(common.Address), new(common.Address), new(big.Int)}, 86 WantArgs: []any{ 87 w3.APtr("0x000000000000000000000000000000000000c0Fe"), 88 w3.APtr("0x000000000000000000000000000000000000dEaD"), 89 big.NewInt(42), 90 }, 91 }, 92 { 93 Event: w3.MustNewEvent("Transfer(address indexed, address, uint256)"), 94 Log: &types.Log{ 95 Topics: []common.Hash{ 96 w3.H("0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"), 97 w3.H("0x000000000000000000000000000000000000000000000000000000000000c0fe"), 98 }, 99 Data: w3.B("0x" + 100 "000000000000000000000000000000000000000000000000000000000000dead" + 101 "000000000000000000000000000000000000000000000000000000000000002a"), 102 }, 103 Args: []any{new(common.Address), new(common.Address), new(big.Int)}, 104 WantArgs: []any{ 105 w3.APtr("0x000000000000000000000000000000000000c0Fe"), 106 w3.APtr("0x000000000000000000000000000000000000dEaD"), 107 big.NewInt(42), 108 }, 109 }, 110 { 111 Event: w3.MustNewEvent("Transfer(address, address indexed, uint256)"), 112 Log: &types.Log{ 113 Topics: []common.Hash{ 114 w3.H("0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"), 115 w3.H("0x000000000000000000000000000000000000000000000000000000000000dead"), 116 }, 117 Data: w3.B("0x" + 118 "000000000000000000000000000000000000000000000000000000000000c0fe" + 119 "000000000000000000000000000000000000000000000000000000000000002a"), 120 }, 121 Args: []any{new(common.Address), new(common.Address), new(big.Int)}, 122 WantArgs: []any{ 123 w3.APtr("0x000000000000000000000000000000000000c0Fe"), 124 w3.APtr("0x000000000000000000000000000000000000dEaD"), 125 big.NewInt(42), 126 }, 127 }, 128 { 129 Event: w3.MustNewEvent("Transfer(address indexed, address indexed, uint256)"), 130 Log: &types.Log{ 131 Topics: []common.Hash{ 132 w3.H("0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"), 133 w3.H("0x000000000000000000000000000000000000000000000000000000000000c0fe"), 134 w3.H("0x000000000000000000000000000000000000000000000000000000000000dead"), 135 }, 136 Data: w3.B("0x000000000000000000000000000000000000000000000000000000000000002a"), 137 }, 138 Args: []any{new(common.Address), new(common.Address), new(big.Int)}, 139 WantArgs: []any{ 140 w3.APtr("0x000000000000000000000000000000000000c0Fe"), 141 w3.APtr("0x000000000000000000000000000000000000dEaD"), 142 big.NewInt(42), 143 }, 144 }, 145 { 146 Event: w3.MustNewEvent("Transfer(address indexed, address indexed, uint256 indexed)"), 147 Log: &types.Log{ 148 Topics: []common.Hash{ 149 w3.H("0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"), 150 w3.H("0x000000000000000000000000000000000000000000000000000000000000c0fe"), 151 w3.H("0x000000000000000000000000000000000000000000000000000000000000dead"), 152 w3.H("0x000000000000000000000000000000000000000000000000000000000000002a"), 153 }, 154 }, 155 Args: []any{new(common.Address), new(common.Address), new(big.Int)}, 156 WantArgs: []any{ 157 w3.APtr("0x000000000000000000000000000000000000c0Fe"), 158 w3.APtr("0x000000000000000000000000000000000000dEaD"), 159 big.NewInt(42), 160 }, 161 }, 162 { // https://github.com/lmittmann/w3/issues/15 163 Event: w3.MustNewEvent("NameRegistered(string name, bytes32 indexed label, address indexed owner, uint cost, uint expires)"), 164 Log: &types.Log{ 165 Address: w3.A("0x283Af0B28c62C092C9727F1Ee09c02CA627EB7F5"), 166 Topics: []common.Hash{ 167 w3.H("0xca6abbe9d7f11422cb6ca7629fbf6fe9efb1c621f71ce8f02b9f2a230097404f"), 168 w3.H("0x4e59ffc7ae105a2b19f7f29b63e9f9c5ac28e27bce744a330804c6a89269cec0"), 169 w3.H("0x000000000000000000000000bd08f39b2523426cc1d6961e2d6a9744b3b432b5"), 170 }, 171 Data: w3.B("0x000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000165a7d3a3e48ad0000000000000000000000000000000000000000000000000000000066e4d0a50000000000000000000000000000000000000000000000000000000000000009656c666172657373690000000000000000000000000000000000000000000000"), 172 }, 173 Args: []any{new(string), new(common.Hash), new(common.Address), new(big.Int), new(big.Int)}, 174 WantArgs: []any{ 175 ptr("elfaressi"), 176 ptr(w3.H("0x4e59ffc7ae105a2b19f7f29b63e9f9c5ac28e27bce744a330804c6a89269cec0")), 177 w3.APtr("0xbD08F39B2523426Cc1d6961e2d6A9744B3B432b5"), 178 big.NewInt(6291943382206637), 179 big.NewInt(1726271653), 180 }, 181 }, 182 } 183 184 for i, test := range tests { 185 t.Run(strconv.Itoa(i), func(t *testing.T) { 186 if err := test.Event.DecodeArgs(test.Log, test.Args...); err != nil { 187 t.Fatalf("Failed to decode args: %v", err) 188 } 189 if diff := cmp.Diff(test.WantArgs, test.Args, 190 cmp.AllowUnexported(big.Int{}), 191 cmpopts.IgnoreUnexported(w3.Event{}), 192 ); diff != "" { 193 t.Fatalf("(-want, +got)\n%s", diff) 194 } 195 }) 196 } 197 }