@eth-sdk/utils
Advanced tools
Comparing version 0.1.1 to 0.1.2
@@ -39,3 +39,3 @@ "use strict"; | ||
} | ||
const payload = hex_1.concatHex('0xff', creator.toLowerCase(), salt.toLowerCase(), byteCode.toLowerCase()); | ||
const payload = hex_1.concatHex('0xff', creator, salt, byteCode); | ||
result = toChecksumAddress(`${constants_1.HEX_PREFIX}${keccak_1.keccak256(payload).slice(2).slice(-40)}`); | ||
@@ -42,0 +42,0 @@ } |
@@ -12,2 +12,3 @@ import * as abi from './abi'; | ||
export * from './tag'; | ||
export * from './units'; | ||
export * from './web3'; | ||
@@ -14,0 +15,0 @@ export * from './constants'; |
@@ -25,4 +25,5 @@ "use strict"; | ||
__export(require("./tag")); | ||
__export(require("./units")); | ||
__export(require("./web3")); | ||
__export(require("./constants")); | ||
//# sourceMappingURL=index.js.map |
@@ -0,1 +1,10 @@ | ||
import { TQuantity } from './types'; | ||
export declare function toNumber(value: any, defaultValue?: number): number; | ||
export declare function toNumberString(value: TQuantity, options?: toNumberString.IOptions): string; | ||
export declare namespace toNumberString { | ||
interface IOptions { | ||
shift?: number; | ||
precision?: number; | ||
} | ||
} | ||
export declare function trimNumberString(value: string): string; |
@@ -7,2 +7,3 @@ "use strict"; | ||
const bn_js_1 = __importDefault(require("bn.js")); | ||
const bn_1 = require("./bn"); | ||
const helpers_1 = require("./helpers"); | ||
@@ -38,2 +39,93 @@ const hex_1 = require("./hex"); | ||
exports.toNumber = toNumber; | ||
function toNumberString(value, options = {}) { | ||
let result = null; | ||
switch (typeof value) { | ||
case 'number': | ||
value = value; | ||
result = value.toString(10); | ||
break; | ||
case 'string': | ||
value = value; | ||
if (hex_1.isHex(value, 'quantity')) { | ||
result = bn_1.toBN(value).toString(10); | ||
} | ||
else if (/^[0-9]*\.?[0-9]+$/g.test(value)) { | ||
result = value; | ||
} | ||
break; | ||
case 'object': | ||
if (bn_js_1.default.isBN(value)) { | ||
result = value.toString(10); | ||
} | ||
break; | ||
} | ||
if (result) { | ||
result = trimNumberString(result); | ||
const { shift, precision } = options; | ||
if (shift) { | ||
if (!result.includes('.')) { | ||
result = `${result}.0`; | ||
} | ||
const extra = '0'.repeat(shift > 0 ? shift : -shift); | ||
result = `${extra}${result}${extra}`; | ||
const dotPosition = result.indexOf('.'); | ||
result = result.replace('.', ''); | ||
if (shift > 0) { | ||
result = [ | ||
result.slice(0, dotPosition + shift), | ||
result.slice(dotPosition + shift), | ||
].join('.'); | ||
} | ||
else { | ||
result = [ | ||
result.slice(0, dotPosition + shift), | ||
result.slice(dotPosition + shift), | ||
].join('.'); | ||
} | ||
result = trimNumberString(result); | ||
} | ||
if (Number.isInteger(precision)) { | ||
if (!result.includes('.')) { | ||
if (precision) { | ||
result = `${result}.${'0'.repeat(precision)}`; | ||
} | ||
} | ||
else if (!precision) { | ||
result = result.slice(0, result.indexOf('.')); | ||
} | ||
else { | ||
const parts = result.split('.'); | ||
const extraCount = parts[1].length - precision; | ||
if (extraCount > 0) { | ||
parts[1] = parts[1].substr(0, precision); | ||
} | ||
else if (extraCount < 0) { | ||
parts[1] = `${parts[1]}${'0'.repeat(-extraCount)}`; | ||
} | ||
result = parts.join('.'); | ||
} | ||
} | ||
} | ||
return result; | ||
} | ||
exports.toNumberString = toNumberString; | ||
function trimNumberString(value) { | ||
let result = null; | ||
if (value) { | ||
const parts = value.split('.'); | ||
parts[0] = parts[0].replace(/^(0+)/g, ''); | ||
parts[1] = (parts[1] || '').replace(/(0+)$/g, ''); | ||
if (!parts[0]) { | ||
parts[0] = '0'; | ||
} | ||
if (parts[1]) { | ||
result = parts.join('.'); | ||
} | ||
else { | ||
result = parts[0]; | ||
} | ||
} | ||
return result; | ||
} | ||
exports.trimNumberString = trimNumberString; | ||
//# sourceMappingURL=number.js.map |
{ | ||
"name": "@eth-sdk/utils", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "Eth sdk utils", | ||
@@ -5,0 +5,0 @@ "author": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
66656
73
1065