0

I have following contract and just want to update its state variable values i.e. totalSupply. But when I try to attempt this task through JavaScript code (given below) by calling its function i.e. 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 i.e. 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)
  })

  })

}

UPDATED: First i consumed all totalSupply (i.e. tokens) .. then i check its totalSupply and i got expected zero token. But, when i call setTotalSupply(10) and then check its values.. it still shows zero ... here is my checking code ...

var ot;

async function init() {
  ot = await contract1.methods.getTotalSupply().call();
  ot = ot.toString(10);
  console.log("Total Suplly is : ", ot);
  if(ot<1)
    console.log("OK")
  else 
    console.log("Bug !")
}
init();
  • What is the printout of your Javascript test? – goodvibration Nov 6 '19 at 14:21
  • sorry i could not got your point.. i didn't make any test... so what you want to know .. please specify ... – Amir Ali Nov 6 '19 at 14:32
  • Your script prints something, right? What is it? – goodvibration Nov 6 '19 at 14:38
  • when i write a code to check its values (after consuming all its supply) then it shows 0 ... But when i call setTotalSupply and set value 10. and then check again its value ... but still it zero ... var ot; async function init() { ot = await contract1.methods.getTotalSupply().call(); ot = ot.toString(10); console.log("Total Suplly is : ", ot); if(ot<1) console.log("OK") else console.log("Bug !") } init(); – Amir Ali Nov 6 '19 at 14:39
  • please see update... – Amir Ali Nov 6 '19 at 14:44
0

by making payable to my state variable and payable setTotalSupply will resolved this issue

| improve this answer | |

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.