forked from svanas/delphereum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb3.eth.chainlink.pas
123 lines (107 loc) · 3.19 KB
/
web3.eth.chainlink.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
{******************************************************************************}
{ }
{ 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.chainlink;
interface
uses
// web3
web3,
web3.eth,
web3.eth.contract,
web3.eth.types;
type
TAggregatorV3 = class(TCustomContract)
public
procedure LatestRoundData(callback: TAsyncTuple);
procedure Decimals(callback: TAsyncQuantity);
procedure Price(callback: TAsyncFloat);
end;
TEthUsd = class(TAggregatorV3)
public
constructor Create(aClient: TWeb3); reintroduce;
end;
procedure eth_usd(client: TWeb3; callback: TAsyncFloat);
implementation
uses
// Delphi
System.Math,
// Velthuis' BigNumbers
Velthuis.BigIntegers,
// web3
web3.coincap;
procedure eth_usd(client: TWeb3; callback: TAsyncFloat);
var
EthUsd: TEthUsd;
begin
// Ethereum price feed is available on Mainnet and Rinkeby and Kovan only.
if client.Chain in [Mainnet, Rinkeby, Kovan] then
begin
EthUsd := TEthUsd.Create(client);
if Assigned(EthUsd) then
begin
EthUsd.Price(procedure(price: Extended; err: IError)
begin
try
callback(price, err);
finally
EthUsd.Free;
end;
end);
EXIT;
end;
end;
// Not on Mainnet or Rinkeby or Kovan? Fall back on api.coincap.io
web3.coincap.ticker('ethereum', procedure(ticker: ITicker; err: IError)
begin
callback(ticker.Price, err);
end);
end;
{ TAggregatorV3 }
procedure TAggregatorV3.LatestRoundData(callback: TAsyncTuple);
begin
web3.eth.call(Client, Contract, 'latestRoundData()', [], callback);
end;
procedure TAggregatorV3.Decimals(callback: TAsyncQuantity);
begin
web3.eth.call(Client, Contract, 'decimals()', [], callback);
end;
procedure TAggregatorV3.Price(callback: TAsyncFloat);
begin
Self.LatestRoundData(procedure(tup: TTuple; err: IError)
begin
if Assigned(err) then
begin
callback(0, err);
EXIT;
end;
Self.Decimals(procedure(decimals: BigInteger; err: IError)
begin
if Assigned(err) then
begin
callback(0, err);
EXIT;
end;
callback(tup[1].toInt64 / Power(10, decimals.AsInteger), nil);
end);
end);
end;
{ TEthUsd}
constructor TEthUsd.Create(aClient: TWeb3);
begin
case aClient.Chain of
Mainnet:
inherited Create(aClient, '0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419');
Rinkeby:
inherited Create(aClient, '0x8A753747A1Fa494EC906cE90E9f37563A8AF630e');
Kovan:
inherited Create(aClient, '0x9326BFA02ADD2366b30bacB125260Af641031331');
end;
end;
end.