Skip to content

Commit cae92f9

Browse files
mxiao-cllstibalaapp-token-issuer-data-feeds[bot]
authored
m^0 external adapter (#3505)
* m^0 external adapter * Remove input param --------- Co-authored-by: Adam Stibal <[email protected]> Co-authored-by: app-token-issuer-data-feeds[bot] <134377064+app-token-issuer-data-feeds[bot]@users.noreply.github.com>
1 parent 45d018f commit cae92f9

25 files changed

+301
-0
lines changed

.changeset/dull-lemons-exist.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@chainlink/m0-adapter': patch
3+
---
4+
5+
Initial version of the M^0 EA

.pnp.cjs

+20
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.
Binary file not shown.
23.2 KB
Binary file not shown.
12.5 KB
Binary file not shown.

packages/sources/m0/CHANGELOG.md

Whitespace-only changes.

packages/sources/m0/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Chainlink External Adapter for m0
2+
3+
This README will be generated automatically when code is merged to `main`. If you would like to generate a preview of the README, please run `yarn generate:readme m0`.

packages/sources/m0/package.json

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"name": "@chainlink/m0-adapter",
3+
"version": "0.0.0",
4+
"description": "Chainlink m0 adapter.",
5+
"keywords": [
6+
"Chainlink",
7+
"LINK",
8+
"blockchain",
9+
"oracle",
10+
"m0"
11+
],
12+
"main": "dist/index.js",
13+
"types": "dist/index.d.ts",
14+
"files": [
15+
"dist"
16+
],
17+
"repository": {
18+
"url": "https://github.com/smartcontractkit/external-adapters-js",
19+
"type": "git"
20+
},
21+
"license": "MIT",
22+
"scripts": {
23+
"clean": "rm -rf dist && rm -f tsconfig.tsbuildinfo",
24+
"prepack": "yarn build",
25+
"build": "tsc -b",
26+
"server": "node -e 'require(\"./index.js\").server()'",
27+
"server:dist": "node -e 'require(\"./dist/index.js\").server()'",
28+
"start": "yarn server:dist"
29+
},
30+
"devDependencies": {
31+
"@types/jest": "27.5.2",
32+
"@types/node": "16.18.115",
33+
"nock": "13.5.4",
34+
"typescript": "5.5.4"
35+
},
36+
"dependencies": {
37+
"@chainlink/external-adapter-framework": "1.5.0",
38+
"tslib": "2.4.1"
39+
}
40+
}
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { AdapterConfig } from '@chainlink/external-adapter-framework/config'
2+
3+
export const config = new AdapterConfig({
4+
API_ENDPOINT: {
5+
description: 'An API endpoint for Data Provider',
6+
type: 'string',
7+
default: 'https://api.m0.xyz',
8+
},
9+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { endpoint as nav } from './nav'
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { AdapterEndpoint } from '@chainlink/external-adapter-framework/adapter'
2+
import { InputParameters } from '@chainlink/external-adapter-framework/validation'
3+
import { EmptyInputParameters } from '@chainlink/external-adapter-framework/validation/input-params'
4+
import { SingleNumberResultResponse } from '@chainlink/external-adapter-framework/util'
5+
import { config } from '../config'
6+
import { httpTransport } from '../transport/nav'
7+
8+
export const inputParameters = new InputParameters({}, [])
9+
10+
export type BaseEndpointTypes = {
11+
Parameters: EmptyInputParameters
12+
Response: SingleNumberResultResponse
13+
Settings: typeof config.settings
14+
}
15+
16+
export const endpoint = new AdapterEndpoint({
17+
name: 'reserves',
18+
aliases: ['por', 'nav'],
19+
transport: httpTransport,
20+
})

packages/sources/m0/src/index.ts

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { expose, ServerInstance } from '@chainlink/external-adapter-framework'
2+
import { Adapter } from '@chainlink/external-adapter-framework/adapter'
3+
import { config } from './config'
4+
import { nav } from './endpoint'
5+
6+
export const adapter = new Adapter({
7+
defaultEndpoint: nav.name,
8+
name: 'M0',
9+
config,
10+
endpoints: [nav],
11+
rateLimiting: {
12+
tiers: {
13+
default: {
14+
rateLimit1m: 1,
15+
note: 'Considered unlimited tier, but setting reasonable limit',
16+
},
17+
},
18+
},
19+
})
20+
21+
export const server = (): Promise<ServerInstance | undefined> => expose(adapter)
+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { HttpTransport } from '@chainlink/external-adapter-framework/transports'
2+
import { BaseEndpointTypes } from '../endpoint/nav'
3+
4+
export interface ResponseSchema {
5+
totalCollateral: number
6+
totalOwedM: number
7+
collateralisation: number
8+
}
9+
10+
export type HttpTransportTypes = BaseEndpointTypes & {
11+
Provider: {
12+
RequestBody: {
13+
method: string
14+
}
15+
ResponseBody: ResponseSchema
16+
}
17+
}
18+
export const httpTransport = new HttpTransport<HttpTransportTypes>({
19+
prepareRequests: (params, config) => {
20+
return params.map((param) => {
21+
return {
22+
params: [param],
23+
request: {
24+
baseURL: config.API_ENDPOINT,
25+
url: '/api/dashboard-backend-api/api/rpc',
26+
method: 'POST',
27+
data: { method: 'navDetails' },
28+
},
29+
}
30+
})
31+
},
32+
parseResponse: (params, response) => {
33+
if (!response.data) {
34+
return params.map((param) => {
35+
return {
36+
params: param,
37+
response: {
38+
errorMessage: `The data provider didn't return any value`,
39+
statusCode: 502,
40+
},
41+
}
42+
})
43+
}
44+
45+
return params.map((param) => {
46+
// total collateral: eligible and non eligible treasuries and cash, in micro dollar denomination
47+
const result = response.data['totalCollateral']
48+
return {
49+
params: param,
50+
response: {
51+
result,
52+
data: {
53+
result,
54+
},
55+
},
56+
}
57+
})
58+
},
59+
})

packages/sources/m0/test-payload.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"requests": [{}]
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`execute nav endpoint should return success 1`] = `
4+
{
5+
"data": {
6+
"result": 54716821540000,
7+
},
8+
"result": 54716821540000,
9+
"statusCode": 200,
10+
"timestamps": {
11+
"providerDataReceivedUnixMs": 978347471111,
12+
"providerDataRequestedUnixMs": 978347471111,
13+
},
14+
}
15+
`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import {
2+
TestAdapter,
3+
setEnvVariables,
4+
} from '@chainlink/external-adapter-framework/util/testing-utils'
5+
import * as nock from 'nock'
6+
import { mockResponseSuccess } from './fixtures'
7+
8+
describe('execute', () => {
9+
let spy: jest.SpyInstance
10+
let testAdapter: TestAdapter
11+
let oldEnv: NodeJS.ProcessEnv
12+
13+
beforeAll(async () => {
14+
oldEnv = JSON.parse(JSON.stringify(process.env))
15+
process.env.API_KEY = process.env.API_KEY ?? 'fake-api-key'
16+
17+
const mockDate = new Date('2001-01-01T11:11:11.111Z')
18+
spy = jest.spyOn(Date, 'now').mockReturnValue(mockDate.getTime())
19+
20+
const adapter = (await import('./../../src')).adapter
21+
adapter.rateLimiting = undefined
22+
testAdapter = await TestAdapter.startWithMockedCache(adapter, {
23+
testAdapter: {} as TestAdapter<never>,
24+
})
25+
})
26+
27+
afterAll(async () => {
28+
setEnvVariables(oldEnv)
29+
await testAdapter.api.close()
30+
nock.restore()
31+
nock.cleanAll()
32+
spy.mockRestore()
33+
})
34+
35+
describe('nav endpoint', () => {
36+
it('should return success', async () => {
37+
const data = {}
38+
mockResponseSuccess()
39+
const response = await testAdapter.request(data)
40+
expect(response.statusCode).toBe(200)
41+
expect(response.json()).toMatchSnapshot()
42+
})
43+
})
44+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import nock from 'nock'
2+
3+
export const mockResponseSuccess = (): nock.Scope =>
4+
nock('https://api.m0.xyz', {
5+
encodedQueryParams: true,
6+
})
7+
.post('/api/dashboard-backend-api/api/rpc', { method: 'navDetails' })
8+
.reply(
9+
200,
10+
() => ({
11+
totalCollateral: 54716821540000,
12+
totalOwedM: 42847433836220,
13+
collateralisation: 0.7830760743407144,
14+
}),
15+
[
16+
'Content-Type',
17+
'application/json',
18+
'Connection',
19+
'close',
20+
'Vary',
21+
'Accept-Encoding',
22+
'Vary',
23+
'Origin',
24+
],
25+
)
26+
.persist()

packages/sources/m0/tsconfig.json

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"extends": "../../tsconfig.base.json",
3+
"compilerOptions": {
4+
"outDir": "dist",
5+
"rootDir": "src"
6+
},
7+
"include": ["src/**/*", "src/**/*.json"],
8+
"exclude": ["dist", "**/*.spec.ts", "**/*.test.ts"]
9+
}
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "../../tsconfig.base.json",
3+
"include": ["src/**/*", "**/test", "src/**/*.json"],
4+
"compilerOptions": {
5+
"noEmit": true
6+
}
7+
}

packages/tsconfig.json

+3
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,9 @@
455455
{
456456
"path": "./sources/lotus"
457457
},
458+
{
459+
"path": "./sources/m0"
460+
},
458461
{
459462
"path": "./sources/marketstack"
460463
},

packages/tsconfig.test.json

+3
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,9 @@
455455
{
456456
"path": "./sources/lotus/tsconfig.test.json"
457457
},
458+
{
459+
"path": "./sources/m0/tsconfig.test.json"
460+
},
458461
{
459462
"path": "./sources/marketstack/tsconfig.test.json"
460463
},

yarn.lock

+13
Original file line numberDiff line numberDiff line change
@@ -5181,6 +5181,19 @@ __metadata:
51815181
languageName: unknown
51825182
linkType: soft
51835183

5184+
"@chainlink/m0-adapter@workspace:packages/sources/m0":
5185+
version: 0.0.0-use.local
5186+
resolution: "@chainlink/m0-adapter@workspace:packages/sources/m0"
5187+
dependencies:
5188+
"@chainlink/external-adapter-framework": "npm:1.5.0"
5189+
"@types/jest": "npm:27.5.2"
5190+
"@types/node": "npm:16.18.115"
5191+
nock: "npm:13.5.4"
5192+
tslib: "npm:2.4.1"
5193+
typescript: "npm:5.5.4"
5194+
languageName: unknown
5195+
linkType: soft
5196+
51845197
"@chainlink/market-closure-adapter@workspace:packages/composites/market-closure":
51855198
version: 0.0.0-use.local
51865199
resolution: "@chainlink/market-closure-adapter@workspace:packages/composites/market-closure"

0 commit comments

Comments
 (0)