0

I am using ganche for local ethereum. I have successfully deployed my contract through web3js on ganache. (The same contract also tested on Remix, all function properly running as per logic). Now i can see my contract on ganache. then i call some of my function (of smart contract) only is responsing , but the functions which can change the status of state variables, are not running. Here is my code ;

async function manCalFun() {
var myContAddr = '0x8175036782E9564C084eD1DA6C44C27150F24316'; // deployed locally on ganache

var myContractAbiDefenition = sources[0].abi;
var myContractInstance = new web3.eth.Contract(myContractAbiDefenition, myContAddr);
 res = await myContractInstance.methods['balances']("0x2cAe18ac37d85CccdE2ebB63D0c391Fe1ac7c36f").call({from: account1, value: web3.utils.toWei('0', 'ether')})
 res = await myContractInstance.methods['buyToken']().call({from: account1, value: web3.utils.toWei('2', 'ether')}) ..... this is not working 
 res = await myContractInstance.methods['setTotalSupply'](5).call({from: account1, value: web3.utils.toWei('0', 'ether')}) ... this is not working
 res = await myContractInstance.methods['getTotalSupply']().call({from: account1, value: web3.utils.toWei('0', 'ether')}).. this working properly
console.log("output: ", res)
} 

and here is ganache log (which shows that its taking my transaction, but do nothing, except for getTotalSupply function:

8:41:14 PM] eth_call
[8:42:06 PM] eth_call
[8:42:20 PM] eth_call

Also i want to know that whether is it necessary to send every transaction with signature (even on local ganache) ?. By the way i had deployed my contract through signing my transaction like this ;

const transaction = contract.deploy(params); 
const options = {
    data: transaction.encodeABI(),
    gas: await transaction.estimateGas({from: account1})
};

const signed = await web3.eth.accounts.signTransaction(options, privateKey1);
receipt = await web3.eth.sendSignedTransaction(signed.rawTransaction);

Source Code of Contract:

pragma solidity 0.5.12;

contract MyContract {

    uint256 public  totalSupply ; 
    mapping( address => uint256) public  balances ;
    address payable public owner;

    constructor(address payable _wallet) public payable {
        totalSupply = 6;
        owner = _wallet;
    }

    function () external payable{
        buyToken();
    }

    function buyToken() public payable {
        require(totalSupply >= (msg.value/1000000000000000000)*2);
        balances[msg.sender] += (msg.value/1000000000000000000)*2;
        totalSupply -=(msg.value/1000000000000000000)*2;

    }
    function getTotalSupply()public view returns  (uint256 ){
        return totalSupply;
    }
       function setTotalSupply(uint256 newSupply) public payable {
        totalSupply = newSupply;

    }

}
  • 1
    but the functions which can change the status of state variables, are not running - you are not calling any such function in the code that you've posted here!!! – goodvibration Nov 19 '19 at 13:39
  • i just comment it ..... then i try one by one ... – Amir Ali Nov 19 '19 at 13:47
  • 1
    For state-changing functions you should use send instead of call. – goodvibration Nov 19 '19 at 14:03
  • oh no..... how I was stupid... missing basic thing... thanks for your help... and say sorry for such silly task.... – Amir Ali Nov 19 '19 at 14:09
  • NP. Not silly at all! – goodvibration Nov 19 '19 at 15:05
0

But the functions which can change the status of state variables are not running.

In order to execute a state-changing function you should use send instead of call.

Is it necessary to send every transaction with signature (even on local ganache)?

You can skip that part if you unlock your account on the node that you're communicating with.

In Ganache, you can do this by passing your private key when you start the process, for example:

ganache-cli --account=0xYourPrivateKey,InitialBalanceInWei

However, please note that this is generally not a recommended working-mode outside of your testing environment, because anyone hacking your node will be able to exploit your account at will.

| improve this answer | |
  • but my accounts are not locked in ganche gui interface.... – Amir Ali Nov 19 '19 at 13:49
  • so is there any other reason ? – Amir Ali Nov 19 '19 at 13:54
  • @AmirAli: Then you can just call yourContract.methods.stateChangingFunc(arg1, arg2, arg3).send({from: yourAccountAddress, gas: ..., gasPrice: ...}). – goodvibration Nov 19 '19 at 14:00

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.