How do I pass arguments to a contract-constructor with web3Js (>=version 1.0)
How do I pass arguments to a contract-constructor with web3Js (>=version 1.0)
Loading saved threads...
hardfork · External communityPost link
External question — Ethereum Stack Exchange
Author: hardfork
Original post: https://ethereum.stackexchange.com/questions/41757
License: CC BY-SA 3.0 — https://creativecommons.org/licenses/by-sa/3.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
I was on
https://www.myetherwallet.com/#contracts
and wanted to deploy a contract.
It worked with a contract with a constructor without arguments, but my contract constructor looks like this:
function CrowdFunding(address _arg1, uint256 _arg2, uint256 _arg3) public {
arg1 = _arg1;
arg2 = _arg2;
arg3 = _arg3;
}
But how do I pass my arguments (arg1, arg2 and arg3)? I'm using web3JS.
Quote
Report
hardfork · External communityPost link
External answer — Ethereum Stack Exchange
Author: hardfork
Original post: https://ethereum.stackexchange.com/a/41771
License: CC BY-SA 3.0 — https://creativecommons.org/licenses/by-sa/3.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
If you are using
a newer version of web3
(version 1.0.0 for example):
// example solidity code
function ContractName(address _arg1, uint256 _arg2, uint256 _arg3) public {
arg1 = _arg1;
arg2 = _arg2;
arg3 = _arg3;
}
Use
web3.eth.abi.encodeParameter()
or
web3.eth.abi.encodeParameters()
to encode your parameters and concatenate them at the end of your bytecode.
const Web3 = require("web3");
const solc = require("solc");
// compile the solidity code
let compiled = solc.compile(source);
// save public interface of contract
let abi = JSON.parse(compiled.contracts[":ContractName"].interface)
// create var with contract
let CrowdFunding = new web3.eth.Contract(abi);
let bytecodeWithParameters = compiled.contracts[':ContractName'].bytecode + web3.eth.abi.encodeParameters(['address', 'uint256', 'uint256'], ['0x08cf02070bb9f167556c677da58e6678bbe871fc', '100000000000000000', '10000']).slice(2);
// slice(2) because we want to remove the '0x' at the beginning.
Now, you can enter
bytecodeWithParameters
on
https://www.myetherwallet.com/#contracts
to deploy (with arguments).
Quote
Report
goodvibration · External communityPost link
External answer — Ethereum Stack Exchange
Author: goodvibration
Original post: https://ethereum.stackexchange.com/a/57587
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
Try this:
let fs = require("fs");
let Web3 = require("web3");
let web3 = new Web3(NODE_ADDRESS);
async function send(transaction) {
let gas = await transaction.estimateGas({from: PUBLIC_KEY});
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(contractName + ".abi").toString();
let bin = fs.readFileSync(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 crowdFunding = await deploy("CrowdFunding", [arg1, arg2, arg3]);
Quote
Report
Post Reply
Checking account access…