Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions libs/ethereum.red
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,33 @@ Red [

eth: context [

eth-to-wei: func [eth /local cnt n d scale][
eth-to-wei: func [eth [string! float! integer!] /local cnt n d scale][
either string? eth [
string-to-i256 eth 18
][
cnt: 0
scale: to-i256 #{0DE0B6B3A7640000} ;-- 1e18
if string? eth [
if d: find/tail eth #"." [
cnt: length? d

if eth == 0.0 [eth: 0] ; integer

if float? eth [ ; non-zero
cnt: 0
n: 1

while [(cnt < 18) and ((eth * n: power 10 cnt) < 1)] [
cnt: cnt + 1
]
eth: to float! eth
]
if cnt > 0 [

d: find/tail to string! eth * n #"."
cnt: cnt +
min length? d 18 - cnt

n: power 10 cnt
eth: eth * n

scale: div256 scale to-i256 n
]
eth: to-i256 any [attempt [to-integer eth] eth]

eth: to-i256 to-integer eth
mul256 eth scale
]
]
Expand Down