Assignment - Event

index.js

var web3 = new Web3(Web3.givenProvider);

var instance;
var user;
var contractAddress = "0x13340FC39784874c1F3916204FB67AE306B71327";

$(document).ready(function() {
    window.ethereum.enable().then(function(accounts){
        instance = new web3.eth.Contract(abi, contractAddress, {from: accounts[0]})
        user = accounts[0];

        console.log(instance);

        instance.events.Birth().on('data', function(event) {
            console.log(event);
            let owner = event.returnValues.owner;
            let kittenId = event.returnValues.kittenId;
            let mumId = event.returnValues.mumId;
            let dadId = event.returnValues.dadId;
            let genes = event.returnValues.genes;
            $("#kittyCreation").css("display", "block");
            $("#kittyCreation").text("owner:" + owner
                                   +" kittyId:" + kittenId 
                                   +" mumId:"  + mumId 
                                   +" dadId:" + dadId
                                   +" genes:" + genes)

        })
        .on("error", console.error);

    })
})

function createKitten() {
    var dnaStr = getDna();
    instance.methods.createKittyGen0(dnaStr).send({}, function(error, txHash){
        if (error)
            console.log(error);
        else
            console.log(txHash);
    })
}
1 Like

Hi all,

I got a problem with Metamask.
Once I press on the create button, it shows the metamask transaction verification, but it won’t let me confirm the transaction.
I don’t know what I did wrong…
I’m guessing it has to do something with the Metamask I’m using…

Can someone help me?

Here’s also my index.js

// initialise web3 library
//givenProvider means that the library uses the provider that's being given. In this case Ganache, in other cases it could be Ropsten or even mainNet
var web3 = new Web3(Web3.givenProvider);

var instance;
var user;
var contractAddress = "0x9f123EfE1C21fDfF53fBeE6737cDa3DbEe522c58";

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

function createKitty() {
    var dnaStr = getDna();
    instance.methods.createKittyGen0(dnaStr).send({}, function(error, txHash){
        if(error){
            console.log(err);
        }
        else{
            console.log(txHash);
        }
    })
}

Have verify if the contract address is the same after the deployment of the contract? the error shows to be trying to execute a function in a non-contract address.

Carlos Z

I’ve checked it twice, and the address is the same. I feel like it has something to do with the metamask version i am using… don’t know if that’s the case. Could it be??

Not sure if this might be the problem, but you can try to reset the cache of metamask.

Carlos Z

Hi man,

Tried it on Brave and Chrome, but still the same.
I just uploaded the whole project on Github.
Can you check out what I did wrong?
I honestly don’t know what’s going on.
Kinda stuck rn

https://github.com/Silfred/NFT_MarketPlace_Phillip

2 Likes

Your repo might be private, I’m getting a “page not found” error, which might be because the repo is private.

Try to change it to public please :nerd_face:

It will be easier for me to download the repo and replicate the issue.

Carlos Z

1 Like

Thanks Carlos, It’s now public

1 Like

So, i have downloaded your repo, create a node project (npm init), then install truffle (npm install truffle -g).

The error from the image is “trying to call a function from a non-contract address”.

I’m not sure if you forgot to initialize your local blockchain using truffle, just need to type in the console truffle develop, you will initialize a local blockchain server that will give you some private keys that you can later use to import in metamask (at least the first private key which will be the deployer of the contract).

You can now do some of the truffle commands inside the truffle console.

Then, after i have added the truffle local blockchain and the private key of the deployer, I just have to deploy the contract in truffle using deploy, that way i deployed your contracts in the truffle local blockchain and it will return me the data about the deployment of each contract.

After changing just the address of the contract deployed on truffle in your index.js file, I refreshed your dapp, randomize a cat and click on “create me”, I was able to successfully trigger the function without the error on metamask.

Carlos Z

2 Likes

Ohh damn, it works with you…
I’ve tried using Ganache, think that that’s where the problem was.
Did I miss something if I wanted to use Ganache?

When I used develop on Ganache, the terminal said that It was a successful deployment.
I even looked on which account it was, and it was the same as the first address on Ganache.

You could use truffle instead of ganache, i switched to truffle because it gave me less issues than ganache.

Try to follow my steps for truffle, let me know if you need any assistance on process.

Carlos Z

After some time it finally worked.
Thanks!!

2 Likes

Hi. After implemnting the event and playing around a bit I noticed two things. First, the obvious, my Event doesn´t get displayed at all. Second, for some reason I can mint on and on without getting warning, that I hit my limit of 10 gen0 bears.

If someone finds the errors in my code I would be very happy. As for now i have to keep looking for what went wrong.
https://github.com/paulsimroth/nft_marketplace

Hey @PaulS96, hope you are ok.

It’s an issue on the contract or in the dapp? just to know what im exactly looking for, i can locally deploy your contract and try the dapp my self to help you solve that issue.

Carlos Z

2 Likes

Hey @thecil
While testing the Bearcontract in the console I had the desired effect. I could create BEars and after the limit was reached I got the error message. So my guess is, that I have an error somewhere in index.js as my frontend acts a bit weird. I had troubles getting Metamask to respond and also the birth event behaves incorrect.

Thanks in advance for the help

1 Like

I’m not sure if its only you, there is nothing wrong with your code, it should be working, after i have made some research, i believe it is related to a latest update on metamask, the window.ethereum.enable() method has been deprecated apparently (check the warnings from the console).

What you could do is to learn https://docs.ethers.io/v5/

Its a web3 library just like web3js, but more commonly used these days.

I tried to run my old repo too, compare yours with mine carefully to find the error, and its also failing like yours, and off course, i have not change it for a long time, it was working before.

I’m going to be offline for some days sadly, but I could drop an update for this when im back on how to migrate the project to ethersjs, is not that hard to be honest, try it by your self and let me know about your progress :nerd_face:

Carlos Z

1 Like

Thanks @thecil for the help.
I kind of guessed that this could be the issue. I will look into it and post an update here.

Hi thecil, how are you doing with this project “Cat Factory-Ethereum Dapp”? Some video instructions are missing to complete the project, could you get them? Let me know if you are interested in trying to solve it together.
Thank you!

VdSR