This is an express server for requesting the price of tokens from the CoinGecko API. Prices are returned in USD.
This server has been created for cases where the CoinGecko API can't be called directly. Instead, a call to this server can be made via IPFS, which will trigger the request to CoinGecko and this server will then return the requested token prices.
For example, the subgraph cannot make HTTP calls so it can use this server as an alternative way of requesting prices for tokens.
clone this repo:
git clone git@github.com:oceanprotocol/subgraph-fetch-price.gitChange directory:
cd subgraph-fetch-priceInstall the dependencies:
npm installStart the server locally
npm run startThere are two paths for requesting prices, one for the current price and one for the historical price:
This gets the current price of the token. The tokenId must be included in the request, a list of valid tokenIds are here. An up to date list can be requested from CoinGecko GET https://api.coingecko.com/api/v3/coins/list
/ipfs/QmTkzDwWqPbnAh5YiV5VwcTLnGdwSNsNTn2aDxdXBFca7D/:tokenId
Example Request:
http://localhost:3000/ipfs/QmTkzDwWqPbnAh5YiV5VwcTLnGdwSNsNTn2aDxdXBFca7D/ocean-protocol
Example response:
{
"usd": 0.491975
}
This request gets the historical price of the token. The tokenId must be included in the request, a list of valid tokenIds are here. An up to date list can be requested from CoinGecko GET https://api.coingecko.com/api/v3/coins/list. The request must also include the timestamp in unix timestamp format (eg. 1550245790).
/ipfs/QmTkzDwWqPbnAh5YiV5VwcTLnGdwSNsNTn2aDxdXBFca7D/:tokenId/:timestamp
Example Request:
http://localhost:3000/ipfs/QmTkzDwWqPbnAh5YiV5VwcTLnGdwSNsNTn2aDxdXBFca7D/ocean-protocol/1620245790
Example response:
{
"usd": 1.4499657614470953
}
Run the following command to start the server in development mode with nodemon:
npm run devRun the following command to build the server:
npm run buildRun the following command to start the server in development:
npm run startRun the following command to run all tests:
npm run testRun the following command to run the formatting and linting tests:
npm run test:formatRun the following command to run the integration tests:
npm run test:integrationRun the following command to build the service in a Docker container:
npm run build:dockerNext, run the following command to start running the RBAC service in the Docker container:
npm run start:dockerNow you are ready to send requests to the server via postman. Make sure to replace the URL to http://localhost:49160 in your requests.
Code style is automatically enforced through ESLint & Prettier rules:
- Git pre-commit hook runs
prettieron staged files, setup with Husky - VS Code suggested extensions and settings for auto-formatting on file save
For running linting and auto-formatting manually, you can use from the root of the project:
# linting check, also runs Typescript typings check
npm run lint
# auto format all files in the project with prettier, taking all configs into account
npm run format