0

我创建了小型以太坊 dapp。这是完美的网络浏览器。

http://slenex.com/slenwallet/ 在此处输入图像描述

但同样是在移动浏览器中加载但不与移动钱包交互 在此处输入图像描述

所以我在元掩码浏览器中尝试了,但应用程序没有加载。 在此处输入图像描述

这是我的代码:

我必须做些什么才能在移动元掩码浏览器中工作吗?

  <!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
        
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/web3/1.2.11/web3.min.js" integrity="sha512-6lf28FmolQdo4ap44cXw7j+thYEZzS1/kXxDkX1ppO//SHCageBS1HmYdqkkL819WfGtQ+7TE+uXEwsxjJXEKQ==" crossorigin="anonymous"></script>
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
        <link rel="stylesheet" type="text/css" href="index.css">
    </head>
    
    <body class="container"> 
    <h2 class="row col-lg-12" >PAY ETHER USING WALLET THROUGH SMART CONTRACT</h2><div>
    <div class="form-group row col-lg-12">
            <input class="col-lg-2 form-control" id="pay" type="text" placeholder="Ether to transfer"/>
            <button class="col-lg-1 btn btn-success" id="paySm">Pay</button></div>  
            <div class="alert alert-success success" role="alert"></div>            
    </div><hr/>
    
    <h3 class="row col-lg-12" >TRANSFER ETHER</h3><div>
    <div class="form-group row col-lg-12">
            <input class="col-lg-4 form-control" id="addr" type="text" placeholder="Address"/>
            <input class="col-lg-2 form-control" id="amt" type="text" placeholder="Ether to transfer"/>
            <button class="col-lg-1 btn btn-dark" id="transfer">Transfer</button></div>  
            <div class="alert alert-dark tx-success" role="alert"></div>            
    </div><hr/>
    
    <h3 class="row col-lg-12" >CONTRACT BALANCE </h3><div>
    <div class="form-group row col-lg-12">
            <button class="col-lg-2 btn btn-warning" id="getBal">Get Balance</button></div>  
            <div class="alert alert-warning bal-success"></div>         
    </div>
    
</body>

<script>
var instance;
var web3;
$(document).ready(function() {
    $(".success").hide(); $(".bal-success").hide(); $(".tx-success").hide();
  if (typeof web3 !== 'undefined') {  // alert("web3 detected");
    startApp(web3);
  }
function startApp(web3) {
    window.ethereum.enable();
    web3.eth.defaultAccount = web3.eth.accounts[0];
    console.log(web3.eth.defaultAccount)
    var rinkebyABI= JSON.parse('[{"inputs":[{"internalType":"address payable","name":"addr1","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"fundtransfer","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"payMe","outputs":[{"internalType":"bool","name":"success","type":"bool"}],"stateMutability":"payable","type":"function"},{"inputs":[],"name":"balance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"}]');
    EtherContract = web3.eth.contract(rinkebyABI);
    var rinkebyInstance = EtherContract.at('0xbB0592720Ed6116dfcda973099E4b484E8D616eB');
    
    // var localABI= JSON.parse('[ { "constant":false,"inputs":[{"name":"addr1","type":"address"},{"name":"amount","type":"uint256"}], "name":"fundtransfer","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false, "inputs":[],"name":"payMe","outputs":[{"name":"success","type":"bool"}],"payable":true,"stateMutability":"payable", "type":"function"},{"constant":true,"inputs":[],"name":"balance","outputs":[{"name":"","type":"uint256"}],"payable": false,"stateMutability":"view","type":"function"}]');
    // EtherContract = web3.eth.contract(localABI);
    // localInstance = EtherContract.at('0xf40D39645F676367D53819537f449A3228c13D5e');
    
    instance = rinkebyInstance;
    }
})

$(document).on("click", "#paySm", function(){
    var amt = $("#pay").val();
    var wei = parseInt(amt)*1e18;
    console.log("Amt: "+ amt);  
    console.log("Default Account: "+ web3.eth.defaultAccount); 
    console.log("Wei: "+ wei);  
    
    instance.payMe({from: web3.eth.defaultAccount, value: wei}, 
        function(err, transactionHash) {
            if (!err) console.log("Hash: "+ transactionHash);
            $(".success").text("Paid " + amt + " ether to contract");
            $(".success").show();
        })      
 });
 
 $(document).on("click", "#transfer", function(){
    var addr = $("#addr").val();
    var amt = $("#amt").val();
    var wei = parseInt(amt)*1e18;
    console.log("To Address: "+ addr); 
    console.log("Wei: "+ wei);
    
    instance.fundtransfer(addr,wei,{from: web3.eth.defaultAccount}, 
    function(err, transactionHash) {
        if (!err) console.log(transactionHash); 
        $(".tx-success").text("Paid " + amt + " ether to " + addr + " Address" );
        $(".tx-success").show();
    })   
 });
 
 $(document).on("click", "#getBal", function(){
    instance.balance({from: web3.eth.defaultAccount}, function(err, balance) {
        if (!err) console.log(balance); 
        $(".bal-success").text("Available balance in contract: " + balance/1e18 + " Ether");
        $(".bal-success").show();
    })   
 });

</script>
</html>

请帮助我。提前致谢..

4

1 回答 1

1

Metamask 移动应用程序要求网站具有有效的 ssl 证书。因此,请确保使用https://而不是http://在访问您的 dapp 时使用。请参考您的特定服务器配置以启用https://.

于 2021-01-09T00:09:57.163 回答