Skip to content

Commit

Permalink
feat: implement logger, warn old tx usage
Browse files Browse the repository at this point in the history
  • Loading branch information
tabaktoni committed Jan 28, 2025
1 parent 88e62cc commit 6b4bc53
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 13 deletions.
26 changes: 26 additions & 0 deletions src/channel/rpc_0_6.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
Expand Down Expand Up @@ -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', {
Expand Down Expand Up @@ -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', {
Expand All @@ -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', {
Expand Down Expand Up @@ -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', {
Expand Down Expand Up @@ -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 = {
Expand Down
26 changes: 26 additions & 0 deletions src/channel/rpc_0_7.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
Expand Down Expand Up @@ -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', {
Expand Down Expand Up @@ -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', {
Expand All @@ -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', {
Expand Down Expand Up @@ -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', {
Expand Down Expand Up @@ -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 = {
Expand Down
4 changes: 2 additions & 2 deletions src/contract/contractFactory.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AccountInterface } from '../account';
import { logger } from '../global/logger';
import {
Abi,
ArgsOrCalldataWithOptions,
Expand Down Expand Up @@ -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;
});

Expand Down
10 changes: 4 additions & 6 deletions src/contract/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TAbi extends AbiKanabi> = AbiWanTypedContract<TAbi> & Contract;

Expand Down Expand Up @@ -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;
});

Expand Down Expand Up @@ -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;
});

Expand All @@ -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(
{
Expand Down
43 changes: 42 additions & 1 deletion src/global/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ interface LogMessage {
timestamp: string;
data?: any;
}

/**
* Logging class providing different levels of log
*/
class Logger {
private static instance: Logger;

Expand Down Expand Up @@ -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);
}
Expand All @@ -114,6 +140,10 @@ class Logger {
return this.config.get<string>('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'
Expand All @@ -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();
4 changes: 2 additions & 2 deletions src/utils/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/wallet/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]) => {
Expand Down

0 comments on commit 6b4bc53

Please sign in to comment.