forked from Militereum/Militereum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.pas
405 lines (361 loc) · 11.6 KB
/
common.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
unit common;
interface
uses
// Delphi
System.Net.URLClient,
System.SysUtils,
// Indy
IdGlobal,
// web3
web3,
web3.eth.etherscan,
// project
server;
const
NUM_CHAINS = 7;
LIMIT = 5000; // USD
type
TEthereumRPCServerHelper = class helper for TEthereumRPCServer
public
function chain(const port: TIdPort): PChain;
function port(const chain: TChain): IResult<TIdPort>;
function apiKey(const port: TIdPort): IResult<string>;
function endpoint(const port: TIdPort): IResult<string>;
end;
type
TSemVer = record
private
Major: Integer;
Minor: Integer;
Patch: Integer;
public
constructor Create(const aMajor, aMinor, aPatch: Integer);
class operator Equal(const A, B: TSemVer): Boolean;
class operator GreaterThan(const A, B: TSemVer): Boolean;
class operator GreaterThanOrEqual(const A, B: TSemVer): Boolean;
end;
function Debug: Boolean;
function Demo: Boolean;
function Simulate: Boolean;
function Ethereum: TChain;
function Etherscan(const chain: TChain): IResult<IEtherscan>;
function Format(const value: Double): string;
function GetTempFileName: string;
function Headers: TNetHeaders;
procedure Open(const URL: string);
function ParseSemVer(const version: string): TSemVer;
function AutoRunEnabled: Boolean;
procedure EnableAutoRun;
procedure DisableAutoRun;
function DarkModeEnabled: Boolean;
procedure EnableDarkMode;
procedure DisableDarkMode;
procedure initialize;
procedure finalize;
procedure beforeShowDialog;
procedure afterShowDialog;
implementation
uses
// Delphi
System.Character,
System.Classes,
System.IOUtils,
{$IFDEF MACOS}
common.mac,
{$ENDIF MACOS}
{$IFDEF MSWINDOWS}
WinAPI.ShellAPI,
WinAPI.Windows,
common.win,
{$ENDIF MSWINDOWS}
{$IFDEF POSIX}
Posix.Stdlib,
Posix.Unistd,
{$ENDIF POSIX}
// web3
web3.eth.alchemy,
// project
docker;
{$I keys/alchemy.api.key}
{$I keys/etherscan.api.key}
function TEthereumRPCServerHelper.chain(const port: TIdPort): PChain;
begin
if (Self.Bindings.Count > 0) and (port = Self.Bindings[0].Port) then
Result := @web3.Ethereum
else if (Self.Bindings.Count > 1) and (port = Self.Bindings[1].Port) then
Result := @web3.Holesky
else if (Self.Bindings.Count > 2) and (port = Self.Bindings[2].Port) then
Result := @web3.Sepolia
else if (Self.Bindings.Count > 3) and (port = Self.Bindings[3].Port) then
Result := @web3.Polygon
else if (Self.Bindings.Count > 4) and (port = Self.Bindings[4].Port) then
Result := @web3.Arbitrum
else if (Self.Bindings.Count > 5) and (port = Self.Bindings[5].Port) then
Result := @web3.Optimism
else if (Self.Bindings.Count > 6) and (port = Self.Bindings[6].Port) then
Result := @web3.Base
else
Result := nil;
if Assigned(Result) then
begin
const endpoint = endpoint(port);
if endpoint.isOk then
Result^.SetRPC(endpoint.Value);
end;
end;
function TEthereumRPCServerHelper.port(const chain: TChain): IResult<TIdPort>;
begin
if (chain = web3.Ethereum) and (Self.Bindings.Count > 0) then
Result := TResult<TIdPort>.Ok(Self.Bindings[0].Port)
else if (chain = web3.Holesky) and (Self.Bindings.Count > 1) then
Result := TResult<TIdPort>.Ok(Self.Bindings[1].Port)
else if (chain = web3.Sepolia) and (Self.Bindings.Count > 2) then
Result := TResult<TIdPort>.Ok(Self.Bindings[2].Port)
else if (chain = web3.Polygon) and (Self.Bindings.Count > 3) then
Result := TResult<TIdPort>.Ok(Self.Bindings[3].Port)
else if (chain = web3.Arbitrum) and (Self.Bindings.Count > 4) then
Result := TResult<TIdPort>.Ok(Self.Bindings[4].Port)
else if (chain = web3.Optimism) and (Self.Bindings.Count > 5) then
Result := TResult<TIdPort>.Ok(Self.Bindings[5].Port)
else if (chain = web3.Base) and (Self.Bindings.Count > 6) then
Result := TResult<TIdPort>.Ok(Self.Bindings[6].Port)
else
Result := TResult<TIdPort>.Err(System.SysUtils.Format('invalid chain: %s', [chain.Name]));
end;
function TEthereumRPCServerHelper.apiKey(const port: TIdPort): IResult<string>;
begin
if (Self.Bindings.Count > 0) and (port = Self.Bindings[0].Port) then
Result := TResult<string>.Ok(ALCHEMY_API_KEY_ETHEREUM)
else if (Self.Bindings.Count > 1) and (port = Self.Bindings[1].Port) then
Result := TResult<string>.Ok(ALCHEMY_API_KEY_HOLESKY)
else if (Self.Bindings.Count > 2) and (port = Self.Bindings[2].Port) then
Result := TResult<string>.Ok(ALCHEMY_API_KEY_SEPOLIA)
else if (Self.Bindings.Count > 3) and (port = Self.Bindings[3].Port) then
Result := TResult<string>.Ok(ALCHEMY_API_KEY_POLYGON)
else if (Self.Bindings.Count > 4) and (port = Self.Bindings[4].Port) then
Result := TResult<string>.Ok(ALCHEMY_API_KEY_ARBITRUM)
else if (Self.Bindings.Count > 5) and (port = Self.Bindings[5].Port) then
Result := TResult<string>.Ok(ALCHEMY_API_KEY_OPTIMISM)
else if (Self.Bindings.Count > 6) and (port = Self.Bindings[6].Port) then
Result := TResult<string>.Ok(ALCHEMY_API_KEY_BASE)
else
Result := TResult<string>.Err(System.SysUtils.Format('invalid port: %d', [port]));
end;
function TEthereumRPCServerHelper.endpoint(const port: TIdPort): IResult<string>;
begin
if (Self.Bindings.Count > 0) and (port = Self.Bindings[0].Port) then
Result := web3.eth.alchemy.endpoint(web3.Ethereum, ALCHEMY_API_KEY_ETHEREUM, core)
else if (Self.Bindings.Count > 1) and (port = Self.Bindings[1].Port) then
Result := web3.eth.alchemy.endpoint(web3.Holesky, ALCHEMY_API_KEY_HOLESKY, core)
else if (Self.Bindings.Count > 2) and (port = Self.Bindings[2].Port) then
Result := web3.eth.alchemy.endpoint(web3.Sepolia, ALCHEMY_API_KEY_SEPOLIA, core)
else if (Self.Bindings.Count > 3) and (port = Self.Bindings[3].Port) then
Result := web3.eth.alchemy.endpoint(web3.Polygon, ALCHEMY_API_KEY_POLYGON, core)
else if (Self.Bindings.Count > 4) and (port = Self.Bindings[4].Port) then
Result := web3.eth.alchemy.endpoint(web3.Arbitrum, ALCHEMY_API_KEY_ARBITRUM, core)
else if (Self.Bindings.Count > 5) and (port = Self.Bindings[5].Port) then
Result := web3.eth.alchemy.endpoint(web3.Optimism, ALCHEMY_API_KEY_OPTIMISM, core)
else if (Self.Bindings.Count> 6) and (port = Self.Bindings[6].Port) then
Result := web3.eth.alchemy.endpoint(web3.Base, ALCHEMY_API_KEY_BASE, core)
else
Result := TResult<string>.Err(System.SysUtils.Format('invalid port: %d', [port]));
if Result.isOk then
if docker.getContainerId(RPCh_CONTAINER_NAME) <> '' then
Result := TResult<string>.Ok(Self.URI(RPCh_PORT_NUMBER) + '/?provider=' + Result.Value);
end;
function Debug: Boolean;
begin
Result := FindCmdLineSwitch('debug');
end;
function Demo: Boolean;
begin
Result := FindCmdLineSwitch('demo');
end;
function Simulate: Boolean;
begin
Result := FindCmdLineSwitch('simulate');
end;
constructor TSemVer.Create(const aMajor, aMinor, aPatch: Integer);
begin
Self.Major := aMajor;
Self.Minor := aMinor;
Self.Patch := aPatch;
end;
class operator TSemVer.Equal(const A, B: TSemVer): Boolean;
begin
Result := (A.Major = B.Major) and (A.Minor = B.Minor) and (A.Patch = B.Patch);
end;
class operator TSemVer.GreaterThan(const A, B: TSemVer): Boolean;
begin
Result := (A.Major > B.Major)
or ((A.Major = B.Major) and (A.Minor > B.Minor))
or ((A.Major = B.Major) and (A.Minor = B.Minor) and (A.Patch > B.Patch));
end;
class operator TSemVer.GreaterThanOrEqual(const A, B: TSemVer): Boolean;
begin
Result := (A > B) or (A = B);
end;
function Ethereum: TChain;
begin
Result := web3.Ethereum.SetRPC(
web3.eth.alchemy.endpoint(web3.Ethereum, ALCHEMY_API_KEY_ETHEREUM, core).Value
);
end;
function Etherscan(const chain: TChain): IResult<IEtherscan>;
begin
if chain = web3.Ethereum then
Result := TResult<IEtherscan>.Ok(web3.eth.etherscan.create(chain, ETHERSCAN_API_KEY_ETHEREUM))
else if chain = web3.Holesky then
Result := TResult<IEtherscan>.Ok(web3.eth.etherscan.create(chain, ETHERSCAN_API_KEY_HOLESKY))
else if chain = web3.Sepolia then
Result := TResult<IEtherscan>.Ok(web3.eth.etherscan.create(chain, ETHERSCAN_API_KEY_SEPOLIA))
else if chain = web3.Polygon then
Result := TResult<IEtherscan>.Ok(web3.eth.etherscan.create(chain, ETHERSCAN_API_KEY_POLYGON))
else if chain = web3.Arbitrum then
Result := TResult<IEtherscan>.Ok(web3.eth.etherscan.create(chain, ETHERSCAN_API_KEY_ARBITRUM))
else if chain = web3.Optimism then
Result := TResult<IEtherscan>.Ok(web3.eth.etherscan.create(chain, ETHERSCAN_API_KEY_OPTIMISM))
else if chain = web3.Base then
Result := TResult<IEtherscan>.Ok(web3.eth.etherscan.create(chain, ETHERSCAN_API_KEY_BASE))
else
Result := TResult<IEtherscan>.Err(System.SysUtils.Format('not supported on %s', [chain.Name]));
end;
function Format(const value: Double): string;
begin
Result := System.SysUtils.Format('%.6f', [value]);
while (Length(Result) > 0) and (Result[High(Result)] = '0') do
Delete(Result, High(Result), 1);
if (Length(Result) > 0) and (Result[High(Result)] = FormatSettings.DecimalSeparator) then
Delete(Result, High(Result), 1);
end;
function GetTempFileName: string;
begin
Result := TPath.GetTempFileName;
System.SysUtils.DeleteFile(Result);
end;
function Headers: TNetHeaders;
begin
Result := [TNetHeader.Create('Content-Type', 'application/json')];
end;
procedure Open(const URL: string);
begin
{$IFDEF MSWINDOWS}
ShellExecute(0, 'open', PChar(URL), nil, nil, SW_SHOWNORMAL);
{$ENDIF MSWINDOWS}
{$IFDEF POSIX}
_system(PAnsiChar('open ' + AnsiString(URL)));
{$ENDIF POSIX}
end;
function ParseSemVer(const version: string): TSemVer;
begin
FillChar(Result, SizeOf(Result), 0);
const SL = TStringList.Create;
try
SL.Delimiter := '.';
SL.StrictDelimiter := True;
SL.DelimitedText := version;
for var i := 0 to Pred(SL.Count) do
begin
var S := SL[i];
var n := Low(S);
while n <= High(S) do
if S[n].IsNumber then
Inc(n)
else
if (S <> '') and S[Low(S)].IsNumber then
Delete(S, n, Length(S) - n + 1)
else
Delete(S, n, 1);
case I of
0: Result.Major := StrToIntDef(S, 0);
1: Result.Minor := StrToIntDef(S, 0);
2: Result.Patch := StrToIntDef(S, 0);
end;
end;
finally
SL.Free;
end;
end;
function AutoRunEnabled: Boolean;
begin
{$IFDEF MACOS}
Result := common.mac.autoRunEnabled;
{$ENDIF MACOS}
{$IFDEF MSWINDOWS}
Result := common.win.autoRunEnabled;
{$ENDIF MSWINDOWS}
end;
procedure EnableAutoRun;
begin
{$IFDEF MACOS}
common.mac.enableAutoRun;
{$ENDIF MACOS}
{$IFDEF MSWINDOWS}
common.win.enableAutoRun;
{$ENDIF MSWINDOWS}
end;
procedure DisableAutoRun;
begin
{$IFDEF MACOS}
common.mac.disableAutoRun;
{$ENDIF MACOS}
{$IFDEF MSWINDOWS}
common.win.disableAutoRun;
{$ENDIF MSWINDOWS}
end;
function DarkModeEnabled: Boolean;
begin
{$IFDEF MACOS}
Result := common.mac.darkModeEnabled;
{$ENDIF MACOS}
{$IFDEF MSWINDOWS}
Result := common.win.darkModeEnabled;
{$ENDIF MSWINDOWS}
end;
procedure EnableDarkMode;
begin
{$IFDEF MACOS}
common.mac.enableDarkMode;
{$ENDIF MACOS}
{$IFDEF MSWINDOWS}
common.win.enableDarkMode;
{$ENDIF MSWINDOWS}
end;
procedure DisableDarkMode;
begin
{$IFDEF MACOS}
common.mac.disableDarkMode;
{$ENDIF MACOS}
{$IFDEF MSWINDOWS}
common.win.disableDarkMode;
{$ENDIF MSWINDOWS}
end;
procedure initialize;
begin
{$IFDEF MACOS}
common.mac.initialize;
{$ENDIF MACOS}
{$IFDEF MSWINDOWS}
common.win.initialize;
{$ENDIF MSWINDOWS}
if DarkModeEnabled then EnableDarkMode;
end;
procedure finalize;
begin
{$IFDEF MSWINDOWS}
common.win.finalize;
{$ENDIF MSWINDOWS}
end;
procedure beforeShowDialog;
begin
{$IFDEF MACOS}
common.mac.beforeShowDialog;
{$ENDIF MACOS}
end;
procedure afterShowDialog;
begin
{$IFDEF MACOS}
common.mac.afterShowDialog;
{$ENDIF MACOS}
end;
end.