Loading...
Interacting with blockcahin is all about transactions. Web3 module has many useful transaction functions which we can use to get all the information about any trnasaction happened on the network. This smartbook will discuss about only get/retrieve/call methods.
You can read this smartbook for all gas operations with web3.
Lets initialise web3 first
const web3 = require('web3');
This method returns a transaction details about any provided transaction hash.
Syntax : web3.eth.getTransaction(transactionHash [, callback])
Parameters :
Returns :
A transaction object is returned which contains following properties :
Example :
web3.eth.getTransaction(
'0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b§234'
).then(console.log);
Output :
> {
"hash": "0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b",
"nonce": 2,
"blockHash": "0xef95f2f1ed3ca60b048b4bf67cde2195961e0bba6f70bcbea9a2c4e133e34b46",
"blockNumber": 3,
"transactionIndex": 0,
"from": "0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b",
"to": "0x6295ee1b4f6dd65047762f924ecd367c17eabf8f",
"value": '123450000000000000',
"gas": 314159,
"gasPrice": '2000000000000',
"input": "0x57cb2fc4"
}
The receipt is available only for mined transactions i.e. confirmed transctions. Note that the receipt is not available for pending transactions and returns null.
Syntax : web3.eth.getTransactionReceipt(hash [, callback])
Parameters :
Returns :
A transaction receipt object is returned with following properties :
Example :
var receipt = web3.eth.getTransactionReceipt(
'0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b
').then(console.log);
Output :
> {
"status": true,
"transactionHash": "0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b",
"transactionIndex": 0,
"blockHash": "0xef95f2f1ed3ca60b048b4bf67cde2195961e0bba6f70bcbea9a2c4e133e34b46",
"blockNumber": 3,
"contractAddress": "0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe",
"cumulativeGasUsed": 314159,
"gasUsed": 30234,
"logs": [{
// logs as returned by getPastLogs, etc.
}, ...]
It returns a transaction based on a block hash or number and the transaction’s index position.
Syntax : getTransactionFromBlock(hashStringOrNumber, indexNumber [, callback])
Parameters :
Returns : A transaction object same as getTransaction method containing following properties
Example :
var transaction = web3.eth.getTransactionFromBlock('0x4534534534', 2).then(console.log);
Output :
> {
"hash": "0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b",
"nonce": 2,
"blockHash": "0xef95f2f1ed3ca60b048b4bf67cde2195961e0bba6f70bcbea9a2c4e133e34b46",
"blockNumber": 3,
"transactionIndex": 0,
"from": "0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b",
"to": "0x6295ee1b4f6dd65047762f924ecd367c17eabf8f",
"value": '123450000000000000',
"gas": 314159,
"gasPrice": '2000000000000',
"input": "0x57cb2fc4"
}
Returns a list of pending transactions of provided account.
Syntax : web3.eth.getPendingTransactions([, callback])
Parameters :
Returns : It returns Array of pending transactions with following properties :
Example :
web3.eth.getPendingTransactions().then(console.log);
Output :
> [
{
hash: '0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b',
nonce: 2,
blockHash: '0xef95f2f1ed3ca60b048b4bf67cde2195961e0bba6f70bcbea9a2c4e133e34b46',
blockNumber: 3,
transactionIndex: 0,
from: '0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b',
to: '0x6295ee1b4f6dd65047762f924ecd367c17eabf8f',
value: '123450000000000000',
gas: 314159,
gasPrice: '2000000000000',
input: '0x57cb2fc4'
v: '0x3d',
r: '0xaabc9ddafffb2ae0bac4107697547d22d9383667d9e97f5409dd6881ce08f13f',
s: '0x69e43116be8f842dcd4a0b2f760043737a59534430b762317db21d9ac8c5034'
},....,{
hash: '0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b',
nonce: 3,
blockHash: '0xef95f2f1ed3ca60b048b4bf67cde2195961e0bba6f70bcbea9a2c4e133e34b46',
blockNumber: 4,
transactionIndex: 0,
from: '0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b',
to: '0x6295ee1b4f6dd65047762f924ecd367c17eabf8f',
value: '123450000000000000',
gas: 314159,
gasPrice: '2000000000000',
input: '0x57cb2fc4'
v: '0x3d',
r: '0xaabc9ddafffb2ae0bac4107697547d22d9383667d9e97f5409dd6881ce08f13f',
s: '0x69e43116be8f842dcd4a0b2f760043737a59534430b762317db21d9ac8c5034'
}
]
Get the number of transactions sent from this address. This function can also help us get nonce as well.
Syntax : web3.eth.getTransactionCount(address [, defaultBlock] [, callback])
Parameters :
Returns : It returns the number of transactions sent from the given address.
Example :
web3.eth.getTransactionCount(
"0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe"
).then(console.log);
Output :
> 1
So these are transaction retrieval methods which web3 provides. We can also extend our web3 module as well, you can check how to extend it in this smartbook.