How can I find out the number of times an RSK transaction has been confirmed on the RSK blockchain?
Transactions do not have a confirmation count per se, but the blocks that they are a part of do indeed have confirmation counts. Thus, the solution lies in "comparing" the block number of a particular transaction.
Using curl
There are several ways to do this,
and the easiest method is the eth_getTransactionByHash
JSON-RPC method:
curl \
-X POST \
-H "Content-Type:application/json" \
--data '{"jsonrpc":"2.0","method":"eth_getTransactionByHash","params":["0xf1ebb8076ad289fbaef4406bb0488be0c5605a58cfa2a6d11540b1f9b0d7ef98"],"id":1}' \
https://public-node.testnet.rsk.co
The above cURL command is for transaction 0xf1ebb8076ad289fbaef4406bb0488be0c5605a58cfa2a6d11540b1f9b0d7ef98
on the RSK Testnet.
The response is copied below:
{
"result" : {
"input" : "0xcbf8...(redacted)",
"nonce" : "0xda62",
"blockNumber" : "0x17fe5c",
"gasPrice" : "0x3938700",
"hash" : "0xf1ebb8076ad289fbaef4406bb0488be0c5605a58cfa2a6d11540b1f9b0d7ef98",
"blockHash" : "0xede9aa2ff4939482186d4e6bd269582352a923db13ef90ad7def0d0dec17a239",
"r" : "0x8c98a16250d157db1fb11e1304684943796710e3f1292a4fb60a0711692f2b8f",
"value" : "0x0",
"s" : "0x49cdc35f66dbea2ba88e3c52dc3f4c68498b844dd79eebafc326803e7163f7fc",
"transactionIndex" : "0x0",
"gas" : "0x17c65",
"from" : "0xd761cc1ceb991631d431f6dde54f07828f2e61d2",
"to" : "0x8bf2f24afbb9dbe4f2a54fd72748fc797bb91f81",
"v" : "0x1c"
},
"jsonrpc" : "2.0",
"id" : 1
}
From "blockNumber" : "0x17fe5c"
we know that the block number of this particular block is 1572444
.
The next step is to compare this block number of this transaction to the latest block number. To do so, we need to use a different JSON-RPC request.
curl \
-X POST \
-H "Content-Type:application/json" \
--data '{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":1}' \
https://public-node.testnet.rsk.co
{
"id" : 1,
"result" : "0x180208",
"jsonrpc" : "2.0"
}
From "result" : "0x180208"
we know that the block number of the most recent block is 1573384
.
node -e "console.log(0x180208 - 0x17fe5c);"
940
We subtract the transaction's block number
from the latest block number,
which yields the answer:
940
in this case - the number of confirmations.
Using web3
You can do the same using web3 (different hash in the example, mainnet):
web3.eth.getTransaction("0x9117f2fab63c89676b6538a317d0b9ec74cc4ac8f375c80c0f2b57223cbd6bb2")
{ hash: '0x9117f2fab63c89676b6538a317d0b9ec74cc4ac8f375c80c0f2b57223cbd6bb2',
nonce: 118329,
blockHash: '0x02c40394a7ed66bc50a0f1853220a395efd1e3cfebea5e0ff36dd5a0a12b2aeb',
blockNumber: 3089723,
transactionIndex: 1,
from: '0x64DCC3BCbeaE8ce586CaBDef79104986bEAFcaD6',
to: '0xBEd51D83CC4676660e3fc3819dfAD8238549B975',
gas: 2000000,
gasPrice: '60000000',
value: '0',
input: '0x5a686699000000000000000000000000000000000000000000000000032d5284006bf8730000000000000000000000000000000000000000000000000000000060214e2a000000000000000000000000504efcadfb020d6bbaec8a5c5bb21453719d0e00',
v: '0x1b',
r: '0x2faaa315b1b3cd7421db1dc5fa243ddfae906282872c2bd9207e7e2dfed8286e',
s: '0x571fa5a28a48755bdf93aacd28d8d7d8986b1e2db0f5450e2355e7f3c91db30b' }
In this case, you get 3089723
from blockNumber
Now, you query the current best block:
web3.eth.getBlockNumber(console.log)
3089747
And, therefore, you have 3089747 - 3089723 = 24
confirmations.
I am not sure if my response will be the best, but this is an option do find it.
I usually search in the explorer.rsk.co
- First you can check the block number which your transaction was mined
- After you can view the last block mined.
- The difference between the block numbers is the number of confirmations
I hope it is useful to you :)
You can also do it with web3.js. As function
const getTxConfirmations = (txHash) => Promise.all([
web3.eth.getTransaction(txHash).then(tx => tx.blockNumber),
web3.eth.getBlockNumber()
]).then(([blockNumber, currentBlockNumber]) => (currentBlockNumber - blockNumber))
And with Truffle console:
truffle(develop)> web3.eth.getTransaction('0x7a28a121c41085ef52d449f64120dbc422ec70b4d324c076c8d89222cf7188c8').then(tx => tx.blockNumber)
1
truffle(develop)> web3.eth.getBlockNumber()
5
truffle(develop)> const getTxConfirmations = (txHash) => Promise.all([web3.eth.getTransaction(txHash).then(tx => tx.blockNumber), web3.eth.getBlockNumber()]).then(([blockNumber, currentBlockNumber]) => (currentBlockNumber - blockNumber))
undefined
truffle(develop)> getTxConfirmations('0x7a28a121c41085ef52d449f64120dbc422ec70b4d324c076c8d89222cf7188c8')
4