Skip to content

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 8 commits into from
Jul 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/shiny-points-melt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@chainlink/nav-libre-adapter': major
---

Adds NAV adapter for LibreCapital
31 changes: 31 additions & 0 deletions .pnp.cjs

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

Binary file not shown.
Empty file.
3 changes: 3 additions & 0 deletions packages/sources/nav-libre/README.md
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`.
44 changes: 44 additions & 0 deletions packages/sources/nav-libre/package.json
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"
}
}
35 changes: 35 additions & 0 deletions packages/sources/nav-libre/src/config/index.ts
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
},
},
)
1 change: 1 addition & 0 deletions packages/sources/nav-libre/src/endpoint/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { endpoint as nav } from './nav'
37 changes: 37 additions & 0 deletions packages/sources/nav-libre/src/endpoint/nav.ts
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,
})
13 changes: 13 additions & 0 deletions packages/sources/nav-libre/src/index.ts
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 packages/sources/nav-libre/src/transport/authentication.ts
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}`,
}
}
44 changes: 44 additions & 0 deletions packages/sources/nav-libre/src/transport/date-utils.ts
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)
}
51 changes: 51 additions & 0 deletions packages/sources/nav-libre/src/transport/fund-dates.ts
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),
requestConfig,
)
if (!sourceResponse.response.data) {
throw new AdapterError({
statusCode: 400,
message: `No fund found`,
})
}

return sourceResponse.response.data
}
Loading
Loading