이더리움 - Web.js

choko's avatar
Jun 29, 2024
이더리움 - Web.js

Web3.js tutorial

 
 
이더리움을 사용하여 블록체인 애플리케이션을 개발하는데는
  • 스마트 컨트랙트 개발
    • → solidity를 통한 개발
  • 블록체인과 상호작용하는 웹사이트 또는 클라이언트 개발
    • web3.js를 통해 이더리움과 상호작용하는 클라이언트를 개발할 수 있다.
    • 계정간의 이더 전송
    • 스마트 컨트랙트에서 데이터 R/W
    • web3.js → JSON RPC → EVM network
    •  
notion image
 

Install

npm install web3
 
  • Geth와 같은 자체 이더리움 노드를 실행할 수 있지만, 블록체인의 많은 데이터를 다운로드 하고 동기화를 유지하여야 한다.
    • 그래서, 편의를 위해 Infura를 통한 이더리옴 노드 엑세스를 한다.
 

Infura

  • 원격 이더리움 노드를 무료로 제공하는 서비스, 원격 이더리움에 접근할 수 있도록 엔드포인트 제공
  • 계좌 잔액 확인
const {Web3} = require('web3') async function getBalance(){ const rpcURL = "https://mainnet.infura.io/v3/6ece1a163c6642c89b7dae9ebec4af93" const web3 = new Web3(rpcURL); const address = "0x90e63c3d53E0Ea496845b7a03ec7548B70014A91" let balance=await web3.eth.getBalance(address); console.log(balance) } getBalance(); // result 0n
 
  • totalSupply, name, symbol, balanceof 확인
async function readContractData() { const address = "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07" const abi = [{"constant":true,"inputs":[],"name":"mintingFinished","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_value","type":"uint256"}],"name":"approve","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_from","type":"address"},{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transferFrom","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"unpause","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_amount","type":"uint256"}],"name":"mint","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"paused","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"}],"name":"balanceOf","outputs":[{"name":"balance","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"finishMinting","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"pause","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_amount","type":"uint256"},{"name":"_releaseTime","type":"uint256"}],"name":"mintTimelocked","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"},{"name":"_spender","type":"address"}],"name":"allowance","outputs":[{"name":"remaining","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"payable":false,"type":"function"},{"anonymous":false,"inputs":[{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Mint","type":"event"},{"anonymous":false,"inputs":[],"name":"MintFinished","type":"event"},{"anonymous":false,"inputs":[],"name":"Pause","type":"event"},{"anonymous":false,"inputs":[],"name":"Unpause","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"owner","type":"address"},{"indexed":true,"name":"spender","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Transfer","type":"event"}]; const contract = new web3.eth.Contract(abi, address) let totalSupply = await contract.methods.totalSupply().call((err, result) => { console.log(result) }) let name = await contract.methods.name().call((err, result) => { console.log(result) }) let symbol = await contract.methods.symbol().call((err, result) => { console.log(result) }) let balanceof = await contract.methods.balanceOf('0xd26114cd6EE289AccF82350c8d8487fedB8A0C07').call((err, result) => { console.log(result) }) console.log("Total Supply: ", totalSupply) console.log("Name: ", name) console.log("Symbol: ", symbol) console.log("Balance of: ", balanceof) } // result Total Supply: 140245398245132780789239631n Name: OMGToken Symbol: OMG Balance of: 30361262360212023356200n
 
  • 이더리움 블록체인 트랜잭션 작동 방식
    • 네트워크에 트랜잭션을 브로드캐스트 하려면, 먼저 서명을 해야 한다.
    • 원격 노드의 서명을 진행하기 위해, 다음 라이브러리를 설치한다.
      • npm install ethereumjs-tx
    • ropsten 테스트넷 address를 2개 생성한다.
    • async function createAddress() { const ropsten_web3 = new Web3('https://ropsten.infura.io/6ece1a163c6642c89b7dae9ebec4af93') let address = await ropsten_web3.eth.accounts.create() console.log(address) } // result (2번 실행) { address: '0x9375B6a317983c97f39BF525abC139ADC3972Fae', privateKey: '0x5c97327b1852d322cc73c26f6cb9ffbcfc493acd025ad4377f49108c504dca00', signTransaction: [Function: signTransaction], sign: [Function: sign], encrypt: [Function: encrypt] } { address: '0xD2eea25e6076d37A10615EE597D92010BD84D609', privateKey: '0xc3164799ad088ffa124f853c1c91471f1ab610dfec6d25256e96be8e5074cd0c', signTransaction: [Function: signTransaction], sign: [Function: sign], encrypt: [Function: encrypt] }
       
       
Share article

Tom의 TIL 정리방