-
Notifications
You must be signed in to change notification settings - Fork 5
/
asset.pas
192 lines (169 loc) · 5.3 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
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
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 SetKind(value: TChangeType);
procedure SetToken(value: IToken);
procedure SetChange(value: IAssetChange);
procedure SetLogo(value: TURL);
procedure SetSpender(value: TAddress);
public
procedure Amount(const symbol: string; const quantity: BigInteger; const decimals: Integer);
property Kind: TChangeType write SetKind;
property Token: IToken write SetToken;
property Change: IAssetChange write SetChange;
property Logo: TURL write SetLogo;
property Spender: TAddress write SetSpender;
end;
procedure approve(const chain: TChain; const tx: transaction.ITransaction; const token: IToken; const spender: TAddress; const quantity: BigInteger; const callback: TProc<Boolean>; const log: TLog);
procedure show(const chain: TChain; const tx: transaction.ITransaction; const change: IAssetChange; const callback: TProc<Boolean>; const log: TLog);
implementation
uses
// Delphi
System.Math,
System.Net.HttpClient,
// web3
web3.defillama,
web3.eth.types,
web3.http,
// project
common,
thread;
{$R *.fmx}
procedure approve(const chain: TChain; const tx: transaction.ITransaction; const token: IToken; const spender: TAddress; 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.Amount(token.Symbol, quantity, token.Decimals);
frmAsset.Show;
end;
procedure show(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.Show;
end;
{ TFrmAsset }
procedure TFrmAsset.SetKind(value: TChangeType);
begin
if value = Transfer then
begin
lblTitle.Text := 'The following token is about to leave your wallet';
lblSpenderTitle.Text := 'Recipient';
end;
end;
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.Kind := value.Change;
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
value.ToString(TWeb3.Create(common.Ethereum), procedure(ens: string; err: IError)
begin
if Assigned(err) then Self.Log(err) else thread.synchronize(procedure
begin
lblSpenderText.Text := ens;
end);
end);
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
TAddress.FromName(TWeb3.Create(common.Ethereum), 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.