0

I don't think the web3.js 1.0 docs cover this.

I found this post. but the answer seems outdated. How to properly create a raw transaction and sign it using web3 in browser

I'd like to use web3.js 1.0 the future is now.

what i have so far:

var web3 = new Web3('https://ropsten.infura.io/<top secret>');
var abi = <abi things>
var address = <contract address>
var myContract = new web3.eth.Contract(abi,address);
myContract.methods.getmessage().call().then(console.log);
2
  • @Jaime you are right. but how else could i get a new answer to an old question? i think it deserves a 2018 web.js 1.0 answer.
    – swisswiss
    Commented May 3, 2018 at 13:38
  • I understand you. I changed the answer as to how to do this with web3 1.0, let me know if this solves your problem.
    – Jaime
    Commented May 3, 2018 at 14:28

1 Answer 1

0

You can do as follow in web3 1.0 as explained here

var Tx = require('ethereumjs-tx');
dataTx = myContract.methods.mymethod(params).encodeABI(); //The encoded ABI of the method

var rawTx = {
  to: 'yourcantract address', 
  data:dataTx 
}

var tx = new Tx(rawTx);
tx.sign('yourprivateKey');

var serializedTx = tx.serialize();
web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'))
.on('receipt', console.log);
5
  • buildTransaction is not a function. the web3.js 1.0 docs have no mention of it. i don't think its the same like in python(the link you pointed me to)
    – swisswiss
    Commented May 3, 2018 at 13:11
  • You are right, the code below is for web3py. I deleted that and I will updated it as soon as possible.
    – Jaime
    Commented May 3, 2018 at 13:13
  • Please check the updated answer and let me know.
    – Jaime
    Commented May 3, 2018 at 13:19
  • I get Uncaught ReferenceError: require is not defined for the first line in your snippet. Any help? Commented Jul 26, 2018 at 11:51
  • john, the issue is that you seem to be running this in a webpage and require is not a valid function of the client side, this assumes you are using a server. If you want to do this on the client side you should try something like this
    – Jaime
    Commented Jul 26, 2018 at 11:55

Not the answer you're looking for? Browse other questions tagged or ask your own question.