0
点赞
收藏
分享

微信扫一扫

web3js方法获取账户信息和余额


​​web3js文档​​

web3浏览器检查

if (typeof window.ethereum !== 'undefined') {
console.log('MetaMask is installed!');
}

获取账户信息

// 引入
//<script src="./node_modules/web3/dist/web3.min.js"></script>
async function get(){
// 实例化web3
window.web3 = new Web3(ethereum);
var web3 = window.web3;
// 请求用户授权 解决web3js无法直接唤起Meta Mask获取用户身份
const enable = await ethereum.enable();
console.log(enable,11)
// 授权获取账户
var accounts = await web3.eth.getAccounts();
// web3.eth.getAccounts().then((e)=>{console.log(e)})
// 取第一个账户
var myAccount = accounts[0];
console.log(myAccount, 1);
// 返回指定地址账户的余额
var balance = await web3.eth.getBalance(myAccount);
console.log(balance, 2)
}

web3js方法获取账户信息和余额_javascript

web3js方法获取账户信息和余额_区块链_02


举报

相关推荐

0 条评论