forked from banteg/etherscan-cache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
87 lines (67 loc) · 2.19 KB
/
app.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
from collections import defaultdict
from functools import wraps
from itertools import cycle
from threading import Lock
import diskcache
import requests
import toml
from eth_utils import to_checksum_address
from fastapi import FastAPI, HTTPException
app = FastAPI()
cache = diskcache.Cache("cache", statistics=True, size_limit=10e9)
config = toml.load(open("config.toml"))
keys = {explorer: cycle(config[explorer]["keys"]) for explorer in config}
def stampede(f):
locks = defaultdict(Lock)
@wraps(f)
def inner(*args, **kwargs):
key = f.__cache_key__(*args, **kwargs)
with locks[key]:
return f(*args, **kwargs)
return inner
@stampede
@cache.memoize()
def get_from_upstream(explorer, module, action, address):
print(f"fetching {explorer} {address}")
resp = requests.get(
config[explorer]["url"],
params={
"module": module,
"action": action,
"address": address,
"apiKey": next(keys[explorer]),
},
headers={ "User-Agent": "Mozilla/5.0" }
)
resp.raise_for_status()
return resp.json()
@app.get("/{explorer}/api")
def cached_api(explorer: str, module: str, action: str, address: str):
if explorer not in config:
raise HTTPException(400, "explorer not supported")
if module not in ["contract"]:
raise HTTPException(400, "module not supported")
if action not in ["getsourcecode", "getabi"]:
raise HTTPException(400, "action not supported")
try:
address = to_checksum_address(address)
except ValueError:
raise HTTPException(400, "invalid address")
return get_from_upstream(explorer, module, action, address)
@app.delete("/{explorer}/api")
def invalidate(explorer: str, address: str):
deleted = 0
for key in cache.iterkeys():
if (key[1], key[4]) == (explorer, address):
deleted += bool(cache.delete(key))
return {'deleted': deleted}
@app.get("/stats")
def cache_stats():
hits, misses = cache.stats()
count = cache._sql("select count(*) from Cache").fetchone()
return {
"hits": hits,
"misses": misses,
"count": count[0],
"size": cache.volume(),
}