on
S4: Web3.js로 InfuraApi를 사용해 데이터 읽기
S4: Web3.js로 InfuraApi를 사용해 데이터 읽기
Web3.js 설치
npm init npm install web3
InfuraApi로 Web3 jsonRPC사용 설정
// getBalance.js const Web3 = require('web3'); const rpcURL = "https://ropsten.infura.io/v3/{PROJECT_ID}"; // 원격 이더리움 노드에 접속할 수 있는 주소 const web3 = new Web3(rpcURL); // web3 객체 생성
잔액을 읽는 getBalance.js 생성해보기 ( 실행 node getBalance.js )
const account = "0xDd41dB13a6edc9eEce69174B0797eBcd707b574B"; web3.eth.getBalance(account) .then((bal) => { console.log("지갑 ${account}의 잔액은... ${bal} wei 입니다."); return web3.utils.fromWei(bal, "ether"); // web3.utils.fromWei 를 사용해 ether 단위로 변경 }) .then((eth) => { console.log(`이더 단위로는 ${eth} ETH 입니다.`); });
트랜잭션 정보를 읽는 getTransaction.js 생성
const txId = "{자신이 만든 트랜잭션의 해시값}"; web3.eth.getTransaction(txId) .then((obj) => { console.log(obj); });
from http://mybc.tistory.com/90 by ccl(A) rewrite - 2021-12-07 22:26:53