From 6b4bc53f77a5b291eb6a06180327db4c86303692 Mon Sep 17 00:00:00 2001 From: Toni Tabak Date: Tue, 28 Jan 2025 23:34:17 +0100 Subject: [PATCH] feat: implement logger, warn old tx usage --- src/channel/rpc_0_6.ts | 26 ++++++++++++++++++++ src/channel/rpc_0_7.ts | 26 ++++++++++++++++++++ src/contract/contractFactory.ts | 4 +-- src/contract/default.ts | 10 +++----- src/global/logger.ts | 43 ++++++++++++++++++++++++++++++++- src/utils/provider.ts | 4 +-- src/wallet/account.ts | 4 +-- 7 files changed, 104 insertions(+), 13 deletions(-) diff --git a/src/channel/rpc_0_6.ts b/src/channel/rpc_0_6.ts index 3a9355f28..f2d658e73 100644 --- a/src/channel/rpc_0_6.ts +++ b/src/channel/rpc_0_6.ts @@ -30,6 +30,7 @@ import { getHexStringArray, toHex, toStorageKey } from '../utils/num'; import { Block, getDefaultNodeUrl, isV3Tx, isVersion, wait } from '../utils/provider'; import { decompressProgram, signatureToHexArray } from '../utils/stark'; import { getVersionsByType } from '../utils/transaction'; +import { logger } from '../global/logger'; const defaultOptions = { headers: { 'Content-Type': 'application/json' }, @@ -448,6 +449,11 @@ export class RpcChannel { nonce: toHex(details.nonce), }, }); + + logger.warn( + 'You are using deprecated transaction version (V0,V1,V2)!, \n Update to the latest V3 transactions!', + { version: RPC.ETransactionVersion.V1, type: RPC.ETransactionType.INVOKE } + ); } else { // V3 promise = this.fetchEndpoint('starknet_addInvokeTransaction', { @@ -493,6 +499,11 @@ export class RpcChannel { nonce: toHex(details.nonce), }, }); + + logger.warn( + 'You are using deprecated transaction version (V0,V1,V2)!, \n Update to the latest V3 transactions!', + { version: RPC.ETransactionVersion.V1, type: RPC.ETransactionType.DECLARE } + ); } else if (isSierra(contract) && !isV3Tx(details)) { // V2 Cairo1 promise = this.fetchEndpoint('starknet_addDeclareTransaction', { @@ -512,6 +523,11 @@ export class RpcChannel { nonce: toHex(details.nonce), }, }); + + logger.warn( + 'You are using deprecated transaction version (V0,V1,V2)!, \n Update to the latest V3 transactions!', + { version: RPC.ETransactionVersion.V2, type: RPC.ETransactionType.DECLARE } + ); } else if (isSierra(contract) && isV3Tx(details)) { // V3 Cairo1 promise = this.fetchEndpoint('starknet_addDeclareTransaction', { @@ -562,6 +578,11 @@ export class RpcChannel { nonce: toHex(details.nonce), }, }); + + logger.warn( + 'You are using deprecated transaction version (V0,V1,V2)!, \n Update to the latest V3 transactions!', + { version: RPC.ETransactionVersion.V1, type: RPC.ETransactionType.DEPLOY_ACCOUNT } + ); } else { // v3 promise = this.fetchEndpoint('starknet_addDeployAccountTransaction', { @@ -650,6 +671,11 @@ export class RpcChannel { nonce: toHex(invocation.nonce), max_fee: toHex(invocation.maxFee || 0), }; + + logger.warn( + 'You are using deprecated transaction version (V0,V1,V2)!, \n Update to the latest V3 transactions!', + { version: invocation.version, type: invocation.type } + ); } else { // V3 details = { diff --git a/src/channel/rpc_0_7.ts b/src/channel/rpc_0_7.ts index d47c2455d..fbc9bb8b8 100644 --- a/src/channel/rpc_0_7.ts +++ b/src/channel/rpc_0_7.ts @@ -30,6 +30,7 @@ import { getHexStringArray, toHex, toStorageKey } from '../utils/num'; import { Block, getDefaultNodeUrl, isV3Tx, isVersion, wait } from '../utils/provider'; import { decompressProgram, signatureToHexArray } from '../utils/stark'; import { getVersionsByType } from '../utils/transaction'; +import { logger } from '../global/logger'; const defaultOptions = { headers: { 'Content-Type': 'application/json' }, @@ -453,6 +454,11 @@ export class RpcChannel { nonce: toHex(details.nonce), }, }); + + logger.warn( + 'You are using deprecated transaction version (V0,V1,V2)!, \n Update to the latest V3 transactions!', + { version: RPC.ETransactionVersion.V1, type: RPC.ETransactionType.INVOKE } + ); } else { // V3 promise = this.fetchEndpoint('starknet_addInvokeTransaction', { @@ -498,6 +504,11 @@ export class RpcChannel { nonce: toHex(details.nonce), }, }); + + logger.warn( + 'You are using deprecated transaction version (V0,V1,V2)!, \n Update to the latest V3 transactions!', + { version: RPC.ETransactionVersion.V1, type: RPC.ETransactionType.DECLARE } + ); } else if (isSierra(contract) && !isV3Tx(details)) { // V2 Cairo1 promise = this.fetchEndpoint('starknet_addDeclareTransaction', { @@ -517,6 +528,11 @@ export class RpcChannel { nonce: toHex(details.nonce), }, }); + + logger.warn( + 'You are using deprecated transaction version (V0,V1,V2)!, \n Update to the latest V3 transactions!', + { version: RPC.ETransactionVersion.V2, type: RPC.ETransactionType.DECLARE } + ); } else if (isSierra(contract) && isV3Tx(details)) { // V3 Cairo1 promise = this.fetchEndpoint('starknet_addDeclareTransaction', { @@ -567,6 +583,11 @@ export class RpcChannel { nonce: toHex(details.nonce), }, }); + + logger.warn( + 'You are using deprecated transaction version (V0,V1,V2)!, \n Update to the latest V3 transactions!', + { version: RPC.ETransactionVersion.V1, type: RPC.ETransactionType.DEPLOY_ACCOUNT } + ); } else { // v3 promise = this.fetchEndpoint('starknet_addDeployAccountTransaction', { @@ -655,6 +676,11 @@ export class RpcChannel { nonce: toHex(invocation.nonce), max_fee: toHex(invocation.maxFee || 0), }; + + logger.warn( + 'You are using deprecated transaction version (V0,V1,V2)!, \n Update to the latest V3 transactions!', + { version: invocation.version, type: invocation.type } + ); } else { // V3 details = { diff --git a/src/contract/contractFactory.ts b/src/contract/contractFactory.ts index f2c918192..96ae758cb 100644 --- a/src/contract/contractFactory.ts +++ b/src/contract/contractFactory.ts @@ -1,4 +1,5 @@ import { AccountInterface } from '../account'; +import { logger } from '../global/logger'; import { Abi, ArgsOrCalldataWithOptions, @@ -66,8 +67,7 @@ export class ContractFactory { this.CallData.validate(ValidateType.DEPLOY, 'constructor', param); return this.CallData.compile('constructor', param); } - // eslint-disable-next-line no-console - console.warn('Call skipped parsing but provided rawArgs, possible malfunction request'); + logger.warn('Call skipped parsing but provided rawArgs, possible malfunction request'); return param; }); diff --git a/src/contract/default.ts b/src/contract/default.ts index 9599a6ee8..ded86590b 100644 --- a/src/contract/default.ts +++ b/src/contract/default.ts @@ -33,6 +33,7 @@ import { cleanHex } from '../utils/num'; import { ContractInterface } from './interface'; import type { GetTransactionReceiptResponse } from '../utils/transactionReceipt'; import type { INVOKE_TXN_RECEIPT } from '../types/provider/spec'; +import { logger } from '../global/logger'; export type TypedContractV2 = AbiWanTypedContract & Contract; @@ -238,8 +239,7 @@ export class Contract implements ContractInterface { this.callData.validate(ValidateType.CALL, method, args); return this.callData.compile(method, args); } - // eslint-disable-next-line no-console - console.warn('Call skipped parsing but provided rawArgs, possible malfunction request'); + logger.warn('Call skipped parsing but provided rawArgs, possible malfunction request'); return args; }); @@ -275,8 +275,7 @@ export class Contract implements ContractInterface { this.callData.validate(ValidateType.INVOKE, method, args); return this.callData.compile(method, args); } - // eslint-disable-next-line no-console - console.warn('Invoke skipped parsing but provided rawArgs, possible malfunction request'); + logger.warn('Invoke skipped parsing but provided rawArgs, possible malfunction request'); return args; }); @@ -293,8 +292,7 @@ export class Contract implements ContractInterface { } if (!nonce) throw new Error(`Nonce is required when invoking a function without an account`); - // eslint-disable-next-line no-console - console.warn(`Invoking ${method} without an account. This will not work on a public node.`); + logger.warn(`Invoking ${method} without an account. This will not work on a public node.`); return this.providerOrAccount.invokeFunction( { diff --git a/src/global/logger.ts b/src/global/logger.ts index d0c1d9565..d01cc0d9a 100644 --- a/src/global/logger.ts +++ b/src/global/logger.ts @@ -8,7 +8,9 @@ interface LogMessage { timestamp: string; data?: any; } - +/** + * Logging class providing different levels of log + */ class Logger { private static instance: Logger; @@ -86,26 +88,50 @@ class Logger { } } + /** + * debug will be displayed when LogLevel level is set to DEBUG(5) + */ public debug(message: string, data?: any): void { this.log('DEBUG', message, data); } + /** + * info will be displayed when LogLevel level is set to DEBUG(5), INFO(4) + */ public info(message: string, data?: any): void { this.log('INFO', message, data); } + /** + * warn will be displayed when LogLevel level is set to DEBUG(5), INFO(4), WARN(3) + */ public warn(message: string, data?: any): void { this.log('WARN', message, data); } + /** + * error will be displayed when LogLevel level is set to DEBUG(5), INFO(4), WARN(3), ERROR(2) + */ public error(message: string, data?: any): void { this.log('ERROR', message, data); } + /** + * fatal will be displayed when LogLevel level is set to DEBUG(5), INFO(4), WARN(3), ERROR(2), FATAL(1) + */ public fatal(message: string, data?: any): void { this.log('FATAL', message, data); } + /** + * Set the logging level you would like system to display + * * 5 DEBUG - show all logs + * * 4 INFO + * * 3 WARN + * * 2 ERROR + * * 1 FATAL + * * 0 OFF - disable logs + */ public setLogLevel(level: LogLevel): void { this.config.set('logLevel', level); } @@ -114,6 +140,10 @@ class Logger { return this.config.get('logLevel', 'INFO'); } + /** + * + * @returns logs levels displayed on the configured LogLevel + */ public getEnabledLogLevels() { const logLevelStringKeys = Object.keys(LogLevelIndex).filter( (v) => Number.isNaN(Number(v)) && v !== 'OFF' @@ -127,4 +157,15 @@ class Logger { } // Export a single instance +/** + * Logger instance, use for the system logging. + * Higher the logger level index, higher the LogLevel required to display log. + * Default should be INFO + * + * DEBUG: 5, + * INFO: 4, + * WARN: 3, + * ERROR: 2, + * FATAL: 1, + */ export const logger = Logger.getInstance(); diff --git a/src/utils/provider.ts b/src/utils/provider.ts index 570d2e16d..c47b67ec6 100644 --- a/src/utils/provider.ts +++ b/src/utils/provider.ts @@ -23,6 +23,7 @@ import { isDecimalString } from './shortString'; import { isBigInt, isNumber, isString } from './typed'; import { compressProgram } from './stark'; import type { GetTransactionReceiptResponse } from './transactionReceipt'; +import { logger } from '../global/logger'; /** * Helper - Async Sleep for 'delay' time @@ -121,8 +122,7 @@ export function parseContract(contract: CompiledContract | string): ContractClas */ export const getDefaultNodeUrl = (networkName?: NetworkName, mute: boolean = false): string => { if (!mute) { - // eslint-disable-next-line no-console - console.warn('Using default public node url, please provide nodeUrl in provider options!'); + logger.info('Using default public node url, please provide nodeUrl in provider options!'); } const nodes = RPC_NODES[networkName ?? NetworkName.SN_SEPOLIA]; const randIdx = Math.floor(Math.random() * nodes.length); diff --git a/src/wallet/account.ts b/src/wallet/account.ts index 54b103161..8cd89409a 100644 --- a/src/wallet/account.ts +++ b/src/wallet/account.ts @@ -36,6 +36,7 @@ import { watchAsset, } from './connect'; import { StarknetWalletProvider } from './types'; +import { logger } from '../global/logger'; // TODO: Remove non address constructor in next major version // Represent 'Selected Active' Account inside Connected Wallet @@ -80,8 +81,7 @@ export class WalletAccount extends Account implements AccountInterface { }); if (!address.length) { - // eslint-disable-next-line no-console - console.warn( + logger.warn( '@deprecated Use static method WalletAccount.connect or WalletAccount.connectSilent instead. Constructor {@link WalletAccount.(format:2)}.' ); requestAccounts(this.walletProvider).then(([accountAddress]) => {