0

I have created an EnergyExchange contract in my React app.js that it is deployed in the network.

Inside the EnergyExchange contract there is a function to create Agent contracts.

What I am trying to do is store the instance of the Agent contract somewhere in the EnergyExchange contract so whenever I want I can retrieve its instance and interact with him within my React web.

I was thinking on doing something like this:

pragma solidity ^0.4.24;

import "./Ownable.sol";
import "./Agent.sol";

contract EnergyExchange is Ownable{
    
     struct Info {
        Agent ainstance;
    }
    mapping (address => Info) public contractInstance;
    
    
   function addAgentContractInstance(Agent _contractInstance) public{
        contractInstance[msg.sender].ainstance = _contractInstance;
       
    }
    
    function getAgentContractInstance() public view returns (Agent){
        //return contractInstance[msg.sender];
        return contractInstance[msg.sender].ainstance;
    }

    
    
    
}

But i am getting the following error when calling the functionlike this in React:

let agentInstance = await AgentContract(this.web3.currentProvider,this.exchange.address,this.state.account);

await this.exchange.addAgentContractInstance(agentInstance,{from:this.state.account}); 

Error:

Uncaught (in promise) Error: Invalid number of arguments to Solidity function

The instance object is causing this error can be seen next:

agent instance Any ideas of what I am doing wrong?

4
  • Yes, you are passing the wrong number of arguments when creating an instance of contract AgentContract (whose code you haven't provided in your question). – goodvibration Jul 12 '20 at 3:54
  • 1
    please provide the code for Age contract without it is.not possible to know what is happening. – Jaime Jul 12 '20 at 5:49
  • Hi I did not pass the agent contract because the problem is not with the creation of the agent contract. The problem is when I store the instance of the created agent contract in the struct through the addAgentContractInstance function. It is like the instance that is actually an object is not being passed appropiately. See in next answer the instance object detail – Víctor Rosillo Jul 12 '20 at 9:50
  • see on the updated question body the instance I am trying to pass to the function – Víctor Rosillo Jul 12 '20 at 9:57
0

The function addAgentContractInstance accepts a contract address as input parameter. When you pass a contract, like Agent in this case, the type parameter is address.

So the call is expecting an address and you pass an TruffleContract instance. To fix the call you have to pass the address of the instance:

this.exchange.addAgentContractInstance(agentInstance.address,

The javascript side could do more and recognize that a TruffleContract instance has an address but it does not, perhaps it could be a good improvement.

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.