Skip to content

Commit

Permalink
processing logs
Browse files Browse the repository at this point in the history
  • Loading branch information
ochaloup committed Oct 30, 2023
1 parent 3de9987 commit 0db4742
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 36 deletions.
25 changes: 20 additions & 5 deletions packages/lib/ledger-utils/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import {
TransportStatusError,
} from '@ledgerhq/errors'
import { CLI_LEDGER_URL_PREFIX, LedgerWallet, Wallet } from './ledger'
import {LoggerPlaceholder, logError} from '@marinade.finance/ts-common'
import {
LoggerPlaceholder,
logError,
logDebug,
} from '@marinade.finance/ts-common'

/**
* Parsing provided argument a ledger url.
Expand All @@ -13,15 +17,16 @@ import {LoggerPlaceholder, logError} from '@marinade.finance/ts-common'
*/
export async function parseLedgerWallet(
pathOrUrl: string,
logger: LoggerPlaceholder
logger?: LoggerPlaceholder
): Promise<Wallet | null> {
pathOrUrl = pathOrUrl.trim()

// trying ledger (https://docs.solana.com/wallet-guide/hardware-wallets/ledger)
if (pathOrUrl.startsWith(CLI_LEDGER_URL_PREFIX)) {
try {
const solanaLedger = await LedgerWallet.instance(pathOrUrl)
logger.debug(
logDebug(
logger,
'Successfully connected to Ledger device of key %s',
solanaLedger.publicKey.toBase58()
)
Expand All @@ -30,11 +35,13 @@ export async function parseLedgerWallet(
if (e instanceof TransportStatusError && 'statusCode' in e) {
if (e.statusCode === 0x6d02) {
logError(
logger,
'Ledger device Solana application is not activated. ' +
'Please, enter the Solana app on your ledger device first.'
)
} else if (e.statusCode === 0x6808) {
logError(
logger,
'Solana application does not permit blind signatures. ' +
'Please, permit it in the Solana app settings at the ledger device first.'
)
Expand All @@ -44,20 +51,28 @@ export async function parseLedgerWallet(
e.message.includes('Invalid channel')
) {
logError(
logger,
'Ledger device seems not being acknowledged to open the ledger manager. ' +
'Please, open ledger manager first on your device.'
)
} else if (e instanceof LockedDeviceError) {
logError('Ledger device is locked. ' + 'Please, unlock it first.')
logError(
logger,
'Ledger device is locked. ' + 'Please, unlock it first.'
)
} else if (
e instanceof Error &&
e.message.includes('read from a closed HID')
) {
logError(
logger,
'Ledger cannot be open, it seems to be closed. Ensure no other program uses it.'
)
} else {
logError(`Failed to connect to Ledger device of key ${pathOrUrl}`)
logError(
logger,
`Failed to connect to Ledger device of key ${pathOrUrl}`
)
}
throw e
}
Expand Down
75 changes: 51 additions & 24 deletions packages/lib/ts-common/src/logging.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,72 @@
/* eslint-disable @typescript-eslint/no-explicit-any */

export type LoggerPlaceholder = {
debug(...data:any): void
info(...data:any): void
warn(...data:any): void
error(...data:any): void
}

export function logDebug(logger: any, ...data: any) {
if (checkIfFunction<LoggerPlaceholder>(logger, 'debug')) {
logger.debug(data)
} else {
console.debug(data)
}
export type LoggerPlaceholder = LoggerDebugAble &
LoggerInfoAble &
LoggerWarnAble &
LoggerErrorAble
export type LoggerDebugAble = {
debug(...data: any): void
}
export type LoggerInfoAble = {
info(...data: any): void
}
export type LoggerWarnAble = {
warn(...data: any): void
}
export type LoggerErrorAble = {
error(...data: any): void
}

export function logDebug(logger: object | undefined, ...data: any) {
if (checkIfFunction<LoggerPlaceholder>(logger, 'debug')) {
logger.debug(data)
} else {
console.debug(data)
}
}

export function logInfo(logger: any, data: any) {
if (checkIfFunction<LoggerPlaceholder>(logger, 'info')) {
logger.info(data)
} else {
console.log(data)
}
export function logInfo(logger: object | undefined, data: any) {
if (checkIfFunction<LoggerPlaceholder>(logger, 'info')) {
logger.info(data)
} else {
console.log(data)
}
}

export function logWarn(logger: any, data: any) {
export function logWarn(logger: object | undefined, data: any) {
if (checkIfFunction<LoggerPlaceholder>(logger, 'warn')) {
logger.warn(data)
} else {
console.warn(data)
}
}

export function logError(logger: any, data: any) {
export function logError(logger: object | undefined, data: any) {
if (checkIfFunction<LoggerPlaceholder>(logger, 'error')) {
logger.error(data)
} else {
console.error(data)
}
}

export function isLevelEnabled(
logger: any | undefined,
level: string
): boolean | undefined {
if (!checkIfFunction<LoggerIsLevelEnabled>(logger, 'isLevelEnabled')) {
return undefined
} else {
return logger.isLevelEnabled(level)
}
}

function checkIfFunction<T>(logger: any | undefined, functionName: string): logger is T {
return (logger !== undefined && typeof logger[functionName] !== 'function')
}
function checkIfFunction<T>(
logger: any | undefined,
functionName: string
): logger is T {
return logger !== undefined && typeof logger[functionName] !== 'function'
}

type LoggerIsLevelEnabled = {
isLevelEnabled(level: string): boolean
}
1 change: 1 addition & 0 deletions packages/lib/web3js-common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
},
"peerDependencies": {
"@solana/web3.js": "^1.78.5",
"@marinade.finance/ts-common": "2.0.15",
"bn.js": "^5.2.1",
"borsh": "^0.7.0",
"bs58": "^5.0.0"
Expand Down
15 changes: 11 additions & 4 deletions packages/lib/web3js-common/src/tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ import {
import { Wallet, instanceOfWallet } from './wallet'
import { serializeInstructionToBase64 } from './txToBase64'
import { ExecutionError } from './error'
import {
LoggerPlaceholder,
logDebug,
logInfo,
logWarn,
isLevelEnabled,
} from '@marinade.finance/ts-common'

export async function executeTx({
connection,
Expand All @@ -41,7 +48,7 @@ export async function executeTx({
| undefined = undefined

if (printOnly) {
console.log('Instructions (SPL Gov base64):')
logInfo(logger, 'Instructions (SPL Gov base64):')
for (const ix of transaction.instructions) {
console.log(' ' + serializeInstructionToBase64(ix))
}
Expand Down Expand Up @@ -131,7 +138,7 @@ export async function executeTx({
logs: (e as SendTransactionError).logs
? (e as SendTransactionError).logs
: undefined,
transaction,
transaction: isLevelEnabled(logger, 'debug') ? transaction : undefined,
})
}
return result
Expand Down Expand Up @@ -247,7 +254,7 @@ export async function splitAndExecuteTx({
feePayer?: PublicKey
simulate?: boolean
printOnly?: boolean
logger?: LoggerStandIn
logger?: LoggerPlaceholder
exceedBudget?: boolean
}): Promise<
VersionedTransactionResponse[] | SimulatedTransactionResponse[] | []
Expand Down Expand Up @@ -320,7 +327,7 @@ export async function splitAndExecuteTx({
}).byteLength
} catch (e) {
// ignore
// logDebug(logger, 'Transaction size calculation failed: ' + e)
logDebug(logger, 'Transaction size calculation failed: ' + e)
}

if (
Expand Down
6 changes: 3 additions & 3 deletions pnpm-lock.yaml

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

0 comments on commit 0db4742

Please sign in to comment.