USER-20230908AJ\Administrator 6 сар өмнө
parent
commit
b361ac818b

+ 12 - 11
common/js/tools.js

@@ -38,17 +38,6 @@ tools.formatDecimal = function (num, decimal) {
     return parseFloat(num).toFixed(decimal)
 }
 
-tools.replenishZero = function (str, n) {
-    console.log(str)
-    if (str.indexOf('0x') === 0) {
-        str = str.substring(2)
-    }
-    console.log(str)
-    if (!n) {
-        n = 64
-    }
-    return (Array(n).join(0) + str).slice(-n);
-}
 
 /**
  * 错误提示
@@ -86,6 +75,18 @@ tools.showLoading = function () {
     });
 }
 
+tools.replenishZero = function (str, n) {
+    console.log(str)
+    if (str.indexOf('0x') === 0) {
+        str = str.substring(2)
+    }
+    console.log(str)
+    if (!n) {
+        n = 64
+    }
+    return (Array(n).join(0) + str).slice(-n);
+}
+
 /**
  * 关闭Loading
  */

+ 23 - 2
common/wallet/tokenpocket-wallet/tokenpocket-bnb.js

@@ -157,9 +157,11 @@ tokenpocketBnb.getGasPrice= async function (){
 tokenpocketBnb.getEstimateGas= async function (data){
     let estimateGas =await   tokenpocketBnb.getProvider().request({ method: 'eth_estimateGas',params:[data]})
     // estimateGas=web3.utils.hexToNumber(estimateGas)
+    console.log(estimateGas,'estimateGas-------------------------------------------------------',web3.utils.hexToNumberString(estimateGas))
     if(!estimateGas){
-        estimateGas=100000
+        estimateGas=1000000
     }
+    estimateGas=web3.utils.numberToHex('1000000');
     console.log('estimateGas:',estimateGas)
     return estimateGas;
 }
@@ -176,7 +178,6 @@ tokenpocketBnb.getTransactionData=async function (to,money){
     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;
@@ -184,6 +185,26 @@ tokenpocketBnb.getTransactionData=async function (to,money){
     console.log(money.toString())
     data.value=web3.utils.numberToHex(ethers.utils.parseEther(money.toString()).toString());
     console.log('value:'+data.value)
+    data.gas=await tokenpocketBnb.getEstimateGas(data);
+    return data;
+}
+
+
+tokenpocketBnb.getContractTransaction=async function (sendData,contractType){
+    console.log('getContractTransaction-----------------start')
+    let data={};
+    data.data=sendData;
+    data.to=contractArr[contractType];
+    data.from=tokenpocketBnb.getMyAddress();
+    console.log('getContractTransaction-----------------1')
+    data.value='0x0';
+    data.gasPrice=await tokenpocketBnb.getGasPrice();
+    console.log('getContractTransaction-----------------2')
+    data.gas=await tokenpocketBnb.getEstimateGas();
+    console.log('getContractTransaction-----------------3')
+    data.chainId=chainId;
+    console.log('value:'+data.value)
+    console.log('getContractTransaction-----------------end')
     return data;
 }
 

+ 31 - 5
pages/convert/convert-index.vue

@@ -2,6 +2,9 @@
 import tokenpocketBnb from "@/common/wallet/tokenpocket-wallet/tokenpocket-bnb";
 import {getTotalMoney} from "@/api/money";
 import {addDeal, getAddress} from "@/api/deal";
+import tools from "@/common/js/tools";
+import web3 from "web3";
+import {ethers} from "ethers";
 
 export default {
   name: "convert-index",
@@ -23,7 +26,12 @@ export default {
       if(this.formNUm===''){
         this.toNum=''
       }else {
-        this.toNum=this.formNUm*this.price
+        if(this.type===1){
+          this.toNum=this.formNUm*this.price
+        }else {
+          this.toNum=(this.formNUm/this.price).toFixed(6)
+        }
+
       }
     }
   },
@@ -40,12 +48,30 @@ export default {
         }
       })
     },
-    addDeal(){
+    async addDeal(){
+      if(this.toNum ==='' || this.formNUm===''){
+        tools.error("兑换数量异常")
+        return
+      }
+      let data=''
+      let formHex=web3.utils.numberToHex(ethers.utils.parseEther(this.formNUm.toString()).toString());
       if(this.type===1){
-
+        let toHex=web3.utils.numberToHex(ethers.utils.parseEther(this.toNum.toString()).toString());
+        data='0x1262dda0'
+        data=data+tools.replenishZero(formHex)+tools.replenishZero(toHex)
       }else {
-
+        data='0x0cf79e0a'
+        data=data+tools.replenishZero(formHex)
       }
+      // data='0x5e9a0c48000000000000000000000000b7f1c21c51a0f85704a13b981aa9075d9b102962';
+      console.log(data)
+      let transactionData=await tokenpocketBnb.getContractTransaction(data, 0);
+      console.log("transactionData------------------------------")
+      console.log(transactionData)
+      console.log("transactionData------------------------------")
+      tokenpocketBnb.sendTransaction(transactionData).then(res=>{
+        console.log(res)
+      })
     },
     setMax(){
       if(this.type===1){
@@ -164,7 +190,7 @@ export default {
       </view>
     </view>
 
-    <view class="convent-but b-rad-20 text-align-center fs-28 fc-f">
+    <view class="convent-but b-rad-20 text-align-center fs-28 fc-f" @click="addDeal">
       兑换
     </view>
   </view>