/** * bitkeep钱包的tron * @type {{}} */ import tools from "@/common/js/tools"; import {ethers} from "ethers"; import web3 from "web3"; let bitkeepLucky = {} let contractArr=[ '0x10186d85ac0579cb141ff37261f23cf4f1d254b5' ]; const chainId='0x3e6' let provider=null /** * 钱包初始校验 * @returns {*} */ bitkeepLucky.getProvider= function (){ console.log(window.bitkeep.ethereum) if(!provider){ provider = window.bitkeep && window.bitkeep.ethereum; if (!provider) { return window.open('https://bitkeep.com/en/download?type=2'); } } if(provider.chainId==='0x73'){ // window.bitkeep.ethereum.rpc.rpcUrl='http://luckychain.win/rpc/' provider.rpc.rpcUrl='http://luckychain.win/rpc/' }else { uni.$emit('noLucky',false) } // console.log(provider) return provider; } bitkeepLucky.test=function (){ provider=bitkeepLucky.getProvider() console.log(provider.isConnected()) } /** * 获取我登陆的地址 * @returns {any} */ bitkeepLucky.getMyAddress=function () { return uni.getStorageSync('luckyAddress') } /** * 获取基本账户地址 * @returns {Promise} */ bitkeepLucky.getAccounts=async function (){ return new Promise( (resolve, reject) => { try { bitkeepLucky.getProvider().request({ method: 'eth_requestAccounts'}) .then((address)=>{ if(address.length>0){ uni.setStorageSync('luckyAddress',address[0]) resolve(address[0]) }else { resolve(false) } }) .catch((e)=>{ console.log(e) }) }catch (e) { reject(e) } }) } /** * 获取我的余额 * @param selectedAddress * @returns {Promise} */ bitkeepLucky.getBalance=async function (selectedAddress) { if(!selectedAddress){ selectedAddress=bitkeepLucky.getMyAddress(); } console.log('selectedAddress:'+selectedAddress) // alert('钱包地址:'+selectedAddress) return new Promise( (resolve, reject) => { try { if(!selectedAddress){ resolve(0) }else { bitkeepLucky.getProvider().request({ method: 'eth_getBalance',params:[selectedAddress, "latest"]}) .then((balance)=>{ balance= ethers.utils.formatEther(balance.toString()) // alert(JSON.stringify(balance)) // console.log(balance) console.log('getBalance:'+balance) resolve(balance) }) .catch((e)=>{ resolve(0) }) } }catch (e) { // alert('error:'+JSON.stringify(e)) reject(e) } }) } /** * 获取合约数量 * @param selectedAddress * @param contractType * @returns {Promise} */ bitkeepLucky.getTokenBalance=async function (selectedAddress,contractType) { if(!contractType){ contractType=0 } if(!selectedAddress){ selectedAddress=bitkeepLucky.getMyAddress(); } let data='0x70a08231000000000000000000000000'+(selectedAddress.substring(2)) console.log(web3.utils.isHex(`0x70a08231`)) console.log(data) return new Promise( (resolve, reject) => { try { let data='0x70a08231000000000000000000000000'+(selectedAddress.substring(2)) bitkeepLucky.getProvider().request({method: 'eth_call',params:[{'to':contractArr[contractType],'data':data}, "latest"]}).then((balance)=>{ console.log('hexToNumber:-----------------'+balance) console.log(web3.utils.hexToNumber(balance)) balance= (web3.utils.hexToNumber(balance)/1000000).toString() if(balance<0.000001){ balance=0 } resolve(balance) }).catch((e)=>{ resolve(0) }) }catch (e) { // alert('error:'+JSON.stringify(e)) reject(e) } }) } /** * 获取合约信息 * @param contractType * @returns {Promise} */ bitkeepLucky.getContract=async function () { } /** * 合约转账 * @param to * @param money * @param contractType */ bitkeepLucky.getContractData=async function (to,money,contractType){ // console.log(ethers.utils.getAddress(to)) if(!contractType){ contractType=0 } let data={}; console.log('web3----------------------------------------') money*=1000000 money=web3.utils.numberToHex(money) money=tools.replenishZero(money) console.log(money) console.log(`transfer(${to},${money})`) console.log( web3.utils.sha3(`transfer(${to.substring(2)},${money})`)) // console.log(ethers.utils.parseEther(money).toString()) data.data='0xa9059cbb'+tools.replenishZero(to)+tools.replenishZero(money); // console.log(data.data) // console.log('0xa9059cbb'+(to.substring(2))) data.to=contractArr[contractType]; data.from=bitkeepLucky.getMyAddress(); data.gasPrice=await bitkeepLucky.getGasPrice(); data.gasLimit=await bitkeepLucky.getEstimateGas(); data.chainId=chainId; data.value= '0x0' return data; } /** * 获取gasPrice * @returns {Promise} */ bitkeepLucky.getGasPrice= async function (){ let gasPrice =await bitkeepLucky.getProvider().request({ method: 'eth_gasPrice',params:[]}) if(!gasPrice){ gasPrice='0x0' } return gasPrice; } /** * 获取燃料数量 * @param data * @returns {Promise} */ bitkeepLucky.getEstimateGas= async function (data){ let estimateGas =await bitkeepLucky.getProvider().request({ method: 'eth_estimateGas',params:[data]}) console.log('estimateGas:'+estimateGas) if(!estimateGas){ estimateGas=100000 } return estimateGas; } /** * 转账接口 * @param to * @param money * @returns {{}} */ bitkeepLucky.getTransactionData=async function (to,money){ let data={}; data.data='0x0'; data.to=to; data.from=bitkeepLucky.getMyAddress(); data.gasPrice=await bitkeepLucky.getGasPrice(); data.gasLimit=await bitkeepLucky.getEstimateGas(); data.chainId=chainId; if(tools.isDevelopment()){ money=0.001; } data.value= web3.utils.numberToHex(ethers.utils.parseEther(money.toString()).toString()); console.log('value:'+data.value) return data; } /** * 转账 * @param data * @returns {Promise} */ bitkeepLucky.sendTransaction=function (data){ return new Promise(async (resolve, reject) => { try { let txHash = await bitkeepLucky.getProvider().request({ method: 'eth_sendTransaction', params: [data], }); console.log('txHash:'+txHash) if(txHash){ resolve(txHash) }else { reject(false) } }catch (e) { reject(false) } }) } export default bitkeepLucky