Curl-ing the blockchain with JSONRPC and creating transactions

Here are some quick snippets that help when you want to get info from Blockchain.

  1. This will query the blockchain endpoint on a given address and give a current number of transactions made by that address. Which will be nonce of the address. Remember that nonce always starts with 0
curl https://go.getblock.io/58ccb1cf74404a4d9c25c6c81dea36b8 -X POST -H "Content-Type: application/json" --data '{"method":"eth_getTransactionCount","params":["0x14D580158D276bd2C3bBf02dC91276aFa19D2169", "latest"],"id":1,"jsonrpc":"2.0"}'


2. This will query the chain and give you transactions qued on a particular address, so if something is pending this should be listed here.

curl -X POST -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"txpool_content","params":[],"id":1}' https://go.getblock.io/58ccb1cf74404a4d9c25c6c81dea36b8 | jq '.result.queued | .["0x14D580158D276bd2C3bBf02dC91276aFa19D2169"]'


3. To do some read-only RPC calls on the chain you can use this snippet

curl -s -X POST -H 'Content-Type: application/json' --data '{"jsonrpc":"2.0","method":"eth_call","params":[ { "from": "<account_address>", "to": "<contract_address>", "data": "0x053f14da", "gas": "0x1e8480" }, "latest" ], "id":"1" }' http://geth-swap.private-localstorev2.testnet.internal


4. To do some writing on the chain, you would need to call this, however, it's important to note that using eth_sendTransaction requires the sender to be unlocked
(geth --unlock "0xYourAccountAddress") on the Ethereum node handling the request, which poses security risks.

curl -s -X POST -H 'Content-Type: application/json' --data '{"jsonrpc":"2.0","method":"eth_sendTransaction","params":[ { "from": "0x62cab2b3b55f341f10348720ca18063cdb779ad5", "to": "0x4370ed4d3a3ffb5be6b5ac7a9f0b781f24b092fa", "data": "0x053f14da", "gas": "0x1e8480" } ], "id":"1" }' http://geth-swap.ph4.testnet.internal

more secure way to do this would be to sign a message locally and send the data like the below

curl -X POST -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"eth_sendRawTransaction","params":["SIGNED_TRANSACTION_DATA"],"id":1}' https://rinkeby.infura.io/v3/YOUR_INFURA_PROJECT_ID


To get signed transaction data you should run the script locally with the private key

// Import ethers library
const { ethers } = require('ethers');

// Define provider using Infura. Replace YOUR_INFURA_PROJECT_ID with your actual Infura Project ID.
const provider = new ethers.providers.JsonRpcProvider('https://mainnet.infura.io/v3/YOUR_INFURA_PROJECT_ID');

// Define the signer's private key
// WARNING: Never hardcode your real private key in production code.
const senderPrivateKey = 'YOUR_PRIVATE_KEY'; // Replace YOUR_PRIVATE_KEY with the actual private key
const wallet = new ethers.Wallet(senderPrivateKey, provider);

// Define the transaction
async function main() {
  let nonce = await provider.getTransactionCount(wallet.address, "latest"); // Get current nonce
  const tx = {
    to: "0xRecipientAddress", // Replace 0xRecipientAddress with the recipient's address
    value: ethers.utils.parseEther("0.01"), // Sending 0.01 ETH
    nonce: nonce,
    // Ethers.js can automatically estimate gas limit and gas price, but you can specify them manually:
    // gasLimit: ethers.utils.hexlify(gasLimit), // Optional
    // gasPrice: ethers.utils.hexlify(gasPrice), // Optional
  };

  // Sign the transaction
  const signedTx = await wallet.signTransaction(tx);

  // Send the signed transaction
  console.log("Sending signed transaction...");
  const txResponse = await provider.sendTransaction(signedTx);
  console.log(`Transaction hash: ${txResponse.hash}`);

  // Wait for the transaction to be mined
  const receipt = await txResponse.wait();
  console.log(`Transaction was mined in block ${receipt.blockNumber}`);
}

main().catch(console.error);


This could be done in other ways as well, like with GO which is stated here

Subscribe to N00b21337
Receive the latest updates directly to your inbox.
Mint this entry as an NFT to add it to your collection.
Verification
This entry has been permanently stored onchain and signed by its creator.