-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
235 lines (215 loc) · 5.73 KB
/
main.py
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
from http.client import HTTPSConnection
import grpc
import json
import os
import ssl
import sys
from graphql import graphql_pb2_grpc
from graphql.graphql_pb2 import Request
from google.protobuf.struct_pb2 import Struct
#
# Code from getting started will go here ...
#
def get_token(api_key):
connection = HTTPSConnection("auth.dfuse.io")
connection.request(
"POST",
"/v1/auth/issue",
json.dumps({"api_key": api_key}),
{"Content-type": "application/json"},
)
response = connection.getresponse()
if response.status != 200:
raise Exception(" Status: %s reason: %s" % (response.status, response.reason))
token = json.loads(response.read().decode())["token"]
connection.close()
return token
def create_client(token, endpoint):
channel = grpc.secure_channel(
endpoint,
credentials=grpc.composite_channel_credentials(
grpc.ssl_channel_credentials(), grpc.access_token_call_credentials(token)
),
)
return graphql_pb2_grpc.GraphQLStub(channel)
def stream_ethereum(client):
variables = Struct()
variables.update({
"addresses": ["0x7a250d5630b4cf539739df2c5dacb4c659f2488d"],
"fields": ["FROM", "ERC20_FROM"],
})
# The client can be re-used for all requests, cache it at the appropriate level
stream = client.Execute(
Request(
query=OPERATION_ETH,
variables=variables,
)
)
for rawResult in stream:
if rawResult.errors:
print("An error occurred")
print(rawResult.errors)
else:
result = json.loads(rawResult.data)
print(result)
# for call in result["searchTransactions"]["node"]["matchingCalls"]:
# undo = result["searchTransactions"]["undo"]
# print(
# "Transfer %s -> %s [%s Ether]%s"
# % (
# call["from"],
# call["to"],
# call["value"],
# " REVERTED" if undo else "",
# )
# )
dfuse_api_key = os.environ.get("DFUSE_API_KEY")
if dfuse_api_key == None or dfuse_api_key == "your dfuse api key here":
raise Exception("you must specify a DFUSE_API_KEY environment variable")
token = get_token(dfuse_api_key)
# The client can be re-used for all requests, cache it at the appropriate level
client = create_client(token, "mainnet.eth.dfuse.io:443")
OPERATION_ETH = """subscription ($addresses: [String!]!, $fields: [TRANSACTION_MATCH_FIELD!]!) {
transactions(addresses: $addresses, matchAnyOf: $fields) {
hash
currentState
transitionName
transition {
... on TrxTransitionPooled {
transaction {
... on Transaction {
hash
to
from
input {
data
json {
type
name
value
}
}
}
}
}
... on TrxTransitionSpeculativelyExecuted {
trace {
... on TransactionTrace {
hash
to
from
flatCalls {
... on Call {
index
parentIndex
depth
callType
from
to
value(encoding: HEX)
gasConsumed
gasLimit
status
failureCause
inputData
method {
hexSignature
textSignature
}
returnData
storageChanges {
key
address
oldValue
newValue
}
balanceChanges {
address
oldValue
newValue
reason
}
logs {
address
topics
data
}
}
}
allLogs {
... on EventLog {
address
topics
data
blockIndex
transactionIndex
}
}
}
}
}
... on TrxTransitionMined {
trace {
... on TransactionTrace {
hash
to
from
flatCalls {
... on Call {
index
parentIndex
depth
callType
from
to
value(encoding: HEX)
gasConsumed
gasLimit
status
failureCause
inputData
method {
hexSignature
textSignature
}
returnData
storageChanges {
key
address
oldValue
newValue
}
balanceChanges {
address
oldValue
newValue
reason
}
logs {
address
topics
data
}
}
}
allLogs {
... on EventLog {
address
topics
data
blockIndex
transactionIndex
}
}
}
}
}
... on TrxTransitionConfirmed {
confirmations
}
}
}
}
"""
stream_ethereum(client)
# print(OPERATION_ETH)