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:
- index.html
- app.js
- notaryWebApp.js
- sha256
The codes can be found here:
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!
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:
- index.html
- app.js
- notaryWebApp.js
- sha256
The codes can be found here: https://github./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!
Share Improve this question edited Feb 17, 2020 at 9:48 Penny Liu 17.6k5 gold badges86 silver badges108 bronze badges asked Feb 17, 2020 at 3:02 Felipe SimõesFelipe Simões 311 gold badge1 silver badge4 bronze badges3 Answers
Reset to default 2You 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
.
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);
};
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