NatLabs / serde

Serialization and Deserialization library for motoko
MIT License
16 stars 3 forks source link

Serializing a motoko object with an array parameter of mixed types... is this even possible? #26

Closed janusdotai closed 7 months ago

janusdotai commented 7 months ago

Thanks again for your wonderful library..

I have a question, is this even possible? I'm trying to call an ETH RPC using HTTP outcalls. Everything works for simple requests, but when I need to send more complex parameters I'm hitting a blocker.

The required JSON to query for example, a chainlink feed to get the latest ETH/USD price is as such:

{"jsonrpc":"2.0","id":5423031679576389,"method":"eth_call","params":[{"data":"0xfeaf968c","to":"0x5f4ec3df9cbd43714fe2740f5e3616155c5b8419"},"latest"]}

However, this type I think is impossible to construct in Motoko? As the params field looks like a mixed type array of object + strings?

Can you think of a structure that can support this for serialization/deserialization using serde? I came up with this but doesn't work as the compiler throws a warning if you try to put mixed types in an array. I'm thinking this type of call is not possible using custom types as the compiler will never allow an array of [?{}, ?Text] correct?

error ethjsonrequest chainlink latestround ethjson

In one debug screenshot you can see the resulting serde jsonRequest params field is an empty object array

Set jsonRequest: "{"id": 1, "jsonrpc": "2.0", "method": "eth_call", "params": [{}]}"

janusdotai commented 7 months ago

Closing this, realized the "latest" parameter is an extra field added by RPC providers and not specific to chainlink or their contracts.