0xbc3609fc…359fsent to0xb3561b99…672c·#4,901,568·view on Etherscan
const fs = require('fs');
const moment = require('moment');
const args = require('minimist')(process.argv.slice(2));
const Accounts = require('web3-eth-accounts');
const Utils = require('web3-utils');
const Web3Factory = require('../src/web3-factory');
const provider = 'https://api.myetherapi.com/eth';
let accounts = new Accounts(provider);
let web3Factory = new Web3Factory(provider);
web3 = web3Factory.createWeb3Eth();
if (!(args.from && args.from.length > 0)) {
console.log('from is required');
return;
}
if (!(args.to && args.to.length > 0)) {
console.log('to is required');
return;
}
if (!args.from.startsWith('0x')) {
args.from = '0x' + args.from;
}
if (!args.to.startsWith('0x')) {
args.to = '0x' + args.to;
}
let wallet = web3.eth.accounts.wallet.create();
wallet.add(args.from);
wallet.add(args.to);
let from = wallet[0];
let to = wallet[1];
if (!args.password) {
console.log('password is required in order to save an encrypted wallet of the accounts involved in the transaction to disk');
return;
}
if (!(args.amount === 'ALL' || args.amount > 0)) {
console.log(`amount is required and must be greater than 0`);
return;
}
let timestamp = moment().utc().format('YYYY-MM-DD-T-HH-mm-ss-SSS') + 'Z';
let encryptedWallet = wallet.encrypt(args.password);
let encryptedWalletFilename = `encrypted-wallet-${timestamp}.json`;
console.log('encrypted wallet with to and from accounts saved to ' + encryptedWalletFilename);
fs.writeFileSync(encryptedWalletFilename, JSON.stringify(encryptedWallet));
let txTransfer = {};
let gasPriceOut;
let gasLimitOut;
web3.eth.getBlockNumber()
.then(latestBlockNumber => web3.eth.getBlock(latestBlockNumber))
.then((latestBlock) => gasLimitOut = latestBlock.gasLimit)
.then(() => args.amount === 'ALL'
? web3.eth.getBalance(from.address)
: Promise.resolve(parseInt(args.amount)))
.then((amount) => {
txTransfer.from = from.address;
txTransfer.to = to.address;
txTransfer.value = amount;
txTransfer.data = Utils.toHex(moment().utc().toISOString());
})
.then(() => web3.eth.getGasPrice())
.then((gasPrice) => gasPriceOut = gasPrice)
.then(() => web3.eth.estimateGas(txTransfer))
.then((gasEstimate) => {
console.log('gas price ' + gasPriceOut);
console.log('gas estimate ' + gasEstimate);
txTransfer.gas = (gasPriceOut * gasEstimate);
if (txTransfer.gas > gasLimitOut) {
txTransfer.gas = gasLimitOut;
console.log('gas limit reached ' + gasLimitOut);
}
txTransfer.value = txTransfer.value - txTransfer.gas;
console.log(`Sending ${txTransfer.value} with ${txTransfer.gas} gas from ${from.address}/${from.privateKey} to ${to.address}/${to.privateKey}`);
return web3.eth.sendTransaction(txTransfer);
})
.then((receipt) => {
console.log(receipt);
})
.catch((error) => console.log(error));