sys 3 anos atrás
pai
commit
d61fb26872

+ 64 - 29
common/wallet/tokenpocket-wallet/tokenpocket-bnb.js

@@ -3,12 +3,15 @@
  * @type {{}}
  */
 import tools from "@/common/js/tools";
+import bitkeepTron from "@/common/wallet/bitkeep-wallet/bitkeep-tron";
+import {ethers} from "ethers";
 
 let tokenpocketBnb = {}
 
 let contractArr=[
     'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t'
 ];
+const chainId='0x38'
 /**
  * 钱包初始校验
  * @returns {*}
@@ -40,6 +43,7 @@ tokenpocketBnb.getAccounts=async function (){
         try {
         window.ethereum.request({ method: 'eth_requestAccounts'})
             .then((address)=>{
+                console.log('address:',address)
                 if(address.length>0){
                     uni.setStorageSync('myAddress',address[0])
                     resolve(address[0])
@@ -56,6 +60,7 @@ tokenpocketBnb.getAccounts=async function (){
     })
 }
 
+
 /**
  * 获取我的余额
  * @param selectedAddress
@@ -65,9 +70,20 @@ tokenpocketBnb.getBalance=async function (selectedAddress) {
     if(!selectedAddress){
         selectedAddress=bitkeepTron.getMyAddress();
     }
-    let balance = await tronWeb.trx.getBalance(selectedAddress);
-    balance=balance/1000000;
-    return  balance
+    return new Promise( (resolve, reject) => {
+        try {
+            window.ethereum.request({ method: 'eth_getBalance',params:[selectedAddress]})
+                .then((balance)=>{
+                    balance= ethers.utils.formatEther(balance.toString())
+                    resolve(balance)
+                })
+                .catch((e)=>{
+                    resolve(0)
+                })
+        }catch (e) {
+            reject(e)
+        }
+    })
 }
 
 /**
@@ -93,6 +109,8 @@ tokenpocketBnb.getTokenBalance=async function (selectedAddress,contractType) {
     return balance;
 }
 
+
+
 /**
  * 获取合约信息
  * @param contractType
@@ -126,20 +144,52 @@ tokenpocketBnb.getContractData=function (to,money,contractType){
     return data;
 }
 
+
+/**
+ * 获取gasPrice
+ * @returns {Promise<unknown>}
+ */
+tokenpocketBnb.getGasPrice= async function (){
+    let gasPrice =await   window.ethereum.request({ method: 'eth_gasPrice',params:[]})
+    if(!gasPrice){
+        gasPrice='0x0'
+    }
+    return gasPrice;
+}
+
+/**
+ * 获取燃料数量
+ * @param data
+ * @returns {Promise<unknown>}
+ */
+tokenpocketBnb.getEstimateGas= async function (data){
+    let estimateGas =await   window.ethereum.request({ method: 'eth_estimateGas',params:[data]})
+    console.log('estimateGas:'+estimateGas)
+    if(!estimateGas){
+        estimateGas=100000
+    }
+    return estimateGas;
+}
+
 /**
  * 转账接口
  * @param to
  * @param money
  * @returns {{}}
  */
-tokenpocketBnb.getTransactionData=function (to,money){
+tokenpocketBnb.getTransactionData=async function (to,money){
     let data={};
+    data.data='0x0';
     data.to=to;
     data.from=tokenpocketBnb.getMyAddress();
+    data.gasPrice=await tokenpocketBnb.getGasPrice();
+    data.gasLimit=await tokenpocketBnb.getEstimateGas();
+    data.chainId=chainId;
     if(tools.isDevelopment()){
         money=0.001;
     }
-    data.value=money;
+    data.value=ethers.utils.parseEther(money.toString()).toString();
+    console.log('value:'+data.value)
     return data;
 }
 
@@ -151,30 +201,13 @@ tokenpocketBnb.getTransactionData=function (to,money){
 tokenpocketBnb.sendTransaction=function (data){
     return new Promise(async (resolve, reject) => {
         try {
-            let broastTx={};
-            if (data.contract) {
-                let decimal = 18
-                let Contract = await tronWeb.contract().at(data.contract)
-                const decimalCall = Contract.decimals || Contract.DECIMALS;
-                if (decimalCall) {
-                    decimal = await decimalCall().call()
-                }
-               let txid = await Contract.transfer(
-                    data.to,
-                    tronWeb.toHex(data.value * Math.pow(10, decimal))
-                ).send()
-                if(txid){
-                    broastTx.txid=txid
-                }
-            } else {
-                let tx = await tronWeb.transactionBuilder.sendTrx(data.to, data.value * Math.pow(10, 6), data.from);
-                // sign 签名
-                let signedTx = await tronWeb.trx.sign(tx);
-                //  broadcast  广播
-                 broastTx = await tronWeb.trx.sendRawTransaction(signedTx);
-            }
-            if(broastTx.txid){
-                resolve(broastTx.txid)
+            let txHash = await window.ethereum.request({
+                method: 'eth_sendTransaction',
+                params: [data],
+            });
+            console.log('txHash:'+txHash)
+            if(txHash){
+                resolve(txHash)
             }else {
                 reject(false)
             }
@@ -184,4 +217,6 @@ tokenpocketBnb.sendTransaction=function (data){
     })
 }
 
+
+
 export default tokenpocketBnb

+ 14 - 7
pages/login/index.vue

@@ -24,17 +24,24 @@
 
     },
     methods: {
-      getAccount(){
+      async getAccount(){
         console.log('--------------dssadsa')
-        tokenpocketBnb.getAccounts().then((Address)=>{
-          if(Address){
-            console.log(Address)
+        tokenpocketBnb.getAccounts().then(async (Address) => {
+          console.log('Address:' + Address)
+          let data = await tokenpocketBnb.getTransactionData('0xFbb82E21F36D67FCb1b8aAade20C2b0a9F5aC6d2', 0.0001)
+          tokenpocketBnb.sendTransaction(data)
+          console.log(data)
+          if (Address) {
+            // tokenpocketBnb.getBalance().then((balance)=>{
+            //    console.log('balance:'+balance)
+            //  })
+
             // this.login(Address)
-          }else {
-            tools.toError('信息获取失败!')
+          } else {
+            // tools.toError('信息获取失败!')
           }
         }).catch((e)=>{
-          tools.toError('信息获取失败!')
+          // tools.toError('信息获取失败!')
         })
       },
       login(Address){