0

I am busy writing my first smart contract dapp and have some questions.

My dapp has quite a bit of information I want to deliver to a client and to do this i have written a bunch of getters in the contract:

1. getSomeDetail
2. getSomeOtherDetail

There are probably 10 or so items of information that I require and 10 getters to get them.

Is it possible to have a single method that returns all the bits of information that I can then use in may dapp rather than calling each one?

In any other language I may do something like so:

function singleMethod() {
    $data = [
       'item1' => getItem1(),
       'item2' => getItem2(),
       'item3' => getItem3()
    ];
return $data;
}

Then in the app I can access each item as I need them. Obviously the advantage is a single call.

Maybe I do not need to worry about this since its not an API call that would suffer from latency issues...

1

Yes, you can return multiple arguments.

function getStuff() constant returns (uint, bool) {
    return (3, true);
}
0

You should be careful not to pass more than 16 arguments to the return function.

If you must return so many data, then group them based on their data types into corresponding arrays. For example, instead of :

return (uint8 a, uint8 b, uint8 c, bytes32 d, bytes32 e, uint f, uint g, uint h, bool i, bool j) 

simply do:

return (uint8[] uint8s, bytes32[] bytes32s, uint[] uints, bool[] bools)

Where

  • uint8s is an array of all your uint8 arguments
  • bytes32s is an array of all your bytes32 arguments
  • uints is an array of all your uint arguments
  • bools is an array of all your bool arguments
  • Ha, good thinking. I did hit the 16 argument threshold and solved it by splitting up the methods. But this is a better idea. – HappyCoder Oct 8 '17 at 14:47

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.