Skip to content

Instantly share code, notes, and snippets.

@joefiorini joefiorini/utils.re
Created Feb 1, 2018

Embed
What would you like to do?
type bn;
type bigNumber;
type hex =
| Hex(string);
type address =
| Address(string);
type numeric =
| FromString(string)
| FromInt(int)
| FromBN(bn)
| FromBigNumber(bigNumber);
type sha3numeric =
| FromNumeric(numeric)
| FromHex(hex);
type sha3input = {
type_: string,
value: sha3numeric
};
type sha3inputShort = {
t: string,
v: sha3numeric
};
type sha3arg =
| Numeric(sha3numeric)
| Sha3Input(sha3input);
[@bs.deriving jsConverter]
type unitType = [
| [@bs.as "noether"] `Noether
| [@bs.as "wei"] `Wei
| [@bs.as "kwei"] `Kwei
| [@bs.as "babbage"] `Babbage
| [@bs.as "femtoether"] `Femtoether
| [@bs.as "mwei"] `Mwei
| [@bs.as "lovelace"] `Lovelace
| [@bs.as "picoether"] `Picoether
| [@bs.as "gwei"] `Gwei
| [@bs.as "shannon"] `Shannon
| [@bs.as "nanoether"] `Nanoether
| [@bs.as "nano"] `Nano
| [@bs.as "szabo"] `Szabo
| [@bs.as "microether"] `Microether
| [@bs.as "micro"] `Micro
| [@bs.as "finney"] `Finney
| [@bs.as "milliether"] `Milliether
| [@bs.as "milli"] `Milli
| [@bs.as "ether"] `Ether
| [@bs.as "kether"] `Kether
| [@bs.as "grand"] `Grand
| [@bs.as "mether"] `Mether
| [@bs.as "gether"] `Gether
| [@bs.as "tether"] `Tether
];
type amount =
| Amount(int, unitType);
[@bs.module "web3-utils"] external randomHex : int => string = "";
/* TODO: [@bs.module "web3-utils"] external bn */
/* [@bs.module "web3-utils"] external isBN : int => bool = "";
[@bs.module "web3-utils"] external isBigNumber : int => bool = ""; */
[@bs.module "web3-utils"] external sha3 : string => string = "";
[@bs.module "web3-utils"] external keccak256 : string => string = "";
[@bs.module "web3-utils"] [@bs.splice]
external soliditySha3 : array(sha3arg) => string = "";
[@bs.module "web3-utils"] external isHex : string => bool = "";
[@bs.module "web3-utils"] external isHexStrict : string => bool = "";
[@bs.module "web3-utils"] external isAddress : string => bool = "";
[@bs.module "web3-utils"] external toChecksumAddress : address => string = "";
[@bs.module "web3-utils"] external checkAddressChecksum : address => bool = "";
[@bs.module "web3-utils"] external toHex : numeric => string = "";
[@bs.module "web3-utils"] external hexToNumberString : string => string = "";
[@bs.module "web3-utils"] external hexToNumber : string => int = "";
[@bs.module "web3-utils"] external numberToHex : int => string = "";
[@bs.module "web3-utils"] external hexToUtf8 : string => string = "";
[@bs.module "web3-utils"] external hexToAscii : string => string = "";
[@bs.module "web3-utils"] external asciiToHex : string => string = "";
[@bs.module "web3-utils"] external hexToBytes : string => array(int) = "";
[@bs.module "web3-utils"] external bytesToHex : array(int) => string = "";
[@bs.module "web3-utils"]
external toWeiJs : (~value: int, ~units: string, unit) => int = "toWei";
let toWei = (value: amount) : int => {
let Amount(num, unitType) = value;
toWeiJs(~value=num, ~units=unitTypeToJs(unitType), ());
};
let wei = toWei(Amount(100, `Wei));
/* let fromWei = (~value: int, ~fromUnitType=Wei): int => {
} */
randomHex(3);
/* soliditySha3([|Numeric(FromString("blah"))|]); */
/*
_
BN
isBN
isBigNumber
sha3
soliditySha3
isHex
isHexStrict
isAddress
toChecksumAddress
checkAddressChecksum
toHex
toBN
hexToNumberString
hexToNumber
numberToHex
hexToUtf8
hexToAscii
utf8ToHex
asciiToHex
hexToBytes
bytesToHex
toWei
fromWei
unitMap
padLeft
padRight
toTwosComplement */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.