-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathweb3.eth.utils.pas
185 lines (170 loc) · 5.4 KB
/
web3.eth.utils.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
{******************************************************************************}
{ }
{ Delphereum }
{ }
{ Copyright(c) 2018 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.utils;
{$I web3.inc}
interface
uses
// Delphi
System.SysUtils,
// Velthuis' BigNumbers
Velthuis.BigIntegers,
// web3
web3,
web3.eth.types;
type
TDenomination = (
noether,
wei,
kwei,
babbage,
femtoether,
mwei,
lovelace,
picoether,
gwei,
shannon,
nanoether,
nano,
szabo,
microether,
micro,
finney,
milliether,
milli,
ether,
kether,
grand,
mether,
gether,
tether
);
function fromWei(wei: TWei; const &to: TDenomination; const decimals: Byte = 18): string;
function toWei(value: string; const from: TDenomination): IResult<TWei>;
function dotToFloat(const value: string): Double;
function floatToDot(const value: Double): string;
implementation
const
ToBase: array[TDenomination] of string = (
'0',
'1',
'1000',
'1000',
'1000',
'1000000',
'1000000',
'1000000',
'1000000000',
'1000000000',
'1000000000',
'1000000000',
'1000000000000',
'1000000000000',
'1000000000000',
'1000000000000000',
'1000000000000000',
'1000000000000000',
'1000000000000000000',
'1000000000000000000000',
'1000000000000000000000',
'1000000000000000000000000',
'1000000000000000000000000000',
'1000000000000000000000000000000');
function fromWei(wei: TWei; const &to: TDenomination; const decimals: Byte): string;
begin
Result := '';
const negative = wei.Negative;
const base = ToBase[&to];
const baseLen = ToBase[&to].Length;
if negative then
wei := wei.Abs;
var whole: BigInteger;
var fract: BigInteger;
BigInteger.DivMod(wei, base, whole, fract);
if not fract.IsZero then
begin
Result := fract.ToString;
while Result.Length < baseLen - 1 do
Result := '0' + Result;
while (Result.Length > 1) and (Result[High(Result)] = '0') do
Delete(Result, High(Result), 1);
while Result.Length > decimals do
Delete(Result, High(Result), 1);
if Length(Result) > 0 then
Result := '.' + Result;
end;
Result := whole.ToString + Result;
if negative then
Result := '-' + Result;
end;
function toWei(value: string; const from: TDenomination): IResult<TWei>;
begin
const base = ToBase[from];
const baseLen = ToBase[from].Length;
// is it negative?
const negative = (value.Length > 0) and (value[System.Low(value)] = '-');
if negative then
Delete(value, System.Low(value), 1);
if (value = '') or (value = '.') then
begin
Result := TResult<TWei>.Err(TError.Create('Error while converting %s to wei. Invalid value.', [value]));
EXIT;
end;
// split it into a whole and fractional part
const comps = value.Split(['.']);
if Length(comps) > 2 then
begin
Result := TResult<TWei>.Err(TError.Create('Error while converting %s to wei. Too many decimal points.', [value]));
EXIT;
end;
var whole: string := comps[0];
var fract: string;
if Length(comps) > 1 then
fract := comps[1];
var output := BigInteger.Multiply(whole, base);
try
if fract.Length > 0 then
begin
while fract.Length < baseLen - 1 do
fract := fract + '0';
output := BigInteger.Add(output, fract);
end;
if negative then
output := BigInteger.Negate(output);
finally
Result := TResult<TWei>.Ok(output);
end;
end;
function dotToFloat(const value: string): Double;
begin
var FS := TFormatSettings.Create;
FS.DecimalSeparator := '.';
Result := StrToFloat(value, FS);
end;
function floatToDot(const value: Double): string;
begin
var FS := TFormatSettings.Create;
FS.DecimalSeparator := '.';
Result := FloatToStr(value, FS);
end;
end.