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;
}
}
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:39send
instead ofcall
. – goodvibration Nov 19 '19 at 14:03