pendulum-chain / oracle-pallet

The Unlicense
0 stars 0 forks source link

Refactor batching server to support coingecko and polygon.io #21

Closed ebma closed 4 months ago

ebma commented 4 months ago

What changed

How it works

The PriceApi in src/api/mod.rs is the high-level interface that can be used to query quotations for all supported assets. The PriceApiImpl will try to query prices from all supported endpoints. While the get_quotations() function returns a Result, it will never return an error, it will just log any errors that are encountered for any endpoint. I decided to implement it this way because it can always happen that a query fails for some reason and we would not like the batching server to stop because of this. All we can do is log (and possibly also send a reporting message somewhere).

We are now using a hardcoded mapping in convert_to_coingecko_id() in src/api/coingecko.rs. We should soon change the CustomMetadata of our runtimes, replacing the diaKeys field with a new type that allows to directly specify the coingecko identifier. This change would require a runtime upgrade (with a migration) and a root transaction to set the new metadata fields though. For the time being, the hardcoded mapping will require us to touch the code every time we want to add support for a new asset.

The API keys needed for Polygon and Coingecko can be passed as CLI arguments or via env variables.

Notes

While the response from polygon.io contains the timestamp of the last bid/ask placed, see here, I refrained from using that timestamp and instead used the timestamp of when the query was performed. This is because even if the last bid/ask was placed a while ago (according to the timestamp in the response), this does not necessarily mean that the price info is stale but could mean that trading is closed.

Testing

I tested this with the Foucoco runtime in Zombienet and the offchain-worker was able to feed the prices appropriately.

Closes #20.

ebma commented 4 months ago

I refactored this again to:

I tested it again with the offchain worker on Foucoco zombienet and it looks good to me. @pendulum-chain/devs please have another look.