1

I am trying to generate a transaction to deploy a smart contract on the chain and to sign it with web3js.

My problem is that I have no idea how to build the transaction object needed for the signTransaction function.

I first tried to create an object using the contract.deploy() function (without sending), but the transaction object needs a gas property, that the deploy() function does not create.

How do I create a transaction object that deploy a contract and how do I sign it with a private key?

5

Got it working with the following method: First, get the contract instance without specifying an address (according to documentation, it is optional):

let abi = JSON.parse(fs.readFileSync(tokenAbi).toString());
        let contractInstance = new web3g.eth.Contract(abi);

Then deploy the contract WITHOUT sending it and use the encodeABI function:

let deploy = newContract.deploy({
              data: bytecode,
              arguments: [my args here
}).encodeABI();

Build a transaction object using the deploy object and other properties as follow:

 let gas = parseInt(4000000).toString(16);
 let gasPrice = parseInt(4000000000).toString(16);
    let transactionObject = {
                     gas: gas,
                     gasPrice: gasPrice,
                     data: deploy,
                     from: req.body.sender_address
                                                };

Note: If you don't encode to hex, geth will return you an error saying it cannot parse from whatever to some Golang type

Then you ca nsign and send the signed transaction

     web3g.eth.accounts.signTransaction(transactionObject, req.body.private_key, function (error, signedTx) {
                         if (error) {
                         //do stuff
                } else {  
                      web3g.eth.sendSignedTransaction(signedTx.rawTransaction)
.on('confirmation', function (number) {//dostuff});
  • 1
    I get: TypeError: Cannot read property 'slice' of undefined at line of: web3g.eth.accounts.signTransaction , why? – kitsune Feb 18 at 11:22
0

web3js-raw node package provides set of helper functions which allow you to deploy a contract easily.

-2

All the answers to your question are answered in the documentation, its full of examples. https://web3js.readthedocs.io/en/1.0/web3.html

Here is an example I got from this resource https://ethereum.gitbooks.io/frontier-guide/content/contract_info.html

source = "contract test { function multiply(uint a) returns(uint d) { return a * 7; } }"
// compile with solc
 contract = eth.compile.solidity(source).test
// create contract object
 var MyContract = eth.contract(contract.info.abiDefinition)
// extracts info from contract, save the json serialisation in the given file, 
 contenthash = admin.saveInfo(contract.info, "~/dapps/shared/contracts/test/info.json")
// send off the contract to the blockchain
MyContract.new({from: primaryAccount, data: contract.code}, function(error, contract){
if(!error && contract.address) {
// calculates the content hash and registers it with the code hash in `HashReg`
// it uses address to send the transaction. 
// returns the content hash that we use to register a url
admin.register(primaryAccount, contract.address, contenthash)
// here you deploy ~/dapps/shared/contracts/test/info.json to a url
admin.registerUrl(primaryAccount, hash, url)
}
});
  • I understand that. But what I want is creating the transaction object that says "deploy the contract" but not deploying it yet. Use this tx object to sign it and then send it. But how do I hve to build the transaction object...? – Florian Castelain Jan 10 '18 at 10:17
  • I updated the answer – Lismore Jan 10 '18 at 11:45
  • I got it working on my side, will propose an edit to your answer tomorow, thanks for your time – Florian Castelain Jan 10 '18 at 11:52
  • I added a new answer as yours is not similar. – Florian Castelain Jan 11 '18 at 0:52

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.