move getBalance in accountParser
This commit is contained in:
parent
3d70028cc5
commit
bb3e87d85e
|
@ -21,10 +21,29 @@ class AccountParser {
|
|||
return accounts;
|
||||
}
|
||||
|
||||
static getHexBalance(balanceString, web3) {
|
||||
if (!balanceString) {
|
||||
return 0xFFFFFFFFFFFFFFFFFF;
|
||||
}
|
||||
if (web3.utils.isHexStrict(balanceString)) {
|
||||
return balanceString;
|
||||
}
|
||||
const match = balanceString.match(/([0-9]+) ?([a-zA-Z]*)/);
|
||||
if (!match[2]) {
|
||||
return web3.utils.toHex(parseInt(match[1], 10));
|
||||
}
|
||||
|
||||
return web3.utils.toHex(web3.utils.toWei(match[1], match[2]));
|
||||
}
|
||||
|
||||
static getAccount(accountConfig, web3, logger) {
|
||||
if (!logger) {
|
||||
logger = console;
|
||||
}
|
||||
let hexBalance = null;
|
||||
if (accountConfig.balance) {
|
||||
hexBalance = AccountParser.getHexBalance(accountConfig.balance, web3);
|
||||
}
|
||||
if (accountConfig.privateKey) {
|
||||
if (!accountConfig.privateKey.startsWith('0x')) {
|
||||
accountConfig.privateKey = '0x' + accountConfig.privateKey;
|
||||
|
@ -33,7 +52,7 @@ class AccountParser {
|
|||
logger.warn(`Private key ending with ${accountConfig.privateKey.substr(accountConfig.privateKey.length - 5)} is not a HEX string`);
|
||||
return null;
|
||||
}
|
||||
return web3.eth.accounts.privateKeyToAccount(accountConfig.privateKey);
|
||||
return Object.assign(web3.eth.accounts.privateKeyToAccount(accountConfig.privateKey), {hexBalance});
|
||||
}
|
||||
if (accountConfig.privateKeyFile) {
|
||||
let fileContent = fs.readFileSync(fs.dappPath(accountConfig.privateKeyFile)).toString();
|
||||
|
@ -46,7 +65,7 @@ class AccountParser {
|
|||
logger.warn(`Private key is not a HEX string in file ${accountConfig.privateKeyFile} at index ${index}`);
|
||||
return null;
|
||||
}
|
||||
return web3.eth.accounts.privateKeyToAccount(key);
|
||||
return Object.assign(web3.eth.accounts.privateKeyToAccount(key), {hexBalance});
|
||||
});
|
||||
}
|
||||
if (accountConfig.mnemonic) {
|
||||
|
@ -59,7 +78,7 @@ class AccountParser {
|
|||
const accounts = [];
|
||||
for (let i = addressIndex; i < addressIndex + numAddresses; i++) {
|
||||
const wallet = hdwallet.derivePath(wallet_hdpath + i).getWallet();
|
||||
accounts.push(web3.eth.accounts.privateKeyToAccount('0x' + wallet.getPrivateKey().toString('hex')));
|
||||
accounts.push(Object.assign(web3.eth.accounts.privateKeyToAccount('0x' + wallet.getPrivateKey().toString('hex')), {hexBalance}));
|
||||
}
|
||||
return accounts;
|
||||
}
|
||||
|
|
|
@ -61,29 +61,13 @@ class Test {
|
|||
this.engine.startService("codeGenerator");
|
||||
}
|
||||
|
||||
getBalance(balanceString) {
|
||||
if (!balanceString) {
|
||||
return 0xFFFFFFFFFFFFFFFFFF;
|
||||
}
|
||||
if (this.web3.utils.isHexStrict(balanceString)) {
|
||||
return balanceString;
|
||||
}
|
||||
const match = balanceString.match(/([0-9]+) ?([a-zA-Z]*)/);
|
||||
if (!match[2]) {
|
||||
return this.web3.utils.toHex(parseInt(match[1], 10));
|
||||
}
|
||||
|
||||
return this.web3.utils.toHex(this.web3.utils.toWei(match[1], match[2]));
|
||||
}
|
||||
|
||||
initWeb3Provider() {
|
||||
if (this.simOptions.node) {
|
||||
this.web3.setProvider(new this.web3.providers.HttpProvider(this.simOptions.node));
|
||||
} else {
|
||||
if (this.simOptions.accounts) {
|
||||
this.simOptions.accounts = this.simOptions.accounts.map((account, index) => {
|
||||
const balance = this.getBalance(this.options.deployment.accounts[index].balance);
|
||||
return {balance, secretKey: account.privateKey};
|
||||
this.simOptions.accounts = this.simOptions.accounts.map((account) => {
|
||||
return {balance: account.hexBalance, secretKey: account.privateKey};
|
||||
});
|
||||
}
|
||||
this.sim = getSimulator();
|
||||
|
|
Loading…
Reference in New Issue