[英](dApp) My dapp doesn't interact with the MetaMask
App = {
web3Provider: null,
contracts: {},
init: function() {
$.getJSON('../real-estate.json', function(data) {
var list = $('#list');
var template = $('#template');
for (i = 0; i < data.length; i++) {
template.find('img').attr('src', data[i].picture);
template.find('.id').text(data[i].id);
template.find('.type').text(data[i].type);
template.find('.area').text(data[i].area);
template.find('.price').text(data[i].price);
list.append(template.html());
}
})
return App.initWeb3();
},
initWeb3: function() {
if (typeof web3 !== 'undefined') {
App.web3Provider = web3.currentProvider;
web3 = new Web3(web3.currentProvider);
} else {
App.web3Provider = new web3.providers.HttpProvider('http://localhost:8545');
web3 = new Web3(App.web3Provider);
}
return App.initContract();
},
initContract: function() {
// Contracts
$.getJSON('Contracts.json', function(data) {
App.contracts.Contracts = TruffleContract(data);
App.contracts.Contracts.setProvider(App.web3Provider);
})
},
buyRealEstate: function() {
var id = $('#id').val();
var name = $('#name').val();
var price = $('#price').val();
var age = $('#age').val();
web3.eth.getAccounts(function(error, accounts) {
if (error) {
console.log(error);
}
var account = accounts[0];
App.contracts.Contracts.deployed().then(function(instance) {
var nameUtf8Encoded = utf8.encode(name);
return instance.buyRealEstate(id, web3.toHex(nameUtf8Encoded), age, { from: account, value: price });
}).then(function() {
$('#name').val('');
$('#age').val('');
$('#buyModal').modal('hide');
return App.loadRealEstates();
}).catch(function(err) {
console.log(err.message);
});
});
},
loadRealEstates: function() {
},
listenToEvents: function() {
}
};
$(function() {
$(window).load(function() {
App.init();
});
$('#buyModal').on('show.bs.modal', function(e) {
var id = $(e.relatedTarget).parent().find('.id').text();
var price = web3.toWei(parseFloat($(e.relatedTarget).parent().find('.price').text() || 0), "ether");
$(e.currentTarget).find('#id').val(id);
$(e.currentTarget).find('#price').val(price);
});
});
这段代码是 app.js。
我目前正在研究 dapp,同时从事一个过去一段时间的项目。
我面临的问题是,当我通过我设置的代码按下按钮时,我必须与元掩码进行交互,但我不能。
我认为这是版本兼容性的问题。
有没有人解决过这个问题或知道如何解决?
我是 dapp 初学者。 我向所有人求求。 (˘・_・˘)在此处输入图片描述
这是我使用它的方式(松露项目)
const getWeb3 = () =>
new Promise((resolve, reject) => {
// Wait for loading completion to avoid race conditions with web3 injection timing.
window.addEventListener("load", async () => {
// Modern dapp browsers...
if (window.ethereum) {
const web3 = new Web3(window.ethereum);
try {
// Request account access if needed
await window.ethereum.enable();
// Accounts now exposed
resolve(web3);
} catch (error) {
reject(error);
}
}
// Legacy dapp browsers...
else if (window.web3) {
// Use Mist/MetaMask's provider.
const web3 = window.web3;
console.log("Injected web3 detected.");
resolve(web3);
}
// Fallback to localhost; use dev console port by default...
else {
const provider = new Web3.providers.HttpProvider(
"http://127.0.0.1:8545"
);
const web3 = new Web3(provider);
console.log("No web3 instance injected, using Local web3.");
resolve(web3);
}
});
});
并将 web 设置为:
const web3 = await getWeb3();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.