August 17, 2022

How to initialize Web3 library in Javascript

To use Web3, there are two ways to enter an RPC address or use a metamask.
Web3를 사용하려면 RPC 주소를 입력하거나 메타마스크를 이용하는 방법이 있다.

 

1. Using RPC Address (Polygon Network).
1. RPC 주소를 입력하는 방법(폴리곤 네트워크).

<script src="https://cdn.jsdelivr.net/npm/web3@1.7.3/dist/web3.min.js"></script>

<script>
const rpcPolygon = "https://polygon-rpc.com";
let web3 = "";
let contract1 = "";
async function main(){
    web3 = await new Web3(rpcPolygon);
    contract1 = await new web3.eth.Contract( [ABI], [Contract Address] );
} main();
</script>



2. Using metamask.
2. 메타마스크를 이용하는 방법.

<script src="https://cdn.jsdelivr.net/npm/web3@1.7.3/dist/web3.min.js"></script>

<script>
let web3 = "";
let contract1 = "";
async function main(){
    web3 = await new Web3(window.ethereum);
    contract1 = await new web3.eth.Contract( [ABI], [Contract Address] );
} main();
</script>