我正面临一个问题,当我正在创建一个合同,我的当地埃瑟姆区块链。我有一份基本的合同来注册区块链上的医生。
因此,当在松露控制台中运行我的合同时,我可以完美地调用我的所有功能,但是,当我创建一个带有前端界面的网页时,我无法打开元请求支付费用。
我的合同有两个基本功能: addDoc和FindDoc。我做了一个测试,创建一个交易使用混合网站,它正常工作。在我的页面上,我仍然可以调用一个findDoc并获得正确的信息,但是当我尝试创建一个事务并支付费用时,元询问并没有显示给我。
我的项目只有4个文件:
代码可以在这里找到:https://github.com/ffelipesimoes/solidity/tree/master/webapp
与区块链的主要交互是notaryWebApp.js文件:
var contract = undefined;
var customProvider = undefined;
var address = "0x6A4494ed32ce0Ab8004fbEAdac534916f88C8d3E";
var abi = undefined;
function notary_init() {
// Check if Web3 has been injected by the browser (Mist/MetaMask)
if (typeof web3 !== 'undefined') {
// Use existing gateway
window.web3 = new Web3(web3.currentProvider);
} else {
alert("No Ethereum interface injected into browser. Read-only access");
}
//contract abi
abi = [...]
contract = new web3.eth.Contract(abi, address);
};
//sends a hash to the blockchain
function notary_send(hash, callback) {
web3.eth.getAccounts(function (error, accounts) {
contract.methods.addDocHash(hash).send({
from: accounts[0]
}, function (error, tx) {
if (error) callback(error, null);
else callback(null, tx);
});
});
};
//looks up a hash on the blockchain
function notary_find(hash, callback) {
contract.methods.findDocHash(hash).call(function (error, result) {
if (error) callback(error, null);
else {
let resultObj = {
mineTime: new Date(result[0] * 1000),
blockNumber: result[1]
}
callback(null, resultObj);
}
});
};从现在开始,谢谢大家!
发布于 2020-02-17 09:59:07
您需要像描述的那样使用window.ethereum和ethereum.enable(),这是由于最近在MetaMask中引入的隐私模式。
在您的示例中,在await window.ethereum.enable()之前调用notary_init(),并使用window.ethereum而不是currentProvider插入web3实例。
发布于 2020-02-18 02:48:42
有那么多。工作方式:
function notary_init() {
// Check if Web3 has been injected by the browser (Mist/MetaMask)
if (typeof web3 !== 'undefined') {
// Use existing gateway
window.web3 = new Web3(web3.currentProvider);
} else {
alert("No Ethereum interface injected into browser. Read-only access");
}
ethereum.enable()
.then(function (accounts) {
// You now have an array of accounts!
// Currently only ever one:
// ['0xFDEa65C8e26263F6d9A1B5de9555D2931A33b825']
})
.catch(function (error) {
// Handle error. Likely the user rejected the login
console.error(error)
})
//contract abi
abi =[...]
contract = new web3.eth.Contract(abi, address);
};发布于 2020-02-17 09:33:56
更喜欢使用直接连接接口和预先设计的脚本来解决这个问题。您可以在pypi中找到一个名为Blockchain的包,这可以解决您的问题,并确保您位于正确的文件夹ad=nd地址上。
https://stackoverflow.com/questions/60255403
复制相似问题