-
Notifications
You must be signed in to change notification settings - Fork 0
/
subscriber_test.go
100 lines (84 loc) · 2.46 KB
/
subscriber_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package ethclient
import (
"context"
"math/big"
"testing"
"time"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/log"
"github.com/stretchr/testify/assert"
)
func TestSubscriber(t *testing.T) {
log.Root().SetHandler(log.StdoutHandler)
log.Root().SetHandler(log.LvlFilterHandler(log.LvlInfo, log.Root().GetHandler()))
// log.Root().SetHandler(log.DiscardHandler())
client := newTestClient(t)
defer client.Close()
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
defer cancel()
// Deploy Test contract.
contractAddr, txOfContractCreation, contract, err := deployTestContract(t, ctx, client)
if err != nil {
t.Fatal(err)
}
t.Log("TestContract creation transaction", "txHex", txOfContractCreation.Hash().Hex(), "contract", contractAddr.Hex())
contains, err := client.ConfirmTx(txOfContractCreation.Hash(), 2, 5*time.Second)
if err != nil {
t.Fatalf("Deploy Contract err: %v", err)
}
assert.Equal(t, true, contains)
// Method args
arg1 := "hello"
arg2 := big.NewInt(100)
arg3 := []byte("world")
// First transact.
opts, err := client.MessageToTransactOpts(ctx, Message{
PrivateKey: privateKey,
})
contractCallTx, err := contract.TestFunc1(opts, arg1, arg2, arg3)
if err != nil {
t.Fatalf("TestFunc1 err: %v", err)
}
t.Log("contractCallTx send sucessul", "txHash", contractCallTx.Hash().Hex())
contains, err = client.ConfirmTx(contractCallTx.Hash(), 2, 20*time.Second)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, true, contains)
// Subscribe logs
logs := make(chan types.Log)
err = client.Subscriber.SubscribeFilterlogs(ctx, ethereum.FilterQuery{}, logs)
if err != nil {
t.Fatal("Subscribe logs err: ", err)
}
logCount := 0
go func() {
for {
select {
case l := <-logs:
logCount++
t.Log("Get log", "block", l.BlockNumber, "tx", l.TxHash.Hex(),
"txIndex", l.TxIndex, "index", l.Index)
case <-ctx.Done():
t.Log("Context done.")
return
}
}
}()
// Second transact.
opts, err = client.MessageToTransactOpts(ctx, Message{
PrivateKey: privateKey,
})
contractCallTx, err = contract.TestFunc1(opts, arg1, arg2, arg3)
if err != nil {
t.Fatalf("TestFunc1 err: %v", err)
}
t.Log("contractCallTx send sucessul", "txHash", contractCallTx.Hash().Hex())
contains, err = client.ConfirmTx(contractCallTx.Hash(), 2, 20*time.Second)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, true, contains)
assert.Equal(t, 4, logCount)
}