forked from svanas/delphereum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb3.eth.uniswap.v2.pas
402 lines (374 loc) · 14.5 KB
/
web3.eth.uniswap.v2.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
{******************************************************************************}
{ }
{ 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.uniswap.v2;
{$I web3.inc}
interface
uses
// Velthuis' BigNumbers
Velthuis.BigIntegers,
// Delphi
System.DateUtils,
System.JSON,
System.SysUtils,
// web3
web3,
web3.eth,
web3.eth.abi,
web3.eth.contract,
web3.eth.erc20,
web3.eth.gas,
web3.eth.types,
web3.eth.utils,
web3.graph,
web3.json,
web3.utils;
type
TFactory = class(TCustomContract)
public
constructor Create(aClient: TWeb3); reintroduce;
procedure GetPair(tokenA, tokenB: TAddress; callback: TAsyncAddress);
end;
TRouter02 = class(TCustomContract)
private
procedure EstimateExactTokensForETH(
from : TAddress; // Sender of the token.
amountIn : BigInteger; // The amount of input tokens to send.
amountOutMin: BigInteger; // The minimum amount of output tokens that must be received for the transaction not to revert.
token0 : TAddress; // The address of the pair token with the lower sort order.
token1 : TAddress; // The address of the pair token with the higher sort order.
&to : TAddress; // Recipient of the ETH.
deadline : TUnixDateTime; // Unix timestamp after which the transaction will revert.
callback : TAsyncQuantity); overload;
procedure SwapExactTokensForETH(
from : TPrivateKey; // Sender of the token.
amountIn : BigInteger; // The amount of input tokens to send.
amountOutMin: BigInteger; // The minimum amount of output tokens that must be received for the transaction not to revert.
token0 : TAddress; // The address of the pair token with the lower sort order.
token1 : TAddress; // The address of the pair token with the higher sort order.
&to : TAddress; // Recipient of the ETH.
deadline : TUnixDateTime; // Unix timestamp after which the transaction will revert.
callback : TAsyncReceipt); overload;
procedure SwapExactETHForTokens(
from : TPrivateKey; // Sender of ETH.
amountIn : BigInteger; // The amount of ETH to send.
amountOutMin: BigInteger; // The minimum amount of output tokens that must be received for the transaction not to revert.
token : TAddress; // The token address.
&to : TAddress; // Recipient of the output tokens.
deadline : TUnixDateTime; // Unix timestamp after which the transaction will revert.
callback : TAsyncReceipt); overload;
public
constructor Create(aClient: TWeb3); reintroduce;
procedure WETH(callback: TAsyncAddress);
procedure EstimateExactTokensForETH(
owner : TAddress; // Sender of the token, and recipient of the ETH.
amountIn : BigInteger; // The amount of input tokens to send.
amountOutMin: BigInteger; // The minimum amount of output tokens that must be received for the transaction not to revert.
token : TAddress; // The address of the token you wish to swap.
minutes : Int64; // Your transaction will revert if it is pending for more than this long.
callback : TAsyncQuantity); overload;
procedure SwapExactTokensForETH(
owner : TPrivateKey; // Sender of the token, and recipient of the ETH.
amountIn : BigInteger; // The amount of input tokens to send.
amountOutMin: BigInteger; // The minimum amount of output tokens that must be received for the transaction not to revert.
token : TAddress; // The address of the token you wish to swap.
minutes : Int64; // Your transaction will revert if it is pending for more than this long.
callback : TAsyncReceipt); overload;
procedure SwapExactETHForTokens(
owner : TPrivateKey; // Sender of ETH.
amountIn : BigInteger; // The amount of ETH to send.
amountOutMin: BigInteger; // The minimum amount of output tokens that must be received for the transaction not to revert.
token : TAddress; // The token address.
minutes : Int64; // Your transaction will revert if it is pending for more than this long.
callback : TAsyncReceipt); overload;
end;
TPair = class(TERC20)
protected
function Query (const field: string): string;
procedure Execute(const field: string; callback: TAsyncFloat);
public
procedure Token0(callback: TAsyncAddress);
procedure Token1(callback: TAsyncAddress);
procedure Token0Price(callback: TAsyncFloat);
procedure Token1Price(callback: TAsyncFloat);
end;
implementation
{ TFactory }
constructor TFactory.Create(aClient: TWeb3);
begin
inherited Create(aClient, '0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f');
end;
// Returns the address of the pair for tokenA and tokenB, if it has been created, else 0x0
procedure TFactory.GetPair(tokenA, tokenB: TAddress; callback: TAsyncAddress);
begin
call(Client, Contract, 'getPair(address,address)', [tokenA, tokenB], procedure(const hex: string; err: IError)
var
pair: TAddress;
begin
if Assigned(err) then
begin
callback(ADDRESS_ZERO, err);
EXIT;
end;
pair := TAddress.New(hex);
if pair.IsZero then
begin
callback(ADDRESS_ZERO, TError.Create('%s does not exist', [tokenA]));
EXIT;
end;
callback(pair, nil)
end);
end;
{ TRouter02 }
constructor TRouter02.Create(aClient: TWeb3);
begin
inherited Create(aClient, '0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D');
end;
// Returns the canonical WETH address; see https://blog.0xproject.com/canonical-weth-a9aa7d0279dd
procedure TRouter02.WETH(callback: TAsyncAddress);
begin
call(Client, Contract, 'WETH()', [], procedure(const hex: string; err: IError)
begin
if Assigned(err) then
callback(ADDRESS_ZERO, err)
else
callback(TAddress.New(hex), nil);
end);
end;
// Estimate the number of gas units needed for a Swap
procedure TRouter02.EstimateExactTokensForETH(
from : TAddress; // Sender of the token.
amountIn : BigInteger; // The amount of input tokens to send.
amountOutMin: BigInteger; // The minimum amount of output tokens that must be received for the transaction not to revert.
token0 : TAddress; // The address of the pair token with the lower sort order.
token1 : TAddress; // The address of the pair token with the higher sort order.
&to : TAddress; // Recipient of the ETH.
deadline : TUnixDateTime; // Unix timestamp after which the transaction will revert.
callback : TAsyncQuantity);
begin
estimateGas(Client, from, Contract,
'swapExactTokensForETH(uint256,uint256,address[],address,uint256)',
[
web3.utils.toHex(amountIn),
web3.utils.toHex(amountOutMin),
&array([token0, token1]),
&to,
deadline
], 0, callback);
end;
// Swaps an exact amount of tokens for as much ETH as possible.
procedure TRouter02.SwapExactTokensForETH(
from : TPrivateKey; // Sender of the token.
amountIn : BigInteger; // The amount of input tokens to send.
amountOutMin: BigInteger; // The minimum amount of output tokens that must be received for the transaction not to revert.
token0 : TAddress; // The address of the pair token with the lower sort order.
token1 : TAddress; // The address of the pair token with the higher sort order.
&to : TAddress; // Recipient of the ETH.
deadline : TUnixDateTime; // Unix timestamp after which the transaction will revert.
callback : TAsyncReceipt);
var
erc20: TERC20;
begin
erc20 := TERC20.Create(Self.Client, token0);
if Assigned(erc20) then
begin
erc20.ApproveEx(from, Self.Contract, amountIn, procedure(rcpt: ITxReceipt; err: IError)
begin
erc20.Free;
if Assigned(err) then
callback(nil, err)
else
web3.eth.write(Client, from, Contract,
'swapExactTokensForETH(uint256,uint256,address[],address,uint256)',
[
web3.utils.toHex(amountIn),
web3.utils.toHex(amountOutMin),
&array([token0, token1]),
&to,
deadline
], callback);
end);
end;
end;
procedure TRouter02.SwapExactETHForTokens(
from : TPrivateKey; // Sender of ETH.
amountIn : BigInteger; // The amount of ETH to send.
amountOutMin: BigInteger; // The minimum amount of output tokens that must be received for the transaction not to revert.
token : TAddress; // The token address.
&to : TAddress; // Recipient of the output tokens.
deadline : TUnixDateTime; // Unix timestamp after which the transaction will revert.
callback : TAsyncReceipt);
begin
Self.WETH(procedure(WETH: TAddress; err: IError)
begin
if Assigned(err) then
begin
callback(nil, err);
EXIT;
end;
web3.eth.write(Client, from, Contract, amountIn,
'swapExactETHForTokens(uint,address[],address, uint deadline)',
[
web3.utils.toHex(amountOutMin),
&array([WETH, token]),
&to,
deadline
], callback);
end);
end;
// Estimate the number of gas units needed for a Swap
procedure TRouter02.EstimateExactTokensForETH(
owner : TAddress; // Sender of the token, and recipient of the ETH.
amountIn : BigInteger; // The amount of input tokens to send.
amountOutMin: BigInteger; // The minimum amount of output tokens that must be received for the transaction not to revert.
token : TAddress; // The address of the token you wish to swap.
minutes : Int64; // Your transaction will revert if it is pending for more than this long.
callback : TAsyncQuantity);
begin
Self.WETH(procedure(WETH: TAddress; err: IError)
begin
if Assigned(err) then
callback(0, err)
else
Self.EstimateExactTokensForETH(
owner,
amountIn,
amountOutMin,
token,
WETH,
owner,
DateTimeToUnix(IncMinute(System.SysUtils.Now, minutes), False),
callback
);
end);
end;
// Swaps an exact amount of tokens for as much ETH as possible.
procedure TRouter02.SwapExactTokensForETH(
owner : TPrivateKey; // Sender of the token, and recipient of the ETH.
amountIn : BigInteger; // The amount of input tokens to send.
amountOutMin: BigInteger; // The minimum amount of output tokens that must be received for the transaction not to revert.
token : TAddress; // The address of the token you wish to swap.
minutes : Int64; // Your transaction will revert if it is pending for more than this long.
callback : TAsyncReceipt);
begin
Self.WETH(procedure(WETH: TAddress; err: IError)
begin
if Assigned(err) then
callback(nil, err)
else
owner.Address(procedure(addr: TAddress; err: IError)
begin
if Assigned(err) then
callback(nil, err)
else
Self.SwapExactTokensForETH(
owner,
amountIn,
amountOutMin,
token,
WETH,
addr,
DateTimeToUnix(IncMinute(System.SysUtils.Now, minutes), False),
callback
);
end);
end);
end;
procedure TRouter02.SwapExactETHForTokens(
owner : TPrivateKey; // Sender of ETH.
amountIn : BigInteger; // The amount of ETH to send.
amountOutMin: BigInteger; // The minimum amount of output tokens that must be received for the transaction not to revert.
token : TAddress; // The token address.
minutes : Int64; // Your transaction will revert if it is pending for more than this long.
callback : TAsyncReceipt);
begin
owner.Address(procedure(&to: TAddress; err: IError)
begin
if Assigned(err) then
begin
callback(nil, err);
EXIT;
end;
Self.SwapExactETHForTokens(
owner,
amountIn,
amountOutMin,
token,
&to,
DateTimeToUnix(IncMinute(System.SysUtils.Now, minutes), False),
callback
);
end);
end;
{ TPair }
// Returns the address of the pair token with the lower sort order.
procedure TPair.Token0(callback: TAsyncAddress);
begin
call(Client, Contract, 'token0()', [], procedure(const hex: string; err: IError)
begin
if Assigned(err) then
callback(ADDRESS_ZERO, err)
else
callback(TAddress.New(hex), nil);
end);
end;
// Returns the address of the pair token with the higher sort order.
procedure TPair.Token1(callback: TAsyncAddress);
begin
call(Client, Contract, 'token1()', [], procedure(const hex: string; err: IError)
begin
if Assigned(err) then
callback(ADDRESS_ZERO, err)
else
callback(TAddress.New(hex), nil);
end);
end;
// Returns a GraphQL query; see https://uniswap.org/docs/v2/API/entities/#pair
function TPair.Query(const field: string): string;
begin
Result := Format('{"query":"{pair(id:\"%s\"){%s}}"}', [string(Contract).ToLower, field]);
end;
// Execute a GraphQL query, return the result as a float (if any)
procedure TPair.Execute(const field: string; callback: TAsyncFloat);
var
data,
pair: TJsonObject;
begin
web3.graph.execute(UNISWAP_V2, Query(field), procedure(resp: TJsonObject; err: IError)
begin
if Assigned(err) then
begin
callback(0, err);
EXIT;
end;
data := web3.json.getPropAsObj(resp, 'data');
if Assigned(data) then
begin
pair := web3.json.getPropAsObj(data, 'pair');
if Assigned(pair) then
begin
callback(EthToFloat(web3.json.getPropAsStr(pair, field)), nil);
EXIT;
end;
end;
callback(0, TGraphError.Create('an unknown error occurred'));
end);
end;
// Token0 per Token1
procedure TPair.Token0Price(callback: TAsyncFloat);
begin
Execute('token0Price', callback);
end;
// Token1 per Token0
procedure TPair.Token1Price(callback: TAsyncFloat);
begin
Execute('token1Price', callback);
end;
end.