forked from svanas/delphereum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb3.eth.gas.station.pas
124 lines (104 loc) · 3.29 KB
/
web3.eth.gas.station.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
{******************************************************************************}
{ }
{ 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.eth.gas.station;
{$I web3.inc}
interface
uses
// Delphi
System.Types,
// web3
web3,
web3.http;
type
IGasPrice = interface
function Outbid : TWei;
function Fastest: TWei;
function Fast : TWei; // expected to be mined in < 2 minutes
function Average: TWei; // expected to be mined in < 5 minutes
function SafeLow: TWei; // expected to be mined in < 30 minutes
end;
TAsyncGasPrice = reference to procedure(price: IGasPrice; err: IError);
function getGasPrice(
const apiKey: string;
callback : TAsyncGasPrice): IAsyncResult; overload;
function getGasPrice(
const apiKey: string;
callback : TAsyncJsonObject): IAsyncResult; overload;
implementation
uses
// Delphi
System.JSON,
System.NetEncoding,
// web3
web3.eth.utils,
web3.json;
type
TGasPrice = class(TInterfacedObject, IGasPrice)
private
FJsonObject: TJsonObject;
public
function Outbid : TWei;
function Fastest: TWei;
function Fast : TWei;
function Average: TWei;
function SafeLow: TWei;
constructor Create(aJsonObject: TJsonObject);
destructor Destroy; override;
end;
constructor TGasPrice.Create(aJsonObject: TJsonObject);
begin
inherited Create;
FJsonObject := aJsonObject;
end;
destructor TGasPrice.Destroy;
begin
if Assigned(FJsonObject) then
FJsonObject.Free;
inherited Destroy;
end;
function TGasPrice.Outbid: TWei;
begin
Result := toWei(FloatToEth(getPropAsExt(FJsonObject, 'fastest') / 8), gwei);
end;
function TGasPrice.Fastest: TWei;
begin
Result := toWei(FloatToEth(getPropAsExt(FJsonObject, 'fastest') / 10), gwei);
end;
function TGasPrice.Fast: TWei;
begin
Result := toWei(FloatToEth(getPropAsExt(FJsonObject, 'fast') / 10), gwei);
end;
function TGasPrice.Average: TWei;
begin
Result := toWei(FloatToEth(getPropAsExt(FJsonObject, 'average') / 10), gwei);
end;
function TGasPrice.SafeLow: TWei;
begin
Result := toWei(FloatToEth(getPropAsExt(FJsonObject, 'safeLow') / 10), gwei);
end;
function getGasPrice(const apiKey: string; callback: TAsyncGasPrice): IAsyncResult;
begin
Result := getGasPrice(apiKey, procedure(obj: TJsonObject; err: IError)
begin
if Assigned(err) then
callback(nil, err)
else
callback(TGasPrice.Create(obj.Clone as TJsonObject), nil);
end);
end;
function getGasPrice(const apiKey: string; callback: TAsyncJsonObject): IAsyncResult;
begin
Result := web3.http.get(
'https://ethgasstation.info/api/ethgasAPI.json?api-key=' + TNetEncoding.URL.Encode(apiKey),
callback
);
end;
end.