Skip to content

Commit

Permalink
feat: add crypto endpoint to tiingo adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
justinkaseman committed May 8, 2021
1 parent 1756fba commit 9d0034d
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 19 deletions.
90 changes: 84 additions & 6 deletions tiingo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@

### Input Parameters

| Required? | Name | Description | Options | Defaults to |
| :-------: | :------: | :-----------------: | :------------------: | :---------: |
| | endpoint | The endpoint to use | [eod](#EOD-Endpoint) | `eod` |
| Required? | Name | Description | Options | Defaults to |
| :-------: | :------: | :-----------------: | :-------------------------------------------------------------------------------------------: | :---------: |
| | endpoint | The endpoint to use | [`eod`](#EOD-Endpoint), [`iex` or `stock`](#IEX-Endpoint), [`top` or `crypto`](#Top-Endpoint) | `crypto` |

---

Expand All @@ -22,10 +22,10 @@ https://api.tiingo.com/documentation/end-of-day

### Input Params

| Required? | Name | Description | Options | Defaults to |
| :-------: | :-------------------------------------------: | :-----------------------: | :-----: | :---------: |
| Required? | Name | Description | Options | Defaults to |
| :-------: | :---------------------------------: | :-----------------------: | :-----: | :---------: |
|| `ticker`, `base`, `from`, or `coin` | The stock ticker to query | | |
| | `field` | The value to return | | `close` |
| | `field` | The value to return | | `close` |

### Sample Input

Expand All @@ -51,3 +51,81 @@ https://api.tiingo.com/documentation/end-of-day
"statusCode": 200
}
```

---

## IEX Endpoint

https://api.tiingo.com/documentation/iex

### Input Params

| Required? | Name | Description | Options | Defaults to |
| :-------: | :---------------------------------: | :-----------------------: | :-----: | :---------: |
|| `ticker`, `base`, `from`, or `coin` | The stock ticker to query | | |
| | `field` | The value to return | | `tngoLast` |

### Sample Input

```json
{
"id": "1",
"data": {
"ticker": "aapl"
}
}
```

### Sample Output

```json
{
"jobRunID": "1",
"result": 130.125,
"statusCode": 200,
"data": {
"result": 130.125
}
}
```

---

## Top Endpoint

The top of order book endpoint from:

https://api.tiingo.com/documentation/crypto

### Input Params

| Required? | Name | Description | Options | Defaults to |
| :-------: | :------------------------: | :----------------------------------------: | :-----: | :---------: |
|| `base`, `from`, or `coin` | The cryptocurrency symbol to query | | |
|| `quote`, `to`, or `market` | The output currency to return the price in | | |
| | `field` | The value to return | | `lastPrice` |

### Sample Input

```json
{
"id": "1",
"data": {
"base": "btc",
"quote": "usd"
}
}
```

### Sample Output

```json
{
"jobRunID": "1",
"data": {
"result": 130.27
},
"result": 130.27,
"statusCode": 200
}
```
2 changes: 1 addition & 1 deletion tiingo/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@chainlink/tiingo-adapter",
"version": "0.0.2",
"version": "0.1.0",
"description": "Chainlink tiingo adapter.",
"keywords": [
"Chainlink",
Expand Down
10 changes: 7 additions & 3 deletions tiingo/src/adapter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Requester, Validator } from '@chainlink/external-adapter'
import { Config, ExecuteWithConfig, ExecuteFactory } from '@chainlink/types'
import { makeConfig, DEFAULT_ENDPOINT } from './config'
import { eod, iex } from './endpoint'
import { eod, iex, top } from './endpoint'

const inputParams = {
endpoint: false,
Expand All @@ -20,9 +20,13 @@ export const execute: ExecuteWithConfig<Config> = async (request, config) => {
return await eod.execute(request, config)

case iex.NAME:
default: {
case 'stock':
return await iex.execute(request, config)
}

case top.NAME:
case 'crypto':
default:
return await top.execute(request, config)
}
}

Expand Down
2 changes: 1 addition & 1 deletion tiingo/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Requester } from '@chainlink/external-adapter'
import { Config } from '@chainlink/types'

export const DEFAULT_ENDPOINT = 'iex'
export const DEFAULT_ENDPOINT = 'top'
export const DEFAULT_BASE_URL = 'https://api.tiingo.com'

export const makeConfig = (prefix?: string): Config => {
Expand Down
68 changes: 68 additions & 0 deletions tiingo/src/endpoint/crypto/top.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { Requester, Validator } from '@chainlink/external-adapter'
import { ExecuteWithConfig, Config } from '@chainlink/types'

export const NAME = 'top'

export interface ResponseSchema {
ticker: string
baseCurrency: string
quoteCurrency: string
topOfBookData: {
askSize: number
bidSize: number
lastSaleTimestamp: string
lastPrice: number
askPrice: number
quoteTimestamp: string
bidExchange: string
lastSizeNotional: number
lastExchange: string
askExchange: string
bidPrice: number
lastSize: number
}[]
}

const customParams = {
base: ['base', 'from', 'coin'],
quote: ['quote', 'to', 'market'],
field: false,
}

// When an invalid symbol is given the response body is empty
const customError = (data: ResponseSchema[]) => !data.length

export const execute: ExecuteWithConfig<Config> = async (request, config) => {
const validator = new Validator(request, customParams)
if (validator.error) throw validator.error

const jobRunID = validator.validated.id
const base = validator.validated.data.base.toLowerCase()
const quote = validator.validated.data.quote.toLowerCase()
const field = validator.validated.data.field || 'lastPrice'

const url = '/tiingo/crypto/top'

const options = {
...config.api,
params: {
token: config.apiKey,
tickers: base + quote,
},
url,
}

const response = await Requester.request(options, customError)
const result = Requester.validateResultNumber(response.data as ResponseSchema[], [
0,
'topOfBookData',
0,
field,
])

return Requester.success(jobRunID, {
data: config.verbose ? { ...response.data, result } : { result },
result,
status: 200,
})
}
1 change: 1 addition & 0 deletions tiingo/src/endpoint/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * as eod from './eod'
export * as iex from './iex'
export * as top from './crypto/top'
16 changes: 8 additions & 8 deletions tiingo/test/adapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,23 @@ describe('execute', () => {
const requests = [
{
name: 'id not supplied',
testData: { data: { ticker: 'aapl' } },
testData: { data: { base: 'btc', quote: 'usd' } },
},
{
name: 'ticker',
testData: { id: jobID, data: { ticker: 'aapl' } },
testData: { id: jobID, data: { base: 'btc', quote: 'usd' } },
},
{
name: 'ticker & field',
testData: { id: jobID, data: { ticker: 'aapl', field: 'open' } },
testData: { id: jobID, data: { endpoint: 'iex', ticker: 'aapl', field: 'open' } },
},
{
name: 'iex endpoint',
testData: { id: jobID, data: { ticker: 'aapl', endpoint: 'price' } },
testData: { id: jobID, data: { ticker: 'aapl', endpoint: 'stock' } },
},
{
name: 'price endpoint',
testData: { id: jobID, data: { ticker: 'aapl', endpoint: 'price' } },
name: 'eod endpoint',
testData: { id: jobID, data: { ticker: 'aapl', endpoint: 'eod' } },
},
]

Expand Down Expand Up @@ -69,11 +69,11 @@ describe('execute', () => {
const requests = [
{
name: 'unknown ticker',
testData: { id: jobID, data: { ticker: 'not_real' } },
testData: { id: jobID, data: { endpoint: 'iex', ticker: 'not_real' } },
},
{
name: 'unknown field',
testData: { id: jobID, data: { ticker: 'aapl', field: 'not_real' } },
testData: { id: jobID, data: { endpoint: 'iex', ticker: 'aapl', field: 'not_real' } },
},
]

Expand Down

0 comments on commit 9d0034d

Please sign in to comment.