forked from apk8s/book-source
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.py
50 lines (39 loc) · 1018 Bytes
/
handler.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
#!/usr/bin/env python3
""" handler.py
OpenFaaS Blockchain function returning the last block
in the chain.
"""
import os
import json
import hexbytes
from web3 import Web3
def handle(event, context):
"""
handle a request to the function
"""
ep_url = "http://eth-geth-tx:8545"
ep = os.getenv('GETH_RPC_ENDPOINT', ep_url)
w3 = Web3(Web3.HTTPProvider(ep))
latest_block = w3.eth.getBlock('latest')
lbd = latest_block.__dict__
return {
"statusCode": 200,
"body": json.loads(
json.dumps(lbd, cls=CustomEncoder)
)
}
class CustomEncoder(json.JSONEncoder):
"""
CustomEncoder decodes HexBytes
in Geth response dict.
"""
def default(self, o):
if isinstance(o, hexbytes.main.HexBytes):
return o.hex()
return json.JSONEncoder.default(self, o)
if __name__ == '__main__':
"""
Run code from command line for testing.
Mock event and context.
"""
print(handle(event={}, context={}))