-
Notifications
You must be signed in to change notification settings - Fork 316
Adds LibreCapital nav EA #3924
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
Merged
Adds LibreCapital nav EA #3924
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
634e97d
Adds EA for LibreCapital
mohamed-mehany 06a3c93
Adds date validation
mohamed-mehany 721370c
Refactors to use SubscribeTransport for multi HTTP requests
mohamed-mehany a7284d8
Adds unit tests, addreses feedback
mohamed-mehany 11aeead
Increases cache max age
mohamed-mehany 54ee154
Adds unit tests for transport nav and improvements
mohamed-mehany ea36969
Addresses comments
mohamed-mehany bc8dbac
Merge branch 'main' into libre-nav-consulting
mmcallister-cll 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/nav-libre-adapter': major | ||
--- | ||
|
||
Adds NAV adapter for LibreCapital |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file not shown.
Empty file.
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,3 @@ | ||
# Chainlink External Adapter for nav-libre | ||
|
||
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 nav-libre`. |
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,44 @@ | ||
{ | ||
"name": "@chainlink/nav-libre-adapter", | ||
"version": "0.0.0", | ||
"description": "Chainlink nav-libre adapter.", | ||
"keywords": [ | ||
"Chainlink", | ||
"LINK", | ||
"blockchain", | ||
"oracle", | ||
"nav-libre" | ||
], | ||
"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": { | ||
"clean": "rm -rf dist && rm -f tsconfig.tsbuildinfo", | ||
"prepack": "yarn build", | ||
"build": "tsc -b", | ||
"server": "node -e 'require(\"./index.js\").server()'", | ||
"server:dist": "node -e 'require(\"./dist/index.js\").server()'", | ||
"start": "yarn server:dist" | ||
}, | ||
"devDependencies": { | ||
"@types/crypto-js": "^4", | ||
"@types/jest": "^29.5.14", | ||
"@types/node": "22.14.1", | ||
"nock": "13.5.6", | ||
"typescript": "5.8.3" | ||
}, | ||
"dependencies": { | ||
"@chainlink/external-adapter-framework": "2.6.0", | ||
"crypto-js": "^4.2.0", | ||
"date-fns": "^4.1.0", | ||
"tslib": "2.4.1", | ||
"uuid": "^11.1.0" | ||
} | ||
} |
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,35 @@ | ||
import { AdapterConfig } from '@chainlink/external-adapter-framework/config' | ||
|
||
export const config = new AdapterConfig( | ||
{ | ||
API_KEY: { | ||
description: 'An API key for Data Provider', | ||
type: 'string', | ||
required: true, | ||
sensitive: true, | ||
}, | ||
SECRET_KEY: { | ||
description: 'A key for Data Provider used in hashing the API key', | ||
type: 'string', | ||
required: true, | ||
sensitive: true, | ||
}, | ||
API_ENDPOINT: { | ||
description: 'An API endpoint for Data Provider', | ||
type: 'string', | ||
default: 'https://api.navfundservices.com', | ||
}, | ||
BACKGROUND_EXECUTE_MS: { | ||
description: | ||
'The amount of time the background execute should sleep before performing the next request', | ||
type: 'number', | ||
default: 120_000, // one call per two minute | ||
}, | ||
}, | ||
{ | ||
envDefaultOverrides: { | ||
CACHE_MAX_AGE: 900_000, // 15 minute cache | ||
RETRY: 0, // Disables retry on Framework | ||
}, | ||
}, | ||
) |
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 @@ | ||
export { endpoint as nav } from './nav' |
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,37 @@ | ||
import { AdapterEndpoint } from '@chainlink/external-adapter-framework/adapter' | ||
import { InputParameters } from '@chainlink/external-adapter-framework/validation' | ||
import { config } from '../config' | ||
import { navLibreTransport } from '../transport/nav' | ||
|
||
export const inputParameters = new InputParameters( | ||
{ | ||
globalFundID: { | ||
required: true, | ||
type: 'number', | ||
description: 'The global fund ID for the Libre fund', | ||
}, | ||
}, | ||
[ | ||
{ | ||
globalFundID: 1234, | ||
}, | ||
], | ||
) | ||
export type BaseEndpointTypes = { | ||
Parameters: typeof inputParameters.definition | ||
Response: { | ||
Result: number | ||
Data: { | ||
navPerShare: number | ||
navDate: string | ||
globalFundID: number | ||
} | ||
} | ||
Settings: typeof config.settings | ||
} | ||
|
||
export const endpoint = new AdapterEndpoint({ | ||
name: 'nav', | ||
transport: navLibreTransport, | ||
inputParameters, | ||
}) |
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,13 @@ | ||
import { expose, ServerInstance } from '@chainlink/external-adapter-framework' | ||
import { Adapter } from '@chainlink/external-adapter-framework/adapter' | ||
import { config } from './config' | ||
import { nav } from './endpoint' | ||
|
||
export const adapter = new Adapter({ | ||
defaultEndpoint: nav.name, | ||
name: 'NAV_LIBRE', | ||
config, | ||
endpoints: [nav], | ||
}) | ||
|
||
export const server = (): Promise<ServerInstance | undefined> => expose(adapter) |
33 changes: 33 additions & 0 deletions
33
packages/sources/nav-libre/src/transport/authentication.ts
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,33 @@ | ||
import CryptoJS from 'crypto-js' | ||
import { v4 as uuidv4 } from 'uuid' | ||
|
||
/** | ||
* Generate the necessary headers for calling the NAV API with a 5-minute-valid signature. | ||
*/ | ||
export const getRequestHeaders = ({ | ||
method, | ||
path, | ||
body, | ||
apiKey, | ||
secret, | ||
}: { | ||
method: string | ||
path: string | ||
body: string | ||
apiKey: string | ||
secret: string | ||
}) => { | ||
const utcNow = new Date().toUTCString() | ||
const nonce = uuidv4() | ||
const contentHash = CryptoJS.SHA256(body).toString(CryptoJS.enc.Base64) | ||
const stringToSign = [apiKey, path, method, utcNow, nonce, contentHash].join(';') | ||
|
||
// Compute the HMAC-SHA256 signature, Base64-encoded | ||
const signature = CryptoJS.HmacSHA256(stringToSign, secret).toString(CryptoJS.enc.Base64) | ||
|
||
return { | ||
'x-date': utcNow, | ||
'x-content-sha256': contentHash, | ||
'x-hmac256-signature': `${apiKey};${nonce};${signature}`, | ||
dskloetc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
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,44 @@ | ||
import { differenceInBusinessDays, format, isValid, parse, subBusinessDays } from 'date-fns' | ||
|
||
// Date format used by the NavLibre API | ||
export const DATE_FORMAT = 'MM-dd-yyyy' | ||
export const MAX_BUSINESS_DAYS = 7 | ||
|
||
/** | ||
* Parse a string in MM-DD-YYYY format. | ||
* Throws if the string is missing or malformed. | ||
*/ | ||
export function parseDateString(dateStr: string): Date { | ||
const parsed = parse(dateStr, DATE_FORMAT, new Date()) | ||
if (!isValid(parsed)) { | ||
throw new Error(`date must be in ${DATE_FORMAT} format: got "${dateStr}"`) | ||
} | ||
return new Date(Date.UTC(parsed.getFullYear(), parsed.getMonth(), parsed.getDate())) | ||
} | ||
|
||
/** | ||
* Guarantee the (from -> to) span is <= `maxBusinessDays`. | ||
* | ||
* If the gap is larger, shift `from` forward so it sits exactly | ||
* `maxBusinessDays` business days before `to` and returns the new `from`. | ||
* | ||
* Returns the original `from` if the gap is smaller or equal. | ||
* | ||
* Example: 7-day limit | ||
* from = 2025-06-25 (Wed) | ||
* to = 2025-07-10 (Thu) | ||
* span = 11 business days -> newFrom = 2025-07-01 | ||
*/ | ||
export function clampStartByBusinessDays( | ||
from: Date, | ||
to: Date, | ||
maxBusinessDays = MAX_BUSINESS_DAYS, | ||
): Date { | ||
const span = differenceInBusinessDays(to, from) | ||
return span > maxBusinessDays ? subBusinessDays(to, maxBusinessDays) : from | ||
} | ||
|
||
/** Convenience formatter so every outbound string is consistent. */ | ||
export function toDateString(d: Date): string { | ||
return format(d, DATE_FORMAT) | ||
} |
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,51 @@ | ||
import { Requester } from '@chainlink/external-adapter-framework/util/requester' | ||
import { AdapterError } from '@chainlink/external-adapter-framework/validation/error' | ||
import { getRequestHeaders } from './authentication' | ||
|
||
export interface FundDatesResponse { | ||
LogID: number | ||
FromDate: string | ||
ToDate: string | ||
} | ||
|
||
export const getFundDates = async ({ | ||
globalFundID, | ||
baseURL, | ||
apiKey, | ||
secret, | ||
requester, | ||
}: { | ||
globalFundID: number | ||
baseURL: string | ||
apiKey: string | ||
secret: string | ||
requester: Requester | ||
}): Promise<FundDatesResponse> => { | ||
const method = 'GET' | ||
const url = `/navapigateway/api/v1/ClientMasterData/GetAccountingDataDates?globalFundID=${globalFundID}` | ||
const requestConfig = { | ||
baseURL: baseURL, | ||
url: url, | ||
method: method, | ||
headers: getRequestHeaders({ | ||
method: method, | ||
path: url, | ||
body: '', | ||
apiKey: apiKey, | ||
secret: secret, | ||
}), | ||
} | ||
|
||
const sourceResponse = await requester.request<FundDatesResponse>( | ||
JSON.stringify(requestConfig), | ||
dskloetc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
requestConfig, | ||
) | ||
if (!sourceResponse.response.data) { | ||
throw new AdapterError({ | ||
statusCode: 400, | ||
message: `No fund found`, | ||
}) | ||
} | ||
|
||
return sourceResponse.response.data | ||
} |
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.