forked from svanas/delphereum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb3.graph.pas
82 lines (70 loc) · 2.23 KB
/
web3.graph.pas
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
{******************************************************************************}
{ }
{ Delphereum }
{ }
{ Copyright(c) 2020 Stefan van As <[email protected]> }
{ Github Repository <https://github.com/svanas/delphereum> }
{ }
{ Distributed under Creative Commons NonCommercial (aka CC BY-NC) license. }
{ }
{******************************************************************************}
unit web3.graph;
{$I web3.inc}
interface
uses
// Delphi
System.Types,
// web3
web3,
web3.http;
type
IGraphError = interface(IError)
['{46F0E07F-F47C-41BC-98BF-B8F7FA24AB91}']
end;
TGraphError = class(TError, IGraphError);
const
UNISWAP_V2 = 'https://api.thegraph.com/subgraphs/name/uniswap/uniswap-v2';
function execute(const URL, query: string; callback: TAsyncJsonObject): IAsyncResult;
implementation
uses
// Delphi
System.Classes,
System.Generics.Collections,
System.JSON,
System.Net.URLClient,
// web3
web3.json;
function execute(const URL, query: string; callback: TAsyncJsonObject): IAsyncResult;
var
source: TStream;
errors: TJsonArray;
begin
source := TStringStream.Create(query);
web3.http.post(
URL,
source,
[TNetHeader.Create('Content-Type', 'application/graphql')],
procedure(resp: TJsonObject; err: IError)
begin
try
if Assigned(err) then
begin
callback(nil, err);
EXIT;
end;
// did we receive an error?
errors := web3.json.getPropAsArr(resp, 'errors');
if Assigned(errors) and (errors.Count > 0) then
begin
callback(resp, TGraphError.Create(web3.json.getPropAsStr(errors.Items[0], 'message')));
EXIT;
end;
// if we reached this far, then we have a valid response object
callback(resp, nil);
finally
source.Free;
end;
end
);
end;
end.