-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added optional logging configuration
- Loading branch information
Showing
8 changed files
with
114 additions
and
63 deletions.
There are no files selected for viewing
This file contains 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,13 +1,9 @@ | ||
PRIVATE_KEY= | ||
PRIVATE_KEY=0x.... | ||
|
||
RPC_CHIADO=https://rpc.chiadochain.net | ||
RPC_ARB_GOERLI=https://goerli-rollup.arbitrum.io/rpc | ||
RPC_GOERLI= | ||
|
||
VEAINBOX_ARBGOERLI_TO_GOERLI_ADDRESS=0x906dE43dBef27639b1688Ac46532a16dc07Ce410 | ||
VEAINBOX_ARBGOERLI_TO_CHIADO_ADDRESS=0xAb53e341121448Ae259Da8fa17f216Cb0e21199C | ||
VEAOUTBOX_ARBGOERLI_TO_GOERLI_ADDRESS=0x906dE43dBef27639b1688Ac46532a16dc07Ce410 | ||
VEAOUTBOX_ARBGOERLI_TO_CHIADO_ADDRESS=0xAb53e341121448Ae259Da8fa17f216Cb0e21199C | ||
|
||
TRANSACTION_BATCHER_CONTRACT_ADDRESS_GOERLI=0xe7953da7751063d0a41ba727c32c762d3523ade8 | ||
TRANSACTION_BATCHER_CONTRACT_ADDRESS_CHIADO=0xcC0a08D4BCC5f91ee9a1587608f7a2975EA75d73 | ||
# Optional | ||
LOG_LEVEL= | ||
LOGTAIL_TOKEN= |
This file contains 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,25 +1,39 @@ | ||
import { Wallet } from "@ethersproject/wallet"; | ||
import VeaSdk from "../src/index"; | ||
import envVar from "../src/utils/envVar"; | ||
import env from "../src/utils/env"; | ||
|
||
async function main() { | ||
// Optional logger configuration | ||
const loggerOptions = { | ||
transportTargetOptions: { | ||
target: "@logtail/pino", | ||
options: { sourceToken: env.require("LOGTAIL_TOKEN") }, | ||
level: env.optional("LOG_LEVEL", "info"), | ||
}, | ||
}; | ||
|
||
// Create the Vea client | ||
const vea = VeaSdk.ClientFactory.arbitrumGoerliToChiadoDevnet(envVar("RPC_ARB_GOERLI"), envVar("RPC_CHIADO")); | ||
const vea = VeaSdk.ClientFactory.arbitrumGoerliToChiadoDevnet( | ||
env.require("RPC_ARB_GOERLI"), | ||
env.require("RPC_CHIADO"), | ||
loggerOptions | ||
); | ||
|
||
// Get the current state root | ||
console.log(`stateRoot=${await vea.outbox.stateRoot()}`); | ||
const logger = vea.logger; | ||
logger.info(`stateRoot=${await vea.outbox.stateRoot()}`); | ||
|
||
// Get a message info | ||
const messageId = 1; | ||
const messageInfo = await vea.getMessageInfo(messageId); | ||
|
||
// Relay the message | ||
const privateKey = envVar("PRIVATE_KEY"); | ||
const privateKey = env.require("PRIVATE_KEY"); | ||
const wallet = new Wallet(privateKey, vea.outboxProvider); | ||
await vea.relay(messageInfo, wallet); | ||
} | ||
|
||
main().catch((e) => { | ||
console.error(e); | ||
VeaSdk.ClientFactory.logger.error(e); | ||
process.exit(1); | ||
}); |
This file contains 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 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 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 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,24 @@ | ||
import dotenv from "dotenv"; | ||
|
||
dotenv.config(); | ||
|
||
namespace env { | ||
export const require = (key: string): string => { | ||
const value = process.env[key]; | ||
if (value === undefined) { | ||
throw new Error(`Environment variable ${key} is undefined`); | ||
} | ||
return value; | ||
}; | ||
|
||
export const optional = (key: string, defaultValue: string): string => { | ||
const value = process.env[key]; | ||
return value === undefined ? defaultValue : value; | ||
}; | ||
|
||
export const optionalNoDefault = (key: string): string | undefined => { | ||
return process.env[key]; | ||
}; | ||
} | ||
|
||
export default env; |
This file was deleted.
Oops, something went wrong.
This file contains 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,28 +1,36 @@ | ||
import pino from "pino"; | ||
import envVar from "./envVar"; | ||
import { TransportTargetOptions } from "pino"; | ||
import dotenv from "dotenv"; | ||
|
||
// TODO: make this env var optional, skip fetch if undefined | ||
const logtailToken = envVar("LOGTAIL_TOKEN"); | ||
const transport = pino.transport({ | ||
targets: [ | ||
{ | ||
target: "@logtail/pino", | ||
options: { sourceToken: logtailToken }, | ||
level: "debug", | ||
}, | ||
{ | ||
target: "pino-pretty", | ||
options: {}, | ||
level: "debug", | ||
}, | ||
], | ||
}); | ||
const logger = pino( | ||
{ | ||
level: envVar("LOG_LEVEL"), // TODO: set it to info if not defined | ||
timestamp: pino.stdTimeFunctions.isoTime, | ||
}, | ||
transport | ||
); | ||
dotenv.config(); | ||
|
||
namespace logger { | ||
export type LoggerOptions = { | ||
level?: string; | ||
transportTargetOptions?: TransportTargetOptions; | ||
}; | ||
|
||
export const createLogger = (options?: LoggerOptions): pino.Logger => { | ||
const targets: TransportTargetOptions[] = [ | ||
{ | ||
target: "pino-pretty", | ||
options: {}, | ||
level: options?.level ?? "info", | ||
}, | ||
]; | ||
|
||
if (options?.transportTargetOptions) { | ||
targets.push(options.transportTargetOptions); | ||
} | ||
|
||
return pino( | ||
{ | ||
level: options?.level ?? "info", | ||
timestamp: pino.stdTimeFunctions.isoTime, | ||
}, | ||
pino.transport({ targets: targets }) | ||
); | ||
}; | ||
} | ||
|
||
export default logger; |