-
Notifications
You must be signed in to change notification settings - Fork 323
Add new package endpoint to anchorage #4187
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
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@chainlink/anchorage-adapter': minor | ||
| --- | ||
|
|
||
| Add new endpoint |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| export { endpoint as packages } from './packages' | ||
| export { endpoint as wallet } from './wallet' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import { AdapterEndpoint } from '@chainlink/external-adapter-framework/adapter' | ||
| import { InputParameters } from '@chainlink/external-adapter-framework/validation' | ||
| import { AdapterInputError } from '@chainlink/external-adapter-framework/validation/error' | ||
| import { config } from '../config' | ||
| import { packagesTransport } from '../transport/packages' | ||
|
|
||
| export const inputParameters = new InputParameters( | ||
| { | ||
| clientReferenceId: { | ||
| required: true, | ||
| type: 'string', | ||
| description: 'Id of the vault', | ||
| }, | ||
| assetType: { | ||
| required: true, | ||
| type: 'string', | ||
| description: 'Asset ticker name', | ||
| }, | ||
| }, | ||
| [ | ||
| { | ||
| clientReferenceId: '123456', | ||
| assetType: 'BTC', | ||
| }, | ||
| ], | ||
| ) | ||
|
|
||
| export type BaseEndpointTypes = { | ||
| Parameters: typeof inputParameters.definition | ||
| Response: { | ||
| Result: string | ||
| Data: { | ||
| result: string | ||
| assets: { | ||
| asset: { | ||
| assetType: string | ||
| } | ||
| quantity: string | ||
| }[] | ||
| } | ||
| } | ||
| Settings: typeof config.settings | ||
| } | ||
|
|
||
| export const endpoint = new AdapterEndpoint({ | ||
| name: 'packages', | ||
| transport: packagesTransport, | ||
| inputParameters, | ||
| customInputValidation: (_, adapterSettings): AdapterInputError | undefined => { | ||
| if (!adapterSettings.COLLATERAL_API_KEY) { | ||
| throw new AdapterInputError({ | ||
| message: 'Missing COLLATERAL_API_KEY', | ||
| statusCode: 400, | ||
| }) | ||
| } | ||
| return | ||
| }, | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| import { EndpointContext } from '@chainlink/external-adapter-framework/adapter' | ||
| import { TransportDependencies } from '@chainlink/external-adapter-framework/transports' | ||
| import { SubscriptionTransport } from '@chainlink/external-adapter-framework/transports/abstract/subscription' | ||
| import { AdapterResponse, makeLogger, sleep } from '@chainlink/external-adapter-framework/util' | ||
| import { Requester } from '@chainlink/external-adapter-framework/util/requester' | ||
| import { AdapterError } from '@chainlink/external-adapter-framework/validation/error' | ||
| import Decimal from 'decimal.js' | ||
| import { BaseEndpointTypes, inputParameters } from '../endpoint/packages' | ||
| import { request } from './requester' | ||
|
|
||
| const logger = makeLogger('PackageTransport') | ||
|
|
||
| type RequestParams = typeof inputParameters.validated | ||
|
|
||
| interface PackageResponse { | ||
| clientReferenceId: string | ||
| collateralAssets: [ | ||
| { | ||
| asset: { | ||
| assetType: string | ||
| } | ||
| quantity: string | ||
| }, | ||
| ] | ||
| } | ||
|
|
||
| export class PackagesTransport extends SubscriptionTransport<BaseEndpointTypes> { | ||
| requester!: Requester | ||
| settings!: BaseEndpointTypes['Settings'] | ||
|
|
||
| async initialize( | ||
| dependencies: TransportDependencies<BaseEndpointTypes>, | ||
| adapterSettings: BaseEndpointTypes['Settings'], | ||
| endpointName: string, | ||
| transportName: string, | ||
| ): Promise<void> { | ||
| await super.initialize(dependencies, adapterSettings, endpointName, transportName) | ||
| this.requester = dependencies.requester | ||
| this.settings = adapterSettings | ||
| } | ||
|
|
||
| async backgroundHandler(context: EndpointContext<BaseEndpointTypes>, entries: RequestParams[]) { | ||
| await Promise.all(entries.map(async (param) => this.handleRequest(param))) | ||
| await sleep(context.adapterSettings.BACKGROUND_EXECUTE_MS) | ||
| } | ||
|
|
||
| async handleRequest(param: RequestParams) { | ||
| let response: AdapterResponse<BaseEndpointTypes['Response']> | ||
| try { | ||
| response = await this._handleRequest(param) | ||
| } catch (e) { | ||
| const errorMessage = e instanceof Error ? e.message : 'Unknown error occurred' | ||
| logger.error(e, errorMessage) | ||
| response = { | ||
| statusCode: (e as AdapterError)?.statusCode || 502, | ||
| errorMessage, | ||
| timestamps: { | ||
| providerDataRequestedUnixMs: 0, | ||
| providerDataReceivedUnixMs: 0, | ||
| providerIndicatedTimeUnixMs: undefined, | ||
| }, | ||
| } | ||
| } | ||
| await this.responseCache.write(this.name, [{ params: param, response }]) | ||
| } | ||
|
|
||
| async _handleRequest( | ||
| params: RequestParams, | ||
| ): Promise<AdapterResponse<BaseEndpointTypes['Response']>> { | ||
| const providerDataRequestedUnixMs = Date.now() | ||
|
|
||
| const response = await request<PackageResponse>( | ||
| this.requester, | ||
| this.settings.API_ENDPOINT, | ||
| 'v2/collateral_management/packages', | ||
| // Validated not null in endpoint | ||
| // eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
| this.settings.COLLATERAL_API_KEY!, | ||
| this.settings.API_LIMIT, | ||
| ) | ||
|
|
||
| const assets = response | ||
| .filter((r) => r.clientReferenceId.toUpperCase() == params.clientReferenceId.toUpperCase()) | ||
| .flatMap((r) => r.collateralAssets) | ||
|
|
||
| const result = assets | ||
| .filter((r) => r.asset.assetType.toUpperCase() == params.assetType.toUpperCase()) | ||
| .reduce((sum, current) => { | ||
| return sum.add(new Decimal(current.quantity)) | ||
| }, new Decimal(0)) | ||
|
|
||
| return { | ||
| data: { | ||
| result: result.toString(), | ||
| assets, | ||
| }, | ||
| statusCode: 200, | ||
| result: result.toString(), | ||
| timestamps: { | ||
| providerDataRequestedUnixMs, | ||
| providerDataReceivedUnixMs: Date.now(), | ||
| providerIndicatedTimeUnixMs: undefined, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| getSubscriptionTtlFromConfig(adapterSettings: BaseEndpointTypes['Settings']): number { | ||
| return adapterSettings.WARMUP_SUBSCRIPTION_TTL | ||
| } | ||
| } | ||
|
|
||
| export const packagesTransport = new PackagesTransport() | ||
34 changes: 34 additions & 0 deletions
34
packages/sources/anchorage/test/integration/__snapshots__/adapter.test.ts.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.