Let's take a look at the APIs to retrieve the gas price, the balance of an address, and information on a mined transaction:
//It's sync. For async use getGasPrice
console.log(web3.eth.gasPrice.toString());
console.log(web3.eth.getBalance("0x407d73d8a49eeb85d32cf465507dd71d507100c1", 45).toString());
console.log(web3.eth.getTransactionReceipt("0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b"));
The output will be of this form:
20000000000
30000000000
{
"transactionHash": "0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b ",
"transactionIndex": 0,
"blockHash": "0xef95f2f1ed3ca60b048b4bf67cde2195961e0bba6f70bcbea9a2c4e133e34b46",
"blockNumber": 3,
"contractAddress": "0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b",
"cumulativeGasUsed": 314159,
"gasUsed": 30234
}
Here is how the preceding method works:
- web3.eth.gasPrice(): Determines the gas price by the x latest blocks' median gas price.
- web3.ethgetBalance(): Returns the balance of any given address. All the hashes should be provided as hexadecimal strings to the web3.js APIs, not as hexadecimal literals. The input for the solidity address type should also be hexadecimal strings.
- web3.eth.getTransactionReceipt(): This is used to get details about a transaction using its hash. It returns a transaction receipt object if the transaction was found in the blockchain; otherwise, it returns null. The transaction receipt object contains the following properties:
- blockHash: The hash of the block where this transaction was
- blockNumber: The block number where this transaction was
- transactionHash: The hash of the transaction
- transactionIndex: The integer of the transactions' index position in the block
- from: The address of the sender
- to: The address of the receiver; null when it's a contract creation transaction
- cumulativeGasUsed: The total amount of gas used when this transaction was executed in the block
- gasUsed: The amount of gas used by this specific transaction alone
- contractAddress: The contract address created if the transaction was a contract creation; otherwise, null
- logs: The array of log objects that this transaction generated