Ethereum 从结果到元掩码
我试图使用表单的输入为metamask事务创建详细信息,获取表单地址和以太金额,然后使用metamask调用它们。元掩码代码在使用地址和金额硬编码时有效。我不知道我会错在哪里 运行npx serve,因为元掩码可能很棘手 谢谢你的帮助 编辑-尝试了一些新东西,但仍然不起作用Ethereum 从结果到元掩码,ethereum,web3,cryptocurrency,metamask,Ethereum,Web3,Cryptocurrency,Metamask,我试图使用表单的输入为metamask事务创建详细信息,获取表单地址和以太金额,然后使用metamask调用它们。元掩码代码在使用地址和金额硬编码时有效。我不知道我会错在哪里 运行npx serve,因为元掩码可能很棘手 谢谢你的帮助 编辑-尝试了一些新东西,但仍然不起作用 <!DOCTYPE html> <html> <head> <script src="https://cdnjs.cloudflare.com/ajax/libs/jq
<!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>
支付
addEventListener('load',async()=>{
if(window.ethereum){
window.web3=新的web3(以太坊);
试一试{
等待以太坊。启用();
initPayButton()
}捕捉(错误){
$('#status').html('用户拒绝帐户访问',错误)
}
}else if(window.web3){
window.web3=新的web3(web3.currentProvider)
initPayButton()
}否则{
$('#status').html('未安装元掩码(或其他Web3提供程序)')
}
})
const initPayButton=()=>{
$('.pay按钮')。单击(()=>{
//paymentAddress是将资金发送到的地址
var paymentAddress=document.getElementById(“地址”).innerHTML
var amountth=document.getElementById(“金额”).innerHTML
web3.eth.sendTransaction({
收件人:paymentAddress,
值:web3.toWei(数量,'ether')
},(错误,事务ID)=>{
如果(错误){
console.log('支付失败',错误)
$('#status').html('支付失败')
}否则{
console.log('Payment successful',transactionId)
$('#status').html('支付成功')
}
})
})
}
函数fill_amount(){
var amountth=document.getElementById(“金额”).innerHTML
}
函数填充地址(){
var paymentAddress=document.getElementById(“地址”).innerHTML
}
在fill\u amount和fill\u address函数中,您使用的是相同的支付地址变量。我相信在fill_amount函数中,变量应该是amountth而不是paymentAddress