diff --git a/src/account/default.ts b/src/account/default.ts index 3152346f5..92a8ff1a5 100644 --- a/src/account/default.ts +++ b/src/account/default.ts @@ -76,6 +76,7 @@ import { import { buildUDCCall, getExecuteCalldata } from '../utils/transaction'; import { getMessageHash } from '../utils/typedData'; import { AccountInterface } from './interface'; +import { config } from '../global/config'; export class Account extends Provider implements AccountInterface { public signer: SignerInterface; @@ -91,9 +92,9 @@ export class Account extends Provider implements AccountInterface { address: string, pkOrSigner: Uint8Array | string | SignerInterface, cairoVersion?: CairoVersion, - transactionVersion: - | typeof ETransactionVersion.V2 - | typeof ETransactionVersion.V3 = ETransactionVersion.V2 // TODO: Discuss this, set to v2 for backward compatibility + transactionVersion: typeof ETransactionVersion.V2 | typeof ETransactionVersion.V3 = config.get( + 'accountTxVersion' + ) ) { super(providerOrOptions); this.address = address.toLowerCase(); diff --git a/src/global/config.ts b/src/global/config.ts index 9c45f3f79..0ae5e2ad3 100644 --- a/src/global/config.ts +++ b/src/global/config.ts @@ -1,8 +1,9 @@ import { DEFAULT_GLOBAL_CONFIG } from './constants'; -type ConfigData = { - [key: string]: any; -} & typeof DEFAULT_GLOBAL_CONFIG; +type DefaultConfig = typeof DEFAULT_GLOBAL_CONFIG; +type CustomConfig = { [key: string]: any }; + +type ConfigData = DefaultConfig & CustomConfig; class Configuration { private static instance: Configuration; @@ -24,18 +25,19 @@ class Configuration { return Configuration.instance; } - public get( - key: K, - defaultValue?: ConfigData[K] - ): ConfigData[K] | undefined { + public get(key: K): DefaultConfig[K]; + public get(key: string, defaultValue?: any): any; + public get(key: string, defaultValue?: any) { return this.config[key] ?? defaultValue; } - public set(key: K, value: ConfigData[K]): void { + public set(key: K, value: DefaultConfig[K]): void; + public set(key: string, value: any): void; + public set(key: string, value: any): void { this.config[key] = value; } - public update(configData: Partial): void { + public update(configData: Partial & CustomConfig): void { this.config = { ...this.config, ...configData, @@ -50,11 +52,15 @@ class Configuration { this.initialize(); } - public delete(key: keyof ConfigData): void { + public delete(key: K): void; + public delete(key: string): void; + public delete(key: string): void { delete this.config[key]; } - public hasKey(key: keyof ConfigData): boolean { + public hasKey(key: K): boolean; + public hasKey(key: string): boolean; + public hasKey(key: string): boolean { return key in this.config; } } diff --git a/src/global/constants.ts b/src/global/constants.ts index 5b2a5fc32..7a2a2a7af 100644 --- a/src/global/constants.ts +++ b/src/global/constants.ts @@ -94,9 +94,11 @@ export const HARDENING_4BYTES = 2147483648n; export const DEFAULT_GLOBAL_CONFIG: { legacyMode: boolean; logLevel: LogLevel; + accountTxVersion: typeof ETransactionVersion.V2 | typeof ETransactionVersion.V3; } = { legacyMode: false, logLevel: 'INFO', + accountTxVersion: ETransactionVersion.V2, }; // Default system messages diff --git a/src/global/logger.ts b/src/global/logger.ts index 537c30d22..58f8646af 100644 --- a/src/global/logger.ts +++ b/src/global/logger.ts @@ -32,7 +32,7 @@ class Logger { } private shouldLog(messageLevel: LogLevelIndex): boolean { - const configLevel = this.config.get('logLevel', 'INFO'); + const configLevel = this.config.get('logLevel', 'INFO'); return messageLevel <= LogLevelIndex[configLevel as LogLevel]; } @@ -137,7 +137,7 @@ class Logger { } public getLogLevel(): LogLevel { - return this.config.get('logLevel', 'INFO'); + return this.config.get('logLevel', 'INFO'); } /** diff --git a/www/docs/guides/configuration.md b/www/docs/guides/configuration.md new file mode 100644 index 000000000..0963af17e --- /dev/null +++ b/www/docs/guides/configuration.md @@ -0,0 +1,91 @@ +--- +sidebar_position: 2.1 +--- + +# Configuration + +Starknet.js has behaviors that can be adjusted through its configurations: `config` and `logger`. + +## Config + +The core global configuration is a singleton object containing case-sensitive global default properties. +Each property can be configured before the rest of the code is run to modify their corresponding behavior. +When they overlap, constructor and method parameters have higher precedence over the global configuration defaults. +Custom keys can also be used to store and use arbitrary values during runtime. + +```ts +import { config } from 'starknet'; + +// Set existing or custom global property +config.set('mode', 'DEFAULT'); + +// Retrieve entire configuration +config.getAll(); + +// Retrieve single global property +config.get('legacyMode'); + +// Update (merge) existing configuration with modified or custom property +config.update({ logLevel: 'DEBUG', newKey: 'value' }); + +// Reset config to initial global configuration +config.reset(); + +// Delete existing global property +config.delete('newKey'); + +// Check existence of the global property +config.hasKey('newKey'); +``` + +### Global parameters and Default Global Configuration + +Default global configuration is the initial state that global configuration starts with. + +Details can be found in [global/constants.ts](https://github.com/starknet-io/starknet.js/blob/develop/src/global/constants.ts) + +```ts + logLevel: 'INFO', // verbosity levels of the system logger, more details under logger + accountTxVersion: ETransactionVersion.V2, // by default use V2 transactions in Account class instances + legacyMode: false, // enable legacy transaction types (note: this could break the code depending on the Starknet version used by the network) +``` + +## Logger + +Logger is a singleton object through which the Starknet.js logs are managed. + +Supported log levels: + +| | | | +| :-----: | --- | ----------------------------- | +| `DEBUG` | 5 | show all logs | +| `INFO` | 4 | show INFO, WARN, ERROR, FATAL | +| `WARN` | 3 | show WARN, ERROR, FATAL | +| `ERROR` | 2 | show ERROR, FATAL | +| `FATAL` | 1 | show only FATAL | +| `OFF` | 0 | disable logs | + +```ts +import { logger } from 'starknet'; + +// set custom log level (can also be done using global config) +logger.setLogLevel('WARN'); + +// get current log level +logger.getLogLevel(); + +// get a list of all verbosity modes that would be displayed with the current log level +logger.getEnabledLogLevels(); +``` + +Developers can also use it to add custom logs. + +```ts +import { logger } from 'starknet'; + +logger.debug('Debug message', additionalDataObject); +logger.info('Info message', additionalDataObject); +logger.warn('Warn message', additionalDataObject); +logger.error('Error message', additionalDataObject); +logger.fatal('Fatal message', additionalDataObject); +``` diff --git a/www/docs/guides/intro.md b/www/docs/guides/intro.md index 7c78839f6..232ea159a 100644 --- a/www/docs/guides/intro.md +++ b/www/docs/guides/intro.md @@ -16,15 +16,11 @@ npm install starknet npm install starknet@next ``` -## Running test locally +## Running tests locally -### With Devnet +Local tests rely on [Starknet Devnet](https://github.com/0xSpaceShard/starknet-devnet-rs), a local testnet emulation. -- RPC Devnet [repo](https://github.com/0xSpaceShard/starknet-devnet-rs) - -Launch the development net. - -Open a new console tab, go to your starknet.js directory, and run: +Launch a Devnet instance and run: ```bash npm run test # all tests @@ -33,7 +29,7 @@ npm run test ./__tests__/contract.test.ts # just one test suite ## Running docs locally -If you want to change documentation and see how it looks before making a PR: +If you want to make changes to the documentation and see how it looks before making a PR: ```bash cd www @@ -41,7 +37,7 @@ npm install # install docusaurus npm run start # fires up a local documentation site ``` -## Compiling Starknet Contracts +## Compiling Starknet contracts Please check the Starknet documentation [here](https://docs.starknet.io/documentation/quick_start/declare_a_smart_contract/#compiling_a_smart_contract) to compile Starknet contracts. diff --git a/www/docs/guides/what_s_starknet.js.md b/www/docs/guides/what_s_starknet.js.md index ffd90cba0..7d43497de 100644 --- a/www/docs/guides/what_s_starknet.js.md +++ b/www/docs/guides/what_s_starknet.js.md @@ -4,7 +4,7 @@ sidebar_position: 2 # What is Starknet.js ? -Starknet.js is a library that helps to connect your website or your Decentralized Application (DAPP) to the blockchain-based Starknet network, using JavaScript / TypeScript language. +Starknet.js is a library that helps connect your website or your Decentralized Application (DAPP) to the blockchain-based Starknet network, using JavaScript / TypeScript. ## Overview @@ -25,7 +25,8 @@ Some important topics that have to be understood: > Understanding what Starknet is and how it works is necessary. Then, you can learn how to interact with it using Starknet.js. So, at this stage, you should be aware of the content of the [Starknet official doc](https://docs.starknet.io/documentation/) and [the Starknet Book](https://book.starknet.io/). -- Only the `RpcProvider` object communicates directly with the network; your DAPP will mainly interact with `Account` and `Contract` objects. You will define with the `RpcProvider` with which network you want to work. You can use the provider to access some low-level data from the network (block, timestamp, ...). +- The `RpcChannel` and `RpcProvider` classes and their methods are used for low-level communication with an RPC node. +- Your DAPP will mainly interact with `Account` and `Contract` class instances; they use underlying `RpcProvider` connections to provide high-level methods for interacting with their Starknet namesakes, accounts and contracts. - `Signer` and `Utils` objects contain many useful functions for interaction with Starknet.js. - The `Contract` object is mainly used to read the memory of a blockchain contract. - The `Account` object is the most useful: