본문 바로가기
블록체인

메타마스크 네트워크 추가 및 네트워크 변경 API

by 후린트 2022. 5. 13.
반응형

지갑 연결시 메타마스크를 사용할 경우 아래 API를 이용해서
네트워크 추가 및 변경에 대한 요청을 진행할 수 있다.

// https://docs.ethers.io/v5/
//ethers.js 라이브러리 사용 
<script src="https://cdn.ethers.io/lib/ethers-5.2.umd.min.js" type="application/javascript"></script>
<script type="text/javascript">
var provider = new ethers.providers.Web3Provider(window.ethereum);

//  baobab 주소 등록 
provider.send('wallet_addEthereumChain', [
{
	chainId: ethers.utils.hexValue(1001),
    chainName: "Klaytn Baobab",
    rpcUrls: ["https://kaikas.baobab.klaytn.net:8651/"],
    nativeCurrency: {
        name: "KLAY",
        symbol: "KLAY",
        decimals: 18,
    },
    blockExplorerUrls: ["https://baobab.scope.klaytn.com/"],
}]
);

// baobab 주소 변경
provider.send('wallet_switchEthereumChain', [{chainId : ethers.utils.hexValue(1001)}])
</script>


<script type="text/javascript">
// metamask api 사용 
window.ethereum.request( {
	"method":'wallet_switchEthereumChain', 
    "params": [{"chainId": '0x'+ (8217).toString(16)}]
 })
 
 </script>
반응형

'블록체인' 카테고리의 다른 글

클레이튼 Gas 가격이 변경됐다.  (0) 2022.03.03