블록체인/Ethers & web3
TX hash로 부터 정보 뽑아내기 (blockNumber, gasUsed, blockTimestamp, status)
체인의정석
2024. 11. 21. 12:17
728x90
txhash로 부터 데이터를 뽑아낼 때에는 다음과 같이 뽑아내면 된다.
const getTxDataFromHash = async (provider, txhash) => {
const txReceipt = await provider.getTransactionReceipt(txhash);
const res = {};
res.blockNumber = Number(txReceipt.blockNumber);
res.gasUsed = Number(txReceipt.gasUsed);
const blockTimestamp = await provider.getBlock(Number(txReceipt.blockNumber));
res.blockTimestamp = blockTimestamp.timestamp;
const status = txReceipt.status === 1 ? "FINALIZED" : "FAILED";
res.status = status;
return res;
}
module.exports = {
getTxDataFromHash
}
provider의 경우 https의 엔드포인트로부터 다음과 같이 설정해주면 된다.
const provider = new ethers.JsonRpcProvider("endpoint");
728x90
반응형