将结果形成元任务



我试图使用表单的输入来创建元任务事务的详细信息,获取表单地址和以太坊金额,然后用元任务调用它们。当使用地址和金额进行硬编码时,元任务代码可以工作。不确定我哪里错了。

运行npx-serve,因为元任务可能很棘手。

<!DOCTYPE html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
<div>
<input onclick="fill_amount" id="amount" type="number" placeholder="eth">
<input onclick="fill_address" id="address" placeholder="address">
<button class="pay-button">Pay</button>
<div id="status"></div>
</div>
<script type="text/javascript">
window.addEventListener('load', async () => {
if (window.ethereum) {
window.web3 = new Web3(ethereum);
try {
await ethereum.enable();
initPayButton()
} catch (err) {
$('#status').html('User denied account access', err)
}
} else if (window.web3) {
window.web3 = new Web3(web3.currentProvider)
initPayButton()
} else {
$('#status').html('No Metamask (or other Web3 Provider) installed')
}
})
const initPayButton = () => {
$('.pay-button').click(() => {
// paymentAddress is where funds will be send to
var paymentAddress = document.getElementById("address").innerHTML
var amountEth = document.getElementById("amount").innerHTML
web3.eth.sendTransaction({
to: paymentAddress,
value: web3.toWei(amountEth, 'ether')
}, (err, transactionId) => {
if  (err) {
console.log('Payment failed', err)
$('#status').html('Payment failed')
} else {
console.log('Payment successful', transactionId)
$('#status').html('Payment successful')
}
})
})
}
function fill_amount() {
var amountEth = document.getElementById("amount").innerHTML
}
function fill_address() {
var paymentAddress = document.getElementById("address").innerHTML
}
</script>
</body>
</html>

在fill_amount&fill_address函数使用相同的支付地址变量。我认为在fill_amount函数中,变量应该是amountEth,而不是paymentAddress。

相关内容

  • 没有找到相关文章

最新更新