123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- /**
- * 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<unknown>}
- */
- 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<unknown>}
- */
- 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<unknown>}
- */
- 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<unknown>}
- */
- 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<unknown>}
- */
- 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
|