使用 Web3 为 Solidity Contract 提供资金时遇到麻烦

问题描述 投票:0回答:0

大家好,我在为我的 web3 合同融资时遇到了麻烦。

我在合约中有一个非常简单的 payable 函数,就是用来收钱的。

function makePayment() public payable returns (bool) {
    return true;
}

对于我的前端,我有一个非常基本的 html 表单,用于输入一些金额,然后是一个为合同提供资金的按钮。

这是我的 HTML:

    <form class="pure-form">
        <input id="name" type="text" placeholder="Fund Contract" />
        <button type="submit" class="sendMoney"></button>
    </form>      

    <script>
        var nameInput = document.getElementById('name');

        document.querySelector('form.pure-form').addEventListener('submit', function (e) {

        //prevent the normal submission of the form
        e.preventDefault();

        // console.log(nameInput.value);       
     });
     </script>

然后在我的 app.js 中我有一个发送钱的功能:

sendMoney: function() {
    // console.log(nameInput.value);       
    var practiceInstance;
    App.contracts.Practice.deployed().then(function (instance) {
        practiceInstance = instance;
        return practiceInstance.makePayment().send ({            
            from: web3.eth.accounts[1],
            value: web3.utils.toWei(nameInput.value, 'ether')
        });
    }).catch(function (err) {
        console.log(err.message);
    });
},

点击“sendMoney”按钮时将调用此 ^ 函数。

所以发生的事情是当我在应用程序中写入一定数量的以太币以发送到合约时,我收到一条日志消息说“无法读取未定义的属性'toWei'”

在我的元掩码中,它显示出于某种原因,0 以太被发送到合约:

javascript ethereum solidity smartcontracts web3js
© www.soinside.com 2019 - 2024. All rights reserved.