Created
January 24, 2020 17:34
-
-
Save mariano-aguero/7cab73c1a34e695227da865198fc2101 to your computer and use it in GitHub Desktop.
MathBN
This file contains hidden or 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
const Big = require('big.js') | |
const BN = require('bn.js') | |
const MathBN = { | |
sub: (a, b) => { | |
const aBN = new Big(a || '0') | |
const bBN = new Big(b || '0') | |
return aBN.sub(bBN).toString(10) | |
}, | |
add: (a, b) => { | |
const aBN = new Big(a || '0') | |
const bBN = new Big(b || '0') | |
return aBN.add(bBN).toString(10) | |
}, | |
addAsBN: (a, b) => { | |
const aBN = new Big(a || '0') | |
const bBN = new Big(b || '0') | |
return aBN.add(bBN) | |
}, | |
gt: (a, b) => { | |
const aBN = new BN(a || '0') | |
const bBN = new BN(b || '0') | |
return aBN.gt(bBN) | |
}, | |
gte: (a, b) => { | |
const aBN = new BN(a || '0') | |
const bBN = new BN(b || '0') | |
return aBN.gte(bBN) | |
}, | |
lt: (a, b) => { | |
const aBN = MathBN.toBig(a) | |
const bBN = MathBN.toBig(b) | |
return aBN.lt(bBN) | |
}, | |
lte: (a, b) => { | |
const aBN = new BN(a || '0') | |
const bBN = new BN(b || '0') | |
return aBN.lte(bBN) | |
}, | |
mul: (a, b) => { | |
const aBN = new Big(a || '0') | |
const bBN = new Big(b || '0') | |
return aBN.mul(bBN).toString() | |
}, | |
div: (a, b) => { | |
const aBN = new Big(a || '0') | |
const bBN = new Big(b || '0') | |
try { | |
return aBN.div(bBN).toString() | |
} catch (err) { | |
console.error(err) | |
return 0 | |
} | |
}, | |
divAsBig: (a, b) => { | |
const aBN = new Big(a || '0') | |
const bBN = new Big(b || '0') | |
try { | |
return aBN.div(bBN) | |
} catch (err) { | |
console.error(err) | |
return 0 | |
} | |
}, | |
min: (a, b) => { | |
const aBN = new BN(a || '0') | |
const bBN = new BN(b || '0') | |
return (aBN.lt(bBN) ? a : b).toString(10) | |
}, | |
max: (a, b) => { | |
const aBN = new BN(a || '0') | |
const bBN = new BN(b || '0') | |
return (aBN.gt(bBN) ? a : b).toString(10) | |
}, | |
toBig: x => { | |
return new Big(x) | |
}, | |
pow: (a, b) => { | |
const aBN = MathBN.toBig(a) | |
return aBN.pow(b) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment