forked from svanas/delphereum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb3.eth.utils.pas
175 lines (161 loc) · 4.33 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
{******************************************************************************}
{ }
{ Delphereum }
{ }
{ Copyright(c) 2018 Stefan van As <[email protected]> }
{ Github Repository <https://github.com/svanas/delphereum> }
{ }
{ Distributed under Creative Commons NonCommercial (aka CC BY-NC) license. }
{ }
{******************************************************************************}
unit web3.eth.utils;
{$I web3.inc}
interface
uses
// Delphi
System.SysUtils,
// Velthuis' BigNumbers
Velthuis.BigIntegers,
// web3
web3,
web3.eth.types;
type
TEthUnit = (
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; &to: TEthUnit; decimals: Byte = 18): string;
function toWei(input: string; &unit: TEthUnit): TWei;
function EthToFloat(const value: string): Extended;
function FloatToEth(value: Extended): string;
implementation
const
UnitToWei: array[TEthUnit] 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; &to: TEthUnit; decimals: Byte): string;
var
negative: Boolean;
base : BigInteger;
baseLen : Integer;
whole : BigInteger;
fraction: BigInteger;
begin
Result := '';
negative := wei.Negative;
base := UnitToWei[&to];
baseLen := UnitToWei[&to].Length;
if negative then
wei := wei.Abs;
BigInteger.DivMod(wei, base, whole, fraction);
if not fraction.IsZero then
begin
Result := fraction.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(input: string; &unit: TEthUnit): TWei;
var
negative: Boolean;
base : BigInteger;
baseLen : Integer;
comps : TArray<string>;
whole : string;
fraction: string;
begin
base := UnitToWei[&unit];
baseLen := UnitToWei[&unit].Length;
// is it negative?
negative := (input.Length > 0) and (input[Low(input)] = '-');
if negative then
Delete(input, Low(input), 1);
if (input = '') or (input = '.') then
raise EWeb3.CreateFmt('Error while converting %s to wei. Invalid value.', [input]);
// split it into a whole and fractional part
comps := input.Split(['.']);
if Length(comps) > 2 then
raise EWeb3.CreateFmt('Error while converting %s to wei. Too many decimal points.', [input]);
whole := comps[0];
if Length(comps) > 1 then
fraction := comps[1];
Result := BigInteger.Multiply(whole, base);
if fraction.Length > 0 then
begin
while fraction.Length < baseLen - 1 do
fraction := fraction + '0';
Result := BigInteger.Add(Result, fraction);
end;
if negative then
Result := BigInteger.Negate(Result);
end;
function EthToFloat(const value: string): Extended;
var
FS: TFormatSettings;
begin
FS := TFormatSettings.Create;
FS.DecimalSeparator := '.';
Result := StrToFloat(value, FS);
end;
function FloatToEth(value: Extended): string;
var
FS: TFormatSettings;
begin
FS := TFormatSettings.Create;
FS.DecimalSeparator := '.';
Result := FloatToStr(value, FS);
end;
end.