Skip to content

Commit

Permalink
Merge branch 'main' into l2ep-upgrade-metis-healthcheck-endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
cl-ea authored Apr 23, 2024
2 parents 16f0fa2 + 473b33b commit 44ef154
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/gorgeous-toys-taste.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@chainlink/tradingeconomics-adapter': patch
---

Added support for cache TTL refresh on heartbeat messages for Forex endpoint
10 changes: 5 additions & 5 deletions .pnp.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
Binary file not shown.
6 changes: 6 additions & 0 deletions packages/sources/tradingeconomics/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@

This document was generated automatically. Please see [README Generator](../../scripts#readme-generator) for more info.

## Known Issues

### CACHE_MAX_AGE interaction with Heartbeat messages

If `CACHE_MAX_AGE` is set below a current heartbeat interval (45000ms), the extended cache TTL feature for out-of-market-hours that relies on heartbeats will not work.

## Environment Variables

| Required? | Name | Description | Type | Options | Default |
Expand Down
5 changes: 5 additions & 0 deletions packages/sources/tradingeconomics/docs/known-issues.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## Known Issues

### CACHE_MAX_AGE interaction with Heartbeat messages

If `CACHE_MAX_AGE` is set below a current heartbeat interval (45000ms), the extended cache TTL feature for out-of-market-hours that relies on heartbeats will not work.
24 changes: 21 additions & 3 deletions packages/sources/tradingeconomics/src/transport/price-ws.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { WebsocketReverseMappingTransport } from '@chainlink/external-adapter-framework/transports'
import {
WebsocketReverseMappingTransport,
WebSocketTransport,
} from '@chainlink/external-adapter-framework/transports'
import { makeLogger } from '@chainlink/external-adapter-framework/util'
import { BaseEndpointTypes } from '../endpoint/price'

Expand Down Expand Up @@ -27,6 +30,14 @@ type WsTransportTypes = BaseEndpointTypes & {
WsMessage: Message
}
}
/*
Tradingeconomics EA currently does not receive asset prices during off-market hours. When a heartbeat message is received during these hours,
we update the TTL of cache entries that EA is requested to provide a price during off-market hours.
*/
const updateTTL = async (transport: WebSocketTransport<WsTransportTypes>, ttl: number) => {
const params = await transport.subscriptionSet.getAll()
transport.responseCache.writeTTL(transport.name, params, ttl)
}

export const wsTransport: WebsocketReverseMappingTransport<WsTransportTypes, string> =
new WebsocketReverseMappingTransport<WsTransportTypes, string>({
Expand All @@ -35,10 +46,17 @@ export const wsTransport: WebsocketReverseMappingTransport<WsTransportTypes, str
return `${WS_API_ENDPOINT}?client=${API_CLIENT_KEY}:${API_CLIENT_SECRET}`
},
handlers: {
message: (message) => {
if (!message.topic || message.topic === 'keepalive') {
message: (message, context) => {
if (!message.topic) {
return []
}
// Check for a heartbeat message, refresh the TTLs of all requested entries in the cache
if (message.topic === 'keepalive') {
wsTransport.lastMessageReceivedAt = Date.now()
updateTTL(wsTransport, context.adapterSettings.CACHE_MAX_AGE)
return []
}

const pair = wsTransport.getReverseMapping(message.s)
if (!pair) {
logger.error(`Pair not found in websocket reverse map for message symbol - ${message.s}`)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,21 @@ exports[`websocket price endpoint should return success 1`] = `
}
`;

exports[`websocket price endpoint should update the ttl after heartbeat is received 1`] = `
{
"data": {
"result": 0.776530152665828,
},
"result": 0.776530152665828,
"statusCode": 200,
"timestamps": {
"providerDataReceivedUnixMs": 1018,
"providerDataStreamEstablishedUnixMs": 1010,
"providerIndicatedTimeUnixMs": 1659472542655,
},
}
`;

exports[`websocket stock endpoint should return success 1`] = `
{
"data": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
setEnvVariables,
mockWebSocketProvider,
MockWebsocketServer,
runAllUntilTime,
} from '@chainlink/external-adapter-framework/util/testing-utils'
import FakeTimers from '@sinonjs/fake-timers'
import { WebSocketClassProvider } from '@chainlink/external-adapter-framework/transports'
Expand Down Expand Up @@ -52,16 +53,25 @@ describe('websocket', () => {
await testAdapter.api.close()
})

describe('price endpoint', () => {
describe('stock endpoint', () => {
it('should return success', async () => {
const response = await testAdapter.request(dataPrice)
const response = await testAdapter.request(dataStock)
expect(response.json()).toMatchSnapshot()
})
})

describe('stock endpoint', () => {
describe('price endpoint', () => {
it('should return success', async () => {
const response = await testAdapter.request(dataStock)
const response = await testAdapter.request(dataPrice)
expect(response.json()).toMatchSnapshot()
})

it('should update the ttl after heartbeat is received', async () => {
// The cache ttl is 90 seconds. Mocked heartbeat message is sent after 10s after connection which should
// update the ttl and therefore after 93 seconds (from the initial message) we can access the asset
await runAllUntilTime(testAdapter.clock, 93000)
const response = await testAdapter.request(dataPrice)
expect(response.statusCode).toBe(200)
expect(response.json()).toMatchSnapshot()
})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ export const mockWebSocketServer = (url: string) => {
topic: 'USDCAD',
}),
)
setTimeout(() => {
socket.send(JSON.stringify({ topic: 'keepalive' }))
}, 10000)
} else {
// stock endpoint
socket.send(
Expand Down
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8917,7 +8917,7 @@ __metadata:
languageName: node
linkType: hard

"@noble/curves@npm:^1.2.0":
"@noble/curves@npm:^1.4.0":
version: 1.4.0
resolution: "@noble/curves@npm:1.4.0"
dependencies:
Expand Down Expand Up @@ -10464,11 +10464,11 @@ __metadata:
linkType: hard

"@solana/web3.js@npm:^1.33.0":
version: 1.91.4
resolution: "@solana/web3.js@npm:1.91.4"
version: 1.91.7
resolution: "@solana/web3.js@npm:1.91.7"
dependencies:
"@babel/runtime": ^7.23.4
"@noble/curves": ^1.2.0
"@noble/curves": ^1.4.0
"@noble/hashes": ^1.3.3
"@solana/buffer-layout": ^4.0.1
agentkeepalive: ^4.5.0
Expand All @@ -10482,7 +10482,7 @@ __metadata:
node-fetch: ^2.7.0
rpc-websockets: ^7.5.1
superstruct: ^0.14.2
checksum: 9f6ac2f64ac05043cb367c09d5b4f4b859060618dbffc7a27da35fccdac0be9bd84b83899c9547efedc061fa7e20af283a9bacff01972bd047089809facfbdfe
checksum: 76a81f52dccb05e519fed0a93033a9b18660b80b1eb4d0593e12de94f51f3e4a6f4b9cd85296ff5849d180b8e633367f675906e22f9c6d2baf0b98a10590f361
languageName: node
linkType: hard

Expand Down

0 comments on commit 44ef154

Please sign in to comment.