0

Hey guys I am running into an issue deploying a smart contract with Web3 to the Ropsten network. The code is running seemingly correctly, and there is a contract address being created, but if I check the transaction hash on Etherscan it shows that there has been an error in executing the contract. I don't think it's the contract itself as I've tested a couple contracts and they both have the same error.

const fs = require("fs");
const solc = require('solc');
let Web3 = require('web3');
const Tx = require('ethereumjs-tx').Transaction;

const testnet = 'https://ropsten.infura.io/v3/APIKEY';

const address = '0x0764eDcCD0278Ee60C421148B8baB812306D90D5';

const privateKeyValue = 'PRIVATEKEY';

const privateKey = new Buffer(privateKeyValue, 'hex');

const web3 = new Web3(new Web3.providers.HttpProvider(testnet));

const input = fs.readFileSync('contract3.sol');
const output = solc.compile(input.toString(), 1);

const abi = JSON.parse(output.contracts[':ERC20Interface'].interface);

const contract = new web3.eth.Contract(abi);

const weiAmount = web3.utils.toWei('1', 'ether');

let data = contract.methods.transfer(address, weiAmount).encodeABI();

async function main(){
  const gasPrice = await web3.eth.getGasPrice();

  const gasPriceHex = await web3.utils.toHex(gasPrice);

  const gasLimitHex = await web3.utils.toHex(7957355);

  const nonce = await web3.eth.getTransactionCount(address);

  const nonceHex = await web3.utils.toHex(nonce);

  const rawTx = {
    // from: address,
    nonce: nonceHex,
    gasPrice: gasPriceHex,
    gasLimit: gasLimitHex,
    data: data,
    value: web3.utils.toHex("97534000000000000"),
    chainId: web3.utils.toHex(3),
  };

  const tx = new Tx(rawTx, {'chain':'ropsten'});
  tx.sign(privateKey);
  const serializedTx = tx.serialize();

  console.log(serializedTx + 'serialized tx');

  const promiseEvent = web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'));

  return promiseEvent
    .on('transactionHash', (txHash) => {
        console.log('TRANSACTION HASH');
        console.log(txHash);
        console.log('TRANSACTION HASH');
      }
    )
    .on('confirmation', (confirmNumber, receipt) => {
        console.log('CONFIRM NUMBER, RECEIPT');
        console.log(confirmNumber, receipt);
        console.log('CONFIRM NUMBER, RECEIPT');
      }
    )
    .on('error', (error => {
        console.log('ERROR');
        console.log(error);
        console.log('ERROR');
      })
    );

}

main();

Here's a link to the transaction hash: https://ropsten.etherscan.io/tx/0x8f703e3f87486baa60725186e9469d65f8355af94c8f9f43baae2d4aa5cb39b0

Versions are as follows: Web3 = 1.2.1 ethereumjs-tx = 2.1.0 solc = 0.4.25

Does anything seem out of sorts here? This is my first project trying to deploy a smart contract so I'm uncertain of the error and the tinkering I've been doing has been fruitless so far. Thanks!

  • I think your solc may need to be updated to something a bit fresher. Have you tried with 0.5.x? – fixanoid Sep 5 at 15:42
1

The transaction does not have a "to" so the EVM will try to deploy a new contract using the data field as bytecode.

You are sending in the data field the following:

 0xa9059cbb0000000000000000000000000764edccd0278ee60c421148b8bab812306d90d50000000000000000000000000000000000000000000000000de0b6b3a7640000

Now 0xa9 is not a valid opcode and the EVM will halt consuming all the reamining gas.

  • Thanks, how should I fix it? What should I add as a to value in the transaction? Thanks again. – Anthony Sep 5 at 18:00
  • If you want to deploy a contract 'data' has to be the contract's bytecode and 'to' has to be empty. When you want to execute a function from the contract 'to' is the contract address and 'data' is the result of encodeABI(). – Ismael Sep 5 at 18:04
  • Thanks I was able to get that up and running really quickly with your help whereas it could have taken me ages without it, thanks a lot! – Anthony Sep 5 at 23:06

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.