Skip to content

Commit 1d91ec0

Browse files
tabaktonipenovicp
andauthored
fix: conf. TxV in Account, conf. string auto. all methods, fix unbdef… (#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]>
1 parent fa4d68b commit 1d91ec0

File tree

7 files changed

+124
-27
lines changed

7 files changed

+124
-27
lines changed

src/account/default.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ import {
7676
import { buildUDCCall, getExecuteCalldata } from '../utils/transaction';
7777
import { getMessageHash } from '../utils/typedData';
7878
import { AccountInterface } from './interface';
79+
import { config } from '../global/config';
7980

8081
export class Account extends Provider implements AccountInterface {
8182
public signer: SignerInterface;
@@ -91,9 +92,9 @@ export class Account extends Provider implements AccountInterface {
9192
address: string,
9293
pkOrSigner: Uint8Array | string | SignerInterface,
9394
cairoVersion?: CairoVersion,
94-
transactionVersion:
95-
| typeof ETransactionVersion.V2
96-
| typeof ETransactionVersion.V3 = ETransactionVersion.V2 // TODO: Discuss this, set to v2 for backward compatibility
95+
transactionVersion: typeof ETransactionVersion.V2 | typeof ETransactionVersion.V3 = config.get(
96+
'accountTxVersion'
97+
)
9798
) {
9899
super(providerOrOptions);
99100
this.address = address.toLowerCase();

src/global/config.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { DEFAULT_GLOBAL_CONFIG } from './constants';
22

3-
type ConfigData = {
4-
[key: string]: any;
5-
} & typeof DEFAULT_GLOBAL_CONFIG;
3+
type DefaultConfig = typeof DEFAULT_GLOBAL_CONFIG;
4+
type CustomConfig = { [key: string]: any };
5+
6+
type ConfigData = DefaultConfig & CustomConfig;
67

78
class Configuration {
89
private static instance: Configuration;
@@ -24,18 +25,19 @@ class Configuration {
2425
return Configuration.instance;
2526
}
2627

27-
public get<K extends keyof ConfigData>(
28-
key: K,
29-
defaultValue?: ConfigData[K]
30-
): ConfigData[K] | undefined {
28+
public get<K extends keyof DefaultConfig>(key: K): DefaultConfig[K];
29+
public get(key: string, defaultValue?: any): any;
30+
public get(key: string, defaultValue?: any) {
3131
return this.config[key] ?? defaultValue;
3232
}
3333

34-
public set<K extends keyof ConfigData>(key: K, value: ConfigData[K]): void {
34+
public set<K extends keyof DefaultConfig>(key: K, value: DefaultConfig[K]): void;
35+
public set(key: string, value: any): void;
36+
public set(key: string, value: any): void {
3537
this.config[key] = value;
3638
}
3739

38-
public update(configData: Partial<ConfigData>): void {
40+
public update(configData: Partial<DefaultConfig> & CustomConfig): void {
3941
this.config = {
4042
...this.config,
4143
...configData,
@@ -50,11 +52,15 @@ class Configuration {
5052
this.initialize();
5153
}
5254

53-
public delete(key: keyof ConfigData): void {
55+
public delete<K extends keyof DefaultConfig>(key: K): void;
56+
public delete(key: string): void;
57+
public delete(key: string): void {
5458
delete this.config[key];
5559
}
5660

57-
public hasKey(key: keyof ConfigData): boolean {
61+
public hasKey<K extends keyof DefaultConfig>(key: K): boolean;
62+
public hasKey(key: string): boolean;
63+
public hasKey(key: string): boolean {
5864
return key in this.config;
5965
}
6066
}

src/global/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,11 @@ export const HARDENING_4BYTES = 2147483648n;
9494
export const DEFAULT_GLOBAL_CONFIG: {
9595
legacyMode: boolean;
9696
logLevel: LogLevel;
97+
accountTxVersion: typeof ETransactionVersion.V2 | typeof ETransactionVersion.V3;
9798
} = {
9899
legacyMode: false,
99100
logLevel: 'INFO',
101+
accountTxVersion: ETransactionVersion.V2,
100102
};
101103

102104
// Default system messages

src/global/logger.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class Logger {
3232
}
3333

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

@@ -137,7 +137,7 @@ class Logger {
137137
}
138138

139139
public getLogLevel(): LogLevel {
140-
return this.config.get<string>('logLevel', 'INFO');
140+
return this.config.get('logLevel', 'INFO');
141141
}
142142

143143
/**

www/docs/guides/configuration.md

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
---
2+
sidebar_position: 2.1
3+
---
4+
5+
# Configuration
6+
7+
Starknet.js has behaviors that can be adjusted through its configurations: `config` and `logger`.
8+
9+
## Config
10+
11+
The core global configuration is a singleton object containing case-sensitive global default properties.
12+
Each property can be configured before the rest of the code is run to modify their corresponding behavior.
13+
When they overlap, constructor and method parameters have higher precedence over the global configuration defaults.
14+
Custom keys can also be used to store and use arbitrary values during runtime.
15+
16+
```ts
17+
import { config } from 'starknet';
18+
19+
// Set existing or custom global property
20+
config.set('mode', 'DEFAULT');
21+
22+
// Retrieve entire configuration
23+
config.getAll();
24+
25+
// Retrieve single global property
26+
config.get('legacyMode');
27+
28+
// Update (merge) existing configuration with modified or custom property
29+
config.update({ logLevel: 'DEBUG', newKey: 'value' });
30+
31+
// Reset config to initial global configuration
32+
config.reset();
33+
34+
// Delete existing global property
35+
config.delete('newKey');
36+
37+
// Check existence of the global property
38+
config.hasKey('newKey');
39+
```
40+
41+
### Global parameters and Default Global Configuration
42+
43+
Default global configuration is the initial state that global configuration starts with.
44+
45+
Details can be found in [global/constants.ts](https://github.com/starknet-io/starknet.js/blob/develop/src/global/constants.ts)
46+
47+
```ts
48+
logLevel: 'INFO', // verbosity levels of the system logger, more details under logger
49+
accountTxVersion: ETransactionVersion.V2, // by default use V2 transactions in Account class instances
50+
legacyMode: false, // enable legacy transaction types (note: this could break the code depending on the Starknet version used by the network)
51+
```
52+
53+
## Logger
54+
55+
Logger is a singleton object through which the Starknet.js logs are managed.
56+
57+
Supported log levels:
58+
59+
| | | |
60+
| :-----: | --- | ----------------------------- |
61+
| `DEBUG` | 5 | show all logs |
62+
| `INFO` | 4 | show INFO, WARN, ERROR, FATAL |
63+
| `WARN` | 3 | show WARN, ERROR, FATAL |
64+
| `ERROR` | 2 | show ERROR, FATAL |
65+
| `FATAL` | 1 | show only FATAL |
66+
| `OFF` | 0 | disable logs |
67+
68+
```ts
69+
import { logger } from 'starknet';
70+
71+
// set custom log level (can also be done using global config)
72+
logger.setLogLevel('WARN');
73+
74+
// get current log level
75+
logger.getLogLevel();
76+
77+
// get a list of all verbosity modes that would be displayed with the current log level
78+
logger.getEnabledLogLevels();
79+
```
80+
81+
Developers can also use it to add custom logs.
82+
83+
```ts
84+
import { logger } from 'starknet';
85+
86+
logger.debug('Debug message', additionalDataObject);
87+
logger.info('Info message', additionalDataObject);
88+
logger.warn('Warn message', additionalDataObject);
89+
logger.error('Error message', additionalDataObject);
90+
logger.fatal('Fatal message', additionalDataObject);
91+
```

www/docs/guides/intro.md

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,11 @@ npm install starknet
1616
npm install starknet@next
1717
```
1818

19-
## Running test locally
19+
## Running tests locally
2020

21-
### With Devnet
21+
Local tests rely on <ins>[Starknet Devnet](https://github.com/0xSpaceShard/starknet-devnet-rs)</ins>, a local testnet emulation.
2222

23-
- RPC Devnet [repo](https://github.com/0xSpaceShard/starknet-devnet-rs)
24-
25-
Launch the development net.
26-
27-
Open a new console tab, go to your starknet.js directory, and run:
23+
Launch a Devnet instance and run:
2824

2925
```bash
3026
npm run test # all tests
@@ -33,15 +29,15 @@ npm run test ./__tests__/contract.test.ts # just one test suite
3329

3430
## Running docs locally
3531

36-
If you want to change documentation and see how it looks before making a PR:
32+
If you want to make changes to the documentation and see how it looks before making a PR:
3733

3834
```bash
3935
cd www
4036
npm install # install docusaurus
4137
npm run start # fires up a local documentation site
4238
```
4339

44-
## Compiling Starknet Contracts
40+
## Compiling Starknet contracts
4541

4642
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.
4743

www/docs/guides/what_s_starknet.js.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ sidebar_position: 2
44

55
# What is Starknet.js ?
66

7-
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.
7+
Starknet.js is a library that helps connect your website or your Decentralized Application (DAPP) to the blockchain-based Starknet network, using JavaScript / TypeScript.
88

99
## Overview
1010

@@ -25,7 +25,8 @@ Some important topics that have to be understood:
2525

2626
> 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/).
2727
28-
- 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, ...).
28+
- The `RpcChannel` and `RpcProvider` classes and their methods are used for low-level communication with an RPC node.
29+
- 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.
2930
- `Signer` and `Utils` objects contain many useful functions for interaction with Starknet.js.
3031
- The `Contract` object is mainly used to read the memory of a blockchain contract.
3132
- The `Account` object is the most useful:

0 commit comments

Comments
 (0)