mirror of https://github.com/status-im/web3.js.git
Add toWei/fromWei and isAddress
This commit is contained in:
parent
5c994ff817
commit
188e9e5325
|
@ -367,7 +367,7 @@ var addFunctionsToContract = function (contract, desc, address) {
|
|||
});
|
||||
|
||||
// transactions do not have any output, cause we do not know, when they will be processed
|
||||
web3.eth.transact(options);
|
||||
web3.eth.sendTransaction(options);
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -586,7 +586,6 @@ var methods = [
|
|||
{ name: 'compile.solidity', call: 'eth_solidity' },
|
||||
{ name: 'compile.lll', call: 'eth_lll' },
|
||||
{ name: 'compile.serpent', call: 'eth_serpent' },
|
||||
{ name: 'logs', call: 'eth_logs' },
|
||||
{ name: 'getBlockTransactionCount', call: transactionCountCall },
|
||||
{ name: 'getBlockUncleCount', call: uncleCountCall },
|
||||
|
||||
|
@ -605,7 +604,8 @@ var methods = [
|
|||
{ name: 'lll', call: 'eth_lll', newMethod: 'compile.lll' },
|
||||
{ name: 'serpent', call: 'eth_serpent', newMethod: 'compile.serpent' },
|
||||
{ name: 'transactionCount', call: transactionCountCall, newMethod: 'getBlockTransactionCount' },
|
||||
{ name: 'uncleCount', call: uncleCountCall, newMethod: 'getBlockUncleCount' }
|
||||
{ name: 'uncleCount', call: uncleCountCall, newMethod: 'getBlockUncleCount' },
|
||||
{ name: 'logs', call: 'eth_logs' }
|
||||
];
|
||||
|
||||
/// @returns an array of objects describing web3.eth api properties
|
||||
|
@ -1526,6 +1526,9 @@ var filterEvents = function (json) {
|
|||
/// TODO: use BigNumber.js to parse int
|
||||
/// TODO: add tests for it!
|
||||
var toEth = function (str) {
|
||||
|
||||
console.warn('This method is deprecated please use eth.fromWei(number, unit) instead.');
|
||||
|
||||
var val = typeof str === "string" ? str.indexOf('0x') === 0 ? parseInt(str.substr(2), 16) : parseInt(str) : str;
|
||||
var unit = 0;
|
||||
var units = c.ETH_UNITS;
|
||||
|
@ -1548,6 +1551,184 @@ var toEth = function (str) {
|
|||
return s + ' ' + units[unit];
|
||||
};
|
||||
|
||||
|
||||
|
||||
/**
|
||||
Takes a number of wei and converts it to any other ether unit.
|
||||
|
||||
Possible units are:
|
||||
|
||||
- kwei/ada
|
||||
- mwei/babbage
|
||||
- gwei/shannon
|
||||
- szabo
|
||||
- finney
|
||||
- ether
|
||||
- kether/grand/einstein
|
||||
- mether
|
||||
- gether
|
||||
- tether
|
||||
|
||||
@method fromWei
|
||||
@param {Number|String} number can be a number or a HEX of a decimal
|
||||
@param {String} unit the unit to convert to
|
||||
@return {Number|Object} When given a BigNumber object it returns one as well, otherwise a number
|
||||
*/
|
||||
var fromWei = function(number, unit) {
|
||||
var isBigNumber = true;
|
||||
|
||||
if(!number)
|
||||
return number;
|
||||
|
||||
if(typeof number === 'string' && number.indexOf('0x') === 0)
|
||||
number = web3.toDecimal(number);
|
||||
|
||||
if(!(number instanceof BigNumber)) {
|
||||
isBigNumber = false;
|
||||
number = new BigNumber(number.toString()); // toString to prevent errors, the user have to handle giving correct bignums themselves
|
||||
}
|
||||
|
||||
|
||||
unit = unit.toLowerCase();
|
||||
|
||||
switch(unit) {
|
||||
case 'kwei':
|
||||
case 'ada':
|
||||
number = number.dividedBy(1000);
|
||||
break;
|
||||
case 'mwei':
|
||||
case 'babbage':
|
||||
number = number.dividedBy(1000000);
|
||||
break;
|
||||
case 'gwei':
|
||||
case 'schannon':
|
||||
number = number.dividedBy(1000000000);
|
||||
break;
|
||||
case 'szabo':
|
||||
number = number.dividedBy(1000000000000);
|
||||
break;
|
||||
case 'finney':
|
||||
number = number.dividedBy(1000000000000000);
|
||||
break;
|
||||
case 'ether':
|
||||
number = number.dividedBy(1000000000000000000);
|
||||
break;
|
||||
case 'kether':
|
||||
case 'grand':
|
||||
case 'einstein':
|
||||
number = number.dividedBy(1000000000000000000000);
|
||||
break;
|
||||
case 'mether':
|
||||
number = number.dividedBy(1000000000000000000000000);
|
||||
break;
|
||||
case 'gether':
|
||||
number = number.dividedBy(1000000000000000000000000000);
|
||||
break;
|
||||
case 'tether':
|
||||
number = number.dividedBy(1000000000000000000000000000000);
|
||||
break;
|
||||
}
|
||||
|
||||
return (isBigNumber) ? number : number.toNumber();
|
||||
};
|
||||
|
||||
/**
|
||||
Takes a number of a unit and converts it to wei.
|
||||
|
||||
Possible units are:
|
||||
|
||||
- kwei/ada
|
||||
- mwei/babbage
|
||||
- gwei/shannon
|
||||
- szabo
|
||||
- finney
|
||||
- ether
|
||||
- kether/grand/einstein
|
||||
- mether
|
||||
- gether
|
||||
- tether
|
||||
|
||||
@method toWei
|
||||
@param {Number|String} number can be a number or a HEX of a decimal
|
||||
@param {String} unit the unit to convert to
|
||||
@return {Number|Object} When given a BigNumber object it returns one as well, otherwise a number
|
||||
*/
|
||||
var toWei = function(number, unit) {
|
||||
var isBigNumber = true;
|
||||
|
||||
if(!number)
|
||||
return number;
|
||||
|
||||
if(typeof number === 'string' && number.indexOf('0x') === 0)
|
||||
number = web3.toDecimal(number);
|
||||
|
||||
if(!(number instanceof BigNumber)) {
|
||||
isBigNumber = false;
|
||||
number = new BigNumber(number.toString());// toString to prevent errors, the user have to handle giving correct bignums themselves
|
||||
}
|
||||
|
||||
|
||||
unit = unit.toLowerCase();
|
||||
|
||||
switch(unit) {
|
||||
case 'kwei':
|
||||
case 'ada':
|
||||
number = number.times(1000);
|
||||
break;
|
||||
case 'mwei':
|
||||
case 'babbage':
|
||||
number = number.times(1000000);
|
||||
break;
|
||||
case 'gwei':
|
||||
case 'schannon':
|
||||
number = number.times(1000000000);
|
||||
break;
|
||||
case 'szabo':
|
||||
number = number.times(1000000000000);
|
||||
break;
|
||||
case 'finney':
|
||||
number = number.times(1000000000000000);
|
||||
break;
|
||||
case 'ether':
|
||||
number = number.times(1000000000000000000);
|
||||
break;
|
||||
case 'kether':
|
||||
case 'grand':
|
||||
case 'einstein':
|
||||
number = number.times(1000000000000000000000);
|
||||
break;
|
||||
case 'mether':
|
||||
number = number.times(1000000000000000000000000);
|
||||
break;
|
||||
case 'gether':
|
||||
number = number.times(1000000000000000000000000000);
|
||||
break;
|
||||
case 'tether':
|
||||
number = number.times(1000000000000000000000000000000);
|
||||
break;
|
||||
}
|
||||
|
||||
return (isBigNumber) ? number : number.toNumber();
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
Checks if the given string is a valid ethereum HEX address.
|
||||
|
||||
@method isAddress
|
||||
@param {String} address the given HEX adress
|
||||
@return {Boolean}
|
||||
*/
|
||||
var isAddress = function(address) {
|
||||
if(address.indexOf('0x') === 0 && address.length !== 42)
|
||||
return false;
|
||||
if(address.indexOf('0x') === -1 && address.length !== 40)
|
||||
return false;
|
||||
|
||||
return /^\w+$/.test(address);
|
||||
};
|
||||
|
||||
|
||||
module.exports = {
|
||||
findIndex: findIndex,
|
||||
toAscii: toAscii,
|
||||
|
@ -1556,7 +1737,10 @@ module.exports = {
|
|||
extractTypeName: extractTypeName,
|
||||
filterFunctions: filterFunctions,
|
||||
filterEvents: filterEvents,
|
||||
toEth: toEth
|
||||
toEth: toEth,
|
||||
toWei: toWei,
|
||||
fromWei: fromWei,
|
||||
isAddress: isAddress
|
||||
};
|
||||
|
||||
|
||||
|
@ -1681,8 +1865,15 @@ var setupMethods = function (obj, methods) {
|
|||
obj[objectMethods[0]] = {};
|
||||
|
||||
obj[objectMethods[0]][objectMethods[1]] = callFunction;
|
||||
} else
|
||||
obj[method.name] = callFunction;
|
||||
|
||||
} else {
|
||||
|
||||
Object.defineProperty(obj, method.name, {
|
||||
enumerable: (method.newMethod) ? false : true,
|
||||
value: callFunction
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
});
|
||||
};
|
||||
|
@ -1717,7 +1908,12 @@ var setupProperties = function (obj, properties) {
|
|||
});
|
||||
};
|
||||
}
|
||||
Object.defineProperty(obj, property.name, proto);
|
||||
|
||||
Object.defineProperty(obj, property.name, {
|
||||
enumerable: (property.newProperty) ? false : true,
|
||||
value: proto
|
||||
});
|
||||
|
||||
});
|
||||
};
|
||||
|
||||
|
@ -1768,6 +1964,11 @@ var web3 = {
|
|||
/// used to transform value/string to eth string
|
||||
toEth: utils.toEth,
|
||||
|
||||
toWei: utils.toWei,
|
||||
fromWei: utils.fromWei,
|
||||
isAddress: utils.isAddress,
|
||||
|
||||
|
||||
/// eth object prototype
|
||||
eth: {
|
||||
contractFromAbi: function (abi) {
|
||||
|
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
@ -57,7 +57,6 @@ var methods = [
|
|||
{ name: 'compile.solidity', call: 'eth_solidity' },
|
||||
{ name: 'compile.lll', call: 'eth_lll' },
|
||||
{ name: 'compile.serpent', call: 'eth_serpent' },
|
||||
{ name: 'logs', call: 'eth_logs' },
|
||||
{ name: 'getBlockTransactionCount', call: transactionCountCall },
|
||||
{ name: 'getBlockUncleCount', call: uncleCountCall },
|
||||
|
||||
|
|
186
lib/utils.js
186
lib/utils.js
|
@ -107,6 +107,9 @@ var filterEvents = function (json) {
|
|||
/// TODO: use BigNumber.js to parse int
|
||||
/// TODO: add tests for it!
|
||||
var toEth = function (str) {
|
||||
|
||||
console.warn('This method is deprecated please use eth.fromWei(number, unit) instead.');
|
||||
|
||||
var val = typeof str === "string" ? str.indexOf('0x') === 0 ? parseInt(str.substr(2), 16) : parseInt(str) : str;
|
||||
var unit = 0;
|
||||
var units = c.ETH_UNITS;
|
||||
|
@ -129,6 +132,184 @@ var toEth = function (str) {
|
|||
return s + ' ' + units[unit];
|
||||
};
|
||||
|
||||
|
||||
|
||||
/**
|
||||
Takes a number of wei and converts it to any other ether unit.
|
||||
|
||||
Possible units are:
|
||||
|
||||
- kwei/ada
|
||||
- mwei/babbage
|
||||
- gwei/shannon
|
||||
- szabo
|
||||
- finney
|
||||
- ether
|
||||
- kether/grand/einstein
|
||||
- mether
|
||||
- gether
|
||||
- tether
|
||||
|
||||
@method fromWei
|
||||
@param {Number|String} number can be a number or a HEX of a decimal
|
||||
@param {String} unit the unit to convert to
|
||||
@return {Number|Object} When given a BigNumber object it returns one as well, otherwise a number
|
||||
*/
|
||||
var fromWei = function(number, unit) {
|
||||
var isBigNumber = true;
|
||||
|
||||
if(!number)
|
||||
return number;
|
||||
|
||||
if(typeof number === 'string' && number.indexOf('0x') === 0)
|
||||
number = web3.toDecimal(number);
|
||||
|
||||
if(!(number instanceof BigNumber)) {
|
||||
isBigNumber = false;
|
||||
number = new BigNumber(number.toString()); // toString to prevent errors, the user have to handle giving correct bignums themselves
|
||||
}
|
||||
|
||||
|
||||
unit = unit.toLowerCase();
|
||||
|
||||
switch(unit) {
|
||||
case 'kwei':
|
||||
case 'ada':
|
||||
number = number.dividedBy(1000);
|
||||
break;
|
||||
case 'mwei':
|
||||
case 'babbage':
|
||||
number = number.dividedBy(1000000);
|
||||
break;
|
||||
case 'gwei':
|
||||
case 'schannon':
|
||||
number = number.dividedBy(1000000000);
|
||||
break;
|
||||
case 'szabo':
|
||||
number = number.dividedBy(1000000000000);
|
||||
break;
|
||||
case 'finney':
|
||||
number = number.dividedBy(1000000000000000);
|
||||
break;
|
||||
case 'ether':
|
||||
number = number.dividedBy(1000000000000000000);
|
||||
break;
|
||||
case 'kether':
|
||||
case 'grand':
|
||||
case 'einstein':
|
||||
number = number.dividedBy(1000000000000000000000);
|
||||
break;
|
||||
case 'mether':
|
||||
number = number.dividedBy(1000000000000000000000000);
|
||||
break;
|
||||
case 'gether':
|
||||
number = number.dividedBy(1000000000000000000000000000);
|
||||
break;
|
||||
case 'tether':
|
||||
number = number.dividedBy(1000000000000000000000000000000);
|
||||
break;
|
||||
}
|
||||
|
||||
return (isBigNumber) ? number : number.toNumber();
|
||||
};
|
||||
|
||||
/**
|
||||
Takes a number of a unit and converts it to wei.
|
||||
|
||||
Possible units are:
|
||||
|
||||
- kwei/ada
|
||||
- mwei/babbage
|
||||
- gwei/shannon
|
||||
- szabo
|
||||
- finney
|
||||
- ether
|
||||
- kether/grand/einstein
|
||||
- mether
|
||||
- gether
|
||||
- tether
|
||||
|
||||
@method toWei
|
||||
@param {Number|String} number can be a number or a HEX of a decimal
|
||||
@param {String} unit the unit to convert to
|
||||
@return {Number|Object} When given a BigNumber object it returns one as well, otherwise a number
|
||||
*/
|
||||
var toWei = function(number, unit) {
|
||||
var isBigNumber = true;
|
||||
|
||||
if(!number)
|
||||
return number;
|
||||
|
||||
if(typeof number === 'string' && number.indexOf('0x') === 0)
|
||||
number = web3.toDecimal(number);
|
||||
|
||||
if(!(number instanceof BigNumber)) {
|
||||
isBigNumber = false;
|
||||
number = new BigNumber(number.toString());// toString to prevent errors, the user have to handle giving correct bignums themselves
|
||||
}
|
||||
|
||||
|
||||
unit = unit.toLowerCase();
|
||||
|
||||
switch(unit) {
|
||||
case 'kwei':
|
||||
case 'ada':
|
||||
number = number.times(1000);
|
||||
break;
|
||||
case 'mwei':
|
||||
case 'babbage':
|
||||
number = number.times(1000000);
|
||||
break;
|
||||
case 'gwei':
|
||||
case 'schannon':
|
||||
number = number.times(1000000000);
|
||||
break;
|
||||
case 'szabo':
|
||||
number = number.times(1000000000000);
|
||||
break;
|
||||
case 'finney':
|
||||
number = number.times(1000000000000000);
|
||||
break;
|
||||
case 'ether':
|
||||
number = number.times(1000000000000000000);
|
||||
break;
|
||||
case 'kether':
|
||||
case 'grand':
|
||||
case 'einstein':
|
||||
number = number.times(1000000000000000000000);
|
||||
break;
|
||||
case 'mether':
|
||||
number = number.times(1000000000000000000000000);
|
||||
break;
|
||||
case 'gether':
|
||||
number = number.times(1000000000000000000000000000);
|
||||
break;
|
||||
case 'tether':
|
||||
number = number.times(1000000000000000000000000000000);
|
||||
break;
|
||||
}
|
||||
|
||||
return (isBigNumber) ? number : number.toNumber();
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
Checks if the given string is a valid ethereum HEX address.
|
||||
|
||||
@method isAddress
|
||||
@param {String} address the given HEX adress
|
||||
@return {Boolean}
|
||||
*/
|
||||
var isAddress = function(address) {
|
||||
if(address.indexOf('0x') === 0 && address.length !== 42)
|
||||
return false;
|
||||
if(address.indexOf('0x') === -1 && address.length !== 40)
|
||||
return false;
|
||||
|
||||
return /^\w+$/.test(address);
|
||||
};
|
||||
|
||||
|
||||
module.exports = {
|
||||
findIndex: findIndex,
|
||||
toAscii: toAscii,
|
||||
|
@ -137,6 +318,9 @@ module.exports = {
|
|||
extractTypeName: extractTypeName,
|
||||
filterFunctions: filterFunctions,
|
||||
filterEvents: filterEvents,
|
||||
toEth: toEth
|
||||
toEth: toEth,
|
||||
toWei: toWei,
|
||||
fromWei: fromWei,
|
||||
isAddress: isAddress
|
||||
};
|
||||
|
||||
|
|
23
lib/web3.js
23
lib/web3.js
|
@ -67,8 +67,15 @@ var setupMethods = function (obj, methods) {
|
|||
obj[objectMethods[0]] = {};
|
||||
|
||||
obj[objectMethods[0]][objectMethods[1]] = callFunction;
|
||||
} else
|
||||
obj[method.name] = callFunction;
|
||||
|
||||
} else {
|
||||
|
||||
Object.defineProperty(obj, method.name, {
|
||||
enumerable: (method.newMethod) ? false : true,
|
||||
value: callFunction
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
});
|
||||
};
|
||||
|
@ -103,7 +110,12 @@ var setupProperties = function (obj, properties) {
|
|||
});
|
||||
};
|
||||
}
|
||||
Object.defineProperty(obj, property.name, proto);
|
||||
|
||||
Object.defineProperty(obj, property.name, {
|
||||
enumerable: (property.newProperty) ? false : true,
|
||||
value: proto
|
||||
});
|
||||
|
||||
});
|
||||
};
|
||||
|
||||
|
@ -154,6 +166,11 @@ var web3 = {
|
|||
/// used to transform value/string to eth string
|
||||
toEth: utils.toEth,
|
||||
|
||||
toWei: utils.toWei,
|
||||
fromWei: utils.fromWei,
|
||||
isAddress: utils.isAddress,
|
||||
|
||||
|
||||
/// eth object prototype
|
||||
eth: {
|
||||
contractFromAbi: function (abi) {
|
||||
|
|
|
@ -4,23 +4,23 @@ var u = require('./test.utils.js');
|
|||
|
||||
describe('web3', function() {
|
||||
describe('eth', function() {
|
||||
u.methodExists(web3.eth, 'balanceAt');
|
||||
u.methodExists(web3.eth, 'stateAt');
|
||||
u.methodExists(web3.eth, 'storageAt');
|
||||
u.methodExists(web3.eth, 'countAt');
|
||||
u.methodExists(web3.eth, 'codeAt');
|
||||
u.methodExists(web3.eth, 'transact');
|
||||
u.methodExists(web3.eth, 'getBalance');
|
||||
u.methodExists(web3.eth, 'getState');
|
||||
u.methodExists(web3.eth, 'getStorage');
|
||||
u.methodExists(web3.eth, 'getTransactionCount');
|
||||
u.methodExists(web3.eth, 'getCode');
|
||||
u.methodExists(web3.eth, 'sendTransaction');
|
||||
u.methodExists(web3.eth, 'call');
|
||||
u.methodExists(web3.eth, 'block');
|
||||
u.methodExists(web3.eth, 'transaction');
|
||||
u.methodExists(web3.eth, 'uncle');
|
||||
u.methodExists(web3.eth, 'compilers');
|
||||
u.methodExists(web3.eth, 'lll');
|
||||
u.methodExists(web3.eth, 'solidity');
|
||||
u.methodExists(web3.eth, 'serpent');
|
||||
u.methodExists(web3.eth, 'getBlock');
|
||||
u.methodExists(web3.eth, 'getTransaction');
|
||||
u.methodExists(web3.eth, 'getUncle');
|
||||
u.methodExists(web3.eth, 'getCompilers');
|
||||
u.methodExists(web3.eth.compile, 'lll');
|
||||
u.methodExists(web3.eth.compile, 'solidity');
|
||||
u.methodExists(web3.eth.compile, 'serpent');
|
||||
u.methodExists(web3.eth, 'logs');
|
||||
u.methodExists(web3.eth, 'transactionCount');
|
||||
u.methodExists(web3.eth, 'uncleCount');
|
||||
u.methodExists(web3.eth, 'getBlockTransactionCount');
|
||||
u.methodExists(web3.eth, 'getBlockUncleCount');
|
||||
|
||||
u.propertyExists(web3.eth, 'coinbase');
|
||||
u.propertyExists(web3.eth, 'listening');
|
||||
|
@ -29,7 +29,7 @@ describe('web3', function() {
|
|||
u.propertyExists(web3.eth, 'accounts');
|
||||
u.propertyExists(web3.eth, 'peerCount');
|
||||
u.propertyExists(web3.eth, 'defaultBlock');
|
||||
u.propertyExists(web3.eth, 'number');
|
||||
u.propertyExists(web3.eth, 'blockNumber');
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
@ -8,7 +8,10 @@ describe('web3', function() {
|
|||
u.methodExists(web3, 'fromAscii');
|
||||
u.methodExists(web3, 'toDecimal');
|
||||
u.methodExists(web3, 'fromDecimal');
|
||||
u.methodExists(web3, 'toEth');
|
||||
// u.methodExists(web3, 'toEth');
|
||||
u.methodExists(web3, 'fromWei');
|
||||
u.methodExists(web3, 'toWei');
|
||||
u.methodExists(web3, 'isAddress');
|
||||
u.methodExists(web3, 'setProvider');
|
||||
u.methodExists(web3, 'reset');
|
||||
|
||||
|
|
Loading…
Reference in New Issue