Dapp Introduction

hey @filip, In my console when I create the instance it is not logged in the console. I am not getting any instance of the contract.
the code is the same as Filip and my Metamask is connected with the localhost. I couldn’t figure it out the problem.
var web3 = new Web3(Web3.givenProvider);
var contractInstance;

$(document).ready(function() {
window.eth_requestAccounts.then(function(accounts){
contractInstance = new web3.eth.Contract(abi, " 0xe987e450Ee116A637DCfae13Ed8e5a142FA00676", {from: accounts[0]});
console.log(contractInstance);
});

})

1 Like

Hey @Maki

Try the following

$(document).ready(function() {
    window.ethereum.enable().then(function(accounts){
      contractInstance = new web3.eth.Contract(abi, **your contract address**);

    });
});

happy coding,
Dani

1 Like

@filip

I am trying to use .on to show the recepit but its not showing up…
Can I have some advice? Below is my main.js code.

var web3 = new Web3(Web3.givenProvider);
var contractInstance;

$(document).ready(function(){
window.ethereum.enable().then(function(accounts){
contractInstance = new web3.eth.Contract(abi, “0xa6C432e167D52cD5b7E10E43e2130a99f7bCcDEA”,{from: accounts[0]});
console.log(contractInstance);
});
$("#add_data_button").click(inputData);
});

function inputData(){
var name = $("#name_input").val();
var age = $("#age_input").val();
var height = $("#height_input").val();
var config = {value: web3.utils.toWei(“1”,“ether”)}
contractInstance.methods.createPerson(name,age,height).send(config)
.on(‘transactionHash’, function(hash){
console.log(hash);
})
.on(‘confirmation’, function(confirmationNumber){
console.log(confirmationNumber);
})
.on (‘recepit’, function(recepit){
console.log(recepit);
})
};

1 Like

Thank you so much for posting this… I knew everything else was correct… It was simply the Cache… Empty Cache and Hard Reload worked for me! I would have been lost for hours and more importantly lost momentum. :heart: :+1: :hugs:

Did you see this option… it worked for me.

1 Like

And… the favicon error was fixed by putting the following line in the head of the index.html file:

<link rel="shortcut icon" href="#" />

Thank you everyone for the helpful links! :hugs:

Hey @Jun_0113

I think you’re not sending the tx.
web3.send () requires a sender account which is not provided. I think the google console will also tell warn you about it.
Add a sender account to your config object

1 Like

Hi,
Metamask is now asking for a chain ID when configuring your custom RPC server. This was optional before but it is now mandatory and I cannot save the network without it. It must match the chain ID returned by the network.
Do anybody have the same issue?
Anybody found out how to find the chain ID of ganache local network?

Chain ID is not required on chromium but it is on firefox.

Hey @filip,

Can you please help me understand how do I retrieve the contract instance if a user refreshes the page?

Let’s say I call connect metamask function ( window.ethereum.enable()) when a user clicks a button and not on componentDidMount . Once connected, I update all the data under respective states, for ex: instance, the address of wallet, network, etc. Now how do I retrieve all those back when a page refreshes? Now I know about state persistence in redux but I think there’s a much easier solution that i am not aware of. Also, I am already using React Context API, so not planning to replace it with redux.

Metamask (the given provider in your case) takes care of it.
Are you facing issues?

Hello
it doesn’t show the contract in console

i checked every thing.
this is the code:
var web3 = new Web3(Web3.givenProvider);
var contractInstance;

$(document).ready(function() {
window.ethereum.enable().then(function(accounts){
contractInstance = new web3.eth.Contract(abi,“0x79ADF13DCba784F5e14ea6AABF7026b781b1Bc9E”,{from:accounts[0]});
console.log(contractInstance);
});

});
can anybody help please?

Two things to check:

  1. Is your contract deployed and running on Ganache
  2. Verify the contract address
1 Like

Hey @sajede.k

Are you running a local server (python)?
If yes then please push your code (front and contracts) on github and send me the repo.

Cheers,
Dani

Hi! I am having the same issue. Did you manage to solve it?

Thank you
checked but still doesn’t work.
sth that I’m doubtful about is that when making ganache network in metamask, Chain ID was required (isn’t optional). what is it?
i copied the one that was for another local network i had in the list.

Thanks a lot
https://github.com/sajedeh-k/dapp.git
really kind of you :grinning:

1 Like

I entered some random chainID and get a error message that provides the network chainID. I then use the one provided in the error message. It seems to work.

Thank you! it worked for me too.

1 Like

As concerning Dapp intro(2 nd video) , would you please explain to me what you did exactly to open the Dapp page using python, because I installed python 3.9 and I opened windows Powershell and I wrote the same command but I got nothing, the Dapp web page did not open.

@filip

1 Like