-
-
Notifications
You must be signed in to change notification settings - Fork 50
/
web3.eth.simulate.pas
212 lines (189 loc) · 5.99 KB
/
web3.eth.simulate.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
unit web3.eth.simulate;
interface
uses
// Delphi
System.JSON,
System.SysUtils,
// Velthuis' BigNumbers
Velthuis.BigIntegers,
// web3
web3,
web3.json;
type
TChangeType = (Approve, Mint, Transfer);
IAssetChange = interface
function Asset : TAssetType;
function Change : TChangeType;
function From : TAddress;
function &To : TAddress;
function Amount : BigInteger;
function Contract: TAddress;
function Name : IResult<string>;
function Symbol : IResult<string>;
function Decimals: IResult<Integer>;
function Logo : IResult<TURL>;
end;
IAssetChanges = interface(IDeserializedArray<IAssetChange>)
function IndexOf(const contract: TAddress): Integer;
function Incoming(const address: TAddress): IAssetChanges;
function Outgoing(const address: TAddress): IAssetChanges;
end;
TCustomAssetChanges = class abstract(TDeserializedArray<IAssetChange>, IAssetChanges)
strict protected
class function CreateAssetChange(const aJsonObject: TJsonValue): IAssetChange; virtual; abstract;
class function CreateAssetChanges(const aJsonArray: TJsonArray): IAssetChanges; virtual; abstract;
public
function Item(const Index: Integer): IAssetChange; override;
function IndexOf(const contract: TAddress): Integer;
function Incoming(const address: TAddress): IAssetChanges;
function Outgoing(const address: TAddress): IAssetChanges;
end;
IRawTransaction = interface
function Marshal: string;
end;
TCustomRawTransaction = class abstract(TInterfacedObject, IRawTransaction)
strict protected
FChain: TChain;
FFrom : TAddress;
FTo : TAddress;
FValue: TWei;
FData : string;
public
function Marshal: string; virtual; abstract;
constructor Create(const chain: TChain; const from, &to: TAddress; const value: TWei; const data: string);
end;
// simulate transaction, return array of asset changes
procedure simulate(
const alchemyApiKey,
tenderlyAccountId,
tenderlyProjectId,
tenderlyaccessKey: string;
const chain : TChain;
const from, &to: TAddress;
const value : TWei;
const data : string;
const callback : TProc<IAssetChanges, IError>);
// simulate transaction, return incoming assets that are honeypots (eg. you cannot sell)
procedure honeypots(
const alchemyApiKey,
tenderlyaccountId,
tenderlyprojectId,
tenderlyaccessKey: string;
const chain : TChain;
const from, &to: TAddress;
const value : TWei;
const data : string;
const callback : TProc<IAssetChanges, IError>);
implementation
uses
// Delphi
System.Generics.Collections,
// web3
web3.eth.alchemy.api,
web3.eth.tenderly,
web3.eth.types;
{---------------------------- TCustomAssetChanges -----------------------------}
function TCustomAssetChanges.Item(const Index: Integer): IAssetChange;
begin
Result := Self.CreateAssetChange(TJsonArray(FJsonValue)[Index]);
end;
function TCustomAssetChanges.IndexOf(const contract: TAddress): Integer;
begin
const count = Self.Count;
if count > 0 then
for Result := 0 to Pred(count) do
if Self.Item(Result).Contract.SameAs(contract) then
EXIT;
Result := -1;
end;
function TCustomAssetChanges.Incoming(const address: TAddress): IAssetChanges;
begin
Result := nil;
if not Assigned(Self.FJsonValue) then
EXIT;
var value := Self.FJsonValue.Clone as TJsonArray;
try
var index := 0;
while index < value.Count do
begin
const change: IAssetChange = Self.CreateAssetChange(value[index]);
if (change.Change in [Mint, Transfer]) and change.&To.SameAs(address) then
Inc(index)
else
value.Remove(index);
end;
Result := Self.CreateAssetChanges(value);
finally
value.Free;
end;
end;
function TCustomAssetChanges.Outgoing(const address: TAddress): IAssetChanges;
begin
Result := nil;
if not Assigned(Self.FJsonValue) then
EXIT;
var value := Self.FJsonValue.Clone as TJsonArray;
try
var index := 0;
while index < value.Count do
begin
const change: IAssetChange = Self.CreateAssetChange(value[index]);
if (change.Change in [Mint, Transfer]) and change.From.SameAs(address) then
Inc(index)
else
value.Remove(index);
end;
Result := Self.CreateAssetChanges(value);
finally
value.Free;
end;
end;
{--------------------------- TCustomRawTransaction ----------------------------}
constructor TCustomRawTransaction.Create(const chain: TChain; const from, &to: TAddress; const value: BigInteger; const data: string);
begin
Self.FChain := chain;
Self.FFrom := from;
Self.FTo := &to;
Self.FValue := value;
Self.FData := data;
end;
{---------------------------------- globals -----------------------------------}
procedure simulate(
const alchemyApiKey,
tenderlyAccountId,
tenderlyProjectId,
tenderlyaccessKey: string;
const chain : TChain;
const from, &to: TAddress;
const value : TWei;
const data : string;
const callback : TProc<IAssetChanges, IError>);
begin
web3.eth.alchemy.api.simulate(alchemyApiKey, chain, from, &to, value, data, procedure(changes: IAssetChanges; err: IError)
begin
if not Assigned(err) then
callback(changes, err)
else
web3.eth.tenderly.simulate(tenderlyAccountId, tenderlyProjectId, tenderlyAccessKey, chain, from, &to, value, data, callback);
end);
end;
procedure honeypots(
const alchemyApiKey,
tenderlyaccountId,
tenderlyprojectId,
tenderlyaccessKey: string;
const chain : TChain;
const from, &to: TAddress;
const value : TWei;
const data : string;
const callback : TProc<IAssetChanges, IError>);
begin
web3.eth.alchemy.api.honeypots(alchemyApiKey, chain, from, &to, value, data, procedure(honeypots: IAssetChanges; err: IError)
begin
if not Assigned(err) then
callback(honeypots, err)
else
web3.eth.tenderly.honeypots(tenderlyAccountId, tenderlyProjectId, tenderlyAccessKey, chain, from, &to, value, data, callback);
end)
end;
end.