0

I want to be able to display the read functions of my contract on a UI.

These are the calls I make to my UI Those functions work perfectly in the console using node but when i try to call them from my UI it doesnt work

contract.methods.sellPrice().call((err, result) => {console.log(result)})       
contract.methods.buyPrice().call((err, result) => {console.log(result)})  
contract.methods.totalSupply().call((err, result) => {console.log(result)}) 

on my UI I use following code to connect to Web3 and to connect to the read functions, but this doesnt work I updated my Code to this

window.addEventListener('load', async () => {
    // Modern dapp browsers...
    if (window.ethereum) {
        window.web3 = new Web3(ethereum);
        try {
            await ethereum.enable();
            var accounts= await web3.eth.getAccounts();
            var option={from: accounts[0] };
            const  contract = new web3.eth.Contract(abi,contractAddress);
             const sellPrice = await contract.methods.sellPrice().call((err, result) => { console.log(result) })

        } catch (error) {
            // User denied account access...
            console.log('error');

        }
    }
    // Legacy dapp browsers...
    else if (window.web3) {
        //window.web3 = new Web3(web3.currentProvider);
        console.log(3);
        // Fallback to localhost; use dev console port by default...
        const provider = new Web3.providers.HttpProvider('https://ropsten.infura.io/v3/9bee77f147884c73bb2852e269dacece');
        web3 = new Web3(provider);
        // Acccounts always exposed
        web3.eth.sendTransaction({/* ... */});
    }
    // Non-dapp browsers...
    else {
        console.log('Non-Ethereum browser detected. You should consider trying MetaMask!');
    }
});
contract.methods.sellPrice().send(), function(err, result) {
    if(err) {
        console.log(err, 'err');
    } else {
        console.log(result, 'result');
    }
}

and why do i get an error : contract is not defined?

3
  • Since you're doing those calls from an async function, you can use await instead of psasing a callback function. For example, const sellPrice = await contract.methods.sellPrice().call();. Regardless of that, you shouldn't be getting "contract is not defined" anywhere in your program, so there's something here that you're not telling us (i.e., either a part of your code which is not present, or this is not the exact error message that you're getting). Jan 18, 2020 at 19:15
  • Come down please. If you want to get help here, then mind your manners to begin with. You might not be targeting the person who's trying to help you, but this attitude still isn't healthy if you want them to keep on trying. Second, refer to the comments you've been given, they might actually help you solve your problem. I wrote very explicitly that the exact error message contract is not defined doesn't look like something you could get on your code, so either this is not exactly your code, or this is not exactly the error message. Jan 18, 2020 at 19:52
  • 1
    BTW, after your edit to the question, it is obvious what you're doing wrong, and that is the fact that you're using contract outside the scope where it is declared. And just so you know, this problem has nothing to do with Ethereum, Web3 or anything related. It stems from a simple violation of the rules of Javascript! Jan 18, 2020 at 20:15

1 Answer 1

0

EDITED

Take a look at my code here, I'm able to request the sellPrice, buyPrice and totalSupply methods from your smart contract.

13
  • why do i need to send a transaction to get the sellPrice? And what parameters should I pass in there? It doesnt take any. And running it without the parameters and alll of that stuff i get again a contract undefined error
    – Coding Joe
    Jan 18, 2020 at 19:45
  • sellPrice, buyPrice and totalSupply all sound to me like contract methods that are NOT changing the state of the contract. Based on what have you determined otherwise??? This dude even wrote very explicitly at the very top of his question: "I want to be able to display the read functions of my contract"... Which pretty much implies that these functions are read-only. Jan 18, 2020 at 19:49
  • My bad, don't take my comment as a answer. I just looked at his contract and these 3 functions are view methods ( not changing the state ). ropsten.etherscan.io/address/… This is the code of his smart contract. @CodingJoe next time please attach the link to your smart contract. Jan 18, 2020 at 19:53
  • 1. No need to attach links. Just copy/paste the actual code (and only the part relevant to the question). 2. I don't quite understand what you mean by "don't take my comment as a answer". What comment??? The answer seems plain wrong, because it makes a false assumption about the problem (being related to calling non-constant functions). Jan 18, 2020 at 19:59
  • @CodingJoe edited my answer, please take a look at my code. #goodvibration That's what I just wanted to say man, I misunderstood the problem. Gosh it's not the of world, everyone makes mistakes. Go out for a fresh air : ) Jan 18, 2020 at 20:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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