FISCO-BCOS / go-sdk

golang SDK of FISCO BCOS
Apache License 2.0
73 stars 57 forks source link

关于合约事件监听的问题 #136

Open likhita-8091 opened 2 years ago

likhita-8091 commented 2 years ago

我是copy代码库中的example下的event示例代码,但是也是监听不到, image

是不是和我的bcos版本有关系,bcos是2.8的版本,go-sdk是dev版本,

bxq2011hust commented 2 years ago

你可以在go-sdk执行下面的命令

bash .ci/integration_test.sh

会产生一个hello.go的文件,里面有main函数,可以参考,如下

func main() {
    configs, err := conf.ParseConfigFile("config.toml")
    if err != nil {
        log.Fatalf("ParseConfigFile failed, err: %v", err)
    }
    client, err := client.Dial(&configs[0])
    if err != nil {
        fmt.Printf("Dial Client failed, err:%v", err)
        return
    }
    address, tx, instance, err := DeployHello(client.GetTransactOpts(), client)
    if err != nil {
        fmt.Printf("Deploy failed, err:%v", err)
        return
    }
    fmt.Println("contract address: ", address.Hex()) // the address should be saved
    fmt.Println("transaction hash: ", tx.Hash().Hex())

    hello := &HelloSession{Contract: instance, CallOpts: *client.GetCallOpts(), TransactOpts: *client.GetTransactOpts()}
    ret, err := hello.Get()
    if err != nil {
        fmt.Printf("hello.Get() failed: %v", err)
        return
    }
    done := make(chan bool)
    err = hello.WatchAllSetValue(nil, func(ret int, logs []types.Log) {
        fmt.Printf("WatchAllSetValue receive statud: %d, logs: %v\n", ret, logs)
        setValue, err := hello.ParseSetValue(logs[0])
        if err != nil {
            fmt.Printf("hello.WatchAllSetValue() failed: %v", err)
            panic("WatchAllSetValue hello.WatchAllSetValue() failed")
        }
        fmt.Printf("receive setValue: %+v\n", *setValue)
        done <- true
    })
    if err != nil {
        fmt.Printf("hello.WatchAllSetValue() failed: %v", err)
        return
    }
    fmt.Printf("Get: %s\n", ret)
    _, _, err = hello.Set("fisco")
    if err != nil {
        fmt.Printf("hello.Set failed: %v", err)
        return
    }
    ret, err = hello.Get()
    if err != nil {
        fmt.Printf("hello.Get() failed: %v", err)
        return
    }
    fmt.Printf("Get: %s\n", ret)
    <-done
    from := common.HexToAddress("0x83309d045a19c44Dc3722D15A6AbD472f95866aC")
    hello.WatchSetValue(nil, func(ret int, logs []types.Log) {
        fmt.Printf("WatchSetValue receive statud: %d, logs: %+v\n", ret, logs)
        setValue, err := hello.ParseSetValue(logs[0])
        if err != nil {
            fmt.Printf("hello.WatchSetValue() failed: %v", err)
            panic("hello.WatchSetValue() failed")
        }
        fmt.Printf("WatchSetValue receive setValue: %+v\n", *setValue)
        done <- true
    }, from, from)
    <-done
}
stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.