-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathweb3.eth.logs.pas
315 lines (281 loc) · 8.81 KB
/
web3.eth.logs.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
{******************************************************************************}
{ }
{ Delphereum }
{ }
{ Copyright(c) 2019 Stefan van As <[email protected]> }
{ Github Repository <https://github.com/svanas/delphereum> }
{ }
{ Distributed under GNU AGPL v3.0 with Commons Clause }
{ }
{ This program is free software: you can redistribute it and/or modify }
{ it under the terms of the GNU Affero General Public License as published }
{ by the Free Software Foundation, either version 3 of the License, or }
{ (at your option) any later version. }
{ }
{ This program is distributed in the hope that it will be useful, }
{ but WITHOUT ANY WARRANTY; without even the implied warranty of }
{ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the }
{ GNU Affero General Public License for more details. }
{ }
{ You should have received a copy of the GNU Affero General Public License }
{ along with this program. If not, see <https://www.gnu.org/licenses/> }
{ }
{******************************************************************************}
unit web3.eth.logs;
{$I web3.inc}
interface
uses
// Delphi
System.JSON,
System.SysUtils,
// Velthuis' BigNumbers
Velthuis.BigIntegers,
// web3
web3,
web3.eth.types;
type
TLog = record
private
FBlockNumber: BigInteger;
FTopics : TTopics;
FData : TTuple;
function GetTopic(const idx: Integer): TArg;
procedure Load(const tx: TJsonValue);
public
function isEvent(const name: string): Boolean;
property BlockNumber: BigInteger read FBlockNumber;
property Topic[const idx: Integer]: TArg read GetTopic;
property Data: TTuple read FData;
end;
PLog = ^TLog;
TStatus = (Idle, Running, Paused, Stopped);
ILogger = interface
['{C2F29D30-00FC-4598-BEF4-59B65A9F55B5}']
function Pause: IError; // pauses the logger
function Start: IError; // starts the logger, adding it to the currect queue
function Status: TStatus; // return the logger's current status
function Stop: IError; // signals the logger to stop
function Wait: IError; // waits for the logger to complete execution
end;
function get(const client: IWeb3; const address: TAddress; const callback: TProc<PLog, IError>): ILogger;
implementation
uses
// Delphi
System.Classes,
System.Generics.Collections,
System.Threading,
// web3
web3.eth,
web3.json,
web3.json.rpc,
web3.utils;
{ TLog }
function TLog.GetTopic(const idx: Integer): TArg;
begin
Result := FTopics[idx];
end;
procedure TLog.Load(const tx: TJsonValue);
begin
FBlockNumber := web3.json.getPropAsStr(tx, 'blockNumber');
// load the "topics"
const tpcs = web3.json.getPropAsArr(tx, 'topics');
if Assigned(tpcs) then
for var tpc := 0 to Pred(tpcs.Count) do
begin
const buf = web3.utils.fromHex(tpcs.Items[tpc].Value);
if Length(buf) >= SizeOf(TArg) then
begin
var arg: TArg;
Move(buf[0], arg.Inner[0], SizeOf(TArg));
FTopics[tpc] := arg;
end;
end;
// load the "data"
var buf := web3.utils.fromHex(web3.json.getPropAsStr(tx, 'data'));
while Length(buf) >= SizeOf(TArg) do
begin
const last = Data.Add;
Move(buf[0], last^.Inner[0], SizeOf(TArg));
Delete(buf, 0, SizeOf(TArg));
end;
end;
function TLog.isEvent(const name: string): Boolean;
begin
const buf = web3.utils.sha3(web3.utils.toHex(name));
var arg: TArg;
Move(buf[0], arg.Inner[0], SizeOf(TArg));
Result := CompareMem(@FTopics[0], @arg, SizeOf(TArg));
end;
{ TLogs }
type
TLogs = TArray<TLog>;
{ TLogsHelper }
type
TLogsHelper = record helper for TLogs
function Add : PLog;
function Last: PLog;
end;
function TLogsHelper.Add: PLog;
begin
SetLength(Self, Length(Self) + 1);
Result := Last;
end;
function TLogsHelper.Last: PLog;
begin
Result := nil;
if Length(Self) > 0 then
Result := @Self[High(Self)];
end;
{ private functions }
function getAsArr(const client: IWeb3; const fromBlock: BigInteger; const address: TAddress): IResult<TJsonArray>;
begin
const request = web3.json.unmarshal(Format(
'{"fromBlock": "%s", "toBlock": %s, "address": %s}', [
web3.utils.toHex(fromBlock, [zeroAs0x0]),
web3.json.quoteString(BLOCK_LATEST, '"'),
web3.json.quoteString(string(address), '"')
]
)) as TJsonObject;
try
const response = client.Call('eth_getLogs', [request]);
if Assigned(response.Value) then
try
const arr = web3.json.getPropAsArr(response.Value, 'result');
if Assigned(arr) then
begin
Result := TResult<TJsonArray>.Ok(arr.Clone as TJsonArray);
EXIT;
end;
finally
response.Value.Free;
end;
Result := TResult<TJsonArray>.Err(response.Error);
finally
request.Free;
end;
end;
function getAsLog(const client: IWeb3; const fromBlock: BigInteger; const address: TAddress): IResult<TLogs>;
begin
const arr = getAsArr(client, fromBlock, address);
if Assigned(arr.Value) then
try
var logs: TLogs := [];
try
for var itm in arr.Value do
begin
const last = logs.Add;
last.Load(itm);
end;
finally
Result := TResult<TLogs>.Ok(logs);
end;
EXIT;
finally
arr.Value.Free;
end;
Result := TResult<TLogs>.Err(arr.Error);
end;
{ public functions }
type
TLogger = class(TTask, ILogger)
strict private
FPaused : Boolean;
FStopped: Boolean;
public
constructor Create(const Proc: TProc);
function Pause: IError;
function Start: IError;
function Status: TStatus;
function Stop: IError;
function Wait: IError;
end;
constructor TLogger.Create(const Proc: TProc);
begin
inherited Create(nil, TNotifyEvent(nil), Proc, TThreadPool.Default, nil);
end;
function TLogger.Pause: IError;
begin
Result := nil;
case Self.Status of
Idle : Result := TError.Create('Cannot pause a logger that is not running');
Running: FPaused := True;
Paused : { nothing };
Stopped: Result := TError.Create('Cannot pause a logger that has already stopped');
end;
end;
function TLogger.Start: IError;
begin
Result := nil;
case Self.Status of
Idle : inherited Start;
Running: { nothing };
Paused : FPaused := False;
Stopped: Result := TError.Create('Cannot start a logger that has already stopped');
end;
end;
function TLogger.Status: TStatus;
begin
Result := Idle;
if FStopped or (Self.GetStatus = TTaskStatus.Completed) then
Result := Stopped
else
if Self.GetStatus in [TTaskStatus.WaitingToRun, TTaskStatus.Running] then
if FPaused then
Result := Paused
else
Result := Running;
end;
function TLogger.Stop: IError;
begin
Result := nil;
case Self.Status of
Idle : Result := TError.Create('Cannot stop a logger that is not running');
Running: FStopped := True;
Paused : FStopped := True;
Stopped: { nothing };
end;
end;
function TLogger.Wait: IError;
begin
Result := nil;
if Self.Status = Paused then
Result := TError.Create('Cannot wait for a paused logger to complete')
else
inherited Wait;
end;
function get(const client: IWeb3; const address: TAddress; const callback: TProc<PLog, IError>): ILogger;
begin
Result := TLogger.Create(procedure
begin
web3.eth.blockNumber(client)
.ifErr(procedure(err: IError)
begin
callback(nil, err)
end)
.&else(procedure(latest: BigInteger)
begin
while (TTask.CurrentTask as ILogger).Status <> Stopped do
begin
TThread.Sleep(500);
if (TTask.CurrentTask as ILogger).Status <> Stopped then
begin
web3.eth.logs.getAsLog(client, latest, address)
.ifErr(procedure(err: IError)
begin
callback(nil, err)
end)
.&else(procedure(logs: TLogs)
begin
for var log in logs do
begin
latest := BigInteger.Max(latest, log.BlockNumber.Succ);
if (TTask.CurrentTask as ILogger).Status <> Paused then
callback(@log, nil);
end;
end);
end;
end;
end);
end);
end;
end.