Felipe Simões
Felipe Simões

Reputation: 31

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.
My contract has basicly 2 functions: addDoc and FindDoc. I did a test creating a transaction using remix website and it worked normal. 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.


My project is just 4 files:

The codes can be found here: https://github.com/ffelipesimoes/solidity/tree/master/webapp

The main interact with blockchain is notaryWebApp.js file:

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!

Upvotes: 1

Views: 13461

Answers (3)

Felipe Simões
Felipe Simões

Reputation: 31

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

Upvotes: 1

William Chong
William Chong

Reputation: 2203

You need to use window.ethereum and ethereum.enable() as described here, this is due to the privacy mode introduced in MetaMask recently.

In your case, call await window.ethereum.enable() before notary_init(), and init the web3 instance with window.ethereum instead of currentProvider.

Upvotes: 2

Edu Dhruv
Edu Dhruv

Reputation: 1

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

Upvotes: 0

Related Questions