I'm new to go-ethereum and even Golang in general so my terminology might be wrong, I'm guessing event handling is also referred to as "subscription"? Anyways, my actual issue is that I want to test out an event on a contract, but I can't seem to get any callbacks.
Here's my code:
func main() {
client, err := ethclient.Dial("wss://ropsten.infura.io/ws")
if err != nil{
log.Fatal(err)
}
fmt.Println("We have a connection!")
privateKey, err := crypto.HexToECDSA("blablabla")
if(err != nil){
log.Fatal(err)
}
publicKey := privateKey.Public()
publicECDSA := publicKey.(*ecdsa.PublicKey)
fromAddress := crypto.PubkeyToAddress(*publicECDSA)
nonce, err := client.PendingNonceAt(context.Background(), fromAddress)
gasPrice, err := client.SuggestGasPrice(context.Background())
auth := bind.NewKeyedTransactor(privateKey)
auth.Nonce = big.NewInt(int64(nonce))
auth.GasLimit = uint64(300000)
auth.GasPrice = gasPrice
auth.Value = big.NewInt(int64(0))
testAddr := common.HexToAddress("blablablabla")
testEvent, err := TestEvent.NewTestEvent(testAddr, client)
if(err != nil){
log.Fatal(err)
}
resChan := make(chan *TestEvent.TestEventHandleEvent)
if(err != nil){
log.Fatal(err)
}
start := uint64(0)
testEvent.WatchHandleEvent(&bind.WatchOpts{
Context: context.Background(),
Start: &start}, resChan)
if(err != nil){
log.Fatal(err)
}
timer := time.NewTimer(5 * time.Second)
go func(){
<- timer.C
_, err := testEvent.Gotcha(auth)
if(err != nil){
log.Fatal(err)
}
}()
code := <-resChan
fmt.Println(code)
close(resChan)
}
Here's my contract:
pragma solidity 0.4.25;
contract TestCon { event HandleEvent(uint64 code); address public owner;
constructor() public{
owner = msg.sender;
}
function Gotcha() public payable {
if(msg.sender == owner)
{
emit HandleEvent(10); // I've deployed the contract from the same address. So this should match.
return;
}
revert();
}
}
what am I doing wrong? Or is that a problem with my contract?
wss://ropsten.infura.io/ws
is complete? I was expecting it to be likewss://ropsten.infura.io/ws/v3/427bec2fd1ca4870a8fbef8af8132e18