合同状态变量的更新导致交易失败

时间:2019-11-07 05:03:26

标签: javascript node.js ethereum solidity smartcontracts

我已将以下合同部署到ropsten网络中,可以进行一些交易。当我用完所有令牌(totalSupply)并确认时(显示totalSupply = 0)。确认后,我致电setTotalSupply。但是当我打电话给setTotalSupply(通过使trx的值为= 0,value = 100gwei或不带值参数..都在意...我的错误在下面给出;

pragma solidity 0.5.1;

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;
        // 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;
    }

}

这是我的.js文件

var Tx = require('ethereumjs-tx').Transaction
const Web3 = require('web3');
const provider = new Web3.providers.HttpProvider("https://ropsten.infura.io/v3/7fb0bdc97cbe419fbdf969.............");
const web3 = new Web3(provider);
const contractAddress1 = '0x66CfBc4C8fC163faf502715963C12216188D4Be1'
const contractABI = [{"constant":true,"inputs":[],"name":"getBalance","..............;
var contract1 = new web3.eth.Contract(contractABI, contractAddress1)
var tx ;
var serializedTx;
var raw ="raw";
var txObject;
const txData1 = contract1.methods.buyToken().encodeABI();
const txData2 = contract1.methods.setTotalSupply(6).encodeABI(); 
const abiData='0x60806040526040..............';
setTotalSupply(contractAddress1, txData2);

function setTotalSupply(contractAddress, txData){

web3.eth.getTransactionCount(account3, (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('100', 'gwei')),
data:txData
}
tx = new Tx(txObject, {chain:'ropsten', hardfork: 'petersburg'})
tx.sign(privateKey3)
serializedTx = tx.serialize()
raw = '0x' + serializedTx.toString('hex')
web3.eth.sendSignedTransaction (raw, (err, txHash)=> {
console.log('err:', err)
console.log('txHash', txHash)
})
})
}

错误:

err: null
txHash 0x730c7466c7681b7f214316c9e93f8bcfdc631260b13afddc91202d6c3854e66d
(node:200) UnhandledPromiseRejectionWarning: Error: Transaction has been reverte
d by the EVM:
{
  "blockHash": "0x4ad3a555e2bb32f4692f9c34bc803f8253387fcbba83ff194e9adf051b6741
9f",
  "blockNumber": 6726152,
  "contractAddress": null,
  "cumulativeGasUsed": 21442,
  "from": "0x7145a49329745209689b5dbbd1ed1906af158958",
  "gasUsed": 21442,
  "logs": [],
  "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000",
  "status": false,
  "to": "0x66cfbc4c8fc163faf502715963c12216188d4be1",
  "transactionHash": "0x730c7466c7681b7f214316c9e93f8bcfdc631260b13afddc91202d6c
3854e66d",
  "transactionIndex": 0
}
    at C:\Users\Jawad\node_modules\web3-core-method\src\index.js:364:46
    at process._tickCallback (internal/process/next_tick.js:68:7)
(node:200) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This e
rror originated either by throwing inside of an async function without a catch b
lock, or by rejecting a promise which was not handled with .catch(). (rejection
id: 1)
(node:200) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprec
ated. In the future, promise rejections that are not handled will terminate the
Node.js process with a non-zero exit code.

2 个答案:

答案 0 :(得分:2)

三种可能的情况:

  1. 您没有正确设置所有者。

  2. 当时的总供应量不为0

  3. 您将一笔价值不菲的交易发送给

答案 1 :(得分:0)

使应付款功能和变量成为可能。