/** * bitkeep钱包的tron * @type {{}} */ import tools from "@/common/js/tools"; import {ethers} from "ethers"; import web3 from "web3"; let tokenpocketBnb = {} let chainId='' let provider; /** * 钱包初始校验 * @returns {*} */ tokenpocketBnb.getProvider= function (){ console.log(window.ethereum) console.log(window.ethereum.isTokenPocket) if (!window.ethereum || typeof window.ethereum.isTokenPocket === 'undefined') { return false } if(!provider){ provider = window.ethereum; } // alert(provider.isConnected()) console.log(chainId) if(!chainId){ provider.request({method:'eth_chainId'}).then((ethChainId)=>{ console.log(ethChainId) if(ethChainId!=='0x38'){ uni.$emit('noBan',false) }else { chainId=ethChainId } }) } return provider; } /** * 获取我登陆的地址 * @returns {any} */ tokenpocketBnb.getMyAddress=function () { return uni.getStorageSync('babAddress') } /** * 获取基本账户地址 * @returns {Promise} */ tokenpocketBnb.getAccounts=async function (){ return new Promise( (resolve, reject) => { try { tokenpocketBnb.getProvider().request({ method: 'eth_requestAccounts'}) .then((address)=>{ if(address.length>0){ uni.setStorageSync('babAddress',address[0]) resolve(address[0]) }else { resolve(false) } }) .catch((e)=>{ console.log(e) }) }catch (e) { reject(e) } }) } /** * 获取我的余额 * @param selectedAddress * @returns {Promise} */ tokenpocketBnb.getBalance=async function (selectedAddress) { if(!selectedAddress){ selectedAddress=tokenpocketBnb.getMyAddress(); } return new Promise( (resolve, reject) => { try { tokenpocketBnb.getProvider().request({ method: 'eth_getBalance',params:[selectedAddress,'latest']}) .then((balance)=>{ balance= ethers.utils.formatEther(balance.toString()) resolve(balance) }) .catch((e)=>{ resolve(0) }) }catch (e) { reject(e) } }) } /** * 获取gasPrice * @returns {Promise} */ tokenpocketBnb.getGasPrice= async function (){ let gasPrice =await tokenpocketBnb.getProvider().request({ method: 'eth_gasPrice',params:[]}) // gasPrice=web3.utils.hexToNumber(gasPrice) console.log('gasPrice:'+gasPrice) if(!gasPrice){ gasPrice='0x12a05f200' } return gasPrice; } /** * 获取燃料数量 * @param data * @returns {Promise} */ tokenpocketBnb.getEstimateGas= async function (data){ let estimateGas =await tokenpocketBnb.getProvider().request({ method: 'eth_estimateGas',params:[data]}) // estimateGas=web3.utils.hexToNumber(estimateGas) if(!estimateGas){ estimateGas=100000 } console.log('estimateGas:',estimateGas) return estimateGas; } /** * 转账接口 * @param to * @param money * @returns {{}} */ tokenpocketBnb.getTransactionData=async function (to,money){ let data={}; data.data='0x0'; data.to=to; data.from=tokenpocketBnb.getMyAddress(); data.gasPrice=await tokenpocketBnb.getGasPrice(); data.gas=await tokenpocketBnb.getEstimateGas(); data.chainId=chainId; if(tools.isDevelopment()){ money=0.00001; } console.log(money.toString()) data.value=web3.utils.numberToHex(ethers.utils.parseEther(money.toString()).toString()); console.log('value:'+data.value) return data; } /** * 转账 * @param data * @returns {Promise} */ tokenpocketBnb.sendTransaction=function (data){ return new Promise(async (resolve, reject) => { console.log(data) try { let txHash = await tokenpocketBnb.getProvider().request({ method: 'eth_sendTransaction', params: [data], }); console.log('txHash:'+txHash) if(txHash){ resolve(txHash) }else { reject(false) } }catch (e) { reject(false) } }) } export default tokenpocketBnb