Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Constructor parameters not being deployed on the blockchain #1118

Closed
sma1692 opened this issue Oct 16, 2017 · 8 comments
Closed

Constructor parameters not being deployed on the blockchain #1118

sma1692 opened this issue Oct 16, 2017 · 8 comments

Comments

@sma1692
Copy link

sma1692 commented Oct 16, 2017

I have written a js script wherein I am trying to deploy a contract from an externally owned account (the address does not belong to eth.accounts but is generated by a separate script). The contract gets deployed however the constructor parameters aren't set. Following is my code for the same. There is no issue with the nonce. I change it periodically.

const bytecode = "6060604052341561000f57600080fd5b60405160208061025a833981016040528080519150505b60008190555b505b61021d8061003d6000396000f3006060604052361561005f5763ffffffff7c010000000000000000000000000000000000000000000000000000000060003504166360fe47b181146100ae5780636d4ce63c146100d657806370a08231146100fb578063cd68100c14610139575b5b73ffffffffffffffffffffffffffffffffffffffff331660008181526002602052604090208054340190556001805473ffffffffffffffffffffffffffffffffffffffff191690911790555b005b34156100b957600080fd5b6100c4600435610175565b60405190815260200160405180910390f35b34156100e157600080fd5b6100c46101bc565b60405190815260200160405180910390f35b341561010657600080fd5b6100c473ffffffffffffffffffffffffffffffffffffffff600435166101c3565b60405190815260200160405180910390f35b341561014457600080fd5b61014c6101d5565b60405173ffffffffffffffffffffffffffffffffffffffff909116815260200160405180910390f35b60006007825b046000557f1f699cf30a85165a1f161e64b4b428f54e60dd974a70166b0816674c70d4f17b8260405190815260200160405180910390a1506000545b919050565b6000545b90565b60026020526000908152604090205481565b60015473ffffffffffffffffffffffffffffffffffffffff16815600a165627a7a72305820fa7e7d7492546dd3874aa83c17b50884c03f211cf239427926dc7fa167fdc8870029";
    const abi = JSON.parse("[{\"constant\":false,\"inputs\":[{\"name\":\"x\",\"type\":\"uint256\"}],\"name\":\"set\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"get\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"\",\"type\":\"address\"}],\"name\":\"balanceOf\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"lastAddress\",\"outputs\":[{\"name\":\"\",\"type\":\"address\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"name\":\"_data\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"constructor\"},{\"payable\":true,\"stateMutability\":\"payable\",\"type\":\"fallback\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":false,\"name\":\"x\",\"type\":\"uint256\"}],\"name\":\"Setting\",\"type\":\"event\"}]");
    var contract = new web3.eth.Contract(abi);
     const gasLimit = 923600;
     const hexdata = contract.deploy({
        data: '0x' + bytecode,
        arguments:[1234]
     }).encodeABI()
     const gasPrice = web3.eth.gasPrice;
    const gasPriceHex = web3.utils.toHex(18000000000);
    const gasLimitHex = web3.utils.toHex(gasLimit);
    const nonce = 8; // nonce i periodically keep changing
    const nonceHex = web3.utils.toHex(nonce);
    const rawTx = {
        nonce: nonceHex,
        gasPrice: gasPriceHex,
        gasLimit: gasLimitHex,
        data: "0x"+hexdata,
        from: creating_address
    };


    const tx = new Tx(rawTx);
    console.log('here1')
    tx.sign(priv);
    console.log('here')
    const serializedTx = tx.serialize();
    web3.eth.sendSignedTransaction('0x' + serializedTx.toString("hex"))
.on('receipt', console.log);

The contract gets deployed there is no issue with that but somehow the values aren't initialised. I tried a few versions, where I add the parameters and use encodeABI() after signing but nothing works. The version I am using is 1.0.0-beta.23. How do I resolve this?

@frozeman
Copy link
Contributor

frozeman commented Oct 19, 2017

@chriseth The encoded constructor arguments are as follows in web3.js data + args:

0x6060604052341561000f57600080fd5b60405160208061025a833981016040528080519150505b60008190555b505b61021d8061003d6000396000f3006060604052361561005f5763ffffffff7c010000000000000000000000000000000000000000000000000000000060003504166360fe47b181146100ae5780636d4ce63c146100d657806370a08231146100fb578063cd68100c14610139575b5b73ffffffffffffffffffffffffffffffffffffffff331660008181526002602052604090208054340190556001805473ffffffffffffffffffffffffffffffffffffffff191690911790555b005b34156100b957600080fd5b6100c4600435610175565b60405190815260200160405180910390f35b34156100e157600080fd5b6100c46101bc565b60405190815260200160405180910390f35b341561010657600080fd5b6100c473ffffffffffffffffffffffffffffffffffffffff600435166101c3565b60405190815260200160405180910390f35b341561014457600080fd5b61014c6101d5565b60405173ffffffffffffffffffffffffffffffffffffffff909116815260200160405180910390f35b60006007825b046000557f1f699cf30a85165a1f161e64b4b428f54e60dd974a70166b0816674c70d4f17b8260405190815260200160405180910390a1506000545b919050565b6000545b90565b60026020526000908152604090205481565b60015473ffffffffffffffffffffffffffffffffffffffff16815600a165627a7a72305820fa7e7d7492546dd3874aa83c17b50884c03f211cf239427926dc7fa167fdc887002900000000000000000000000000000000000000000000000000000000000004d2

Is this correct?

@chriseth
Copy link
Contributor

@frozeman hard to tell, but it looks ok, I mean it is just a single uint, right? I'm pretty sure the error is elsewhere. @sma1692 do you have a transaction hash and source code? Did you try a debugger?

@Pris17
Copy link

Pris17 commented Oct 26, 2017

@frozeman @sma1692 @chriseth Even I am facing the same issue. I am unable to retrieve constructor params from contract after contract is deployed.

@sma1692
Copy link
Author

sma1692 commented Oct 27, 2017

@frozeman @chriseth @Pris17 It worked after I downgraded to a lower version. 1.0.0-beta.19 to be precise. The constructor parameters were initialised on the deployed contract. There must be some issue with beta.23.
I can run the code again with beta23 to get the tx hash. @chriseth By source code you mean the solidity source code?

@pubkey
Copy link

pubkey commented Feb 24, 2018

Looks like this is fixed now. I tried it and everything worked as expected on 1.0.0-beta.30. Can be closed?

@sma1692
Copy link
Author

sma1692 commented Feb 25, 2018

Yes, it worked in the version mentioned above.

@sathyamlokare
Copy link

@pubkey @sma1692 @chriseth @Pris17 @frozeman

var Web3 = require('web3');
var web3 = new Web3(new Web3.providers.HttpProvider("https://rinkeby.infura.io/"));

var Tx = require('ethereumjs-tx');
const testHashCoinAdvanced = require('./build/TokenERC20.json');

var privateKey = new Buffer('f6da78c45601d9ae02fcdb161e44a15a99e5e1fb1aa931dd95366add5dec367e', 'hex')


const deploy = async()=>{
  // testCoinRinkeby = new web3.eth.Contract(JSON.parse(testHashCoinAdvanced[':TokenERC20'].interface), null, {
  //             data: testHashCoinAdvanced[':TokenERC20'].bytecode,
  //         });
  //
  // testCoinContractDeploy = await testCoinRinkeby.deploy({
  //             arguments: [ 1000,"HashCoin","HC" ]
  //         });


           var contract = new web3.eth.Contract(JSON.parse(testHashCoinAdvanced[':TokenERC20'].interface));
          const hexdata = contract.deploy({
              data: '0x' + testHashCoinAdvanced[':TokenERC20'].bytecode,
              arguments:[1000,"HashCoin","HC"]
           }).encodeABI()

           const nonce = await web3.eth.getTransactionCount( "0x7Fb11F6A52e20BDFfaa08c468cD4848b901b7B70" );
           const nonceHex = web3.utils.toHex(nonce)
// const estimatedGas = await testCoinContractDeploy.estimateGas();
const gasPriceHex = web3.utils.toHex(18000000000);
const gasLimit = 923600;
const gasLimitHex = web3.utils.toHex(gasLimit);
  var rawTx = {
      nonce: nonceHex,
      gasPrice: gasPriceHex,
      gasLimit: gasLimitHex,
      data: "0x"+hexdata,
      from:'0x7Fb11F6A52e20BDFfaa08c468cD4848b901b7B70'
  }

  var tx = new Tx(rawTx);
  tx.sign(privateKey);

  var serializedTx = tx.serialize();

  console.log(serializedTx.toString('hex')); // f86180825208825208948005ceb675d2ff8c989cc95354438b9fab56868101801ca096e0cb2e633f07a7ee1f761dba1109c18a44550692305c03c72403ffa0b8fc12a012482fd916fa0a05396fadbf13b39619193e9f80dd5a0fd32086257cc3a11796

  web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'))
      .on('receipt', console.log);

};
deploy();

It gets deployed here , but it says TxReciept Status failed, can anyone tell me whats the issue here ?

here are the transactions
https://rinkeby.etherscan.io/address/0x7fb11f6a52e20bdffaa08c468cd4848b901b7b70

@nivida
Copy link
Contributor

nivida commented Aug 9, 2018

@sathyamlokare Please ask your question on https://ethereum.stackexchange.com/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants