0

I have developed a EnergyExchange DApp in the Ganache local network with React that consist of one main contract "EnergyExchange". This main contract has a function to deploy new Agent contracts.

So what I do is to store the deployed agents contracts in a mapping so later on when I want to interact with them I call the next function:

let contractInstance = await this.getAgentInstance();

The detail of the function can be seen here:

async getAgentInstance(){
  var AgentContract = web3.eth.contract([
    {
      "inputs": [
        {
          "name": "_agentAddress",
          "type": "address"
        },
        {
          "name": "_agentID",
          "type": "uint256"
        },
        {
          "name": "_agentCreationDate",
          "type": "uint256"
        },
        {
          "name": "_agentAvailableEnergyToSell",
          "type": "uint256"
        },
        {
          "name": "_exchangeAddress",
          "type": "address"
        }
      ],
      "payable": false,
      "stateMutability": "nonpayable",
      "type": "constructor"
    },
    {
      "anonymous": false,
      "inputs": [
        {
          "indexed": false,
          "name": "a",
          "type": "uint256"
        }
      ],
      "name": "onAvailableEnergyToSellUpdated",
      "type": "event"
    },
    {
      "constant": false,
      "inputs": [
        {
          "name": "_agentAvailableEnergyToSell",
          "type": "uint256"
        }
      ],
      "name": "updateAvailableEnergyToSell",
      "outputs": [],
      "payable": false,
      "stateMutability": "nonpayable",
      "type": "function"
    },
    {
      "constant": true,
      "inputs": [],
      "name": "getAgentDetails",
      "outputs": [
        {
          "name": "",
          "type": "address"
        },
        {
          "name": "",
          "type": "uint256"
        },
        {
          "name": "",
          "type": "uint256"
        },
        {
          "name": "",
          "type": "uint256"
        }
      ],
      "payable": false,
      "stateMutability": "view",
      "type": "function"
    },
    {
      "constant": false,
      "inputs": [
        {
          "name": "_pricePerUnit",
          "type": "uint256"
        },
        {
          "name": "_unit",
          "type": "uint256"
        }
      ],
      "name": "addSellOrder",
      "outputs": [],
      "payable": false,
      "stateMutability": "nonpayable",
      "type": "function"
    },
    {
      "constant": true,
      "inputs": [],
      "name": "getSellOffers",
      "outputs": [
        {
          "name": "",
          "type": "uint256[]"
        },
        {
          "name": "",
          "type": "address[]"
        },
        {
          "name": "",
          "type": "uint256[]"
        },
        {
          "name": "",
          "type": "uint256[]"
        },
        {
          "name": "",
          "type": "uint256[]"
        },
        {
          "name": "",
          "type": "bool[]"
        }
      ],
      "payable": false,
      "stateMutability": "view",
      "type": "function"
    },
    {
      "constant": false,
      "inputs": [
        {
          "name": "_sellOrderId",
          "type": "uint256"
        }
      ],
      "name": "cancelAddedSellOrder",
      "outputs": [],
      "payable": false,
      "stateMutability": "nonpayable",
      "type": "function"
    },
    {
      "constant": false,
      "inputs": [
        {
          "name": "_sellOrderId",
          "type": "uint256"
        },
        {
          "name": "_sellerAddress",
          "type": "address"
        }
      ],
      "name": "buyEnergy",
      "outputs": [],
      "payable": true,
      "stateMutability": "payable",
      "type": "function"
    }
  ]);
  return await AgentContract.at(this.state.agentInstance);
};

That function returns the specific agent instance associated to the account is being used in the browser with metamask. See below the console.log of the instance:

instance retrieved image

As you can see, the getAgentInstance() function is returning the desired output.

The error appears when trying to interact with the deployed contract through the retrieved instance. For example when calling a get function:

async agentGetDetails(){
  
  
  let contractInstance = await this.getAgentInstance();
  console.log(contractInstance);
  let details = await contractInstance.getAgentDetails({from:this.state.account});

  

  console.log('function get agentdetails executed');
  }

And here it goes the solidity function we are calling:

function getAgentDetails() public view isOwner returns(address,uint,uint,uint){
    return (agentAddress,agentID,agentCreationDate,agentAvailableEnergyToSell);
    
}

The error that i get is detailed next:

error image

Any ideas of what is happening? I already tested the contract and all its functions in both Remix and Truffle-test.

Also if I add an error handling to the function like this:

let details = await contractInstance.getAgentDetails.call({from:this.state.account},function(error,result){
      console.log("error when executing the function",result);
    });
    

It is weird because i receive the expected output as an error in the result but in a weird format.(note the Xs):

error handling result

Thanks

  • What error do you get? – Ismael Jul 12 at 21:22
  • I added the error. sorry i forgot – Víctor Rosillo Jul 13 at 15:10
  • What version of web3 are you using? From syntax it appears to be v0.20, but you use it with async/await that is only supported by v1.2. – Ismael Jul 13 at 21:02
0

web3.eth.Contract() takes a capital C at Contract.

And maybe try with this syntax :

getAgentInstance() {
  return new web3.eth.Contract(
    [
      /* JSON-interface of the Agent contract
        ...
      */
    ],
    this.state.agentInstance // supposing that you store the address of the instance you want to access there.
  );
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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