1

I want to deployed to deploy the following contract that has a constructor with the following arguments.

contract test{
    constructor(address _recipient, address _user){
      ...
    }
}

I want to deploy this using web3's sendrawtransaction

How do I go about making the raw TX to do so? I already have the signing key generated. Just not sure what to put in the "data" field of the TX and where to put in the arguments.

1 Answer 1

2

Try this (using web3.js v1):

let fs = require("fs");
let Web3 = require("web3");
let web3 = new Web3(NODE_ADDRESS);

async function send(transaction) {
    let gas = await transaction.estimateGas({from: ACCOUNT_ADDRESS});
    let options = {
        to  : transaction._parent._address,
        data: transaction.encodeABI(),
        gas : gas
    };
    let signedTransaction = await web3.eth.accounts.signTransaction(options, PRIVATE_KEY);
    return await web3.eth.sendSignedTransaction(signedTransaction.rawTransaction);
}

async function deploy(contractName, contractArgs) {
    let abi = fs.readFileSync(BINARY_DIR + contractName + ".abi").toString();
    let bin = fs.readFileSync(BINARY_DIR + contractName + ".bin").toString();
    let contract = new web3.eth.Contract(JSON.parse(abi));
    let handle = await send(contract.deploy({data: "0x" + bin, arguments: contractArgs}));
    console.log(`${contractName} contract deployed at address ${handle.contractAddress}`);
    return new web3.eth.Contract(JSON.parse(abi), handle.contractAddress);
}

Usage example:

let myContract = await deploy("MyContract", [arg1, arg2, arg3]);
...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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