forked from gagliardetto/solana-go
/
logsSubscribe.go
121 lines (108 loc) · 3.13 KB
/
logsSubscribe.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright 2021 github.com/gagliardetto
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ws
import (
"github.com/desperatee/solana-go"
"github.com/desperatee/solana-go/rpc"
)
type LogResult struct {
Context struct {
Slot uint64
} `json:"context"`
Value struct {
// The transaction signature.
Signature solana.Signature `json:"signature"`
// Error if transaction failed, null if transaction succeeded.
Err interface{} `json:"err"`
// Array of log messages the transaction instructions output
// during execution, null if simulation failed before the transaction
// was able to execute (for example due to an invalid blockhash
// or signature verification failure)
Logs []string `json:"logs"`
} `json:"value"`
}
type LogsSubscribeFilterType string
const (
// Subscribe to all transactions except for simple vote transactions.
LogsSubscribeFilterAll LogsSubscribeFilterType = "all"
// Subscribe to all transactions including simple vote transactions.
LogsSubscribeFilterAllWithVotes LogsSubscribeFilterType = "allWithVotes"
)
// LogsSubscribe subscribes to transaction logging.
func (cl *Client) LogsSubscribe(
// Filter criteria for the logs to receive results by account type.
filter LogsSubscribeFilterType,
commitment rpc.CommitmentType, // (optional)
) (*LogSubscription, error) {
return cl.logsSubscribe(
filter,
commitment,
)
}
// LogsSubscribe subscribes to all transactions that mention the provided Pubkey.
func (cl *Client) LogsSubscribeMentions(
// Subscribe to all transactions that mention the provided Pubkey.
mentions solana.PublicKey,
// (optional)
commitment rpc.CommitmentType,
) (*LogSubscription, error) {
return cl.logsSubscribe(
rpc.M{
"mentions": []string{mentions.String()},
},
commitment,
)
}
// LogsSubscribe subscribes to transaction logging.
func (cl *Client) logsSubscribe(
filter interface{},
commitment rpc.CommitmentType,
) (*LogSubscription, error) {
params := []interface{}{filter}
conf := map[string]interface{}{}
if commitment != "" {
conf["commitment"] = commitment
}
genSub, err := cl.subscribe(
params,
conf,
"logsSubscribe",
"logsUnsubscribe",
func(msg []byte) (interface{}, error) {
var res LogResult
err := decodeResponseFromMessage(msg, &res)
return &res, err
},
)
if err != nil {
return nil, err
}
return &LogSubscription{
sub: genSub,
}, nil
}
type LogSubscription struct {
sub *Subscription
}
func (sw *LogSubscription) Recv() (*LogResult, error) {
select {
case d := <-sw.sub.stream:
return d.(*LogResult), nil
case err := <-sw.sub.err:
return nil, err
}
}
func (sw *LogSubscription) Unsubscribe() {
sw.sub.Unsubscribe()
}