我想了解智能合约的前端是如何工作的。我正在尝试在我的计算机上运行此代码,但元掩码始终未定义。您能否详细解释为什么会发生这种情况。为什么它不连接到元掩码提供程序?
<script>
$(".sendButton").click(function(){
let Web3 = require('web3');
if (typeof web3 !== 'undefined'){
web3 = new Web3(web3.currentProvider);
}
else {
alert('You have to install MetaMask !');
}
const abi = some abi
const contractAddress = "some contract";
let MyContract = web3.eth.contract(abi);
let myContractInstance = MyContract.at(contractAddress);
let functionData = myContractInstance.setMessage.getData($('#inputString').val());
web3.eth.sendTransaction({
to:contractAddress,
from:web3.eth.accounts[0],
data: functionData,
},
function(error, response){
console.log(response);
});
});
</script>
</body>
</html>