web3操作metamask插件

web3 教程

https://web3.tryblockchain.org/web3-js-in-action.html

vue项目中使用web3 :  web3.js 使用教程第一篇(version:1.3.5)- 简单的API | 登链社区 | 深入浅出区块链技术

web3.js 使用教程第二篇(version:1.3.5)- 合约交互,发送ETH,发送ERC20代币 | 登链社区 | 深入浅出区块链技术

一、简单的调起metamask插件

 <!DOCTYPE html>
	<head>
		<title>测试调起metamask插件</title>
		
		<script>
		  
			async function login(){
				if (typeof window.ethereum !== 'undefined') {
					//监听钱包切换
					ethereum.on("accountsChanged", function(accounts) {
						console.log('钱包切换')
						window.location.reload();
					});
					//监听链网络改变
					ethereum.on("chainChanged",()=>{
						console.log('链切换')
						window.location.reload();
					});
					
				
					let addr=await ethereum.request({ method: 'eth_requestAccounts' });//授权连接钱包
					console.log('用户钱包地址:',addr[0]);
					
				
					
				}else{
					console.log('未安装钱包插件!');
				}
			}
			login();
			
			
			
		</script>
	</head>
	
	<body>
		
		
	</body>
</html>

猜你喜欢

转载自blog.csdn.net/wuhualong1314/article/details/122761780
今日推荐