ethereum - 将结果形成元掩码
问题描述
我正在尝试使用来自表单的输入来创建元掩码交易的详细信息,获取表单地址和以太币数量,然后使用元掩码调用它们。当使用地址和数量进行硬编码时,元掩码代码可以工作。不知道我哪里错了。
运行 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。
推荐阅读
- php - Laravel 在另一个具有关系的表中的多个链接列?
- r - 用于读取多个 csv (tsv) 文件的 R 进度条
- r - R:修改一个以来自多个先前行的数据为条件的变量
- rest - VSTS 使用 API 在排队时设置构建参数
- android - Android Webview SwipeRefreshLayout 不允许向下滚动
- qt5 - Gumstix Overo SSD1306 OLED
- mysql - 仅检索单个记录的 Node.js MySQL 代码有什么问题
- elasticsearch - Elasticsearch hits.total 与 OR 不同
- r - 在 ggduo 散点图矩阵中显示相关指数
- angular - nouislider angular 2 格式工具提示