Skip to content

Commit

Permalink
fix: conf. TxV in Account, conf. string auto. all methods, fix unbdef… (
Browse files Browse the repository at this point in the history
#1311)

* fix: conf. TxV in Account, conf. string auto. all methods, fix unbdefined for known, docs

* docs: docs

* docs: adjust heading structure

---------

Co-authored-by: Petar Penovic <[email protected]>
  • Loading branch information
tabaktoni and penovicp authored Feb 5, 2025
1 parent fa4d68b commit 1d91ec0
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 27 deletions.
7 changes: 4 additions & 3 deletions src/account/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down
28 changes: 17 additions & 11 deletions src/global/config.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -24,18 +25,19 @@ class Configuration {
return Configuration.instance;
}

public get<K extends keyof ConfigData>(
key: K,
defaultValue?: ConfigData[K]
): ConfigData[K] | undefined {
public get<K extends keyof DefaultConfig>(key: K): DefaultConfig[K];
public get(key: string, defaultValue?: any): any;
public get(key: string, defaultValue?: any) {
return this.config[key] ?? defaultValue;
}

public set<K extends keyof ConfigData>(key: K, value: ConfigData[K]): void {
public set<K extends keyof DefaultConfig>(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<ConfigData>): void {
public update(configData: Partial<DefaultConfig> & CustomConfig): void {
this.config = {
...this.config,
...configData,
Expand All @@ -50,11 +52,15 @@ class Configuration {
this.initialize();
}

public delete(key: keyof ConfigData): void {
public delete<K extends keyof DefaultConfig>(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<K extends keyof DefaultConfig>(key: K): boolean;
public hasKey(key: string): boolean;
public hasKey(key: string): boolean {
return key in this.config;
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/global/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/global/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Logger {
}

private shouldLog(messageLevel: LogLevelIndex): boolean {
const configLevel = this.config.get<string>('logLevel', 'INFO');
const configLevel = this.config.get('logLevel', 'INFO');
return messageLevel <= LogLevelIndex[configLevel as LogLevel];
}

Expand Down Expand Up @@ -137,7 +137,7 @@ class Logger {
}

public getLogLevel(): LogLevel {
return this.config.get<string>('logLevel', 'INFO');
return this.config.get('logLevel', 'INFO');
}

/**
Expand Down
91 changes: 91 additions & 0 deletions www/docs/guides/configuration.md
Original file line number Diff line number Diff line change
@@ -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);
```
14 changes: 5 additions & 9 deletions www/docs/guides/intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,11 @@ npm install starknet
npm install starknet@next
```

## Running test locally
## Running tests locally

### With Devnet
Local tests rely on <ins>[Starknet Devnet](https://github.com/0xSpaceShard/starknet-devnet-rs)</ins>, 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
Expand All @@ -33,15 +29,15 @@ 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
npm install # install docusaurus
npm run start # fires up a local documentation site
```

## Compiling Starknet Contracts
## Compiling Starknet contracts

Please check the Starknet documentation <ins>[here](https://docs.starknet.io/documentation/quick_start/declare_a_smart_contract/#compiling_a_smart_contract)</ins> to compile Starknet contracts.

Expand Down
5 changes: 3 additions & 2 deletions www/docs/guides/what_s_starknet.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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:
Expand Down

0 comments on commit 1d91ec0

Please sign in to comment.