My scenario is to make transactions (deploy contract, calling its functions, etc.) with arguments (which are dynamically created/extracted from some json abi file). For example;
function extractConstructorWithArguments()
{
if (item.type === "constructor")
constrObject.push(item.inputs)
}
above function making constObject as follows;
[ [ { internalType: 'address payable', name: '_wallet', type: 'address' } ] ]
/////////// extracting parameters types and giving them valid dummy values
for(c=0; c<constrObject.length; c++){ // number of constructor
for(d=0; d<(constrObject[c]).length; d++) // number of parameters
{
temp = constrObject[c][d].type
switch(temp) {
case 'string':
constrObject[c][d] = "ABC"
break;
case 'uint8':
constrObject[c][d] = 126
break;
case 'uint256':
constrObject[c][d] = 34535354
break;
case 'address':
constrObject[c][d] = '0x2cAe18ac37d85CccdE2ebB63D0c391Fe1ac7c36f'
break;
}
}
}
above code produce this output
[ [ '0x2cAe18ac37d85CccdE2ebB63D0c391Fe1ac7c36f' ] ]
///////////////////// deploying contract now ///////
async function deployCon(key) {
var paramLen= (constrObject[key]).length;
const contract = new web3.eth.Contract(sources[key].abi);
if(paramLen == 0) {
params = {
data: '0x' + sources[key].bytecode,
};
}
else {
params = {
data: '0x' + sources[key].bytecode,
arguments: constrObject[key] // HERE I Need Your suggestion
};
}
const transaction = contract.deploy(params);
const signed = await web3.eth.accounts.signTransaction(options, privateKey1);
receipt = await web3.eth.sendSignedTransaction(signed.rawTransaction);
}
When i deploy above contract , there is following error;
(node:12556) UnhandledPromiseRejectionWarning: Error: invalid type (arg="type", value="0x2cAe18ac37d85CccdE2ebB63D0c391Fe1ac7c36f")
at Object.throwError (C:\Users\aa\Desktop\temp\node_modules\ethers\utils\errors.js:68:17)
at getParamCoder (C:\Users\aa\Desktop\temp\node_modules\ethers\utils\abi-coder.js:862:12)
at AbiCoder.<anonymous> (C:\Users\aa\Desktop\temp\node_modules\ethers\utils\abi-coder.js:895:25)
at Array.forEach (<anonymous>)
at AbiCoder.encode (C:\Users\aa\Desktop\temp\node_modules\ethers\utils\abi-coder.js:884:15)
at ABICoder.encodeParameters (C:\Users\aa\Desktop\temp\node_modules\web3-eth-abi\src\index.js:96:27)
at C:\Users\aa\Desktop\temp\node_modules\web3-eth-contract\src\index.js:432:24
at Array.map (<anonymous>)
at Object._encodeMethodABI (C:\Users\aa\Desktop\temp\node_modules\web3-eth-contract\src\index.js:431:12)
at deployCon (C:\Users\aa\Desktop\temp\deploy1.js:178:23)
at main (C:\Users\aa\Desktop\temp\deploy1.js:142:25)
at Object.<anonymous> (C:\Users\aa\Desktop\temp\deploy1.js:150:1)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
(node:12556) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:12556) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
I am confused why my changed variable is not working. ConstrObject is an array, may be this causing error.
Smart Contract Source Code:
pragma solidity 0.5.12;
contract MyContract {
uint256 public totalSupply ;
mapping( address => uint256) public balances ;
address payable public owner;
constructor(address payable _wallet) public payable {
totalSupply = 6;
owner = _wallet;
}
function () external payable{
buyToken();
}
function buyToken() public payable {
require(totalSupply >= (msg.value/1000000000000000000)*2);
balances[msg.sender] += (msg.value/1000000000000000000)*2;
// wallet.tranfer(msg.value);
totalSupply -=(msg.value/1000000000000000000)*2;
}
function getTotalSupply()public view returns (uint256 ){
return totalSupply;
}
function setTotalSupply(uint256 newSupply) public payable {
// require(msg.sender == owner && totalSupply<1);
totalSupply = newSupply;
}
}
JSON file:
"abi": [
{
"inputs": [
{
"internalType": "address payable",
"name": "_wallet",
"type": "address"
}
],
"payable": true,
"stateMutability": "payable",
"type": "constructor"
},
UPDATED: Source code of contract and JSON portion of constructor
arguments: [arg1, arg2, arg3]
.arguments: [[arg1, arg2, arg3]]
.constrObject[key]
.