developer advocate at Chainstack here!
I just found this and thought I could add an update here in case someone will need this! 😁
When using web3.js
, make sure to install it and import it correctly.
Note that Ropsten and Rinkeby are considered deprecated. I recommend using Goerli instead. Check out this article about the merge where you can find which testnets are deprecated!
Install web3.js
Install it with npm
or Yarn
, you can reference the web3.js docs too.
NPM
npm install web3
Yarn
yarn add web3
Import web3.js in your script
Then import it in your script at the top.
const Web3 = require("web3");
eth_getTransaction example
Now you are ready to query the blockchain using web3.js
; the following script is an example using the eth_getTransaction
method.
const Web3 = require("web3");
const node_url = "CHAINSTACK_NODE_URL";
const web3 = new Web3(node_url);
web3.eth.getTransaction("0xf915903ecc67ab20a5162ae13eec36e3a68ca558765ada1779847e0a0c35479c", (err, tx) => {
console.log(tx)
})
this will return:
{
blockHash: '0xd233ac25d1bd643ce5170cd557e0206b7bd2ece3352fa5edaec3181ac0668fc4',
blockNumber: 14831527,
from: '0x31cA75bA3DB4A8A61304dFAa854Bd301Dbc3484d',
gas: 21000,
gasPrice: '82115541266',
maxFeePerGas: '115650024606',
maxPriorityFeePerGas: '1500000000',
hash: '0xf915903ecc67ab20a5162ae13eec36e3a68ca558765ada1779847e0a0c35479c',
input: '0x',
nonce: 41,
to: '0xd5E72C75C3d4bAFdb62Dee079d2bF9ef614125B6',
transactionIndex: 371,
value: '30000000000000000',
type: 2,
accessList: [],
chainId: '0x1',
v: '0x1',
r: '0x313004f2935447fc7d4584beecc3bafaf44231346f57c0b74b14770eddecfd98',
s: '0x7ad1056eff0c2d12bfb78b85776326a4d5c5ba07d598576e8c2073929b456861'
}
Find more code examples in the Ethereum API section of the Chainstack docs.
If you wish to try different node providers try Chainstack. We support many EMV-based protocols.
- Sign up with Chainstack.
- Deploy a node.
- View node access and credentials.
web3.eth.getTransaction(...)