Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SportMonks adapter #12

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/strategy/adapters.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@
"dnsproof",
"genesisvolatility",
"ciphertrace",
"bookmakerratings"
"bookmakerratings",
"sportmonks"
]
},
"2-step": {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@
"dnsproof",
"genesisvolatility",
"ciphertrace",
"bookmakerratings"
"bookmakerratings",
"sportmonks"
],
"scripts": {
"lint": "yarn workspaces run lint",
Expand Down
3 changes: 3 additions & 0 deletions sportmonks/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
...require('../.eslintrc.ts.js'),
}
195 changes: 195 additions & 0 deletions sportmonks/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
# Chainlink External Adapter for SportMonks

### Environment Variables

| Required? | Name | Description | Options | Defaults to |
| :-------: | :-----: | :-----------------------------------------------------------------------------------: | :-----: | :---------: |
| ✅ | API_KEY | An API key that can be obtained from the data [provider](https://www.sportmonks.com/) | | |

---

### Input Parameters

| Required? | Name | Description | Options | Defaults to |
| :-------: | :------: | :-----------------: | :------------------------------------------------------------------------------: | :-----------: |
| | endpoint | The endpoint to use | [match-results](#Match-Results-Endpoint), [toss-results](#Toss-Results-Endpoint) | match-results |

---

## Match Results Endpoint

Returns the winning team of a specific match

### Input Params

| Required? | Name | Description | Options | Defaults to |
| :-------: | :---------: | :---------------------------: | :-----: | :---------: |
| ✅ | `round` | The round to query | | |
| | `season_id` | The id of the season to query | | `708` |

### Sample Input

```json
{
"id": "1",
"data": {
"round": "1st Match",
"season_id": "708"
}
}
```

### Sample Output

```json
{
"jobRunID": "278c97ffadb54a5bbb93cfec5f7b5503",
"data": {
"data": [
{
"resource": "fixtures",
"id": 24915,
"league_id": 1,
"season_id": 708,
"stage_id": 2262,
"round": "1st Match",
"localteam_id": 6,
"visitorteam_id": 8,
"starting_at": "2021-04-09T14:00:00.000000Z",
"type": "T20",
"live": true,
"status": "Finished",
"last_period": null,
"note": "Royal Challengers Bangalore won by 2 wickets (With 0 ball remaining)",
"venue_id": 58,
"toss_won_team_id": 8,
"winner_team_id": 8,
"draw_noresult": null,
"first_umpire_id": 326,
"second_umpire_id": 158,
"tv_umpire_id": 57,
"referee_id": 734,
"man_of_match_id": 2744,
"man_of_series_id": null,
"total_overs_played": 20,
"elected": "bowling",
"super_over": false,
"follow_on": false,
"localteam_dl_data": { "score": null, "overs": null, "wickets_out": null },
"visitorteam_dl_data": { "score": null, "overs": null, "wickets_out": null },
"rpc_overs": null,
"rpc_target": null,
"weather_report": []
}
],
"links": {
"first": "https://cricket.sportmonks.com/api/v2.0/fixtures?page=1",
"last": "https://cricket.sportmonks.com/api/v2.0/fixtures?page=1",
"prev": null,
"next": null
},
"meta": {
"current_page": 1,
"from": 1,
"last_page": 1,
"links": [Array],
"path": "https://cricket.sportmonks.com/api/v2.0/fixtures",
"per_page": 100,
"to": 56,
"total": 56
},
"result": "away"
},
"result": "away",
"statusCode": 200
}
```

## Toss Results Endpoint

Returns the winning team of a specific toss

### Input Params

| Required? | Name | Description | Options | Defaults to |
| :-------: | :---------: | :---------------------------: | :-----: | :---------: |
| ✅ | `round` | The round to query | | |
| | `season_id` | The id of the season to query | | `708` |

### Sample Input

```json
{
"id": "1",
"data": {
"round": "1st Match",
"season_id": "708"
}
}
```

### Sample Output

```json
{
"jobRunID": "278c97ffadb54a5bbb93cfec5f7b5503",
"data": {
"data": [
{
"resource": "fixtures",
"id": 24915,
"league_id": 1,
"season_id": 708,
"stage_id": 2262,
"round": "1st Match",
"localteam_id": 6,
"visitorteam_id": 8,
"starting_at": "2021-04-09T14:00:00.000000Z",
"type": "T20",
"live": true,
"status": "Finished",
"last_period": null,
"note": "Royal Challengers Bangalore won by 2 wickets (With 0 ball remaining)",
"venue_id": 58,
"toss_won_team_id": 8,
"winner_team_id": 8,
"draw_noresult": null,
"first_umpire_id": 326,
"second_umpire_id": 158,
"tv_umpire_id": 57,
"referee_id": 734,
"man_of_match_id": 2744,
"man_of_series_id": null,
"total_overs_played": 20,
"elected": "bowling",
"super_over": false,
"follow_on": false,
"localteam_dl_data": { "score": null, "overs": null, "wickets_out": null },
"visitorteam_dl_data": { "score": null, "overs": null, "wickets_out": null },
"rpc_overs": null,
"rpc_target": null,
"weather_report": []
}
],
"links": {
"first": "https://cricket.sportmonks.com/api/v2.0/fixtures?page=1",
"last": "https://cricket.sportmonks.com/api/v2.0/fixtures?page=1",
"prev": null,
"next": null
},
"meta": {
"current_page": 1,
"from": 1,
"last_page": 1,
"links": [Array],
"path": "https://cricket.sportmonks.com/api/v2.0/fixtures",
"per_page": 100,
"to": 56,
"total": 56
},
"result": "away"
},
"result": "away",
"statusCode": 200
}
```
46 changes: 46 additions & 0 deletions sportmonks/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"name": "@chainlink/sportmonks-adapter",
"version": "0.0.1",
"description": "Chainlink sportmonks adapter.",
"keywords": [
"Chainlink",
"LINK",
"blockchain",
"oracle",
"sportmonks"
],
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"dist"
],
"repository": {
"url": "https://github.com/smartcontractkit/external-adapters-js",
"type": "git"
},
"license": "MIT",
"scripts": {
"prepublishOnly": "yarn build && yarn test:unit",
"setup": "yarn build",
"build": "tsc -b",
"lint": "eslint --ignore-path ../.eslintignore . --ext .js,.jsx,.ts,.tsx",
"lint:fix": "eslint --ignore-path ../.eslintignore . --ext .js,.jsx,.ts,.tsx --fix",
"test": "mocha --exit --timeout 3000 -r ts-node/register 'test/**/*.test.ts'",
"test:unit": "mocha --exit --grep @integration --invert -r ts-node/register 'test/**/*.test.ts'",
"test:integration": "mocha --exit --timeout 3000 --grep @integration -r ts-node/register 'test/**/*.test.ts'",
"server": "node -e 'require(\"./index.js\").server()'",
"server:dist": "node -e 'require(\"./dist/index.js\").server()'",
"start": "yarn server:dist"
},
"devDependencies": {
"@types/chai": "^4.2.11",
"@types/express": "^4.17.6",
"@types/mocha": "^7.0.2",
"@types/node": "^14.0.13",
"@typescript-eslint/eslint-plugin": "^3.9.0",
"@typescript-eslint/parser": "^3.9.0",
"ts-node": "^8.10.2",
"typescript": "^3.9.7"
},
"dependencies": {}
}
38 changes: 38 additions & 0 deletions sportmonks/src/adapter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Requester, Validator, AdapterError } from '@chainlink/external-adapter'
import { Config, ExecuteWithConfig, ExecuteFactory } from '@chainlink/types'
import { makeConfig, DEFAULT_ENDPOINT } from './config'
import { matchResults, tossResults } from './endpoint'

const inputParams = {
endpoint: false,
}

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

Requester.logConfig(config)

const jobRunID = validator.validated.id
const endpoint = validator.validated.data.endpoint || DEFAULT_ENDPOINT

switch (endpoint.toLowerCase()) {
case matchResults.NAME: {
return await matchResults.execute(request, config)
}
case tossResults.NAME: {
return await tossResults.execute(request, config)
}
default: {
throw new AdapterError({
jobRunID,
message: `Endpoint ${endpoint} not supported.`,
statusCode: 400,
})
}
}
}

export const makeExecute: ExecuteFactory<Config> = (config) => {
return async (request) => execute(request, config || makeConfig())
}
11 changes: 11 additions & 0 deletions sportmonks/src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Requester } from '@chainlink/external-adapter'
import { Config } from '@chainlink/types'

export const DEFAULT_ENDPOINT = 'match-results'
export const DEFAULT_BASE_URL = 'https://cricket.sportmonks.com/api/v2.0/'

export const makeConfig = (prefix?: string): Config => {
const config = Requester.getDefaultConfig(prefix, true)
config.api.baseURL = config.api.baseURL || DEFAULT_BASE_URL
return config
}
2 changes: 2 additions & 0 deletions sportmonks/src/endpoint/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * as matchResults from './matchResults'
export * as tossResults from './tossResults'
44 changes: 44 additions & 0 deletions sportmonks/src/endpoint/matchResults.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { Requester, Validator } from '@chainlink/external-adapter'
import { ExecuteWithConfig, Config } from '@chainlink/types'

export const NAME = 'match-results'

const customParams = {
round: true,
season_id: false,
}

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 round = validator.validated.data.round
const season_id = validator.validated.data.season_id || 708
const url = `fixtures/`

const params = {
'filter[season_id]': season_id,
api_token: config.apiKey,
}

const options = { ...config.api, params, url }
const response = await Requester.request(options)

const match = response.data.data.find((entry: { round: string }) => entry.round === round)
let result = null

if (match.status !== 'Finished') {
return Requester.errored(jobRunID, 'Match has not finished')
} else if (match.winner_team_id === match.localteam_id) {
result = 'home'
} else if (match.winner_team_id === match.visitorteam_id) {
result = 'away'
}

return Requester.success(jobRunID, {
data: config.verbose ? { ...response.data, result } : { result },
result,
status: 200,
})
}
Loading