Web 3 coding Assignment

Hello Everyone!

@Bhujanga

I was not sure where to put this. This is the assignment for this video : https://academy.ivanontech.com/products/ethereum-dapp-programming/categories/2147538595/posts/2147670622

So I am not sure if everything is correct or not because I got metamask popping up when I click on the createKittyFunction, but I will leave my code here and a screenshot just in case. What I suspect that it is something to do with the metamask account, but I am not really sure why the gas limit popping up, if I for example set it to 50000 and click on confirm the transaction is sent but it is not appearing on the console.

$("#CreateKitty").click(() =>{
  var dnaStr = getDna;

 instance.methods.createKittyGen0(dnaStr).send({}, function (error, txHash){
  if(error)
    console.log(error);
    else{
      console.log(txHash);
    }
  })
})

Screenshot here:

Screenshot

The errors I am getting are:

inpage.js:1 MetaMask - RPC Error: TxGasUtil - Trying to call a function on a non-contract address {code: -32603, message: "TxGasUtil - Trying to call a function on a non-contract address", data: {…}}
(anonymous) @ inpage.js:1
(anonymous) @ inpage.js:17
_runReturnHandlers @ inpage.js:17
_processRequest @ inpage.js:17
async function (async)
_processRequest @ inpage.js:17
_handle @ inpage.js:17
handle @ inpage.js:17
_rpcRequest @ inpage.js:1
sendAsync @ inpage.js:1
s.send @ web3.min.js:1
n @ web3.min.js:1
(anonymous) @ web3.min.js:1
u @ web3.min.js:1
(anonymous) @ web3.min.js:1
_handle @ inpage.js:17
async function (async)
_handle @ inpage.js:17
handle @ inpage.js:17
_rpcRequest @ inpage.js:1
sendAsync @ inpage.js:1
s.send @ web3.min.js:1
n @ web3.min.js:1
t @ web3.min.js:1
t @ web3.min.js:1
o._executeMethod @ web3.min.js:1
(anonymous) @ catSettings.js:190
dispatch @ jquery-3.4.1.js:1608
v.handle @ jquery-3.4.1.js:1570
catSettings.js:192 Error: TxGasUtil - Trying to call a function on a non-contract address
{
  "originalError": {
    "errorKey": "transactionErrorNoContract",
    "getCodeResponse": "0x"
  }
}
    at Object._fireError (web3.min.js:1)
    at u (web3.min.js:1)
    at web3.min.js:1
    at s._handle (inpage.js:17)
web3.min.js:1 Uncaught (in promise) Error: TxGasUtil - Trying to call a function on a non-contract address
{
  "originalError": {
    "errorKey": "transactionErrorNoContract",
    "getCodeResponse": "0x"
  }
}
    at Object._fireError (web3.min.js:1)
    at u (web3.min.js:1)
    at web3.min.js:1
    at s._handle (inpage.js:17)
1 Like

On the screenshot, it looks like your MetaMask is connected to Kovan Testnetwork, but your app is running on localhost:8000.
I guess you are using Ganache? then you have to connect your MetaMask to your local Ganache Instance…

1 Like

Hello @Riki I check your code and find a few things:

  1. The getDna is not a function where to get this DNA. Or if it is a function you should call it like
    getDna()
  2. Is what @Bhujanga is saying about you are using Kovan testnet instead localhost.
  3. Remember, the only address allowed to create kitties is the Owner address (the one that deploy the contract)

Hopes this help. Let me know if is going good

@Bhujanga @kenn.eth

Okay,

I have changed things as you requested.

Code here:

$("#CreateKitty").click(() =>{
    var dnaStr = getDna();
  
   instance.methods.createKittyGen0(dnaStr).send({}, function (error, txHash){
    if(error)
      console.log(error);
      else{
        console.log(txHash);
      }
    })
      Kittycontract.events.Birth({
      filter: {kittenId: [0,1], owner: "0x6Fe0d95e351D9d2041e7DDf31D8BC3cc74C60183"},

    }, function(error,event){ 
      if(error)
      console.log(error);
      else{
        console.log(event);
      }
       });

      $.on("KittyCreated", function(_mumId){
        console.log(_mumId);
      })

If I click on it I got the following thing again, and I do believe I am connected now to Ganache:

Error

So I am not sure if this is an issue but when Filip does it it automatically sets everything he just needs to click on the confirm button.

Regarding the contract address I am getting this error in Metamask: inpage.js:1 MetaMask - RPC Error: TxGasUtil - Trying to call a function on a non-contract address

I am leaving a screenshot also after I got the migration from the truffle develop that which address I am using in the index.js file

address I am using

the address is the Kittycontract.sol contract address which is: “0x28FC8F35feEdAA156fc9Db3c56E3fCe0F1bb6f98” and that is what I am using in the var ContractAddress in the index.js file.

What else can be wrong?

Are you using the owner address to query this function? Remember that this is a only owner function.

Hi Kenneth,

The ‘owner address’ is the 3rd account in my MetaMask wallet. Prior to linking my Ganache blockchain to MetaMask, I had two accounts in my metaMask wallet that are connected to the mainnet.

Once I created my Ganache local chain and linked it to this dApp, I imported the other accounts into my MetaMask wallet. The “owner” address is “Account 3.” However, I can’t get MetaMask to use this Account3 when I am interacting with the contract.

In fact, when I first load up the contract, MetaMask won’t connect to Ganache. I have to manually log in to the main net, or testnet first. Then, I manually select my “Ganache” network… Just seems like a mess

Yeah you have to change manually when you initialize your local blockchain. You have to choose the account you want to use and make sure it have enough funds and then reload the page. Remember that metamask have to connect your account with the contract.

Hello,

I seemed to have gotten past this… However, now I have a new problem…

Every time I truffle migrate --reset a new contract address is used upon deployment - as expected. I renew the python3 -m http.server 8000 and refresh the website.

These steps worked in the past and i could continue testing my dapp.

NOW, when I follow these steps and go to create a Kitty, I can’t successfully create the nft because the contract address is STUCK from a few deployments ago. I hope this makes sense… I don’t know if this is a problem with ganache, metamask… ?

Any idea on how to get the contract to start deploying again with the updated contract address from truffle migrate?

Hey @William, hope you are great.

Could you please show an screenshot about the error that is showed to you?

Also which local node are you using? ganache or truffle develop?
If it is ganache, i might advice you to try with truffle develop, some times ganache just freeze and bug your entire dev environment (causing some transactions not being executed or accepted properly).

Carlos Z

After I make modifications to my smart contract, should truffle compile and then commanding the python3 -m http.server 8000 to relaunch give me immediate results when I play around with the UI, etc?

I’m wondering if this is a Ganache thing and if this is normal for ganache to give developers trouble. Feels like i have to break down the entire blockchain and create a new one, each time I want to view my modifications on the localhost:8000 website

1 Like

Hey @William, hope you are well.

Sorry for the delay on the answer, now about ganache, at least for me has been really common that gave me any issue, it just freeze, stop working or something, so what i have made is to stop using ganache UI, the options are:

  • ganache-cli.
  • truffle develop (which a local blockchain node, same has ganache)
  • hardhat node ( which is the same than mentioned above)

Let me know if you need more help for this.

Carlos Z

1 Like

Hi Carlos, Thanks for the reply. I guess it’s good to hear i wasn’t going crazy.

I’ve been able to work through the obstacles… one more question:

The last two digits of the Kitty’s genes aren’t being picked up when the NFT is submitted. They remain at “0”

NFT

here is my GitHub repo if you don’t mind taking a look?
https://github.com/WLaMarche/NFT-Cats-Marketplace.git

Hey @William ! The issue here is in your getDna() function.


function getDna(){
    var dna = ''
    dna += $('#dnabody').html()
    dna += $('#dnaeyes').html()
    dna += $('#dnapupils').html()
    dna += $('#dnaears').html()
    dna += $('#dnaeartips').html()
    dna += $('#dnatummy').html()
    dna += $('#dnapaws').html()
    dna += $('#dnashadow').html()
    dna += $('#dnashape').html()
    dna += $('#dnadecoration').html()
    dna += $('#dnaanimation').html()
    //HERE
    return parseInt(dna)
}

Here you are returning parseInt(dna) wich is converting to INT number that dont have enough digits to fit the length or your DNA 18 digits. Int numbers are maximum 16 digits.

Solution is to return and use it as string not number. You can use it as string in contract functions.
So just dont convert it.
Hopes its help

@kenn.eth @Carlos

Hello again.

I have found this older post and I decided to reopen this one and not create a new one.

I am still stuck with this Transaction issue which comes from Metamask:

I am showing you the address I am using now:

But if I set the gas fee to 50000 and press confirm, metamask says that the transaction has been confirmed:

So I really dont know what is going on because then in the console I am not getting the transaction hash.

I have updated my code to Github page if you can look on it guys, meanwhile I will try somethings out but I have done everything this part as Philip suggested in the videos: https://github.com/Riki0923/CriptyKitties

And regarding the owner’s account. Isn’t that always accounts [0]in this contract?

Thanks for the assist as always.

Github Link:

1 Like

Hey @Riki, hope you are well.

Are you running your blockchain locally? You can also try to run the truffle local blockchain.

Just type in your console truffle develop and it will create a local blockchain for you, including a mnemonic seed for all the accounts of that blockchain, at the start of the message it will show the RPC, which you can use in metamask, instead of using ganache, some times it goes buggy (ganache).

Carlos Z

1 Like

Hello @thecil long time no see :slight_smile:

Okay I have done ask you asked and now this txGas error thing has disappeared and I got a new one, not sure how to solve it but meanwhile I will keep searching.

So I have set up the truffle test network and everything and when I would like to click on createKitty I get this error:

So I did not get this error using Ganache, and my createKittyGen0 function seems to work if I am trying out in truffle. Is this because this instance.methods are not working in a truffle testchain?

Okay I have found something I think this is the issue:

If I go to the methods section it does not show the createkitty, getkitty and kittygen0 functions.

Is this the reason why I am getting this error? Is this some kind of overflow stuck?

Regards,

Riki

Check your abi, apparently you have not updated it, it does not contain the cretate kitties functions, so then the dapp does not know what you are calling as a method (because the method is not defined in the abi)

https://github.com/Riki0923/CriptyKitties/blob/main/academy-kitties-template/client/abi.js

Carlos Z

Okay I have done this one and this error is gone now.

Now I am getting this when I would like to create a Kitty on the truffle network. It looks like is it still in connection with ganache but I am no longer using that?

Just in case I have deleted ganache, restarted windows, clear browsing cache etc… Now I am getting the same error again in turffle network:

Must Ganache need to be installed in order to run the truffle test network?

Riki

1 Like

I think its progress, you are triggering a revert, which one exactly? depends on the function your triggering, its always easier to put a error message require(msg.sender == owner, "Msg.sender is not the owner") for example.

Now keep in mind that you should just use 1 local blockchain, or it is from ganache, or truffle, boths using the same port ends in this kind of issues.

So you could just use stick with ganache if you to, but truffle could be easier at the end, just have to run truffle develop to start the node, import the first account private key into metamask (which will be the one that deployer of the contracts).

Then, in another terminal window, just use truffle compile and then truffle deploy --reset, which will deploy the contracts to the new local blockchain. Keep in mind that the contract will be deployed in a new address.

Also keep in mind the RPC from the truffle node, at the img in the linked reply days ago.

Carlos Z

Okay this helped a lot because the error is that the msg.sender is not the owner.

So how do you find out in truffle who is the owner then (msg.sender)? Shouldn’t be account[0] by default? Or is it the address(0) in this case?

1 Like