-
Notifications
You must be signed in to change notification settings - Fork 336
feat(price-pusher): allow mnemonic from MNEMONIC env var #3689
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,6 +1,8 @@ | ||||||||||
| /* eslint-disable @typescript-eslint/no-unsafe-return */ | ||||||||||
| /* eslint-disable @typescript-eslint/no-unnecessary-type-parameters */ | ||||||||||
| /* eslint-disable @typescript-eslint/no-explicit-any */ | ||||||||||
| import fs from "node:fs"; | ||||||||||
|
|
||||||||||
| import type { HermesClient, HexString } from "@pythnetwork/hermes-client"; | ||||||||||
|
|
||||||||||
| import type { PriceItem } from "./interface.js"; | ||||||||||
|
|
@@ -60,6 +62,21 @@ export const assertDefined = <T>(value: T | undefined): T => { | |||||||||
| } | ||||||||||
| }; | ||||||||||
|
|
||||||||||
| export function readMnemonic(mnemonicFile: string | undefined): string { | ||||||||||
| if (mnemonicFile !== undefined && mnemonicFile !== "") { | ||||||||||
| return fs.readFileSync(mnemonicFile, "utf8").trim(); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| const envMnemonic = process.env.MNEMONIC; | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔴 Missing biome-ignore for The new
Suggested change
Was this helpful? React with 👍 or 👎 to provide feedback. |
||||||||||
| if (envMnemonic !== undefined && envMnemonic !== "") { | ||||||||||
| return envMnemonic.trim(); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| throw new Error( | ||||||||||
| "No mnemonic provided. Pass --mnemonic-file or set the MNEMONIC environment variable.", | ||||||||||
| ); | ||||||||||
| } | ||||||||||
|
devin-ai-integration[bot] marked this conversation as resolved.
|
||||||||||
|
|
||||||||||
| export async function filterInvalidPriceItems( | ||||||||||
| hermesClient: HermesClient, | ||||||||||
| priceItems: PriceItem[], | ||||||||||
|
|
||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| // biome-ignore-all lint/style/noProcessEnv: test file manipulates env vars | ||
|
|
||
| import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; | ||
| import { tmpdir } from "node:os"; | ||
| import { join } from "node:path"; | ||
|
|
||
| import { readMnemonic } from "../src/utils.js"; | ||
|
|
||
| describe("readMnemonic", () => { | ||
| let tmpDir: string; | ||
| const originalMnemonic = process.env.MNEMONIC; | ||
|
|
||
| beforeEach(() => { | ||
| tmpDir = mkdtempSync(join(tmpdir(), "price-pusher-test-")); | ||
| delete process.env.MNEMONIC; | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| rmSync(tmpDir, { recursive: true, force: true }); | ||
| if (originalMnemonic === undefined) { | ||
| delete process.env.MNEMONIC; | ||
| } else { | ||
| process.env.MNEMONIC = originalMnemonic; | ||
| } | ||
| }); | ||
|
|
||
| it("reads mnemonic from file when path supplied", () => { | ||
| const path = join(tmpDir, "mnemonic"); | ||
| writeFileSync(path, "from file mnemonic\n"); | ||
| expect(readMnemonic(path)).toBe("from file mnemonic"); | ||
| }); | ||
|
|
||
| it("falls back to MNEMONIC env var when no file path supplied", () => { | ||
| process.env.MNEMONIC = "from env mnemonic"; | ||
| expect(readMnemonic(undefined)).toBe("from env mnemonic"); | ||
| }); | ||
|
|
||
| it("treats empty-string file path as not supplied", () => { | ||
| process.env.MNEMONIC = "from env mnemonic"; | ||
| expect(readMnemonic("")).toBe("from env mnemonic"); | ||
| }); | ||
|
|
||
| it("file source takes precedence over env var", () => { | ||
| const path = join(tmpDir, "mnemonic"); | ||
| writeFileSync(path, "from file\n"); | ||
| process.env.MNEMONIC = "from env"; | ||
| expect(readMnemonic(path)).toBe("from file"); | ||
| }); | ||
|
|
||
| it("throws when neither file nor env var supplied", () => { | ||
| expect(() => readMnemonic(undefined)).toThrow( | ||
| /No mnemonic provided/, | ||
| ); | ||
| }); | ||
|
|
||
| it("throws when env var is empty string", () => { | ||
| process.env.MNEMONIC = ""; | ||
| expect(() => readMnemonic(undefined)).toThrow( | ||
| /No mnemonic provided/, | ||
| ); | ||
| }); | ||
| }); |
Uh oh!
There was an error while loading. Please reload this page.