Adds implementations for querying prices from polygon.io, coingecko, and 'custom' endpoints (for now just the AMPE price from the squid)
Removes queries to the DIA endpoint
The supply field uses the data of the '24-hour trading volume' field. This aligns to the previous implementation which was using the 24 hour trading volume reported by DIA, though the data apparently differs a lot. This doesn't matter too much as we don't use the supply field for anything.
Simplifies the overall structure and unifies types.
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.
What changed
structopt
withclap
for argument parsingsupply
field uses the data of the '24-hour trading volume' field. This aligns to the previous implementation which was using the 24 hour trading volume reported by DIA, though the data apparently differs a lot. This doesn't matter too much as we don't use thesupply
field for anything.How it works
The
PriceApi
insrc/api/mod.rs
is the high-level interface that can be used to query quotations for all supported assets. ThePriceApiImpl
will try to query prices from all supported endpoints. While theget_quotations()
function returns aResult
, 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()
insrc/api/coingecko.rs
. We should soon change theCustomMetadata
of our runtimes, replacing thediaKeys
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.