简体   繁体   English

State Solidity 合约变量无法通过 JavaScript 更新

[英]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.我有以下合同,只想更新其 state 变量值,即 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.但是,当我尝试通过 JavaScript 代码(如下所示)调用其 function 即 setTotalSupply 来尝试此任务时,tts 值不会更新。

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以下是我的 JavaScript 代码用于上述目的

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.部署合约时,您没有在构造函数中为_wallet变量提供正确的地址,因此它不会通过 setTotalSupply 中的 require 语句。


Is totalSupply equal to 0 when you call setTotalSupply?调用 setTotalSupply 时,totalSupply 是否等于 0?

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.因为你在 setTotalSupply 中有一条 require 语句,如果它不为 0,那么事务将被还原,totalSupply 的值不会被更新。

making payable totalSupply and function setTotalSupply, then made trx with value zero will solved this error...使应付totalSupply和function setTotalSupply,然后使值为零的trx将解决此错误...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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