0

I have MyContract.json file of some contract(assume i haven't its source code). In .json file, there is abi and bytecode. If i want to deploy such contract (through web3js), I am facing following problems. However, this question becomes more complex, when there are thousand of contracts, which is my final task. But here i am just limit my self to only one contract for understanding purpose.

  1. If such contract need some arguments in its constructor, then how can i know its parameters with valid values, so that it could be deployed, successfully.
  2. How can I call its different functions (getters, setters)?

For question no. 1; I am sharing here my code for its deployment. It is valid and working if contract has constructor which take owner address in its parameter. This code is tested on some known contract with source code. However, if constructor takes no parameters or different parameters or lot of other parameters, this code will fail to deploy, because i must have to pass valid number of parameters and valid values.

var compiledContract = require('./build/MyContract.json');

async function deployCon() {

const contract = new web3.eth.Contract(compiledContract.abi);
const params = {
    data: '0x' + compiledContract.bytecode,
    arguments: [account1]
};
const transaction = contract.deploy(params); 
const options = {
    data: transaction.encodeABI(),
    gas: await transaction.estimateGas({from: account1})
};
// console.log(options)
const signed = await web3.eth.accounts.signTransaction(options, privateKey1);
receipt = await web3.eth.sendSignedTransaction(signed.rawTransaction);
console.log(`Contract deployed at address: ${receipt.contractAddress}`);
return receipt;

}

For question no. 2; i am totally blind how to call contract's functions shown here as ??????? (which will come to know through abi of .json file alongwith parameters!).

var  myContAddr = receipt.contractAddress;
    var myContractAbiDefenition = compiledContract.abi;
    var myContractInstance = new web3.eth.Contract(myContractAbiDefenition, myContAddr);
    await myContractInstance.methods.???????.send({from: account1, value: web3.utils.toWei(amount, 'ether')})

1 Answer 1

1

For your question no. 1: Search for "type": "constructor" in your ABI. From this object, you can see the inputs that has an array of parameters along with their name and type. Example:

  {
    "inputs": [
      {
        "internalType": "uint256",
        "name": "_ff",
        "type": "uint256"
      },
      {
        "internalType": "string",
        "name": "_name",
        "type": "string"
      }
    ],
    "payable": false,
    "stateMutability": "nonpayable",
    "type": "constructor"
  }

In the above example, you can see the constructor has two parameters: _ff and name of type uint256 and string respectively.

For question no. 2: once the contract is instantiated, you can log all the methods as well as public state variable, by using console.log(myContractInstance.methods).

Otherwise, you can look into ABI where type is function. But this is may be a more complex way.

Updated:

let abi = JSON ABI;

for (let item of abi) {
    // this will return an array of constructor parameters along with their name and type
    if (item.type === "constructor") console.log(item.inputs);
    // result [{internalType: "uint256", name: "_ff", type: "uint256"}, {internalType: "string", name: "_name", type: "string"}]

    // this will return only the names
    // you can push the names into array
    // then iterate over the array to call them
    if (item.type === "function") console.log(item.name);
}

Hope it will help.

8
  • thanks for your answer. OK but how to extract constructor and its parameter from such complex abi using web3 ?? ... it should be more feasible for me, if you do favor to give some sample code (as i am newbie )... For 2nd question, i can get methods name and print them ... but how to call them individually like myContractInstance.methods.MY_RETRIEVED_FUNCTION_NAME().send()
    – Amir Ali
    Nov 15, 2019 at 10:07
  • please look at this example to call the methods: web3js.readthedocs.io/en/v1.2.2/web3-eth-contract.html#id28
    – Yahya
    Nov 15, 2019 at 10:18
  • i think i could not explained ... I know how to call function... but how to append the different function names (extracted from methods= myContractInstance.methods)... for example this (myContractInstance.methods) will give us method name i.e. setTotalSupply which is stored in methods[0], and getTotalSupply is stored in methods[1]. then how to call (or append this extracted names to myContractInstance.methods. ------. send()
    – Amir Ali
    Nov 15, 2019 at 10:26
  • I don't understand what you mean by append. if you know the name of method, let say setTotalSupply, simply call it as myContractInstance.methods.setTotalSupply().call().
    – Yahya
    Nov 15, 2019 at 10:33
  • i means that when i call let say var methods = myContractInstance.methods, so all method names will be saved into methods variable. then how to retrieve names of methods from 'methods' variable and then i call i.e. myContractInstance.methods.methods[0].send () // i know this will not work, but i want to call dynamically (run time) method[0] should be replaced with actual names...
    – Amir Ali
    Nov 15, 2019 at 10:43

Your Answer

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

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