How to do it...

We are now going to learn how to manage accounts and send transactions:

  1. To view the list of accounts available in the connected node, use the following command:
// v0.2x.x
web3.eth.accounts;

// v1.x.x
web3.eth.getAccounts().then(console.log);
  1. To create an account using the web3 object, run the following command. Starting from 1.x.x, web3.js includes added support for account management:
// For v1.x.x
web3.eth.accounts.create();
web3.eth.accounts.create("<random entropy>");

The create function accepts an entropy as input for randomness. If not specified, it will generate a random hex of 32 bytes by using web3.utils.randomHex(32) and will use it as the entropy input. This method will return the new address, private key, and methods associated with it:

{ 
address: '0x824e470cCac64CC5fa4Abe953e64FA360EA11366',
privateKey: '0x782174a3e404424af...499baffd30e6105f',
signTransaction: [Function: signTransaction],
sign: [Function: sign],
encrypt: [Function: encrypt]
}
  1. If you already have a private key, import it using the privateKeyToAccount function:
// For v1.x.x
web3.eth.accounts.privateKeyToAccount("<privateKey>");
  1. Unlock your account with the passphrase before sending a transaction. Authentication works differently for a raw transaction, which we will focus on in the Sending a raw transaction recipe later in this chapter. You can try unlocking your account with the following scripts:
// For v0.2x.x
web3.personal.unlockAccount("<address>", "<password>", <duration>);

// For v1.x.x
web3.eth.personal.unlockAccount("<address>", "<password>", <duration>)
.then(console.log);
It is recommended to reduce the unlock duration or to lock the account immediately after a transaction with lockAccount. If you are in a development environment and don't want to lock and unlock an account frequently, then specify the duration as 0 to avoid automatic locking after a certain period.
  1. To send a transaction, use the sendTransaction method with the required parameters:
web3.eth.sendTransaction(transaction_object [, callback])

You can use the following parameters to create the transaction object:

  • from: Address of the sending account. The default is web3.eth.defaultAccount.
  • to (optional): Address of the destination account. Leave it blank if you are creating a contract.
  • value (optional): The value transferred through the transaction in Wei.
  • gas (optional): The maximum amount of gas to use for the transaction.
  • gasPrice (optional): The price of gas for this transaction in Wei. The default is web3.eth.gasPrice.
  • data (optional): Either the data of the function call or the contract initialization code.
  • nonce (optional): Nonce value for the transaction. You can use this to overwrite a transaction by providing the same nonce value as the older transaction.

Here is an example that uses these parameters:

web3.eth.sendTransaction({
from: "0xce5C2D181f6DD99091351f6E6056c333A969AEC9",
to: "",
gas:21000,
gasPrice: 20000000000, // 20 Gwei
value: 200000
}, function(error, transactionHash) {
if(!error)
console.log(
transactionHash);
}
)
  1. Starting from web3.js 1.0.0, the sendTransaction method returns a promise combined with an event emitter. This can help perform different tasks based on various stages of a transaction. Consider the following example:
web3.eth.sendTransaction({
    from: "0xce5C2D181f6DD99091351f6E6056c333A969AEC9",
    to: 0x71495cd51c5356B1f0769dB5027DC0588010dC14,
    value: '10000000000000000'
})
.on('transactionHash', function(hash){
    console.log(hash);
})
.on('receipt', function(receipt){
    console.log(receipt);
})
.on('confirmation', function(confirmationNumber, receipt){ 
console.log(confirmationNumber);
})
.on('error', console.error);

Let's look into each event in detail:

  • transactionHash: It is fired immediately after a transaction hash is available.
  • receipt: It is fired when the transaction receipt is generated.
  • confirmation: It is fired for every confirmation up to the 12th confirmation, starting from 0th confirmation.
  • error: It is fired if an error occurs during the transaction. For out of gas errors, the second parameter is the transaction receipt.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.227.10.45