1

I have deployed my contract locally into Ganache (GUI) and then send/call lot of transactions. After these transactions, now I want to check the current status of all state variables. Is there any way/API to get to know the current status of all state variables of a contract at run time (through program ).

My solution toward this is that I am using web3/javascript/nodejs and i have written a program which extract all getter functions from contract ABI. Because, i think these getters later will be utilized to know the status of all state variables of contract (I think this logic may not be true to fulfill my objectives...). I am not sure about it.

Here is my logic for getters

if(item.type === "function"  && item.stateMutability==='view')
    getters[i].push({
    contract: i,
    func: item.name,
    input: item.inputs
}

Later i write a function to to get the status of state variables.

async function getStatus(receipt, key, statusArr) {
  var myFunc; 
  var  myContAddr = receipt.contractAddress;
  var myContractAbiDefenition = sources[key].abi;
  var myContractInstance = new web3.eth.Contract(myContractAbiDefenition, myContAddr);

  for(j=0; j < getters[key].length; j++){
      myFunc = getters[key][j].func;

      try {
        res = await myContractInstance.methods[myFunc](...getters[key][j].input).call({from: account1})  
        console.log("Getter output", res);

      } catch (error) {
        console.log("Getter output: ERROR !");

      }
  }
}

Updated1: my actual code of getters for maping of item.input.length > = 0 is as follow. In this code, i just give some dummy values as per input type...

if(item.type === "function"  && item.stateMutability==='view'){ 
                      // console.log(item.name)  
                      getterFunctionsNameObject[i].push({
                        contract: i,
                        func: item.name,
                        input: item.inputs.map(({ type }) => {
                          switch(type) {
                            case 'address': {
                              return "0x2cAe18ac37d85CccdE2ebB63D0c391Fe1ac7c36f"
                            } 
                            case 'address[]': {
                              return ['0x2cAe18ac37d85CccdE2ebB63D0c391Fe1ac7c36f', '0xc4ae9984fbbf2416205e81e725e92496ad1eb087']
                            }
                            case 'string': {
                              return 'ABC'
                            }
                            case 'uint8': {
                              return 126
                            }
                            case 'uint256': {
                              return 3546546
                            }
                            case 'uint256[]': {
                              return [546546, 546546]
                            }
                            case 'bool': {
                              return true
                            }
                            case 'bytes': {
                              return "0x3333"
                            }
                            case 'bytes32': {
                              return "0x564b25c8fcd6766f672d43252c8ee2597ad6c7a35315cf13e3b4d0"
                            }
                          }
                        })
                      })

      }
11
  • Won't work for array getters (which take uint index as input). Won't work for mapping getters either (which take type key as input, where type is the type of the mapping key). Dec 2, 2019 at 9:11
  • For your code to work, you should verify that the number of input arguments is 0 (item.inputs.length == 0). Of course, it will not cover all of your contract's getters. Dec 2, 2019 at 9:12
  • I know i have cut this mapping to show you clear question .. for simplicity ... but actually i am doing little bit complex ... like this ..input: item.inputs.map(({ type }) => { switch(type) { case 'string': { return 'ABC'
    – Amir Ali
    Dec 2, 2019 at 9:13
  • my code is working ... i have already manage how to handle for item.input.lenght>=0 ...
    – Amir Ali
    Dec 2, 2019 at 9:14
  • see my update 1.
    – Amir Ali
    Dec 2, 2019 at 9:18

0

Your Answer

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