forked from Militereum/Militereum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasset.pas
251 lines (224 loc) · 6.81 KB
/
asset.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
unit asset;
interface
uses
// Delphi
System.Classes,
System.SysUtils,
// FireMonkey
FMX.Controls,
FMX.Controls.Presentation,
FMX.Objects,
FMX.StdCtrls,
FMX.Types,
// Velthuis' BigNumbers
Velthuis.BigIntegers,
// web3
web3,
web3.eth.simulate,
web3.eth.tokenlists,
// project
base,
transaction;
type // MUST have the same order as the steps in checks.getSpenderStatus()
TSpenderStatus = (isEOA, isUnverified, isPhisher, isSanctioned, isGood);
type
TFrmAsset = class(TFrmBase)
imgLogo: TImage;
lblTitle: TLabel;
lblTokenTitle: TLabel;
lblSpenderTitle: TLabel;
lblSpenderText: TLabel;
lblTokenText: TLabel;
lblAmountTitle: TLabel;
lblAmountText: TLabel;
procedure lblTokenTextClick(Sender: TObject);
procedure lblSpenderTextClick(Sender: TObject);
strict private
FToken: TAddress;
procedure SetToken(value: IToken);
procedure SetChange(value: IAssetChange);
procedure SetLogo(value: TURL);
procedure SetSpender(value: TAddress);
procedure SetStatus(const value: TSpenderStatus);
public
procedure Amount(const symbol: string; const quantity: BigInteger; const decimals: Integer);
property Token: IToken write SetToken;
property Change: IAssetChange write SetChange;
property Logo: TURL write SetLogo;
property Spender: TAddress write SetSpender;
property Status: TSpenderStatus write SetStatus;
end;
procedure approve(
const chain : TChain;
const tx : transaction.ITransaction;
const token : IToken;
const spender : TAddress;
const status : TSpenderStatus;
const quantity: BigInteger;
const callback: TProc<Boolean>; const log: TLog); overload;
procedure transfer(
const chain : TChain;
const tx : transaction.ITransaction;
const change : IAssetChange;
const callback: TProc<Boolean>; const log: TLog);
procedure approve(
const chain : TChain;
const tx : transaction.ITransaction;
const change : IAssetChange;
const status : TSpenderStatus;
const callback: TProc<Boolean>; const log: TLog); overload;
implementation
uses
// Delphi
System.Math,
System.Net.HttpClient,
// web3
web3.defillama,
web3.http,
// project
cache,
common,
thread;
{$R *.fmx}
procedure approve(
const chain : TChain;
const tx : transaction.ITransaction;
const token : IToken;
const spender : TAddress;
const status : TSpenderStatus;
const quantity: BigInteger;
const callback: TProc<Boolean>; const log: TLog);
begin
const frmAsset = TFrmAsset.Create(chain, tx, callback, log);
frmAsset.Token := token;
frmAsset.Spender := spender;
frmAsset.Status := status;
frmAsset.Amount(token.Symbol, quantity, token.Decimals);
frmAsset.Show;
end;
procedure transfer(
const chain : TChain;
const tx : transaction.ITransaction;
const change : IAssetChange;
const callback: TProc<Boolean>; const log: TLog);
begin
const frmAsset = TFrmAsset.Create(chain, tx, callback, log);
frmAsset.Change := change;
frmAsset.lblTitle.Text := 'The following token is about to leave your wallet';
frmAsset.lblSpenderTitle.Text := 'Recipient';
frmAsset.Show;
end;
procedure approve(
const chain : TChain;
const tx : transaction.ITransaction;
const change : IAssetChange;
const status : TSpenderStatus;
const callback: TProc<Boolean>; const log: TLog);
begin
const frmAsset = TFrmAsset.Create(chain, tx, callback, log);
frmAsset.Change := change;
frmAsset.Status := status;
frmAsset.Show;
end;
{ TFrmAsset }
procedure TFrmAsset.SetToken(value: IToken);
begin
FToken := value.Address;
lblTokenText.Text := (function(const value: IToken): string
begin
if value.Name <> '' then
Result := value.Name
else
Result := string(value.Address);
end)(value);
Self.Logo := value.Logo;
end;
procedure TFrmAsset.SetChange(value: IAssetChange);
begin
FToken := value.Contract;
lblTokenText.Text := (function(const value: IAssetChange): string
begin
if value.Name.Value <> '' then
Result := value.Name.Value
else
Result := string(value.Contract);
end)(value);
Self.Logo := value.Logo.Value;
Self.Spender := value.&To;
Self.Amount(value.Symbol.Value, value.Amount, value.Decimals.Value);
end;
procedure TFrmAsset.SetLogo(value: TURL);
begin
if value <> '' then
web3.http.get(value, [], procedure(img: IHttpResponse; err: IError)
begin
if Assigned(err) then Self.Log(err) else if Assigned(img) then thread.synchronize(procedure
begin
try
imgLogo.Bitmap.LoadFromStream(img.ContentStream);
except end;
end);
end);
end;
procedure TFrmAsset.SetSpender(value: TAddress);
begin
lblSpenderText.Text := string(value);
if not common.Demo then
cache.getFriendlyName(Self.Chain, value, procedure(friendly: string; err: IError)
begin
if Assigned(err) then Self.Log(err) else thread.synchronize(procedure
begin
lblSpenderText.Text := friendly;
end);
end);
end;
procedure TFrmAsset.SetStatus(const value: TSpenderStatus);
const
RS_AN_EOA = 'an EOA';
RS_AN_UNVERIFIED_CONTRACT = 'an unverified contract';
RS_A_KNOWN_PHISHER = 'a known phisher';
RS_A_SANCTIONED_ADDRESS = 'a sanctioned address';
RS_SOMEONE_ELSE = 'someone else';
const
SpenderTitle: array[TSpenderStatus] of string = (
RS_AN_EOA, // isEOA
RS_AN_UNVERIFIED_CONTRACT, // isUnverified
RS_A_KNOWN_PHISHER, // isPhisher
RS_A_SANCTIONED_ADDRESS, // isSanctioned
RS_SOMEONE_ELSE // isGood
);
begin
lblTitle.Text := System.SysUtils.Format(lblTitle.Text, [SpenderTitle[value]]);
Self.Blocked := value <> TSpenderStatus.isGood;
end;
procedure TFrmAsset.Amount(const symbol: string; const quantity: BigInteger; const decimals: Integer);
begin
if quantity = web3.Infinite then
lblAmountText.Text := 'Unlimited'
else
web3.defillama.price(Self.Chain, FToken, procedure(price: Double; err: IError)
begin
thread.synchronize(procedure
begin
if Assigned(err) or (price = 0) then
lblAmountText.Text := System.SysUtils.Format('%s %s', [symbol, common.Format(quantity.AsDouble / Round(Power(10, decimals)))])
else
lblAmountText.Text := System.SysUtils.Format('$ %.2f', [(quantity.AsDouble / Round(Power(10, decimals))) * price]);
end);
end);
end;
procedure TFrmAsset.lblTokenTextClick(Sender: TObject);
begin
common.Open(Self.Chain.Explorer + '/token/' + string(FToken));
end;
procedure TFrmAsset.lblSpenderTextClick(Sender: TObject);
begin
cache.fromName(lblSpenderText.Text, procedure(address: TAddress; err: IError)
begin
if not Assigned(err) then
common.Open(Self.Chain.Explorer + '/address/' + string(address))
else
common.Open(Self.Chain.Explorer + '/address/' + lblSpenderText.Text);
end);
end;
end.