-
Notifications
You must be signed in to change notification settings - Fork 2
Description
This may not be something specifically in scope for the CityCoins API, but thinking about the amount of routing we do to satisfy all the contract paths and parameters, there is a common pattern that could be generalized.
The original thought behind the CityCoins API was to make an endpoint for every contract function, and I still think specific APIs that follow this pattern could be very useful to both user interfaces and external integrations.
However, this may be useful for other projects that want to the use an API structure like this one, and I felt like the idea was worth documenting as "something to think about" when the V2 upgrades come downstream.
What if there were a simple route that accepted the following path and query parameters?
https://api.citycoins.co/query/deployer/contract_name/function_name?param1=value1¶m2=value2
Which breaks down as:
- prefix:
https://api.citycoins.co/query/ - path:
deployer- deployer address of contract - path:
contract_name- contract name - path:
function_name- function name in contract - query:
param1- function parameters, if any - query:
param2- function parameters, if any
An example using is-block-winner:
This would essentially be a "query builder" in which you could put any contract, function, and related parameters then receive a result.
Pros:
- this would open up a way to query any contract without dependencies
- there is no additional code to add if contract functions change
- there is no effort required to query new contracts on chain
- could serve as a fallback method for getting data
- e.g. using this method until something more official like an endpoint is implemented
- e.g. using this method to query data only needed for some time (like the vote info)
Cons:
- knowing the correct type for the query parameters and converting them may be a challenge
- knowing how to process the types of returned data may be a challenge
- could both of those be worked around using the contract ABI?
Curious what others think!
Spit-balling a few more reasons I think this could be useful:
- this can be achieved with Cloudflare Workers free tier, and their pricing is very fair when scaling
- this separates the data layer from the application layer, which allows for others to build UIs, tools, and implement integrations in a consistent fashion
- this allows for caching, where a strategy could be set based on the type of data and stored in Cloudflare Workers KV
- if data never changes, can return results directly from KV first then store if not found
- custom TTL values can be set depending on how often data should be refreshed
- patterns like checking if the block height changed can be applied to limit queries
- this allows low-level access to HTTP headers and responses, which can:
- help increase security
- solve cross-origin request problems
- and much much more
- this allows for cron-type scripts to run using the same defined functions, which can:
- update a user's info on a regular basis (or a list of users)
- monitor chain state and key metrics
- collect contract data for processing / re-use