-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
web3.defillama.pas
148 lines (131 loc) · 4.96 KB
/
web3.defillama.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
{******************************************************************************}
{ }
{ Delphereum }
{ }
{ Copyright(c) 2022 Stefan van As <svanas@runbox.com> }
{ 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.defillama;
interface
uses
// Delphi
System.JSON,
System.SysUtils,
System.Types,
// web3
web3;
type
ICoin = interface
function Symbol : string; // most common symbol used to identify this asset on an exchange
function Price : Double; // volume-weighted price based on real-time market data, translated to USD
function Decimals: Integer;
end;
function coin(const chain: TChain; const address: TAddress; const callback: TProc<ICoin, IError>): IAsyncResult; overload;
function coin(const chain: TChain; const address: TAddress; const callback: TProc<TJsonValue, IError>): IAsyncResult; overload;
function price(const chain: TChain; const address: TAddress; const callback: TProc<Double, IError>): IAsyncResult;
implementation
uses
// web3
web3.eth.types,
web3.http,
web3.json;
type
TCoin = class(TDeserialized, ICoin)
public
function Symbol : string;
function Price : Double;
function Decimals: Integer;
end;
function TCoin.Symbol: string;
begin
Result := getPropAsStr(FJsonValue, 'symbol');
end;
function TCoin.Price: Double;
begin
Result := getPropAsDouble(FJsonValue, 'price');
end;
function TCoin.Decimals: Integer;
begin
Result := getPropAsInt(FJsonValue, 'decimals');
end;
function network(chain: TChain): string; inline;
begin
if chain = Ethereum then
Result := 'ethereum'
else if chain = Optimism then
Result := 'optimism'
else if chain = RSK then
Result := 'rsk'
else if chain = BNB then
Result := 'bsc'
else if chain = Gnosis then
Result := 'xdai'
else if chain = Polygon then
Result := 'polygon'
else if chain = Fantom then
Result := 'fantom'
else if chain = Arbitrum then
Result := 'arbitrum'
else if chain = Base then
Result := 'base'
else if chain = PulseChain then
Result := 'pulse';
end;
function coin(const chain: TChain; const address: TAddress; const callback: TProc<ICoin, IError>): IAsyncResult;
begin
Result := coin(chain, address, procedure(obj: TJsonValue; err: IError)
begin
if Assigned(err) then
begin
callback(nil, err);
EXIT;
end;
const coins = getPropAsObj(obj, 'coins');
if not Assigned(coins) then
begin
callback(nil, TError.Create('coins is null'));
EXIT;
end;
const coin = getPropAsObj(coins, Format('%s:%s', [network(chain), address.ToChecksum]));
if not Assigned(coin) then
begin
callback(nil, TError.Create('defillama does not have current price of %s on %s', [address.Abbreviated, network(chain)]));
EXIT;
end;
callback(TCoin.Create(coin), nil);
end);
end;
function coin(const chain: TChain; const address: TAddress; const callback: TProc<TJsonValue, IError>): IAsyncResult;
begin
Result := web3.http.get(
Format('https://coins.llama.fi/prices/current/%s:%s/', [network(chain), address.ToChecksum]),
[], callback
);
end;
function price(const chain: TChain; const address: TAddress; const callback: TProc<Double, IError>): IAsyncResult;
begin
Result := coin(chain, address, procedure(coin: ICoin; err: IError)
begin
if not Assigned(coin) then
callback(0, err)
else
callback(coin.Price, err);
end);
end;
end.