-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathweb3.eth.balancer.v2.pas
870 lines (798 loc) · 25.9 KB
/
web3.eth.balancer.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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
{******************************************************************************}
{ }
{ Delphereum }
{ }
{ Copyright(c) 2022 Stefan van As <[email protected]> }
{ 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.eth.balancer.v2;
{$I web3.inc}
interface
uses
// Delphi
System.SysUtils,
// Velthuis' BigNumbers
Velthuis.BigIntegers,
// web3
web3,
web3.eth.abi,
web3.eth.contract,
web3.eth.logs,
web3.eth.tokenlists,
web3.eth.types;
type
TSwapKind = (
GivenIn,
GivenOut
);
ISingleSwap = interface end;
TSingleSwap = class(TInterfacedObject, IContractStruct, ISingleSwap)
private
FPoolId : TBytes32;
FKind : TSwapKind;
FAssetIn : TAddress;
FAssetOut: TAddress;
FAmount : BigInteger;
public
function Tuple: TArray<Variant>;
function PoolId(const Value: TBytes32): TSingleSwap;
function Kind(const Value: TSwapKind): TSingleSwap;
function AssetIn(const Value: TAddress): TSingleSwap;
function AssetOut(const Value: TAddress): TSingleSwap;
function Amount(const Value: BigInteger): TSingleSwap;
end;
ISwapStep = interface end;
TSwapStep = class(TInterfacedObject, IContractStruct, ISwapStep)
private
FPoolId : TBytes32;
FAssetInIndex : Integer;
FAssetOutIndex: Integer;
FAmount : BigInteger;
public
function Tuple: TArray<Variant>;
function PoolId(const Value: TBytes32): TSwapStep;
function AssetInIndex(const Value: Integer): TSwapStep;
function AssetOutIndex(const Value: Integer): TSwapStep;
function Amount(const Value: BigInteger): TSwapStep;
end;
TOnSwap = reference to procedure(
BlockNo : BigInteger;
PoolId : TBytes32;
TokenIn : TAddress;
TokenOut : TAddress;
AmountIn : BigInteger;
AmountOut: BigInteger);
TVault = class(TCustomContract)
public
constructor Create(const aClient: IWeb3); reintroduce;
class function DeployedAt: TAddress;
procedure Swap(
const owner : TPrivateKey;
const swap : ISingleSwap;
const limit : BigInteger;
const deadline: BigInteger;
const callback: TProc<ITxReceipt, IError>);
procedure BatchSwap(
const owner : TPrivateKey;
const kind : TSwapKind;
const swaps : TArray<ISwapStep>;
const assets : TArray<TAddress>;
const limits : TArray<BigInteger>;
const deadline: BigInteger;
const callback: TProc<ITxReceipt, IError>);
procedure QueryBatchSwap(
const owner : TAddress;
const kind : TSwapKind;
const swaps : TArray<ISwapStep>;
const assets : TArray<TAddress>;
const callback: TProc<TArray<BigInteger>, IError>);
procedure WETH(const callback: TProc<TAddress, IError>);
end;
// get the Balancer token list
procedure tokens(const chain: TChain; const callback: TProc<TTokens, IError>);
// easy access function: simulate a trade between two tokens, returning Vault asset deltas.
procedure simulate(
const client : IWeb3;
const owner : TAddress;
const kind : TSwapKind;
const assetIn : TAddress;
const assetOut: TAddress;
const amount : BigInteger;
const callback: TProc<TArray<BigInteger>, IError>);
// easy access function: make a trade between two tokens.
procedure swap(
const client : IWeb3;
const owner : TPrivateKey; // owner of the tokens we are sending to the pool
const kind : TSwapKind; // the type of swap we want to perform - either (a) "Given In" or (b) "Given Out"
const assetIn : TAddress; // the address of the token which we are sending to the pool
const assetOut: TAddress; // the address of the token which we will receive in return
const amount : BigInteger; // the amount of tokens we (a) are sending to the pool, or (b) want to receive from the pool
const limit : BigInteger; // the "other amount" aka (a) minimum amount of tokens to receive, or (b) maximum amount of tokens to send
const deadline: BigInteger; // your transaction will revert if it is still pending after this Unix epoch
const callback: TProc<ITxReceipt, IError>);
// easy access function: listen for swaps between two tokens.
function listen(const client: IWeb3; const callback: TOnSwap): ILogger;
implementation
uses
// Delphi
System.Classes,
System.Generics.Collections,
System.JSON,
System.Types,
// web3
web3.eth,
web3.eth.erc20,
web3.graph,
web3.json,
web3.utils;
{ TSingleSwap }
function TSingleSwap.Tuple: TArray<Variant>;
begin
Result := [
web3.utils.toHex(bytes32ToByteArray(Self.FPoolId)), // bytes32
Self.FKind, // uint8
Self.FAssetIn, // address
Self.FAssetOut, // address
web3.utils.toHex(Self.FAmount), // uint256
'0b0' // bytes
];
end;
function TSingleSwap.PoolId(const Value: TBytes32): TSingleSwap;
begin
Self.FPoolId := Value;
Result := Self;
end;
function TSingleSwap.Kind(const Value: TSwapKind): TSingleSwap;
begin
Self.FKind := Value;
Result := Self;
end;
function TSingleSwap.AssetIn(const Value: TAddress): TSingleSwap;
begin
Self.FAssetIn := Value;
Result := Self;
end;
function TSingleSwap.AssetOut(const Value: TAddress): TSingleSwap;
begin
Self.FAssetOut := Value;
Result := Self;
end;
function TSingleSwap.Amount(const Value: BigInteger): TSingleSwap;
begin
Self.FAmount := Value;
Result := Self;
end;
{ TSwapStep }
function TSwapStep.Tuple: TArray<Variant>;
begin
Result := [
web3.utils.toHex(bytes32ToByteArray(Self.FPoolId)), // bytes32
Self.FAssetInIndex, // uint256
Self.FAssetOutIndex, // uint256
web3.utils.toHex(Self.FAmount), // uint256
'0b0' // bytes
];
end;
function TSwapStep.PoolId(const Value: TBytes32): TSwapStep;
begin
Self.FPoolId := Value;
Result := Self;
end;
function TSwapStep.AssetInIndex(const Value: Integer): TSwapStep;
begin
Self.FAssetInIndex := Value;
Result := Self;
end;
function TSwapStep.AssetOutIndex(const Value: Integer): TSwapStep;
begin
Self.FAssetOutIndex := Value;
Result := Self;
end;
function TSwapStep.Amount(const Value: BigInteger): TSwapStep;
begin
Self.FAmount := Value;
Result := Self;
end;
{ TFundManagement }
type
TFundManagement = class(TInterfacedObject, IContractStruct)
private
FSender: TAddress;
FFromInternalBalance: Boolean;
FRecipient: TAddress;
FToInternalBalance: Boolean;
public
function Tuple: TArray<Variant>;
property Sender : TAddress write FSender;
property FromInternalBalance: Boolean write FFromInternalBalance;
property Recipient : TAddress write FRecipient;
property ToInternalBalace : Boolean write FToInternalBalance;
end;
function TFundManagement.Tuple: TArray<Variant>;
begin
Result := [
Self.FSender, // address
Self.FFromInternalBalance, // bool
Self.FRecipient, // address
Self.FToInternalBalance // bool
];
end;
{ TVault }
constructor TVault.Create(const aClient: IWeb3);
begin
inherited Create(aClient, Self.DeployedAt);
end;
class function TVault.DeployedAt: TAddress;
begin
Result := '0xBA12222222228d8Ba445958a75a0704d566BF2C8';
end;
procedure TVault.Swap(
const owner : TPrivateKey;
const swap : ISingleSwap;
const limit : BigInteger;
const deadline: BigInteger;
const callback: TProc<ITxReceipt, IError>);
begin
owner.GetAddress
.ifErr(procedure(err: IError)
begin
callback(nil, err)
end)
.&else(procedure(address: TAddress)
begin
const funds: IContractStruct = TFundManagement.Create;
with funds as TFundManagement do
begin
Sender := address.ToChecksum;
Recipient := address.ToChecksum;
end;
web3.eth.write(Client, owner, Contract,
'swap(' +
'(bytes32,uint8,address,address,uint256,bytes),' + // SingleSwap
'(address,bool,address,bool),' + // FundManagement
'uint256,' + // limit
'uint256' + // deadline
')',
[swap, funds, web3.utils.toHex(limit), web3.utils.toHex(deadline)],
callback
);
end);
end;
procedure TVault.BatchSwap(
const owner : TPrivateKey;
const kind : TSwapKind;
const swaps : TArray<ISwapStep>;
const assets : TArray<TAddress>;
const limits : TArray<BigInteger>;
const deadline: BigInteger;
const callback: TProc<ITxReceipt, IError>);
begin
owner.GetAddress
.ifErr(procedure(err: IError)
begin
callback(nil, err)
end)
.&else(procedure(address: TAddress)
begin
const funds: IContractStruct = TFundManagement.Create;
with funds as TFundManagement do
begin
Sender := address.ToChecksum;
Recipient := address.ToChecksum;
end;
web3.eth.write(Client, owner, Contract,
'batchSwap(' +
'uint8,' + // kind
'(bytes32,uint256,uint256,uint256,bytes)[],' + // SwapSteps
'address[],' + // assets
'(address,bool,address,bool),' + // FundManagement
'int256[],' + // limits
'uint256' + // deadline
')',
[
Ord(kind),
(
function: TContractArray
begin
Result := TContractArray.Create;
for var swap in swaps do Result.Add(swap);
end
)(),
&array(assets),
funds,
&array(limits),
web3.utils.toHex(deadline)
],
callback
);
end);
end;
procedure TVault.QueryBatchSwap(
const owner : TAddress;
const kind : TSwapKind;
const swaps : TArray<ISwapStep>;
const assets : TArray<TAddress>;
const callback: TProc<TArray<BigInteger>, IError>);
begin
const funds: IContractStruct = TFundManagement.Create;
with funds as TFundManagement do
begin
Sender := owner.ToChecksum;
Recipient := owner.ToChecksum;
end;
web3.eth.call(Client, owner, Contract,
'queryBatchSwap(' +
'uint8,' + // kind
'(bytes32,uint256,uint256,uint256,bytes)[],' + // SwapSteps
'address[],' + // assets
'(address,bool,address,bool)' + // FundManagement
')',
[
Ord(kind),
(
function: TContractArray
begin
Result := TContractArray.Create;
for var swap in swaps do Result.Add(swap);
end
)(),
&array(assets),
funds
],
procedure(tup: TTuple; err: IError)
begin
callback(
(
function: TArray<BigInteger>
begin
Result := [];
if Assigned(tup) then
for var arg in tup.ToArray do Result := Result + [arg.toInt256];
end
)(),
err
);
end
);
end;
procedure TVault.WETH(const callback: TProc<TAddress, IError>);
begin
call(Client, Contract, 'WETH()', [], procedure(hex: string; err: IError)
begin
if Assigned(err) then
callback(TAddress.Zero, err)
else
callback(TAddress.Create(hex), nil);
end);
end;
{----------- get the pool id for a single swap between two tokens -------------}
type
IPoolDoesNotExist = interface(IError)
['{98E7E985-B74E-4D20-84A4-E8A2F8060D56}']
end;
type
TPoolDoesNotExist = class(TError, IPoolDoesNotExist)
public
constructor Create;
end;
constructor TPoolDoesNotExist.Create;
begin
inherited Create('Pool does not exist');
end;
procedure getPoolId(const chain: TChain; const asset0, asset1: TAddress; const callback: TProc<string, IError>);
const
QUERY = '{"query":"{pools(where: {tokensList: [\"%s\", \"%s\"]}, orderBy: totalLiquidity, orderDirection: desc) { id }}"}';
begin
const execute = procedure(token0, token1: TAddress; callback: TProc<string, IError>)
begin
const SUBGRAPH = (function(chain: TChain): IResult<string>
begin
if chain = Ethereum then
Result := TResult<string>.Ok('https://api.studio.thegraph.com/query/75376/balancer-v2/version/latest')
else if chain = Sepolia then
Result := TResult<string>.Ok('https://api.studio.thegraph.com/query/24660/balancer-sepolia-v2/version/latest')
else if chain = Polygon then
Result := TResult<string>.Ok('https://api.studio.thegraph.com/query/75376/balancer-polygon-v2/version/latest')
else if chain = Arbitrum then
Result := TResult<string>.Ok('https://api.studio.thegraph.com/query/75376/balancer-arbitrum-v2/version/latest')
else if chain = Optimism then
Result := TResult<string>.Ok('https://api.studio.thegraph.com/query/75376/balancer-optimism-v2/version/latest')
else if chain = Gnosis then
Result := TResult<string>.Ok('https://api.studio.thegraph.com/query/75376/balancer-gnosis-chain-v2/version/latest')
else if chain = Base then
Result := TResult<string>.Ok('https://api.studio.thegraph.com/query/24660/balancer-base-v2/version/latest')
else
Result := TResult<string>.Err(TError.Create('%s not supported', [chain.Name]));
end)(chain);
if SUBGRAPH.isErr then
begin
callback('', SUBGRAPH.Error);
EXIT;
end;
web3.graph.execute(SUBGRAPH.Value, Format(QUERY, [string(token0), string(token1)]), procedure(response: TJsonObject; err: IError)
begin
if Assigned(err) then
begin
callback('', err);
EXIT;
end;
const data = web3.json.getPropAsObj(response, 'data');
if Assigned(data) then
begin
const pools = web3.json.getPropAsArr(data, 'pools');
if Assigned(pools) and (pools.Count > 0) then
begin
callback(web3.json.getPropAsStr(pools[0], 'id'), nil);
EXIT;
end;
end;
callback('', TPoolDoesNotExist.Create);
end);
end;
execute(asset0, asset1, procedure(id: string; err: IError)
begin
if Assigned(err) and Supports(err, IPoolDoesNotExist) then
execute(asset1, asset0, callback)
else
callback(id, err);
end);
end;
{------------------------ get the Balancer token list -------------------------}
procedure tokens(const chain: TChain; const callback: TProc<TTokens, IError>);
begin
web3.eth.tokenlists.tokens('https://raw.githubusercontent.com/balancer/tokenlists/main/generated/balancer.tokenlist.json', procedure(tokens: TTokens; err: IError)
begin
if Assigned(err) or not Assigned(tokens) then
begin
callback(nil, err);
EXIT;
end;
var I := 0;
while I < tokens.Length do
if tokens[I].ChainId <> chain.Id then
Delete(tokens, I, 1)
else
Inc(I);
callback(tokens, nil);
end);
end;
{---------- easy access function: returns the Vault's WETH instance -----------}
procedure weth(const client: IWeb3; const callback: TProc<TAddress, IError>);
begin
const vault = TVault.Create(client);
try
vault.WETH(callback);
finally
vault.Free;
end;
end;
{-------------- router function: computes the steps for a trade ---------------}
type
IPool = interface
function Id : TBytes32;
function AssetIn : TAddress;
function AssetOut: TAddress;
end;
TPool = class(TInterfacedObject, IPool)
strict private
FId : TBytes32;
FAssetIn : TAddress;
FAssetOut: TAddress;
public
constructor Create(const aId: TBytes32; const aAssetIn, aAssetOut: TAddress);
function Id : TBytes32;
function AssetIn : TAddress;
function AssetOut: TAddress;
end;
constructor TPool.Create(const aId: TBytes32; const aAssetIn, aAssetOut: TAddress);
begin
inherited Create;
Self.FId := aId;
Self.FAssetIn := aAssetIn;
Self.FAssetOut := aAssetOut;
end;
function TPool.Id: TBytes32;
begin
Result := FId;
end;
function TPool.AssetIn: TAddress;
begin
Result := FAssetIn;
end;
function TPool.AssetOut: TAddress;
begin
Result := FAssetOut;
end;
type
IPools = interface
function First: IPool;
function Length: Integer;
function ToAssets: TArray<TAddress>;
function ToLimits(const kind: TSwapKind; const limit: BigInteger): TArray<BigInteger>;
function ToSwapSteps(const kind: TSwapKind; const amount: BigInteger): TArray<ISwapStep>;
end;
TPools = class(TInterfacedObject, IPools)
strict private
Inner: TArray<IPool>;
public
constructor Create(const pools: TArray<IPool>);
function First: IPool;
function Length: Integer;
function ToAssets: TArray<TAddress>;
function ToLimits(const kind: TSwapKind; const limit: BigInteger): TArray<BigInteger>;
function ToSwapSteps(const kind: TSwapKind; const amount: BigInteger): TArray<ISwapStep>;
end;
constructor TPools.Create(const pools: TArray<IPool>);
begin
inherited Create;
Self.Inner := pools;
end;
function TPools.First: IPool;
begin
if Self.Length > 0 then
Result := Self.Inner[0]
else
Result := nil;
end;
function TPools.Length: Integer;
begin
Result := System.Length(Self.Inner);
end;
function TPools.ToAssets: TArray<TAddress>;
begin
Result := [];
if Self.Length = 0 then
EXIT;
for var I := 0 to Pred(Self.Length) do
Result := Result + [Self.Inner[I].AssetIn];
Result := Result + [Self.Inner[High(Self.Inner)].AssetOut];
end;
// returns the minimum or maximum amount of each token the vault is allowed to transfer
function TPools.ToLimits(const kind: TSwapKind; const limit: BigInteger): TArray<BigInteger>;
begin
Result := [];
if Self.Length = 0 then
EXIT;
SetLength(Result, Self.Length + 1);
if Kind = GivenOut then
begin
// maximum number of tokens to send
Result[0] := (function: BigInteger
begin
if limit.IsZero then
Result := web3.MaxInt256
else if limit.IsNegative then
Result := BigInteger.Abs(limit)
else
Result := limit;
end)();
// minimum amount of tokens to receive
Result[High(Result)] := 0;
end
else
begin
// maximum number of tokens to send
Result[0] := web3.MaxInt256;
// minimum amount of tokens to receive
Result[High(Result)] := (function: BigInteger
begin
if limit.IsZero or limit.IsNegative then
Result := limit
else
Result := BigInteger.Negate(limit);
end)();
end;
end;
function TPools.ToSwapSteps(const kind: TSwapKind; const amount: BigInteger): TArray<ISwapStep>;
begin
Result := [];
if Self.Length = 0 then
EXIT;
var I := 0;
if kind = GivenOut then
I := High(Self.Inner);
while ((kind = GivenOut) and (I > -1)) or ((kind = GivenIn) and (I < Self.Length)) do
begin
Result := Result + [
TSwapStep.Create
.PoolId(Self.Inner[I].Id)
.AssetInIndex(I)
.AssetOutIndex(I + 1)
.Amount((function: BigInteger
begin
if ((kind = GivenIn) and (I = 0)) or ((kind = GivenOut) and (I = High(Self.Inner))) then
Result := amount
else
Result := 0;
end)())
];
if kind = GivenOut then
Dec(I)
else
Inc(I);
end;
end;
procedure getPools(
const client : IWeb3;
const assetIn : TAddress;
const assetOut: TAddress;
const callback: TProc<IPools, IError>);
begin
// step #1: get the pool id for a single swap
getPoolId(client.Chain, assetIn, assetOut, procedure(poolId: string; err: IError)
begin
if not Assigned(err) then
callback(TPools.Create([
TPool.Create(byteArrayToBytes32(web3.utils.fromHex(poolId)), assetIn, assetOut)
]), nil)
else
// step #2: get the Vault's WETH instance
weth(client, procedure(weth: TAddress; err: IError)
begin
if Assigned(err) then
callback(nil, err)
else
if assetIn.SameAs(weth) or assetOut.SameAs(weth) then
callback(nil, TPoolDoesNotExist.Create)
else
// step #3: get the pool IDs for a batch swap
getPoolId(client.Chain, assetIn, weth, procedure(pool1: string; err: IError)
begin
if Assigned(err) then
callback(nil, err)
else
getPoolId(client.Chain, weth, assetOut, procedure(pool2: string; err: IError)
begin
if Assigned(err) then
callback(nil, err)
else
callback(TPools.Create([
TPool.Create(byteArrayToBytes32(web3.utils.fromHex(pool1)), assetIn, weth),
TPool.Create(byteArrayToBytes32(web3.utils.fromHex(pool2)), weth, assetOut)
]), nil);
end);
end);
end);
end);
end;
{--------- easy access function: simulate a trade between two tokens ----------}
procedure simulate(
const client : IWeb3;
const owner : TAddress;
const kind : TSwapKind;
const assetIn : TAddress;
const assetOut: TAddress;
const amount : BigInteger;
const callback: TProc<TArray<BigInteger>, IError>);
begin
// step #1: get the pool IDs for a trade
getPools(client, assetIn, assetOut, procedure(pools: IPools; err: IError)
begin
if Assigned(err) then
begin
callback(nil, err);
EXIT;
end;
// step #2: simulate a call to `batchSwap`
const vault = TVault.Create(client);
try
vault.QueryBatchSwap(
owner,
kind,
pools.ToSwapSteps(kind, amount),
pools.ToAssets,
callback
);
finally
vault.Free;
end;
end);
end;
{----------- easy access function: make a trade between two tokens ------------}
procedure swap(
const client : IWeb3;
const owner : TPrivateKey;
const kind : TSwapKind;
const assetIn : TAddress;
const assetOut: TAddress;
const amount : BigInteger;
const limit : BigInteger;
const deadline: BigInteger;
const callback: TProc<ITxReceipt, IError>);
begin
// step #1: get the pool IDs for a trade
getPools(client, assetIn, assetOut, procedure(pools: IPools; err: IError)
begin
if Assigned(err) then
begin
callback(nil, err);
EXIT;
end;
// step #2: grant token spend allowance to the vault
web3.eth.erc20.approve(web3.eth.erc20.Create(client, assetIn), owner, TVault.DeployedAt, web3.Infinite, procedure(rcpt: ITxReceipt; err: IError)
begin
if Assigned(err) then
begin
callback(nil, err);
EXIT;
end;
// step #3: execute a swap
const vault = TVault.Create(client);
try
if pools.Length > 1 then
// execute a batch swap
vault.BatchSwap(
owner,
kind,
pools.ToSwapSteps(kind, amount),
pools.ToAssets,
pools.ToLimits(kind, limit),
deadline,
callback
)
else
// execute a single swap, saving ~6,000 gas
vault.Swap(
owner,
// initialize which pool we're trading with and what kind of swap we want to perform
TSingleSwap.Create
.PoolId(pools.First.Id)
.Kind(kind)
.AssetIn(assetIn)
.AssetOut(assetOut)
.Amount(amount),
(
function: BigInteger
begin
if limit.IsZero then
begin
if kind = GivenOut then
Result := web3.Infinite
else
Result := limit;
end
else
Result := BigInteger.Abs(limit);
end
)(),
deadline,
callback
);
finally
vault.Free;
end;
end);
end);
end;
{---------- easy access function: listen for swaps between two tokens ---------}
function listen(const client: IWeb3; const callback: TOnSwap): ILogger;
begin
Result := web3.eth.logs.get(client, TVault.DeployedAt, procedure(log: PLog; err: IError)
begin
if Assigned(log) and Assigned(callback) then
if log^.isEvent('Swap(bytes32,address,address,uint256,uint256)') then
callback(log^.BlockNumber, // blockNo
log^.Topic[1].toBytes32, // poolId
log^.Topic[2].toAddress, // tokenIn
log^.Topic[3].toAddress, // tokenOut
log^.Data[0].toUInt256, // amountIn
log^.Data[1].toUInt256); // amountOut
end);
end;
end.