0

I am trying to make a DAPP.

I followed this link -

https://medium.com/@takleakshar/how-to-build-a-decentralized-full-stack-app-in-ethereum-and-react-42e63d45a208

This is how my 'Setup.js' looks.

import Web3 from 'web3';

const web3 = new Web3(new 
Web3.providers.HttpProvider("http://localhost:8545"));

const marketplaceContract = new web3.eth.Contract( ABI , 'Smart 
Contract Address', {
from: '0x123....', // default from address
gasPrice: '20000000000' // default gas price in wei, 20 gwei in this 
case
});

//const marketplaceContract = 
web3.eth.contract(MarketABI).at(MarketAddress);
export { marketplaceContract };

As you can see, I am importing the smart contract with the name 'marketplaceContract'.

When I call a function from the smart contract using-

marketplaceContract.addGoods(name, user, age, brand, size, sp);

I get the output as

TypeError: _Setup__WEBPACK_IMPORTED_MODULE_5__.marketplaceContract.addAccounts is not a function

Although, addAccounts is a function.

I don't know if this is a problem with the version of web3. Please kindly help. Is there any other way to call the functions in my React-JS file?

PS - I don't know if I have to put the whole code here. If required, please tell me, I will put both the smart contract and the React file here. I didn't put it because it's very big. Please don't start scorning me.

EDITS

When I have a .call() function, it works well. But when I have a function that stores some data in a struct datatype in the Blockchain, it does not work. I used .send({from: accounts[0]}) for that function. Then the error pops up

Unhandled Rejection (TypeError): Cannot read property 'match' of undefined

I searched for this error. I deleted my package-lock.json file and node_modules and then did npm install. Still, I get the same error. How should I proceed?

1

Please check https://web3js.readthedocs.io/en/1.0/web3-eth-contract.html#id10 . You have to add .methods to your contract instance and then call the smart contract method.

  • Thanks a lot. It helped a lot!!! – Shailesh Mishra Jul 3 at 4:54
  • When I am using it for .call(), it's working absolutely fine. Then, I used .send(). I get the error as Unhandled Rejection (TypeError): Cannot read property 'match' of undefined. What can I do? Please help. – Shailesh Mishra Jul 3 at 10:42
  • You can read the difference between .call() and .send() here - web3js.readthedocs.io/en/1.0/… and web3js.readthedocs.io/en/1.0/… . Basically .call() is used in web3 to call constant methods and transaction creation is not needed (you don't have to sign anything with your private key and you won't be charged eth fee) meanwhile .send() is used when you want to change contract state (you have to create transaction and sign it with your private key). – Miroslav Nedelchev Jul 4 at 4:13
  • Thanks Miroslav. I read that up. I had to change some state in the smart contract but send() wasn't working because of truffle console. But, now it's sorted. – Shailesh Mishra Jul 4 at 5:12
0
getMinABI: function () {
    return [
        {
            "constant": true,
            "inputs": [{"name": "_owner", "type": "address"}],
            "name": "balanceOf",
            "outputs": [{"name": "balance", "type": "uint256"}],
            "type": "function"
        },
        {
            "constant": true,
            "inputs": [],
            "name": "decimals",
            "outputs": [{"name": "", "type": "uint8"}],
            "type": "function"
        },
        {
            "constant": false,
            "inputs": [{"name": "_to", "type": "address"}, {"name": "_value", "type": "uint256"}],
            "name": "transfer",
            "outputs": [{"name": "success", "type": "bool"}],
            "payable": false,
            "stateMutability": "nonpayable",
            "type": "function"
        }
    ];
}

let contract = new Web3.eth.Contract(this.getMinABI(), 'contract address', {
     from: 'your address'
});

contract.methods.balanceOf('your address').call().then((balance) => {
   console.log(balance)
});

Hope it help you

0

Answer to the EDIT part:

There's some issue with truffle console. If you call the function with truffle console running, then you will get the same error. Recompile and again migrate the smart contract. Run the DAPP again using npm start.

There we go. Everything runs smoothly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.