1

By signed transaction using web3 in nodejs

Transaction hash

0x3c7688326afc83e6524e8c491f203852e34aa0f572d952569b6253e98dbd83a1

Data

0xf9bcd5450000000000000000000000008d19c274d690f84efd4a443c21c4f4a1b77720ed000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000036162630000000000000000000000000000000000000000000000000000000000

Receipt

{"receipt":{"blockHash":"0x54f01d72226e3cd75d7b5c6952cac149dab47aabffbf5251d87f86613f223657","blockNumber":549010,"contractAddress":null,"cumulativeGasUsed":23384,"from":"0x0a889e242e6afaaaf51d9e40b2f089ab03889f97","gasUsed":23384,"logs":[],"logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","root":"0x4a0f79b6b74ce735aebfaa2e19ef066f500a6fa3051922fd193abe4219a158b8","to":"0x32ffcced3e2d7979cf7005fd8a9de681408f70dc","transactionHash":"0x3c7688326afc83e6524e8c491f203852e34aa0f572d952569b6253e98dbd83a1","transactionIndex":0}}

Using remix

Transaction hash

0x7b7c6006c078abaf10150e685f4bb7b755bf3de2e6baa29105128a7ef4f838f9

data

0xf9bcd5450000000000000000000000008d19c274d690f84efd4a443c21c4f4a1b77720ed000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000036162630000000000000000000000000000000000000000000000000000000000

Receipt

receipt={"receipt":{"blockHash":"0x8e365df7f4d07510b69a9f540cefc39c47ec117e7cfdd5516c15b92abc1d7488","blockNumber":549012,"contractAddress":null,"cumulativeGasUsed":99095,"from":"0x0a889e242e6afaaaf51d9e40b2f089ab03889f97","gasUsed":99095,"logs":[{"address":"0x32fFcceD3e2d7979cf7005Fd8A9DE681408F70Dc","topics":["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef","0x0000000000000000000000000a889e242e6afaaaf51d9e40b2f089ab03889f97","0x0000000000000000000000008d19c274d690f84efd4a443c21c4f4a1b77720ed"],"data":"0x000000000000000000000000000000000000000000000000000000000000012c","blockNumber":549012,"transactionHash":"0x7b7c6006c078abaf10150e685f4bb7b755bf3de2e6baa29105128a7ef4f838f9","transactionIndex":0,"blockHash":"0x8e365df7f4d07510b69a9f540cefc39c47ec117e7cfdd5516c15b92abc1d7488","logIndex":0,"removed":false,"id":"log_8e8a9b3e"}],"logsBloom":"0x00000000000000000000000000000000000080000000000000000000000000000000000000000400000000000000002000000000000000000000000000000000000000000000000000000008000000000000000000000000000000200000000000000000000000000000000004000000000000000000000000000010000008000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800000000000000000000","root":"0x156468c1bbde543ecae47841270c2037f4bc284df744e4a1ea7b5171bea1704e","to":"0x32ffcced3e2d7979cf7005fd8a9de681408f70dc","transactionHash":"0x7b7c6006c078abaf10150e685f4bb7b755bf3de2e6baa29105128a7ef4f838f9","transactionIndex":0}}

Solidity (function)

function createUser(address _address, string _name) onlyOwner public {
         User memory newUser;// for temporary data
         newUser.id=_address;
         newUser.name=_name;

         //check if exists
         users[newUser.id]=newUser;
         _totalUser++;

         //Send initial ether to that public key
         balances[_address] = balances[_address].add(NEW_ACCOUNT_TOKEN);
         _totalSupply=_totalSupply.add(NEW_ACCOUNT_TOKEN);

         emit Transfer(owner,_address , _totalSupply);
    }

Nodejs + web3js

const contract = new web3.eth.Contract(abi, process.env.CONTRACT_ADDRESS);
const data = contract.methods.createUser('0x40c6f8cda775e5fc26c3f7782518f146a1fdf746', 'abc').encodeABI();
var privateKey = Buffer.from(process.env.WALLET_PRIVATE_KEY, 'hex');

web3.eth.estimateGas({
    to: process.env.WALLET_ADDRESS,
    data: data
}).then((estimateGas) => {
    log('estimateGas: ' + estimateGas);

    web3.eth.getTransactionCount(process.env.WALLET_ADDRESS).then((nonce) => {
        log('nonce:' + nonce); //
        var rawTx = {
            nonce: nonce,
            from: process.env.WALLET_ADDRESS,
            to: process.env.CONTRACT_ADDRESS,
            gasPrice: web3.utils.toHex(web3.utils.toWei('2', 'gwei')),
            gas: estimateGas,
            data: data
        }

        var ethTx = new EthereumTx(rawTx);
        ethTx.sign(privateKey);

        var serializedTx = ethTx.serialize();

        web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'))
            .once('transactionHash', function (hash) {
                log('hash: ' + hash)
                res.json({
                    'status': true,
                    'msg': 'Successfully submitted',
                    'data': {
                        'tx': hash
                    }
                });
            })
            .once('receipt', function (receipt) {
                log('receipt for ' + receipt.transactionHash);
            })
            .on('error', function (error) {
                log(error);
            })
            .then(function (receipt) { // will be fired once the receipt is mined
                log(receipt.transactionHash + ' is mined'  );
                log('tx: ' + receipt.transactionHash + ' \n' + 'cumulativeGasUsed  : '  + receipt.cumulativeGasUsed
                    + ', blockNumber: ' + receipt.blockNumber +', blockHash:  '+ receipt.blockHash + ' \nlogs : ' + receipt.logs)
            });
    });
});

Both are singed transaction in same contract by same private key. In both case input parameters of function are same that's why data is same for both case. But Remix transaction is updating the internal state and web3 (using nodejs) is not. And transaction log is also missing for web3js (nodejs) transaction. Total user count is not changing from web3js+nodejs

What could be the possible reasons?

  • Can you share your solidity and JavaScript code? It will be helpful for reproducing the issue and understanding it better. – Soham Lawar Sep 30 '18 at 12:53
  • @SohamLawar udpated – sharif2008 Sep 30 '18 at 13:04
0

After receiving the receipt field the status field was undefined that is the transaction was not being mined due to low gas supplied. Finally the problem was solved after providing enough gas by default

 var rawTx = {
            nonce: nonce,
            from: process.env.WALLET_ADDRESS,
            to: process.env.CONTRACT_ADDRESS,
            gasPrice: web3.utils.toHex(web3.utils.toWei('2', 'gwei')),
            gas: 1500000,
            data: data
        }

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.