forked from svanas/delphereum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb3.eth.blocknative.mempool.sgc.pas
297 lines (262 loc) · 7.64 KB
/
web3.eth.blocknative.mempool.sgc.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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
{******************************************************************************}
{ }
{ Delphereum }
{ }
{ Copyright(c) 2021 Stefan van As <[email protected]> }
{ Github Repository <https://github.com/svanas/delphereum> }
{ }
{ Distributed under Creative Commons NonCommercial (aka CC BY-NC) license. }
{ }
{ https://docs.blocknative.com/websocket }
{ }
{******************************************************************************}
unit web3.eth.blocknative.mempool.sgc;
{$I web3.inc}
interface
uses
// Delphi
System.JSON,
System.SysUtils,
// SgcWebSockets
sgcWebSocket,
sgcWebSocket_Classes,
sgcWebSocket_Types,
// web3;
web3,
web3.eth.blocknative.mempool,
web3.json;
type
TSgcMempool = class(TCustomMempool, IMempool)
strict private
FClient: TsgcWebSocketClient;
function GetClient: TsgcWebSocketClient;
strict protected
function TryBlocknativeError(const Text: string): Boolean;
procedure DoMessage(Conn: TsgcWsConnection; const Text: string);
procedure DoError(Conn: TsgcWsConnection; const Error: string);
procedure DoException(Conn: TsgcWsConnection; E: Exception);
procedure DoDisconnect(Conn: TsgcWsConnection; Code: Integer);
property Client: TsgcWebSocketClient read GetClient;
public
constructor Create(
const chain : TChain;
const apiKey: string;
onEvent : TAsyncJsonObject;
onError : TAsyncError;
onDisconnect: TProc);
destructor Destroy; override;
class function Subscribe(
const chain : TChain;
const apiKey : string;
const address: TAddress;
onEvent : TAsyncJsonObject;
onError : TAsyncError;
onDisconnect : TProc): IMempool; overload; override;
class function Subscribe(
const chain : TChain;
const apiKey : string;
const address: TAddress;
const filters: IFilters;
const abi : TJsonArray;
onEvent : TAsyncJsonObject;
onError : TAsyncError;
onDisconnect : TProc): IMempool; overload; override;
procedure Unsubscribe(const address: TAddress);
procedure Initialize;
procedure Disconnect;
function Connected: Boolean;
end;
implementation
constructor TSgcMempool.Create(
const chain : TChain;
const apiKey: string;
onEvent : TAsyncJsonObject;
onError : TAsyncError;
onDisconnect: TProc);
begin
inherited Create;
FChain := chain;
FApiKey := apiKey;
FOnEvent := onEvent;
FOnError := onError;
FOnDisconnect := onDisconnect;
end;
function TSgcMempool.GetClient: TsgcWebSocketClient;
begin
if not Assigned(FClient) then
begin
FClient := TsgcWebSocketClient.Create(nil);
FClient.NotifyEvents := neNoSync;
FClient.OnMessage := DoMessage;
FClient.OnError := DoError;
FClient.OnException := DoException;
FClient.OnDisconnect := DoDisconnect;
FClient.HeartBeat.Enabled := True;
FClient.HeartBeat.Interval := 30; // seconds
FClient.HeartBeat.Timeout := 0;
FClient.URL := BLOCKNATIVE_ENDPOINT;
end;
if not FClient.Active then repeat until FClient.Connect;
Result := FClient;
end;
destructor TSgcMempool.Destroy;
begin
if Assigned(FClient) then
try
if FClient.Active then FClient.Disconnect;
finally
FClient.Free;
end;
inherited Destroy;
end;
function TSgcMempool.TryBlocknativeError(const Text: string): Boolean;
var
response: TJsonValue;
begin
response := unmarshal(Text);
Result := Assigned(response);
if Result then
try
// did we receive an error?
Result := SameText(getPropAsStr(response, 'status'), 'error');
if not Result then
EXIT;
if Assigned(FOnEvent) then
FOnEvent(nil, TError.Create(getPropAsStr(response, 'reason')));
finally
response.Free;
end;
end;
procedure TSgcMempool.DoMessage(Conn: TsgcWsConnection; const Text: string);
var
response: TJsonValue;
event : TJsonObject;
begin
if TryBlocknativeError(Text) then
EXIT;
response := unmarshal(Text);
if not Assigned(response) then
begin
if Assigned(FOnError) then FOnError(TError.Create(Text));
EXIT;
end;
try
event := getPropAsObj(response, 'event');
if not Assigned(event) then
EXIT;
if Assigned(FOnEvent) then FOnEvent(event, nil);
finally
response.Free;
end;
end;
procedure TSgcMempool.DoError(Conn: TsgcWsConnection; const Error: string);
begin
if TryBlocknativeError(Error) then
EXIT;
if Assigned(FOnError) then FOnError(TError.Create(Error));
end;
procedure TSgcMempool.DoException(Conn: TsgcWsConnection; E: Exception);
begin
if TryBlocknativeError(E.Message) then
EXIT;
if Assigned(FOnError) then FOnError(TError.Create(E.Message));
end;
procedure TSgcMempool.DoDisconnect(Conn: TsgcWsConnection; Code: Integer);
begin
if Assigned(FOnDisconnect) then FOnDisconnect;
end;
class function TSgcMempool.Subscribe(
const chain : TChain;
const apiKey : string;
const address: TAddress;
onEvent : TAsyncJsonObject;
onError : TAsyncError;
onDisconnect : TProc): IMempool;
var
&output: TSgcMempool;
payload: TJsonObject;
begin
&output := TSgcMempool.Create(
chain,
apiKey,
onEvent,
onError,
onDisconnect
);
&output.Initialize;
payload := unmarshal(&output.CreatePayload('accountAddress', 'watch')) as TJsonObject;
if Assigned(payload) then
try
payload.AddPair('account', unmarshal(Format('{"address":"%s"}', [address])));
&output.Client.WriteData(marshal(payload));
finally
payload.Free;
end;
Result := &output;
end;
class function TSgcMempool.Subscribe(
const chain : TChain;
const apiKey : string;
const address: TAddress;
const filters: IFilters;
const abi : TJsonArray;
onEvent : TAsyncJsonObject;
onError : TAsyncError;
onDisconnect : TProc): IMempool;
var
&output: TSgcMempool;
payload: TJsonObject;
config : TJsonObject;
begin
&output := TSgcMempool.Create(
chain,
apiKey,
onEvent,
onError,
onDisconnect
);
&output.Initialize;
payload := unmarshal(&output.CreatePayload('configs', 'put')) as TJsonObject;
if Assigned(payload) then
try
config := unmarshal(Format('{"scope":"%s","watchAddress":true}', [address])) as TJsonObject;
if Assigned(config) then
begin
if Assigned(filters) then
config.AddPair('filters', filters.AsArray);
if Assigned(abi) then
config.AddPair('abi', abi);
payload.AddPair('config', config);
end;
&output.Client.WriteData(marshal(payload));
finally
payload.Free;
end;
Result := &output;
end;
procedure TSgcMempool.Unsubscribe(const address: TAddress);
var
payload: TJsonObject;
begin
payload := unmarshal(CreatePayload('accountAddress', 'unwatch')) as TJsonObject;
if Assigned(payload) then
try
payload.AddPair('account', unmarshal(Format('{"address":"%s"}', [address])));
Client.WriteData(marshal(payload));
finally
payload.Free;
end;
end;
procedure TSgcMempool.Initialize;
begin
Client.WriteData(CreatePayload('initialize', 'checkDappId'));
end;
procedure TSgcMempool.Disconnect;
begin
if Self.Connected then FClient.Disconnect;
end;
function TSgcMempool.Connected: Boolean;
begin
Result := Assigned(FClient) and FClient.Active;
end;
end.