Closed mloit closed 2 years ago
Good feature request. It is not really related to the sdk codebase though, as it is an implementation of the moralis web3 api. Better is to add a feature request to our roadmap: https://roadmap.moralis.io/
Closing this issue, as it is not something we can fix in the sdk now.
Not sure I'd classify it as a feature, as the results are incorrect IMHO. Having said that I understand now that this is happening at a higher level, so I'll bubble up the request. Thanks
I've come across a couple of seemingly related cases where the result type goes against what would be expected.
The first case is where a contract is returning multiple anonymous values. One would expect an array of indexed values, but instead an object with index names is returned.
The second case is when a contract is returning a structure with multiple values. One would expect to receive an object with properties having the corresponding names as defined in the ABI, but instead an array is returned. This carries forward to when an array of structs is returned, the SDK is producing an array of arrays, instead of an array of objects.
Contract Returning Multiple Values
When a contract function is called that returns multiple values the SDK converts this into an
object
with numbered parameters. This should be more correctly returned as anarray
, as the parameters are anonymous and only defined by their order.Example constructions
Below are some example constructions in solidity and the resultant ABI along with their returned outputs from within the Moralis SDK.
This is the call that was used to test
Contract function:
This is the contract function that was created for testing purposes while debugging this and other issues
Resultant ABI:
This is the relevant ABI snippet passed in the
abi
optionReceived Result
While this is technically okay, it would be more sensible if returned as an array, as access is via index, and not name. Couple this with the result from a when a Struct is returned, it's as if the relationships are reversed.
Expected/Preferred Result
This way we can test for the number of returned elements with
result.length
, as well as iterate through withforeach
Contract Returning Stuct
When a contract function is called that returns multiple values the SDK converts this into an
object
with numbered parameters. This should be more correctly returned as anarray
, as the parameters are anonymous and only defined by their order.Example constructions
Contract function:
Resultant ABI:
Received Result
This is a larger problem than the first case, as the names of the elements of the results have effectively been stripped away, making any code using it prone to breaking if the Contract/ABI was updated re-ordering the parameters.
Expected/Preferred Result
This would be the correct and expected result, and we can access the parameters based on their proper names as opposed to relying on their order within the struct. This is more sensible as an object with named parameters, and would be immune to problems associated with re-ordering.
This last case is compounded when an array of struct is returned. In that case an array of arrays is returned, having lost all contextual information.
Checklist
Environment
Hosted webpage. Following scripts are loaded on the page
Server
Client