I'm implementing an app with a Factory design pattern. My Factory is creating instances of myContract, that I load thanks to myContract.at(address) like below:

this.myContract = TruffleContract(Contract)
this.myContract .setProvider(this.web3Provider)
this.myContractABI = this.web3.eth.contract(this.leaseContract.abi.abi);
this.myContractInstance = this.myContract.at(leaseAddress)

This is working fine! I can then get my properties from a promise:

this.myContractInstance.propertyContract().then((propertyContract) => {
          this.setState({ property = propertyContract })       

This works fine!

But the problem is that I've 15 properties in my smart contract. If I have to get them one by one I'm thinking it's ugly code and it's not really efficient?

Can I get a promise on the whole smart contract and get all my properties in one promise ? Something like this maybe:

this.myContract.at(leaseAddress).then((contractInstance) => {

  this.setState({ propertyA = contractInstance.propertyA })
  this.setState({ propertyB = contractInstance.propertyA })
  this.setState({ propertyC = contractInstance.propertyA })

I think you get the idea :) Is that possible?


  • 2
    You could use Promise.all. That doesn't avoid listing all the properties you want to fetch, but it gives them all back to you in a single callback. – user19510 Aug 17 '18 at 8:38
  • 2
    You could create a function that finds all the function in the ABI that are declared view. Then, you could have it call all those functions using Promise.all and store the results in an object. I'd only do this if it's likely to be used many times, though. – Henk Aug 17 '18 at 9:59
  • Thanks, but it seems it's something that happens all the time no? Being able to store all the fields from a smart contract into react states? Seems complicated. I was using a struct before, and returning directly the struct in the promise was simple. However, struct in solidity is limited to 9 variables, which is not enough. I feel like something simpler should be used, or am I missing something? if I have Contract A with 20 public uint, how do I get them all in one call? Having 20 calls seems not efficient, right? – Xad Aug 17 '18 at 19:27

Your Answer

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

Browse other questions tagged or ask your own question.