简体   繁体   English

与 Metamask web3.js 交互

[英]Interacting with Metamask web3.js

I am facing a problem when I am creating a contract on my local ethereum blockchain.我在本地以太坊区块链上创建合同时遇到问题。 I have a basic contract to register a doc on blockchain.我有一份在区块链上注册文档的基本合同。
So, when a run my contract in truffle console I can call all my functions perfectly, but, when I create a webpage with a front end interface, I can't open metamask to pay a fee.所以,当我在 truffle 控制台中运行我的合约时,我可以完美地调用我的所有功能,但是,当我创建一个带有前端界面的网页时,我无法打开 metamask 来支付费用。
My contract has basicly 2 functions: addDoc and FindDoc.我的合约基本上有两个功能:addDoc 和 FindDoc。 I did a test creating a transaction using remix website and it worked normal.我做了一个使用 remix 网站创建交易的测试,它工作正常。 At my page, I still can call a findDoc and get answer with the correct informations, but when I try creating a transaction and pay a fee, the metamask doesn't show me.在我的页面上,我仍然可以调用 findDoc 并获得正确信息的答案,但是当我尝试创建交易并支付费用时,元掩码不会显示给我。


My project is just 4 files:我的项目只有 4 个文件:

  • index.html索引.html
  • app.js应用程序.js
  • notaryWebApp.js notaryWebApp.js
  • sha256 sha256

The codes can be found here: https://github.com/ffelipesimoes/solidity/tree/master/webapp代码可以在这里找到: https : //github.com/ffelipesimoes/solidity/tree/master/webapp

The main interact with blockchain is notaryWebApp.js file:与区块链的主要交互是 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);
        }
    });
};

since now, thank you all!从现在开始,谢谢大家!

You need to use window.ethereum and ethereum.enable() as described here , this is due to the privacy mode introduced in MetaMask recently.您需要按照此处所述使用window.ethereumethereum.enable() ,这是由于最近在 MetaMask 中引入的隐私模式

In your case, call await window.ethereum.enable() before notary_init() , and init the web3 instance with window.ethereum instead of currentProvider .在您的情况下,请在notary_init() await window.ethereum.enable()之前调用await window.ethereum.enable() ,并使用window.ethereum而不是currentProvider初始化 web3 实例。

Prefer using direct connection interface and predesigned scripts to solve this problem.最好使用直接连接接口和预先设计的脚本来解决这个问题。 You could find a package named Blockchain itself in pypi this could solve your problems and make sure that you are on the right folder ad=nd address您可以在 pypi 中找到一个名为 Blockchain 本身的包,这可以解决您的问题并确保您位于正确的文件夹 ad=nd 地址

thnks so much.非常感谢。 Worked that way:那样工作:

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);
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM