본문 바로가기
  • 개발 / 공부 / 일상
BlockChain

(BlockChain) Web3 라이브러리 (ws)

by JJeongHyun 2023. 2. 15.
반응형
  • express와 web3 라이브러리 설치
    • npm i express web3
const express = require("express");
const Web3 = require("web3");
const app = express();
const web3 = new Web3("ws://localhost:8888");
  • geth에서 websocket 열기
    • --ws --ws.port 8888 --ws.origins "*" : ws에 대한 설정 추가
    • geth 연결 명령어
      • geth --datadir ~/myGeth --http --http.addr "0.0.0.0" --http.port 8080 -http.corsdomain "*" --http.api "admin,miner,txpool,web3,personal,eth,net" --allow.insecure-unlock --syncmode full --networkid 50 --ws --ws.port 8888 --ws.origins "*" console
       

geth 연결 명령어 후 Ubuntu 화면

 

  • web3.eth.subscribe
web3.eth.subscribe("newBlockHeaders", (error, result) => {
  if (!error) {
    console.log(result);
  }
});
app.listen(8000, () => {
  console.log("Server Opend");
});

node index로 Server Opend

  • 채굴 시작 후 ws 통신 확인
    • miner.setEtherbase(eth.accounts[0])
    • miner.start(1)
     

miner 설정
채굴 후 생성된 blockHeader console 결과 창