【发布时间】:2020-02-17 03:02:58
【问题描述】:
我在本地以太坊区块链上创建合约时遇到问题。我有一份在区块链上注册文档的基本合同。
所以,当我在 truffle 控制台中运行我的合约时,我可以完美地调用我的所有功能,但是,当我创建一个带有前端界面的网页时,我无法打开 metamask 来支付费用。
我的合约基本上有两个功能:addDoc 和 FindDoc。我做了一个使用 remix 网站创建交易的测试,它工作正常。在我的页面上,我仍然可以调用 findDoc 并获得正确信息的答案,但是当我尝试创建交易并支付费用时,元掩码不显示。
我的项目只有 4 个文件:
- index.html
- app.js
- notaryWebApp.js
- sha256
代码可以在这里找到: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);
}
});
};
从现在开始,谢谢大家!
【问题讨论】:
标签: javascript blockchain ethereum solidity metamask