ethereumjs / ethereumjs-tx

Project is in active development and has been moved to the EthereumJS VM monorepo.
https://github.com/ethereumjs/ethereumjs-vm/tree/master/packages/tx
Mozilla Public License 2.0
779 stars 235 forks source link

Transaction failed upon updation of contract state variable #182

Closed saarshah closed 4 years ago

saarshah commented 4 years ago

I have deployed my following contract to ropsten network, making some transaction ok. when I consumed all my tokens (totalSupply) and confirmed it. After confirmation, i call setTotalSupply. But when i called setTotalSupply (through making trx with value =0, value=100gwei or without value parameter .. all are in vein... my error is given below;

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

}

and here is my .js file

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) }) }) } ERROR:

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.
holgerd77 commented 4 years ago

I think atm no one of the current maintainers will have time to debug and analyze such complex usage example beside everyday maintenance work TBH. Please report if you find a solution here, thanks!

alcuadrado commented 4 years ago

Hey @saarshah!

I almost sure that your problem is that you are sending value to a non-payable function. I noticed this because your transaction had some ETH. The only other things that can make it fail is if you try to set the supply with a different account from owner (not the case), or if the supply wasn't exhausted (you should check this).

Debugging contracts on live networks can be really hard, so my recommendation is to include every relevant scenario (like setting the supply after exhausting it) in your test suite and running those tests locally. I released this tool a few weeks ago that will be helpful for that.

This error isn't related to this library though. The transaction was created and signed correctly, and that's why it got included Ropsten's blockchain and appears on etherscan. The failure comes from the contract's code, so I'm closing this issue.

saarshah commented 4 years ago

yes making payable works thanks