web3充值小记

import Web3 from 'web3'
if(window.ethereum){
window.web3 = new Web3(window.ethereum);
try{

window.ethereum.enable();

}catch(error){

console.error('awdawd')

}
} else if(window.web3){
window.web3 = new Web3(window.web3.currentProvider);
}else{
window.web3 = new Web3.providers.HttpProvider('http://本机IP测试')
}

//充值
try {

        await web3.eth.getAccounts((err,res) =>{
            let addr = res[0];
            web3.eth.sendTransaction({
              from: addr,
              to: '',//转账地址
              value: Web3.utils.toWei(/转账金额/, "ether")
            }).on('transactionHash', function(hash){
                console.log(hash,'-----hash-----')
            })
            .on('receipt', function(receipt){
              console.log(receipt,'-----receipt-----')
            }).on('confirmation', function(confirmationNumber, receipt){
              console.log(confirmationNumber, receipt,'-----confirmationNumber, receipt-----')
            }).on('error', console.error);
        })
        this.show2 = false
      } catch (error) {
        // User denied account access
      }