-
Notifications
You must be signed in to change notification settings - Fork 15
/
api.html
107 lines (107 loc) · 4.51 KB
/
api.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Chainlog historical API - MakerDAO</title>
<meta charset="UTF-8">
<meta name="referrer" content="origin">
<link rel="icon" href="data:;base64,iVBORw0KGgo=">
</head>
<body>
<h1>Chainlog historical API</h1>
<h2>Usage</h2>
<h3>Get a list of all available chains and versions</h3>
<pre>GET <a href="https://chainlog.sky.money/api/index.json">https://chainlog.sky.money/api/index.json</a></pre>
<h3>Get the active version on a given chain</h3>
<pre>GET https://chainlog.sky.money/api/{chain}/active.json</pre>
<h3>Get a specific version on a given chain</h3>
<pre>GET https://chainlog.sky.money/api/{chain}/{version}.json</pre>
<h2>Code examples</h2>
You can set your JavaScript app to use this API <a href="https://github.com/makerdao/chainlog-ui/blob/main/examples/api.js">in the following way</a>:
<pre>
const url = "https://chainlog.sky.money/api/mainnet/active.json";
const response = await fetch(url);
const chainlog = await response.json();
console.log(chainlog);
</pre>
If you care about decentralization, however, <b>do not use this API</b>. Instead <a href="https://github.com/makerdao/chainlog-ui/blob/main/examples/ethers.js">query the blockchain directly</a>:
<pre>
const address = "0xda0ab1e0017debcd72be8599041a2aa3ba7e740f";
const abi = [
"function list() external view returns (bytes32[])",
"function getAddress(bytes32) external view returns (address)"
];
const provider = ethers.getDefaultProvider();
const chainlog = new ethers.Contract(address, abi, provider);
const list = await chainlog.list();
const result = {};
for (let keyHex of list) {
const key = ethers.utils.parseBytes32String(keyHex);
const address = await chainlog.getAddress(keyHex);
result[key] = address;
}
console.log(result);
</pre>
Please note that the above code will only retreive the latest data.<br/><br/>
You can also ditch web3 libraries and perform low-level JSON RPC calls. Here is an <a href="https://github.com/makerdao/chainlog-ui/blob/main/examples/python.py">example with Python</a>:
<pre>
keys = requests.post("https://mainnet.infura.io/v3/" + infura_key, json={
"jsonrpc": "2.0",
"method": "eth_call",
"params": [{
"to": "0xdA0Ab1e0017DEbCd72Be8599041a2aa3bA7e740F",
"data": "0x0f560cd7"
}, "latest"],
"id": 0
}).json()["result"][130:]
result = {}
for i in range(0, len(keys), 64):
address = "0x" + requests.post("https://mainnet.infura.io/v3/" + infura_key, json={
"jsonrpc": "2.0",
"method": "eth_call",
"params": [{
"to": "0xdA0Ab1e0017DEbCd72Be8599041a2aa3bA7e740F",
"data": "0x21f8a721" + keys[i:i+64]
}, "latest"],
"id": 0
}).json()["result"][26:]
key = bytes.fromhex(keys[i:i+64]).replace(b"\x00", b"").decode("ascii")
result[key] = address
print(result)
</pre>
The above code has the advantage that it will allow you to retreive historical data as long as you are using an archival node. In order to do that, replace <code>"latest"</code> with a specific block number.
<h2>Available resources in this API</h2>
<pre>
https://chainlog.sky.money/api
├── <a href="https://chainlog.sky.money/api/index.json">index.json</a>
<div id="canvas"></div>
</pre>
<script>
const main = async () => {
const canvas = document.getElementById("canvas");
const response = await fetch("api/index.json");
const list = await response.json();
const chains = Object.keys(list);
for (let i = 0; i < chains.length; i++) {
const chain = chains[i];
if (i === chains.length - 1) {
canvas.innerHTML += "└── " + chain + "<br/>";
}
else {
canvas.innerHTML += "├── " + chain + "<br/>";
}
const pipe1 = i === chains.length - 1 ? " " : "│ ";
if (list[chain].active !== null) {
canvas.innerHTML += pipe1 + "├── " + '<a href="api/' + chain + '/active.json">active.json</a><br/>';
}
const versions = list[chain].all;
for (let i = 0; i < versions.length; i++) {
const version = versions[i];
const pipe2 = i === versions.length - 1 ? "└── " : "├── ";
canvas.innerHTML += pipe1 + pipe2 + '<a href="api/' + chain + '/' + version + '.json">' + version + '.json</a><br/>';
}
}
}
main();
</script>
</body>
</html>