diff --git a/src/Formatters/OptionalQuantityFormatter.php b/src/Formatters/OptionalQuantityFormatter.php new file mode 100644 index 0000000..c191755 --- /dev/null +++ b/src/Formatters/OptionalQuantityFormatter.php @@ -0,0 +1,37 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Formatters; + +use InvalidArgumentException; +use Web3\Utils; +use Web3\Formatters\IFormatter; +use Web3\Validators\TagValidator; + +class OptionalQuantityFormatter implements IFormatter +{ + /** + * format + * + * @param mixed $value + * @return string + */ + public static function format($value) + { + if (TagValidator::validate($value)) { + return $value; + } + $value = Utils::toString($value); + $bn = Utils::toBn($value); + + return '0x' . $bn->toHex(true); + } +} \ No newline at end of file