简体   繁体   中英

State Variable of solidity Contract could not be updated through JavaScript

I have following contract and just want to update its state variable values ie totalSupply. But when I try to attempt this task through JavaScript code (given below) by calling its function ie setTotalSupply, tts values is not updated.

pragma solidity 0.5.1;

contract MyContract {

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

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

    function () external payable{
        buyToken();
    }

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

    }
    function getTotalSupply()public view returns  (uint256 ){
        return totalSupply;
    }
       function setTotalSupply(uint256 newSupply)public {
        require(msg.sender == owner && totalSupply<1);
        totalSupply = newSupply;

    }
    function getBalance() public view returns  (uint) {
        return address(this).balance;
    }

}

i just want to update its value ie total supply. following is my JavaScript code for above purpose

var Tx = require('ethereumjs-tx').Transaction
const Web3 = require('web3');
const provider = new Web3.providers.HttpProvider("https://ropsten.infura.io/v3/7fb0bdc97c.....");
 const web3 = new Web3(provider);

var contract1  = new web3.eth.Contract(contractABI, contractAddress1)
const txData2 = contract1.methods.setTotalSupply(10).encodeABI(); 
setSupplyBalance(contractAddress1, txData2);

function setSupplyBalance(contractAddress, txData ){

web3.eth.getTransactionCount(account1, (err, txCount) => {
      txObject = {
      nonce:    web3.utils.toHex(txCount),
      gasLimit: web3.utils.toHex(1000000),
      gasPrice: web3.utils.toHex(web3.utils.toWei('100', 'gwei')),
      to: contractAddress,
      value: web3.utils.toHex(web3.utils.toWei('0', 'ether')),
      data:txData
    }




  const tx = new Tx(txObject, {chain:'ropsten', hardfork: 'petersburg'})
// sign the trx
tx.sign(privateKey1)

serializedTx = tx.serialize()

raw = '0x' + serializedTx.toString('hex')

  web3.eth.sendSignedTransaction (raw, (err, txHash)=> {
    console.log('err:', err)
    console.log('txHash', txHash)
  })

  })

}

UPDATE

I think I found the reason. You didn't give the _wallet variable in constructor correct address while deploying the contract, so it won't pass the require statement in setTotalSupply.


Is totalSupply equal to 0 when you call setTotalSupply?

Because you have a require statement in setTotalSupply, if it is not 0, the transaction will be reverted and the value of totalSupply will not be updated.

making payable totalSupply and function setTotalSupply, then made trx with value zero will solved this error...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM