diff --git a/libs/ethereum.red b/libs/ethereum.red index edb3836..053b8ac 100644 --- a/libs/ethereum.red +++ b/libs/ethereum.red @@ -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 ] ]