forked from Militereum/Militereum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvaults.fyi.pas
228 lines (204 loc) · 6.24 KB
/
vaults.fyi.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
unit vaults.fyi;
interface
uses
// Delphi
System.SysUtils,
// Velthuis' BigNumbers
Velthuis.BigIntegers,
// web3
web3;
type
IToken = interface
function Name: string;
function Address: TAddress;
function Symbol: string;
end;
IVault = interface
function Name: string;
function Address: TAddress;
function TVL: BigInteger;
function Token: IToken;
function APY: Double; // 7-day avg
function URL: string;
end;
procedure network(const chain: TChain; const callback: TProc<string, IError>);
procedure vault(const network: string; const contract: TAddress; const callback: TProc<IVault, IError>);
procedure vaults(const network, symbol: string; const callback: TProc<TArray<IVault>, IError>);
procedure better(const chain: TChain; const contract: TAddress; const callback: TProc<IVault, IError>);
implementation
uses
// Delphi
System.JSON,
System.Net.URLClient,
// web3
web3.eth.types,
web3.http,
web3.json;
const VAULTS_API_BASE = 'https://api.vaults.fyi/v1/';
procedure network(const chain: TChain; const callback: TProc<string, IError>);
begin
web3.http.get(VAULTS_API_BASE + 'networks', [TNetHeader.Create('accept', 'application/json'), TNetHeader.Create('x-api-key', {$I keys/vaults.fyi.api.key})],
procedure(response: TJsonValue; err: IError)
begin
if Assigned(err) then
begin
callback('', err);
EXIT;
end;
if not(response is TJsonArray) then
begin
callback('', TError.Create('response is not an array'));
EXIT;
end;
for var network in response as TJsonArray do
if getPropAsUInt64(network, 'chainId') = chain.Id then
begin
callback(getPropAsStr(network, 'name'), nil);
EXIT;
end;
callback('', TError.Create('%s not supported', [chain.Name]));
end);
end;
type
TToken = class(TDeserialized, IToken)
function Name: string;
function Address: TAddress;
function Symbol: string;
end;
function TToken.Name: string;
begin
Result := getPropAsStr(FJsonValue, 'name');
end;
function TToken.Address: TAddress;
begin
Result := TAddress.Create(getPropAsStr(FJsonValue, 'assetAddress'));
end;
function TToken.Symbol: string;
begin
Result := getPropAsStr(FJsonValue, 'symbol');
end;
type
TVault = class(TDeserialized, IVault)
public
function Name: string;
function Address: TAddress;
function TVL: BigInteger;
function Token: IToken;
function APY: Double; // 7-day avg
function URL: string;
end;
function TVault.Name: string;
begin
Result := getPropAsStr(FJsonValue, 'name');
end;
function TVault.Address: TAddress;
begin
Result := TAddress.Create(getPropAsStr(FJsonValue, 'address'));
end;
function TVault.TVL: BigInteger;
begin
Result := getPropAsBigInt(FJsonValue, 'tvl');
end;
function TVault.Token: IToken;
begin
Result := TToken.Create(getPropAsObj(FJsonValue, 'token'));
end;
function TVault.APY: Double;
begin
Result := 0;
const apy = getPropAsObj(FJsonValue, 'apy');
if Assigned(apy) then
begin
Result := getPropAsInt(apy, '7day');
if Result > 0 then Result := Result / 100;
end;
end;
function TVault.URL: string;
begin
Result := Format('https://www.vaults.fyi/vaults/%s/%s', [getPropAsStr(FJsonValue, 'network'), getPropAsStr(FJsonValue, 'address')]);
end;
procedure vault(const network: string; const contract: TAddress; const callback: TProc<IVault, IError>);
begin
web3.http.get(VAULTS_API_BASE + Format('vaults/%s/%s', [network, contract.ToChecksum]), [TNetHeader.Create('accept', 'application/json'), TNetHeader.Create('x-api-key', {$I keys/vaults.fyi.api.key})],
procedure(response: TJsonValue; err: IError)
begin
if Assigned(err) then
begin
callback(nil, err);
EXIT;
end;
callback(TVault.Create(response), nil);
end);
end;
procedure vaults(const network, symbol: string; const callback: TProc<TArray<IVault>, IError>);
type
TNext = reference to procedure(const page: Integer);
begin
var next : TNext;
var result: TArray<IVault> := [];
try
next := procedure(const page: Integer)
begin
web3.http.get(VAULTS_API_BASE + Format('detailed/vaults?network=%s&token=%s&page=%d', [network, symbol, page]), [TNetHeader.Create('accept', 'application/json'), TNetHeader.Create('x-api-key', {$I keys/vaults.fyi.api.key})],
procedure(response: TJsonValue; err: IError)
begin
const data = getPropAsArr(response, 'data');
if not Assigned(data) then
begin
callback([], TError.Create('data is not an array'));
EXIT;
end;
for var vault in data do result := result + [TVault.Create(vault)];
const page = getPropAsInt(response, 'next_page');
if page > 0 then
next(page)
else
callback(result, nil);
end);
end;
finally
next(0);
end;
end;
procedure better(const chain: TChain; const contract: TAddress; const callback: TProc<IVault, IError>);
begin
// first of all, get the vaults.fyi network string
network(chain, procedure(network: string; err: IError)
begin
if Assigned(err) then
begin
callback(nil, err);
EXIT;
end;
// is our contract address in the vaults.fyi API?
vault(network, contract, procedure(this: IVault; err: IError)
begin
if Assigned(err) or not Assigned(this) then
begin
callback(nil, nil); // vault does not exist
EXIT;
end;
// if yes, compare the APY with the other vaults in the vaults.fyi API
vaults(network, this.Token.Symbol, procedure(vaults: TArray<IVault>; err: IError)
begin
if Assigned(err) then
begin
callback(nil, err);
EXIT;
end;
var best: IVault := nil;
try
// if another vault provides for a higher APY while holding the same TVL (or more), return the better vault
for var that in vaults do if (not that.Address.SameAs(this.Address)) and (that.APY > this.APY) and (that.TVL >= this.TVL) then
begin
this := that;
best := that;
end;
finally
callback(best, nil);
end;
end);
end);
end);
end;
end.