-
Notifications
You must be signed in to change notification settings - Fork 1
/
limestone.ts
84 lines (73 loc) · 1.59 KB
/
limestone.ts
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
import { run } from "ar-gql";
import Arweave from "arweave";
const VERSION = "0.005";
const client = new Arweave({
host: "arweave.net",
port: 443,
protocol: "https",
});
interface LimestoneResult {
updated?: Date;
price?: number;
}
type Token = "AR" | "ETH";
interface LimestoneInput {
type: "data-latest" | "data-configuration";
token: Token;
}
const findGraphQL = async (
parameters: LimestoneInput
): Promise<LimestoneResult> => {
const res = (
await run(
`
{
transactions(
tags: [
{ name: "app", values: "Limestone" }
{ name: "type", values: "${parameters.type}" }
{ name: "token", values: "${parameters.token}" }
{ name: "version", values: "${VERSION}" }
]
block: { min: ${(await client.network.getInfo()).height - 50} }
first: 1
) {
edges {
node {
tags {
name
value
}
}
}
}
}
`
)
).data.transactions.edges;
if (res[0]) {
const tags = res[0].node.tags;
let price = undefined,
updated = undefined;
tags.forEach((tag) => {
if (tag.name === "value") {
price = parseFloat(tag.value);
}
if (tag.name === "time") {
updated = new Date(parseInt(tag.value));
}
});
return {
price: price,
updated: updated,
};
} else {
throw new Error("Invalid data returned from Arweave.");
}
};
export const getPrice = async (token: Token) => {
return await findGraphQL({
type: "data-latest",
token,
});
};