-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(evm): reject tx whenever a decimals conversion produces non-zero …
…remainder
- Loading branch information
Showing
7 changed files
with
117 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,55 @@ | ||
package util | ||
|
||
import "math/big" | ||
import ( | ||
"math/big" | ||
) | ||
|
||
const ethereumDecimals = uint32(18) | ||
|
||
func adaptDecimals(value *big.Int, fromDecimals, toDecimals uint32) *big.Int { | ||
v := new(big.Int).Set(value) // clone value | ||
func adaptDecimals(value *big.Int, fromDecimals, toDecimals uint32) (result *big.Int, remainder *big.Int) { | ||
result = new(big.Int) | ||
remainder = new(big.Int) | ||
exp := big.NewInt(10) | ||
if toDecimals > fromDecimals { | ||
exp.Exp(exp, big.NewInt(int64(toDecimals-fromDecimals)), nil) | ||
return v.Mul(v, exp) | ||
result.Mul(value, exp) | ||
} else { | ||
exp.Exp(exp, big.NewInt(int64(fromDecimals-toDecimals)), nil) | ||
result.DivMod(value, exp, remainder) | ||
} | ||
exp.Exp(exp, big.NewInt(int64(fromDecimals-toDecimals)), nil) | ||
return v.Div(v, exp) | ||
return | ||
} | ||
|
||
// wei => base tokens | ||
func EthereumDecimalsToBaseTokenDecimals(value *big.Int, baseTokenDecimals uint32) uint64 { | ||
v := adaptDecimals(value, ethereumDecimals, baseTokenDecimals) | ||
if !v.IsUint64() { | ||
func EthereumDecimalsToBaseTokenDecimals(value *big.Int, baseTokenDecimals uint32) (result uint64, remainder *big.Int) { | ||
r, m := adaptDecimals(value, ethereumDecimals, baseTokenDecimals) | ||
if !r.IsUint64() { | ||
panic("cannot convert ether value to base tokens: too large") | ||
} | ||
return v.Uint64() | ||
return r.Uint64(), m | ||
} | ||
|
||
func MustEthereumDecimalsToBaseTokenDecimalsExact(value *big.Int, baseTokenDecimals uint32) (result uint64) { | ||
r, m := EthereumDecimalsToBaseTokenDecimals(value, baseTokenDecimals) | ||
if m.Sign() != 0 { | ||
panic("cannot convert ether value to base tokens: non-exact conversion") | ||
} | ||
return r | ||
} | ||
|
||
// base tokens => wei | ||
func BaseTokensDecimalsToEthereumDecimals(value uint64, baseTokenDecimals uint32) *big.Int { | ||
return adaptDecimals(new(big.Int).SetUint64(value), baseTokenDecimals, ethereumDecimals) | ||
func BaseTokensDecimalsToEthereumDecimals(value uint64, baseTokenDecimals uint32) (result *big.Int, remainder uint64) { | ||
r, m := adaptDecimals(new(big.Int).SetUint64(value), baseTokenDecimals, ethereumDecimals) | ||
if !m.IsUint64() { | ||
panic("cannot convert ether value to base tokens: too large") | ||
} | ||
return r, m.Uint64() | ||
} | ||
|
||
func MustBaseTokensDecimalsToEthereumDecimalsExact(value uint64, baseTokenDecimals uint32) (result *big.Int) { | ||
r, m := BaseTokensDecimalsToEthereumDecimals(value, baseTokenDecimals) | ||
if m != 0 { | ||
panic("cannot convert base tokens value to ether: non-exact conversion") | ||
} | ||
return r | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters