package main
import (
"context"
"fmt"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"log"
"math/big"
"strings"
)
const ABI = `[
{
"anonymous": false,
"inputs": [
{
"indexed": false,
"internalType": "address",
"name": "addr",
"type": "address"
},
{
"indexed": false,
"internalType": "uint64",
"name": "ocId",
"type": "uint64"
}
],
"name": "buyEvent",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": false,
"internalType": "uint64",
"name": "ocId",
"type": "uint64"
},
{
"indexed": false,
"internalType": "uint8",
"name": "isprofit",
"type": "uint8"
},
{
"indexed": false,
"internalType": "uint40",
"name": "exerciseTime",
"type": "uint40"
},
{
"indexed": false,
"internalType": "uint32",
"name": "amount",
"type": "uint32"
},
{
"indexed": false,
"internalType": "uint256",
"name": "profit",
"type": "uint256"
}
],
"name": "exerciseEvent",
"type": "event"
},
{
"inputs": [],
"name": "buyOptionById",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
]`
func main() {
EventOption()
}
func EventOption() {
abi:=GetABI(ABI)
cli, err := ethclient.Dial("ws://192.168.19.66:7545")
if err != nil {
fmt.Println("连接私链失败:", err)
return
}
//函数结束自动关闭连接
defer cli.Close()
contractAddress := common.HexToAddress("0xf3ed85874fbb39681e8b076554ac69f2255c1028")
query := ethereum.FilterQuery{
Addresses: []common.Address{contractAddress},
}
logs := make(chan types.Log)
sub, err := cli.SubscribeFilterLogs(context.Background(), query, logs)
if err != nil {
log.Fatal(err)
}
for {
select {
case err := <-sub.Err():
log.Fatal(err)
case vLog := <-logs:
switch vLog.Topics[0].String() {
//buyEvent事件 这里的hash为事件名buyEvent(address,uint64)进行keccack256计算得出的
case "0xbf5ed60d1b60f93841065247988234acab61d25f0cdb23dae3507eb01809e42f":
//这步是对监听到的DATA数据进行解析
intr, err := abi.Events["buyEvent"].Inputs.UnpackValues(vLog.Data)
if err != nil {
panic(err)
}
//打印监听到的参数
fmt.Println(intr[0].(common.Address).String(),intr[1].(*big.Int).Uint64())
fmt.Println("交易hash",vLog.TxHash.String())
//exerciseEvent事件 exerciseEvent(uint64,uint8,uint40,uint32,uint256)
case "0xc32df39ae9f5cc595be756fd57aa0a8976bf1c8a5aad5199dc2001efa4a384b5":
//这步是对监听到的DATA数据进行解析
intr, err := abi.Events["exerciseEvent"].Inputs.UnpackValues(vLog.Data)
if err != nil {
panic(err)
}
list:=[]interface{}{}
for _,v:=range intr{
list=append(list,v)
}
//打印监听到的参数
fmt.Println(list)
fmt.Println("交易hash",vLog.TxHash.String())
}
}
}
}
//获取ABI对象
func GetABI(abiJSON string)abi.ABI {
wrapABI,err:=abi.JSON(strings.NewReader(abiJSON))
if err!=nil {
panic(err)
}
return wrapABI
}
附上测试用的合约代码
pragma solidity ^0.6.8;
contract Attacker{
event buyEvent(address addr,uint64 ocId);
event exerciseEvent(uint64 ocId,uint8 isprofit,uint40 exerciseTime,uint32 amount,uint256 profit);
function buyOptionById() external {
address addr=msg.sender;
emit buyEvent(addr,666666);
emit exerciseEvent(111,222,333,444,555);
}
}