Skip to content

Commit

Permalink
Merge branch 'main' into link-not-working
Browse files Browse the repository at this point in the history
  • Loading branch information
app-token-issuer-data-feeds[bot] authored Feb 6, 2025
2 parents 33c3dc0 + a1a5b26 commit 294cffa
Show file tree
Hide file tree
Showing 10 changed files with 178 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/happy-moles-change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@chainlink/glv-token-adapter': minor
---

Add LWBA endpoint
23 changes: 23 additions & 0 deletions packages/composites/glv-token/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,29 @@ Request:
}
```

## Crypto-lwba Endpoint

Supported names for this endpoint are: `cryptolwba`.

### Input Params

| Required? | Name | Aliases | Description | Type | Options | Default | Depends On | Not Valid With |
| :-------: | :--: | :-----: | :---------: | :----: | :-----: | :-----: | :--------: | :------------: |
|| glv | | Glv address | string | | | | |

### Example

Request:

```json
{
"data": {
"endpoint": "crypto-lwba",
"glv": "0x528A5bac7E746C9A509A1f4F6dF58A03d44279F9"
}
}
```

---

MIT License
1 change: 1 addition & 0 deletions packages/composites/glv-token/src/endpoint/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { endpoint as price } from './price'
export { endpoint as lwba } from './lwba'
38 changes: 38 additions & 0 deletions packages/composites/glv-token/src/endpoint/lwba.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import {
AdapterEndpoint,
LwbaResponseDataFields,
} from '@chainlink/external-adapter-framework/adapter'
import { InputParameters } from '@chainlink/external-adapter-framework/validation'
import { config } from '../config'
import { glvLwbaTransport } from '../transport/lwba'

export const inputParameters = new InputParameters(
{
glv: {
required: true,
type: 'string',
description: 'Glv address',
},
},
[
{
glv: '0x528A5bac7E746C9A509A1f4F6dF58A03d44279F9',
},
],
)

export type BaseEndpointTypesLwba = {
Parameters: typeof inputParameters.definition
Response: LwbaResponseDataFields & {
Data: {
sources: Record<string, string[]>
}
}
Settings: typeof config.settings
}

export const endpoint = new AdapterEndpoint({
name: 'crypto-lwba',
transport: glvLwbaTransport,
inputParameters,
})
4 changes: 2 additions & 2 deletions packages/composites/glv-token/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { expose, ServerInstance } from '@chainlink/external-adapter-framework'
import { Adapter } from '@chainlink/external-adapter-framework/adapter'
import { config } from './config'
import { price } from './endpoint'
import { lwba, price } from './endpoint'

export const adapter = new Adapter({
defaultEndpoint: price.name,
name: 'GLV_TOKEN',
config,
endpoints: [price],
endpoints: [price, lwba],
})

export const server = (): Promise<ServerInstance | undefined> => expose(adapter)
3 changes: 2 additions & 1 deletion packages/composites/glv-token/src/transport/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { AdapterResponse, makeLogger } from '@chainlink/external-adapter-framewo
import { AdapterDataProviderError } from '@chainlink/external-adapter-framework/validation/error'
import glvAbi from '../config/glvReaderAbi.json'
import { BaseEndpointTypes, inputParameters } from '../endpoint/price'
import { BaseEndpointTypesLwba } from '../endpoint/lwba'
import {
mapParameter,
mapSymbol,
Expand Down Expand Up @@ -41,7 +42,7 @@ type RequestParams = typeof inputParameters.validated
* `formatResponse()` to produce different output shapes.
*/
export abstract class BaseGlvTransport<
T extends BaseEndpointTypes,
T extends BaseEndpointTypes | BaseEndpointTypesLwba,
> extends SubscriptionTransport<T> {
abstract backgroundHandler(
context: EndpointContext<T>,
Expand Down
44 changes: 44 additions & 0 deletions packages/composites/glv-token/src/transport/lwba.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { BaseGlvTransport } from './base'
import { EndpointContext } from '@chainlink/external-adapter-framework/adapter'
import { TypeFromDefinition } from '@chainlink/external-adapter-framework/validation/input-params'
import { AdapterResponse, sleep } from '@chainlink/external-adapter-framework/util'
import { BaseEndpointTypesLwba } from '../endpoint/lwba'

export class GlvLwbaTransport extends BaseGlvTransport<BaseEndpointTypesLwba> {
async backgroundHandler(
context: EndpointContext<BaseEndpointTypesLwba>,
entries: TypeFromDefinition<BaseEndpointTypesLwba['Parameters']>[],
): Promise<void> {
await Promise.all(entries.map(async (param) => this.handleRequest(param)))
await sleep(context.adapterSettings.BACKGROUND_EXECUTE_MS)
}

async handleRequest(
param: TypeFromDefinition<BaseEndpointTypesLwba['Parameters']>,
): Promise<void> {
const response = await this._handleRequest(param).catch((e) => this.handleError(e))
await this.responseCache.write(this.name, [{ params: param, response }])
}

protected formatResponse(
result: number,
minimizedValue: number,
maximizedValue: number,
sources: Record<string, string[]>,
timestamps: any,
): AdapterResponse<BaseEndpointTypesLwba['Response']> {
return {
data: {
mid: result,
bid: minimizedValue,
ask: maximizedValue,
sources,
},
statusCode: 200,
result: null,
timestamps,
}
}
}

export const glvLwbaTransport = new GlvLwbaTransport()
16 changes: 13 additions & 3 deletions packages/composites/glv-token/test-payload.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
{
"requests": [{
"glv": "0x528A5bac7E746C9A509A1f4F6dF58A03d44279F9"
}]
"requests": [
{
"glv": "0x528A5bac7E746C9A509A1f4F6dF58A03d44279F9"
},
{
"endpoint": "price",
"glv": "0x528A5bac7E746C9A509A1f4F6dF58A03d44279F9"
},
{
"endpoint": "crypto-lwba",
"glv": "0x528A5bac7E746C9A509A1f4F6dF58A03d44279F9"
}
]
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`execute lwba endpoint should return success 1`] = `
{
"data": {
"ask": 1.1473068612168396,
"bid": 1.1470467994160611,
"mid": 1.1471768303164502,
"sources": {
"ETH": [
"tiingo",
"coinmetrics",
"ncfx",
],
"USDC": [
"tiingo",
"coinmetrics",
"ncfx",
],
},
},
"result": null,
"statusCode": 200,
"timestamps": {
"providerDataReceivedUnixMs": 978347471111,
"providerDataRequestedUnixMs": 978347471111,
},
}
`;

exports[`execute price endpoint should return success 1`] = `
{
"data": {
Expand Down
22 changes: 22 additions & 0 deletions packages/composites/glv-token/test/integration/adapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import {
mockTiingoEAResponseSuccess,
} from './fixtures'
import { ethers } from 'ethers'
import {
LwbaResponseDataFields,
validateLwbaResponse,
} from '@chainlink/external-adapter-framework/adapter'

jest.mock('ethers', () => ({
...jest.requireActual('ethers'),
Expand Down Expand Up @@ -116,4 +120,22 @@ describe('execute', () => {
expect(response.json()).toMatchSnapshot()
})
})

describe('lwba endpoint', () => {
it('should return success', async () => {
const data = {
endpoint: 'crypto-lwba',
glv: '0x528A5bac7E746C9A509A1f4F6dF58A03d44279F9',
}
mockTiingoEAResponseSuccess('ETH')
mockNCFXEAResponseSuccess('ETH')
mockCoinmetricsEAResponseSuccess('ETH')
const response = await testAdapter.request(data)
expect(response.statusCode).toBe(200)
expect(response.json()).toMatchSnapshot()
const resJson = response.json()
const resData = resJson.data as LwbaResponseDataFields['Data']
validateLwbaResponse(resData.bid, resData.mid, resData.ask)
})
})
})

0 comments on commit 294cffa

Please sign in to comment.